-
Notifications
You must be signed in to change notification settings - Fork 43
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add env var to bypass hooks execution #96
Changes from 5 commits
79136a8
3d0ff4c
c437eb7
10f1ded
6ad9fe4
062a539
d0b35e1
b506dbd
095089d
47dec20
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
const fs = require('fs') | ||
const spc = require("./simple-git-hooks"); | ||
const path = require("path") | ||
const { execSync } = require('child_process'); | ||
|
||
const { version: packageVersion } = require('./package.json'); | ||
|
||
|
@@ -126,108 +127,124 @@ function getInstalledGitHooks(hooksDir) { | |
return result | ||
} | ||
|
||
afterEach(() => { | ||
afterEach(() => { | ||
[ | ||
projectWithConfigurationInAlternativeSeparateJsPath, | ||
projectWithConfigurationInAlternativeSeparateCjsPath, | ||
projectWithConfigurationInSeparateCjsPath, | ||
projectWithConfigurationInSeparateJsPath, | ||
projectWithConfigurationInAlternativeSeparateJsonPath, | ||
projectWithConfigurationInSeparateJsonPath, | ||
projectWithConfigurationInPackageJsonPath, | ||
projectWithIncorrectConfigurationInPackageJson, | ||
projectWithoutConfiguration, | ||
projectWithConfigurationInPackageJsonPath, | ||
projectWithUnusedConfigurationInPackageJsonPath, | ||
projectWithCustomConfigurationFilePath, | ||
].forEach((testCase) => { | ||
delete process.env.SKIP_SIMPLE_GIT_HOOKS; | ||
removeGitHooksFolder(testCase); | ||
}); | ||
}); | ||
}); | ||
|
||
test('creates git hooks if configuration is correct from .simple-git-hooks.js', () => { | ||
createGitHooksFolder(projectWithConfigurationInAlternativeSeparateJsPath) | ||
|
||
spc.setHooksFromConfig(projectWithConfigurationInAlternativeSeparateJsPath) | ||
const installedHooks = getInstalledGitHooks(path.normalize(path.join(projectWithConfigurationInAlternativeSeparateJsPath, '.git', 'hooks'))) | ||
expect(JSON.stringify(installedHooks)).toBe(JSON.stringify({'pre-commit':`#!/bin/sh\nexit 1`, 'pre-push':`#!/bin/sh\nexit 1`})) | ||
|
||
removeGitHooksFolder(projectWithConfigurationInAlternativeSeparateJsPath) | ||
expect(JSON.stringify(installedHooks)).toBe(JSON.stringify({'pre-commit':`${spc.PREPEND_SCRIPT}exit 1`, 'pre-push':`${spc.PREPEND_SCRIPT}exit 1`})) | ||
}) | ||
|
||
|
||
test('creates git hooks if configuration is correct from .simple-git-hooks.cjs', () => { | ||
createGitHooksFolder(projectWithConfigurationInAlternativeSeparateCjsPath) | ||
|
||
spc.setHooksFromConfig(projectWithConfigurationInAlternativeSeparateCjsPath) | ||
const installedHooks = getInstalledGitHooks(path.normalize(path.join(projectWithConfigurationInAlternativeSeparateCjsPath, '.git', 'hooks'))) | ||
expect(JSON.stringify(installedHooks)).toBe(JSON.stringify({'pre-commit':`#!/bin/sh\nexit 1`, 'pre-push':`#!/bin/sh\nexit 1`})) | ||
|
||
removeGitHooksFolder(projectWithConfigurationInAlternativeSeparateCjsPath) | ||
expect(JSON.stringify(installedHooks)).toBe(JSON.stringify({'pre-commit':`${spc.PREPEND_SCRIPT}exit 1`, 'pre-push':`${spc.PREPEND_SCRIPT}exit 1`})) | ||
}) | ||
|
||
|
||
test('creates git hooks if configuration is correct from simple-git-hooks.cjs', () => { | ||
createGitHooksFolder(projectWithConfigurationInSeparateCjsPath) | ||
|
||
spc.setHooksFromConfig(projectWithConfigurationInSeparateCjsPath) | ||
const installedHooks = getInstalledGitHooks(path.normalize(path.join(projectWithConfigurationInSeparateCjsPath, '.git', 'hooks'))) | ||
expect(JSON.stringify(installedHooks)).toBe(JSON.stringify({'pre-commit':`#!/bin/sh\nexit 1`, 'pre-push':`#!/bin/sh\nexit 1`})) | ||
|
||
removeGitHooksFolder(projectWithConfigurationInSeparateCjsPath) | ||
expect(JSON.stringify(installedHooks)).toBe(JSON.stringify({'pre-commit':`${spc.PREPEND_SCRIPT}exit 1`, 'pre-push':`${spc.PREPEND_SCRIPT}exit 1`})) | ||
}) | ||
|
||
|
||
test('creates git hooks if configuration is correct from simple-git-hooks.js', () => { | ||
createGitHooksFolder(projectWithConfigurationInSeparateJsPath) | ||
|
||
spc.setHooksFromConfig(projectWithConfigurationInSeparateJsPath) | ||
const installedHooks = getInstalledGitHooks(path.normalize(path.join(projectWithConfigurationInSeparateJsPath, '.git', 'hooks'))) | ||
expect(JSON.stringify(installedHooks)).toBe(JSON.stringify({'pre-commit':`#!/bin/sh\nexit 1`, 'pre-push':`#!/bin/sh\nexit 1`})) | ||
|
||
removeGitHooksFolder(projectWithConfigurationInSeparateJsPath) | ||
expect(JSON.stringify(installedHooks)).toBe(JSON.stringify({'pre-commit':`${spc.PREPEND_SCRIPT}exit 1`, 'pre-push':`${spc.PREPEND_SCRIPT}exit 1`})) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think, its best to move this to separate variables, so the code not repeated: Im thinking of something like this: wyt?
Then we can use it in tests like so:
you can also try to use loadash compare functions, they can compare two objects deeply, so we won't need to use STRINGIFY hack There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. used lodash.isequal package for this |
||
}) | ||
|
||
|
||
test('creates git hooks if configuration is correct from .simple-git-hooks.json', () => { | ||
createGitHooksFolder(projectWithConfigurationInAlternativeSeparateJsonPath) | ||
|
||
spc.setHooksFromConfig(projectWithConfigurationInAlternativeSeparateJsonPath) | ||
const installedHooks = getInstalledGitHooks(path.normalize(path.join(projectWithConfigurationInAlternativeSeparateJsonPath, '.git', 'hooks'))) | ||
expect(JSON.stringify(installedHooks)).toBe(JSON.stringify({'pre-commit':`#!/bin/sh\nexit 1`, 'pre-push':`#!/bin/sh\nexit 1`})) | ||
|
||
removeGitHooksFolder(projectWithConfigurationInAlternativeSeparateJsonPath) | ||
expect(JSON.stringify(installedHooks)).toBe(JSON.stringify({'pre-commit':`${spc.PREPEND_SCRIPT}exit 1`, 'pre-push':`${spc.PREPEND_SCRIPT}exit 1`})) | ||
}) | ||
|
||
|
||
test('creates git hooks if configuration is correct from simple-git-hooks.json', () => { | ||
createGitHooksFolder(projectWithConfigurationInSeparateJsonPath) | ||
|
||
spc.setHooksFromConfig(projectWithConfigurationInSeparateJsonPath) | ||
const installedHooks = getInstalledGitHooks(path.normalize(path.join(projectWithConfigurationInSeparateJsonPath, '.git', 'hooks'))) | ||
expect(JSON.stringify(installedHooks)).toBe(JSON.stringify({'pre-commit':`#!/bin/sh\nexit 1`, 'pre-push':`#!/bin/sh\nexit 1`})) | ||
|
||
removeGitHooksFolder(projectWithConfigurationInSeparateJsonPath) | ||
expect(JSON.stringify(installedHooks)).toBe(JSON.stringify({'pre-commit':`${spc.PREPEND_SCRIPT}exit 1`, 'pre-push':`${spc.PREPEND_SCRIPT}exit 1`})) | ||
}) | ||
|
||
|
||
test('creates git hooks if configuration is correct from package.json', () => { | ||
createGitHooksFolder(projectWithConfigurationInPackageJsonPath) | ||
|
||
spc.setHooksFromConfig(projectWithConfigurationInPackageJsonPath) | ||
const installedHooks = getInstalledGitHooks(path.normalize(path.join(projectWithConfigurationInPackageJsonPath, '.git', 'hooks'))) | ||
expect(JSON.stringify(installedHooks)).toBe(JSON.stringify({'pre-commit':`#!/bin/sh\nexit 1`})) | ||
expect(JSON.stringify(installedHooks)).toBe(JSON.stringify({'pre-commit':`${spc.PREPEND_SCRIPT}exit 1`})) | ||
|
||
removeGitHooksFolder(projectWithConfigurationInPackageJsonPath) | ||
}) | ||
|
||
|
||
test('fails to create git hooks if configuration contains bad git hooks', () => { | ||
createGitHooksFolder(projectWithIncorrectConfigurationInPackageJson) | ||
|
||
expect(() => spc.setHooksFromConfig(projectWithIncorrectConfigurationInPackageJson)).toThrow('[ERROR] Config was not in correct format. Please check git hooks or options name') | ||
|
||
removeGitHooksFolder(projectWithIncorrectConfigurationInPackageJson) | ||
}) | ||
|
||
|
||
test('fails to create git hooks if not configured', () => { | ||
createGitHooksFolder(projectWithoutConfiguration) | ||
|
||
expect(() => spc.setHooksFromConfig(projectWithoutConfiguration)).toThrow('[ERROR] Config was not found! Please add `.simple-git-hooks.js` or `simple-git-hooks.js` or `.simple-git-hooks.json` or `simple-git-hooks.json` or `simple-git-hooks` entry in package.json.') | ||
|
||
removeGitHooksFolder(projectWithoutConfiguration) | ||
}) | ||
|
||
|
||
test('removes git hooks', () => { | ||
createGitHooksFolder(projectWithConfigurationInPackageJsonPath) | ||
|
||
spc.setHooksFromConfig(projectWithConfigurationInPackageJsonPath) | ||
|
||
let installedHooks = getInstalledGitHooks(path.normalize(path.join(projectWithConfigurationInPackageJsonPath, '.git', 'hooks'))) | ||
expect(JSON.stringify(installedHooks)).toBe(JSON.stringify({'pre-commit':`#!/bin/sh\nexit 1`})) | ||
expect(JSON.stringify(installedHooks)).toBe(JSON.stringify({'pre-commit':`${spc.PREPEND_SCRIPT}exit 1`})) | ||
|
||
spc.removeHooks(projectWithConfigurationInPackageJsonPath) | ||
|
||
installedHooks = getInstalledGitHooks(path.normalize(path.join(projectWithConfigurationInPackageJsonPath, '.git', 'hooks'))) | ||
expect(JSON.stringify(installedHooks)).toBe(JSON.stringify({})) | ||
|
||
removeGitHooksFolder(projectWithConfigurationInPackageJsonPath) | ||
}) | ||
|
||
|
||
test('creates git hooks and removes unused git hooks', () => { | ||
createGitHooksFolder(projectWithConfigurationInPackageJsonPath) | ||
|
||
|
@@ -241,9 +258,8 @@ 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\nexit 1`})) | ||
expect(JSON.stringify(installedHooks)).toBe(JSON.stringify({'pre-commit':`${spc.PREPEND_SCRIPT}exit 1`})) | ||
|
||
removeGitHooksFolder(projectWithConfigurationInPackageJsonPath) | ||
}) | ||
|
||
|
||
|
@@ -261,11 +277,11 @@ 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\nexit 1`})) | ||
expect(JSON.stringify(installedHooks)).toBe(JSON.stringify({'commit-msg': '# do nothing', 'pre-commit':`${spc.PREPEND_SCRIPT}exit 1`})) | ||
|
||
removeGitHooksFolder(projectWithUnusedConfigurationInPackageJsonPath) | ||
}) | ||
|
||
|
||
test.each([ | ||
['npx', 'simple-git-hooks', './git-hooks.js'], | ||
['node', require.resolve(`./cli`), './git-hooks.js'], | ||
|
@@ -275,7 +291,60 @@ 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\nexit 1`, 'pre-push':`#!/bin/sh\nexit 1`})) | ||
|
||
removeGitHooksFolder(projectWithCustomConfigurationFilePath) | ||
expect(JSON.stringify(installedHooks)).toBe(JSON.stringify({'pre-commit':`${spc.PREPEND_SCRIPT}exit 1`, 'pre-push':`${spc.PREPEND_SCRIPT}exit 1`})) | ||
}) | ||
|
||
|
||
test("bypasses hooks when SKIP_SIMPLE_GIT_HOOKS is set to 1", () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can also try grouping these test cases using |
||
execSync("git init \ | ||
&& git config user.name github-actions \ | ||
&& git config user.email [email protected]", { cwd: projectWithConfigurationInPackageJsonPath }); | ||
|
||
createGitHooksFolder(projectWithConfigurationInPackageJsonPath); | ||
|
||
spc.setHooksFromConfig(projectWithConfigurationInPackageJsonPath); | ||
process.env.SKIP_SIMPLE_GIT_HOOKS = '1'; // Set environment variable | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you might need to unset variable at the end of test case.. Maybe move to after Each ? Not sure There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, I was clearing it in global afterEach, now moved to local afterEach in describe simple git hooks env var tests |
||
let errorOccured = false; | ||
try { | ||
execSync('git add . && git commit --allow-empty -m "Test commit" && git commit --allow-empty -am "Change commit msg"', { | ||
cwd: projectWithConfigurationInPackageJsonPath, | ||
env: { ...process.env }, | ||
}); | ||
} catch (e) { | ||
console.log(e) | ||
console.log(e.stdout.toString()) | ||
console.log(e.stderr.toString()) | ||
errorOccured = true; | ||
} | ||
|
||
expect(errorOccured).toBe(false); | ||
}); | ||
|
||
|
||
test("hook executes when bypass var is not set", () => { | ||
execSync( | ||
"git init \ | ||
&& git config user.name github-actions \ | ||
&& git config user.email [email protected]", | ||
{ cwd: projectWithConfigurationInPackageJsonPath } | ||
); | ||
|
||
createGitHooksFolder(projectWithConfigurationInPackageJsonPath); | ||
|
||
spc.setHooksFromConfig(projectWithConfigurationInPackageJsonPath); | ||
|
||
let errorOccured = false; | ||
try { | ||
execSync( | ||
'git add . && git commit --allow-empty -m "Test commit" && git commit --allow-empty -am "Change commit msg"', | ||
{ | ||
cwd: projectWithConfigurationInPackageJsonPath, | ||
} | ||
); | ||
} catch (e) { | ||
errorOccured = true; | ||
} | ||
|
||
expect(errorOccured).toBe(true); | ||
}); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's best to also include use cases. I've seen people struggle with skipping git hooks when using 3rd party git clients
Normally (from the terminal) you can bypass hooks by using
--skip-hooks
option if im not mistaken :-)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Like so:
Using with 3rd party clients:
...