Skip to content
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

Merged
merged 10 commits into from
Jan 4, 2024

Conversation

bhanufyi
Copy link
Contributor

@bhanufyi bhanufyi commented Sep 4, 2023

ref -
#87

@bhanufyi
Copy link
Contributor Author

bhanufyi commented Sep 9, 2023

@toplenboren can you please have a look at this, when you have time. Thank you 😄

Copy link
Owner

@toplenboren toplenboren left a 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 :-)

README.md Outdated Show resolved Hide resolved
simple-git-hooks.js Outdated Show resolved Hide resolved
simple-git-hooks.test.js Outdated Show resolved Hide resolved
@@ -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.
Copy link
Owner

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 :-)

Copy link
Owner

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:

...

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`}))
Copy link
Owner

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

Copy link
Contributor Author

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

})


test("bypasses hooks when SKIP_SIMPLE_GIT_HOOKS is set to 1", () => {
Copy link
Owner

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

createGitHooksFolder(projectWithConfigurationInPackageJsonPath);

spc.setHooksFromConfig(projectWithConfigurationInPackageJsonPath);
process.env.SKIP_SIMPLE_GIT_HOOKS = '1'; // Set environment variable
Copy link
Owner

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

Copy link
Contributor Author

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

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);
Copy link
Owner

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

Copy link
Contributor Author

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

})

describe('Tests for skipping git hooks using SKIP_SIMPLE_GIT_HOOKS env var', () => {
Copy link
Owner

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

Screenshot 2023-10-07 at 21 32 15

--

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 :-)

Copy link
Contributor Author

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


describe('Tests for skipping git hooks using SKIP_SIMPLE_GIT_HOOKS env var', () => {

const GIT_USER_NAME = "github-actions";
Copy link
Owner

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

Copy link
Contributor Author

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

@bhanufyi
Copy link
Contributor Author

bhanufyi commented Nov 3, 2023

@toplenboren added the improvements that you have suggested, can you please have a look when you get some time. Thank you

@bhanufyi
Copy link
Contributor Author

Hi @toplenboren, pinging to see if we can merge this pr, or need anymore changes

@toplenboren
Copy link
Owner

Hi, I think I already approved it last time, but will review your PR again this weekends

Thanks

@bhanufyi
Copy link
Contributor Author

bhanufyi commented Jan 3, 2024

@toplenboren any expected timeline for merging this PR ?

@toplenboren
Copy link
Owner

toplenboren commented Jan 3, 2024

@toplenboren any expected timeline for merging this PR ?

Yep. gonna do it this week

@toplenboren toplenboren merged commit 20121df into toplenboren:master Jan 4, 2024
4 checks passed
@jochemkeller
Copy link

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 master README documentation shows that the feature should be available 🤪 that was a confusing 15 minutes there..

@@ -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"
Copy link
Contributor

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants