Commit in git only if tests pass

前端 未结 3 450
春和景丽
春和景丽 2020-12-13 00:24

I\'ve recently started using git, and also begun unit testing (using Python\'s unittest module). I\'d like to run my tests each time I commit, and only commit i

相关标签:
3条回答
  • 2020-12-13 00:47

    Could you parse the result of the python test session and make sure to exit your pre-commit hook with a non-zero status?

    The hook should exit with non-zero status after issuing an appropriate message if it wants to stop the commit.

    So if your python script does not return the appropriate status for any reason, you need to determine that status directly from the pre-commit hook script.
    That would ensure the commit does not go forward if the tests failed.
    (or you could call from the hook a python wrapper which would call the tests, and ensure a sys.exit(exit_status) according to the test results).

    0 讨论(0)
  • 2020-12-13 01:00

    I would check to make sure that each step of the way, your script returns a non-zero exit code on failure. Check to see if your python3.1 foo.py --test returns a non-zero exit code if a test fails. Check to make sure your make test command returns a non-zero exit code. And finally, check that your pre-commit hook itself returns a non-zero exit code on failure.

    You can check for a non-zero exit code by adding || echo $? to the end of a command; that will print out the exit code if the command failed.

    The following example works for me (I'm redirecting stderr to /dev/null to avoid including too much extraneous output here):

    $ python3.1 test.py 2>/dev/null || echo $?
    1
    $ make test 2>/dev/null || echo $?
    python3.1 test.py
    2
    $ .git/hooks/pre-commit 2>/dev/null || echo $?
    python3.1 test.py
    1
    

    test.py:

    import unittest
    
    class TestFailure(unittest.TestCase):
        def testFail(self):
            assert(False)
    
    if __name__ == '__main__':
        unittest.main()
    

    Makefile:

    test:
        python3.1 test.py
    

    .git/hooks/pre-commit:

    #!/bin/sh
    make test || exit 1
    

    Note the || exit 1. This isn't necessary if make test is the last command in the hook, as the exit status of the last command will be the exit status of the script. But if you have later checks in your pre-commit hook, then you need to make sure you exit with an error; otherwise, a successful command at the end of the hook will cause your script to exit with a status of 0.

    0 讨论(0)
  • 2020-12-13 01:03

    Another option, if you don't want to handle manually pre-commit's: There is nice tool to run tests and syntax checks for Python, Ruby and so on: github/overcommit

    0 讨论(0)
提交回复
热议问题