How to link pre-commit git hook with rspec tests and prevent commit?

How can I add a pre-commit hook that will run my rspec tests, and if any failure fails to complete the commit. I can run the tests, but not prevent the commit when they fail.

I copied .git/hooks/pre-commit.sample to .git/hooks/pre-commit

I added an rspec spec at the bottom.

Testing is performed as part of a commit ... but a failure does not stop completing a commit.

 $ git commit -m'test' ....................................................................................................F............. Failures: 1) Link Good url SHOULD be valid Failure/Error: expect(link.valid_get?).to be false #true expected false got true # ./spec/models/link_spec.rb:26:in `block (2 levels) in <top (required)>' Finished in 32.78 seconds (files took 3.58 seconds to load) 114 examples, 1 failure Failed examples: rspec ./spec/models/link_spec.rb:24 # Link Good url SHOULD be valid [79230846_hook_to_run_tests 6c09570] test 1 file changed, 1 insertion(+) create mode 100644 xx 

Perhaps I need another way to run rspec tests that will raise the non-zero error that I need?

It is currently located below:

  ... echo echo " git config hooks.allownonascii true" echo exit 1 fi rspec spec exec git diff-index --check --cached $against -- 

I do all this in a branch (and not in a master). I didn't know if that mattered.

+6
source share
2 answers

I found that using

 exec rspec spec 

in

 .git/hooks/pre-commit 

gave me the desired functionality -
commit commits rspec trigger tests, and commit only completes if all rspec tests pass.

+6
source

Should pre-commit itself exit null, or does it just call rspec?

Perhaps all you have to do is add exit $? after calling rspec.

+1
source

Source: https://habr.com/ru/post/975585/


All Articles