Skip to content

Commit

Permalink
feat: export prepend script, reuse in tests
Browse files Browse the repository at this point in the history
bhanufyi committed Sep 24, 2023

Verified

This commit was signed with the committer’s verified signature.
blizzz Arthur Schiwon
1 parent c437eb7 commit 10f1ded
Showing 2 changed files with 20 additions and 90 deletions.
16 changes: 9 additions & 7 deletions simple-git-hooks.js
Original file line number Diff line number Diff line change
@@ -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
}
94 changes: 11 additions & 83 deletions simple-git-hooks.test.js
Original file line number Diff line number Diff line change
@@ -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`}))
})


0 comments on commit 10f1ded

Please sign in to comment.