如何预先提交git hook运行rspec测试并阻止提交?

时间:2014-09-20 21:20:34

标签: ruby-on-rails git rspec githooks pre-commit-hook

如何添加将运行我的rspec测试的预提交挂钩,如果有任何失败,将无法完成提交。我可以让测试运行但不会阻止它们的任何失败提交

我已将.git/hooks/pre-commit.sample复制到.git/hooks/pre-commit

我在底部附近添加了rspec spec

测试作为提交的一部分运行...但是失败并不能阻止提交完成。

$ 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 x.x 

也许我需要一种不同的方式来运行rspec测试,这会引发我需要的非零错误?

目前它位于底部:

    ...
    echo
    echo "  git config hooks.allownonascii true"
    echo
    exit 1
fi

rspec spec

exec git diff-index --check --cached $against --

我在一个分支(不是主人)中做这一切。不知道这是否相关。

2 个答案:

答案 0 :(得分:8)

我发现使用

exec rspec spec

.git/hooks/pre-commit

给了我所需的功能 -
提交尝试触发rspec测试运行,并且只有在所有rspec测试通过后才提交提交。

答案 1 :(得分:1)

如果预提交挂钩本身正在执行退出零,或者它只是调用rspec?

您可能需要做的就是在rspec调用之后添加exit $?

相关问题