-
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
Conversation
@toplenboren can you please have a look at this, when you have time. Thank you 😄 |
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.
Hi! thanks for the PR! Looks good, but have a few points that can be polished :-)
@@ -158,6 +158,18 @@ You should use `--no-verify` option | |||
|
|||
https://bobbyhadz.com/blog/git-commit-skip-hooks#skip-git-commit-hooks | |||
|
|||
If you need to bypass hooks for multiple Git operations, setting the SKIP_SIMPLE_GIT_HOOKS environment variable can be more convenient. Once set, all subsequent Git operations in the same terminal session will bypass the associated hooks. |
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:
...
simple-git-hooks.test.js
Outdated
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 comment
The 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?
const TEST_SCRIPT =
${spc.PREPEND_SCRIPT}${exit 1}`
Then we can use it in tests like so:
expect(JSON.stringify(installedHooks)).toBe(JSON.stringify({'pre-commit':``${TEST_SCRIPT}
, 'pre-push':${spc.TEST_SCRIPT}
}))
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 comment
The reason will be displayed to describe this comment to others. Learn more.
used lodash.isequal package for this
simple-git-hooks.test.js
Outdated
}) | ||
|
||
|
||
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 comment
The reason will be displayed to describe this comment to others. Learn more.
You can also try grouping these test cases using describe
syntax. It it a good practice
simple-git-hooks.test.js
Outdated
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 comment
The 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 comment
The 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
simple-git-hooks.test.js
Outdated
expect(JSON.stringify(installedHooks)).toBe(JSON.stringify({'pre-commit':`#!/bin/sh\nexit 1`, 'pre-push':`#!/bin/sh\nexit 1`})) | ||
|
||
removeGitHooksFolder(projectWithConfigurationInSeparateCjsPath) | ||
expect(isEqual(installedHooks, { 'pre-commit': TEST_SCRIPT, 'pre-push': TEST_SCRIPT })).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.
I think we can extract { 'pre-commit': TEST_SCRIPT, 'pre-push': TEST_SCRIPT }
as variable too
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.
yes, extracted into COMMON_GIT_HOOKS variable
simple-git-hooks.test.js
Outdated
}) | ||
|
||
describe('Tests for skipping git hooks using SKIP_SIMPLE_GIT_HOOKS env var', () => { |
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 nice to see you taking a look to jest describe
syntax here!
But it makes no sense if it is used only in your tests. Let me try to be more clear on what I meant in the previous review:
So, we have our test code setup like this:
// Get project root directory
test('getProjectRootDirectory returns correct dir in typical case:', () => {
expect(spc.getProjectRootDirectoryFromNodeModules('var/my-project/node_modules/simple-git-hooks')).toBe('var/my-project')
})
test('getProjectRootDirectory returns correct dir when used with windows delimiters:', () => {
expect(spc.getProjectRootDirectoryFromNodeModules('user\\allProjects\\project\\node_modules\\simple-git-hooks')).toBe('user/allProjects/project')
})
We can use describe to make it look like this:
describe('Get Project Root Directory tests', () => {
it('returns correct dir in typical case:', () => {
expect(spc.getProjectRootDirectoryFromNodeModules('var/my-project/node_modules/simple-git-hooks')).toBe('var/my-project')
})
it('returns correct dir when used with windows delimiters:', () => {
expect(spc.getProjectRootDirectoryFromNodeModules('user\\allProjects\\project\\node_modules\\simple-git-hooks')).toBe('user/allProjects/project')
})
})
This way tests can be grouped, which allows us to see pretty output: here is an example if we do the manipulations above
--
I propose two solutions:
- refactor all the tests to use describe syntax
- or simply leave your tests as is (do not use describe syntax) - I will do refactoring later :-)
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.
I have used describe, it syntax, let me if it needs further work
simple-git-hooks.test.js
Outdated
|
||
describe('Tests for skipping git hooks using SKIP_SIMPLE_GIT_HOOKS env var', () => { | ||
|
||
const GIT_USER_NAME = "github-actions"; |
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.
I think we can do this in GitHub actions itself, do not see why we need some GH actions specific code in repo
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.
In the tests, I'm performing commit git operation which requires to set username and email, so I have used random values, let me know if it can be done in a better way
@toplenboren added the improvements that you have suggested, can you please have a look when you get some time. Thank you |
Hi @toplenboren, pinging to see if we can merge this pr, or need anymore changes |
Hi, I think I already approved it last time, but will review your PR again this weekends Thanks |
@toplenboren any expected timeline for merging this PR ? |
Yep. gonna do it this week |
Hi @toplenboren, thanks for merging the PR and many thanks to @bhanuprasadcherukuvada for adding the feature 🙏 it's very handy for my current use-case! Can I kindly request that this feature be made available through a new NPM version? After some debugging I found out that this feature isn't available in the current latest version of simple-git-hooks, even though the GitHub |
@@ -44,6 +44,7 @@ | |||
"clean-publish": "^4.2.0", | |||
"eslint": "^7.19.0", | |||
"jest": "^26.6.3", | |||
"lint-staged": "^10.5.4" | |||
"lint-staged": "^10.5.4", | |||
"lodash.isequal": "^4.5.0" |
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.
What is this? I saw no usage?
ref -
#87