From 10f1ded7cfc96e04c38ab56cc58e248c781c6758 Mon Sep 17 00:00:00 2001 From: bhanuprasadcherukuvada Date: Mon, 25 Sep 2023 00:54:42 +0530 Subject: [PATCH] feat: export prepend script, reuse in tests --- simple-git-hooks.js | 16 ++++--- simple-git-hooks.test.js | 94 +++++----------------------------------- 2 files changed, 20 insertions(+), 90 deletions(-) diff --git a/simple-git-hooks.js b/simple-git-hooks.js index 59797ed..e18d0a9 100644 --- a/simple-git-hooks.js +++ b/simple-git-hooks.js @@ -34,6 +34,13 @@ const VALID_GIT_HOOKS = [ const VALID_OPTIONS = ['preserveUnused'] +const PREPEND_SCRIPT = + "#!/bin/sh\n\n" + + 'if [ "$SIMPLE_GIT_HOOKS" = "0" ]; then\n' + + ' echo "[INFO] SIMPLE_GIT_HOOKS is set to 0, skipping hook."\n' + + " exit 0\n" + + "fi\n\n"; + /** * Recursively gets the .git folder path from provided directory * @param {string} directory @@ -178,13 +185,7 @@ function _setHook(hook, command, projectRoot=process.cwd()) { return } - const prependScript = - "#!/bin/sh\n\n" + - 'if [ "$SIMPLE_GIT_HOOKS" = "0" ]; then\n' + - ' echo "[INFO] SIMPLE_GIT_HOOKS is set to 0, skipping hook."\n' + - " exit 0\n" + - "fi\n\n"; - const hookCommand = prependScript + command + const hookCommand = PREPEND_SCRIPT + command const hookDirectory = gitRoot + '/hooks/' const hookPath = path.normalize(hookDirectory + hook) @@ -367,4 +368,5 @@ module.exports = { getProjectRootDirectoryFromNodeModules, getGitProjectRoot, removeHooks, + PREPEND_SCRIPT } diff --git a/simple-git-hooks.test.js b/simple-git-hooks.test.js index 6e37377..b38880b 100644 --- a/simple-git-hooks.test.js +++ b/simple-git-hooks.test.js @@ -154,15 +154,7 @@ test('creates git hooks if configuration is correct from .simple-git-hooks.js', spc.setHooksFromConfig(projectWithConfigurationInAlternativeSeparateJsPath) const installedHooks = getInstalledGitHooks(path.normalize(path.join(projectWithConfigurationInAlternativeSeparateJsPath, '.git', 'hooks'))) - expect(JSON.stringify(installedHooks)).toBe(JSON.stringify({'pre-commit':`${"#!/bin/sh\n\n" + - 'if [ "$SIMPLE_GIT_HOOKS" = "0" ]; then\n' + - ' echo "[INFO] SIMPLE_GIT_HOOKS is set to 0, skipping hook."\n' + - " exit 0\n" + - "fi\n\n"}exit 1`, 'pre-push':`${"#!/bin/sh\n\n" + - 'if [ "$SIMPLE_GIT_HOOKS" = "0" ]; then\n' + - ' echo "[INFO] SIMPLE_GIT_HOOKS is set to 0, skipping hook."\n' + - " exit 0\n" + - "fi\n\n"}exit 1`})) + expect(JSON.stringify(installedHooks)).toBe(JSON.stringify({'pre-commit':`${spc.PREPEND_SCRIPT}exit 1`, 'pre-push':`${spc.PREPEND_SCRIPT}exit 1`})) }) @@ -171,15 +163,7 @@ test('creates git hooks if configuration is correct from .simple-git-hooks.cjs', spc.setHooksFromConfig(projectWithConfigurationInAlternativeSeparateCjsPath) const installedHooks = getInstalledGitHooks(path.normalize(path.join(projectWithConfigurationInAlternativeSeparateCjsPath, '.git', 'hooks'))) - expect(JSON.stringify(installedHooks)).toBe(JSON.stringify({'pre-commit':`${"#!/bin/sh\n\n" + - 'if [ "$SIMPLE_GIT_HOOKS" = "0" ]; then\n' + - ' echo "[INFO] SIMPLE_GIT_HOOKS is set to 0, skipping hook."\n' + - " exit 0\n" + - "fi\n\n"}exit 1`, 'pre-push':`${"#!/bin/sh\n\n" + - 'if [ "$SIMPLE_GIT_HOOKS" = "0" ]; then\n' + - ' echo "[INFO] SIMPLE_GIT_HOOKS is set to 0, skipping hook."\n' + - " exit 0\n" + - "fi\n\n"}exit 1`})) + expect(JSON.stringify(installedHooks)).toBe(JSON.stringify({'pre-commit':`${spc.PREPEND_SCRIPT}exit 1`, 'pre-push':`${spc.PREPEND_SCRIPT}exit 1`})) }) @@ -188,15 +172,7 @@ test('creates git hooks if configuration is correct from simple-git-hooks.cjs', spc.setHooksFromConfig(projectWithConfigurationInSeparateCjsPath) const installedHooks = getInstalledGitHooks(path.normalize(path.join(projectWithConfigurationInSeparateCjsPath, '.git', 'hooks'))) - expect(JSON.stringify(installedHooks)).toBe(JSON.stringify({'pre-commit':`${"#!/bin/sh\n\n" + - 'if [ "$SIMPLE_GIT_HOOKS" = "0" ]; then\n' + - ' echo "[INFO] SIMPLE_GIT_HOOKS is set to 0, skipping hook."\n' + - " exit 0\n" + - "fi\n\n"}exit 1`, 'pre-push':`${"#!/bin/sh\n\n" + - 'if [ "$SIMPLE_GIT_HOOKS" = "0" ]; then\n' + - ' echo "[INFO] SIMPLE_GIT_HOOKS is set to 0, skipping hook."\n' + - " exit 0\n" + - "fi\n\n"}exit 1`})) + expect(JSON.stringify(installedHooks)).toBe(JSON.stringify({'pre-commit':`${spc.PREPEND_SCRIPT}exit 1`, 'pre-push':`${spc.PREPEND_SCRIPT}exit 1`})) }) @@ -205,15 +181,7 @@ test('creates git hooks if configuration is correct from simple-git-hooks.js', ( spc.setHooksFromConfig(projectWithConfigurationInSeparateJsPath) const installedHooks = getInstalledGitHooks(path.normalize(path.join(projectWithConfigurationInSeparateJsPath, '.git', 'hooks'))) - expect(JSON.stringify(installedHooks)).toBe(JSON.stringify({'pre-commit':`${"#!/bin/sh\n\n" + - 'if [ "$SIMPLE_GIT_HOOKS" = "0" ]; then\n' + - ' echo "[INFO] SIMPLE_GIT_HOOKS is set to 0, skipping hook."\n' + - " exit 0\n" + - "fi\n\n"}exit 1`, 'pre-push':`${"#!/bin/sh\n\n" + - 'if [ "$SIMPLE_GIT_HOOKS" = "0" ]; then\n' + - ' echo "[INFO] SIMPLE_GIT_HOOKS is set to 0, skipping hook."\n' + - " exit 0\n" + - "fi\n\n"}exit 1`})) + expect(JSON.stringify(installedHooks)).toBe(JSON.stringify({'pre-commit':`${spc.PREPEND_SCRIPT}exit 1`, 'pre-push':`${spc.PREPEND_SCRIPT}exit 1`})) }) @@ -222,15 +190,7 @@ test('creates git hooks if configuration is correct from .simple-git-hooks.json' spc.setHooksFromConfig(projectWithConfigurationInAlternativeSeparateJsonPath) const installedHooks = getInstalledGitHooks(path.normalize(path.join(projectWithConfigurationInAlternativeSeparateJsonPath, '.git', 'hooks'))) - expect(JSON.stringify(installedHooks)).toBe(JSON.stringify({'pre-commit':`${"#!/bin/sh\n\n" + - 'if [ "$SIMPLE_GIT_HOOKS" = "0" ]; then\n' + - ' echo "[INFO] SIMPLE_GIT_HOOKS is set to 0, skipping hook."\n' + - " exit 0\n" + - "fi\n\n"}exit 1`, 'pre-push':`${"#!/bin/sh\n\n" + - 'if [ "$SIMPLE_GIT_HOOKS" = "0" ]; then\n' + - ' echo "[INFO] SIMPLE_GIT_HOOKS is set to 0, skipping hook."\n' + - " exit 0\n" + - "fi\n\n"}exit 1`})) + expect(JSON.stringify(installedHooks)).toBe(JSON.stringify({'pre-commit':`${spc.PREPEND_SCRIPT}exit 1`, 'pre-push':`${spc.PREPEND_SCRIPT}exit 1`})) }) @@ -239,15 +199,7 @@ test('creates git hooks if configuration is correct from simple-git-hooks.json', spc.setHooksFromConfig(projectWithConfigurationInSeparateJsonPath) const installedHooks = getInstalledGitHooks(path.normalize(path.join(projectWithConfigurationInSeparateJsonPath, '.git', 'hooks'))) - expect(JSON.stringify(installedHooks)).toBe(JSON.stringify({'pre-commit':`${"#!/bin/sh\n\n" + - 'if [ "$SIMPLE_GIT_HOOKS" = "0" ]; then\n' + - ' echo "[INFO] SIMPLE_GIT_HOOKS is set to 0, skipping hook."\n' + - " exit 0\n" + - "fi\n\n"}exit 1`, 'pre-push':`${"#!/bin/sh\n\n" + - 'if [ "$SIMPLE_GIT_HOOKS" = "0" ]; then\n' + - ' echo "[INFO] SIMPLE_GIT_HOOKS is set to 0, skipping hook."\n' + - " exit 0\n" + - "fi\n\n"}exit 1`})) + expect(JSON.stringify(installedHooks)).toBe(JSON.stringify({'pre-commit':`${spc.PREPEND_SCRIPT}exit 1`, 'pre-push':`${spc.PREPEND_SCRIPT}exit 1`})) }) @@ -256,11 +208,7 @@ test('creates git hooks if configuration is correct from package.json', () => { spc.setHooksFromConfig(projectWithConfigurationInPackageJsonPath) const installedHooks = getInstalledGitHooks(path.normalize(path.join(projectWithConfigurationInPackageJsonPath, '.git', 'hooks'))) - expect(JSON.stringify(installedHooks)).toBe(JSON.stringify({'pre-commit':`${"#!/bin/sh\n\n" + - 'if [ "$SIMPLE_GIT_HOOKS" = "0" ]; then\n' + - ' echo "[INFO] SIMPLE_GIT_HOOKS is set to 0, skipping hook."\n' + - " exit 0\n" + - "fi\n\n"}exit 1`})) + expect(JSON.stringify(installedHooks)).toBe(JSON.stringify({'pre-commit':`${spc.PREPEND_SCRIPT}exit 1`})) }) @@ -287,11 +235,7 @@ test('removes git hooks', () => { spc.setHooksFromConfig(projectWithConfigurationInPackageJsonPath) let installedHooks = getInstalledGitHooks(path.normalize(path.join(projectWithConfigurationInPackageJsonPath, '.git', 'hooks'))) - expect(JSON.stringify(installedHooks)).toBe(JSON.stringify({'pre-commit':`${"#!/bin/sh\n\n" + - 'if [ "$SIMPLE_GIT_HOOKS" = "0" ]; then\n' + - ' echo "[INFO] SIMPLE_GIT_HOOKS is set to 0, skipping hook."\n' + - " exit 0\n" + - "fi\n\n"}exit 1`})) + expect(JSON.stringify(installedHooks)).toBe(JSON.stringify({'pre-commit':`${spc.PREPEND_SCRIPT}exit 1`})) spc.removeHooks(projectWithConfigurationInPackageJsonPath) @@ -314,11 +258,7 @@ test('creates git hooks and removes unused git hooks', () => { spc.setHooksFromConfig(projectWithConfigurationInPackageJsonPath) installedHooks = getInstalledGitHooks(installedHooksDir); - expect(JSON.stringify(installedHooks)).toBe(JSON.stringify({'pre-commit':`${"#!/bin/sh\n\n" + - 'if [ "$SIMPLE_GIT_HOOKS" = "0" ]; then\n' + - ' echo "[INFO] SIMPLE_GIT_HOOKS is set to 0, skipping hook."\n' + - " exit 0\n" + - "fi\n\n"}exit 1`})) + expect(JSON.stringify(installedHooks)).toBe(JSON.stringify({'pre-commit':`${spc.PREPEND_SCRIPT}exit 1`})) }) @@ -337,11 +277,7 @@ test('creates git hooks and removes unused but preserves specific git hooks', () spc.setHooksFromConfig(projectWithUnusedConfigurationInPackageJsonPath) installedHooks = getInstalledGitHooks(installedHooksDir); - expect(JSON.stringify(installedHooks)).toBe(JSON.stringify({'commit-msg': '# do nothing', 'pre-commit':`${"#!/bin/sh\n\n" + - 'if [ "$SIMPLE_GIT_HOOKS" = "0" ]; then\n' + - ' echo "[INFO] SIMPLE_GIT_HOOKS is set to 0, skipping hook."\n' + - " exit 0\n" + - "fi\n\n"}exit 1`})) + expect(JSON.stringify(installedHooks)).toBe(JSON.stringify({'commit-msg': '# do nothing', 'pre-commit':`${spc.PREPEND_SCRIPT}exit 1`})) }) @@ -355,15 +291,7 @@ test.each([ spc.setHooksFromConfig(projectWithCustomConfigurationFilePath, args) const installedHooks = getInstalledGitHooks(path.normalize(path.join(projectWithCustomConfigurationFilePath, '.git', 'hooks'))) - expect(JSON.stringify(installedHooks)).toBe(JSON.stringify({'pre-commit':`${"#!/bin/sh\n\n" + - 'if [ "$SIMPLE_GIT_HOOKS" = "0" ]; then\n' + - ' echo "[INFO] SIMPLE_GIT_HOOKS is set to 0, skipping hook."\n' + - " exit 0\n" + - "fi\n\n"}exit 1`, 'pre-push':`${"#!/bin/sh\n\n" + - 'if [ "$SIMPLE_GIT_HOOKS" = "0" ]; then\n' + - ' echo "[INFO] SIMPLE_GIT_HOOKS is set to 0, skipping hook."\n' + - " exit 0\n" + - "fi\n\n"}exit 1`})) + expect(JSON.stringify(installedHooks)).toBe(JSON.stringify({'pre-commit':`${spc.PREPEND_SCRIPT}exit 1`, 'pre-push':`${spc.PREPEND_SCRIPT}exit 1`})) })