diff options
| author | satahippy <satahippy@gmail.com> | 2017-10-30 23:00:18 +0200 | 
|---|---|---|
| committer | satahippy <satahippy@gmail.com> | 2017-10-30 23:00:32 +0200 | 
| commit | eae04bf7b0620a0ef950dd39af7f07f3c88fd15f (patch) | |
| tree | 8bb4911b668b66a009ca4a45afc1e3a0e974d7be /git/test/test_index.py | |
| parent | c7f657fb20c063dfc2a653f050accc9c40d06a60 (diff) | |
| download | gitpython-eae04bf7b0620a0ef950dd39af7f07f3c88fd15f.tar.gz | |
IndexFile.commit() now runs pre-commit and post-commit and commit-msg hooks.
Diffstat (limited to 'git/test/test_index.py')
| -rw-r--r-- | git/test/test_index.py | 114 | 
1 files changed, 85 insertions, 29 deletions
diff --git a/git/test/test_index.py b/git/test/test_index.py index e8d38a09..cf746140 100644 --- a/git/test/test_index.py +++ b/git/test/test_index.py @@ -729,35 +729,6 @@ class TestIndex(TestBase):              assert fkey not in index.entries          index.add(files, write=True) -        if is_win: -            hp = hook_path('pre-commit', index.repo.git_dir) -            hpd = osp.dirname(hp) -            if not osp.isdir(hpd): -                os.mkdir(hpd) -            with open(hp, "wt") as fp: -                fp.write("#!/usr/bin/env sh\necho stdout; echo stderr 1>&2; exit 1") -            # end -            os.chmod(hp, 0o744) -            try: -                index.commit("This should fail") -            except HookExecutionError as err: -                if is_win: -                    self.assertIsInstance(err.status, OSError) -                    self.assertEqual(err.command, [hp]) -                    self.assertEqual(err.stdout, '') -                    self.assertEqual(err.stderr, '') -                    assert str(err) -                else: -                    self.assertEqual(err.status, 1) -                    self.assertEqual(err.command, hp) -                    self.assertEqual(err.stdout, 'stdout\n') -                    self.assertEqual(err.stderr, 'stderr\n') -                    assert str(err) -            else: -                raise AssertionError("Should have cought a HookExecutionError") -            # end exception handling -            os.remove(hp) -        # end hook testing          nc = index.commit("2 files committed", head=False)          for fkey in keys: @@ -859,3 +830,88 @@ class TestIndex(TestBase):          r = Repo.init(rw_dir)          r.index.add([fp])          r.index.commit('Added [.exe') + +    @with_rw_repo('HEAD', bare=True) +    def test_pre_commit_hook_success(self, rw_repo): +        index = rw_repo.index +        hp = hook_path('pre-commit', index.repo.git_dir) +        hpd = osp.dirname(hp) +        if not osp.isdir(hpd): +            os.mkdir(hpd) +        with open(hp, "wt") as fp: +            fp.write("#!/usr/bin/env sh\nexit 0") +        os.chmod(hp, 0o744) +        index.commit("This should not fail") + +    @with_rw_repo('HEAD', bare=True) +    def test_pre_commit_hook_fail(self, rw_repo): +        index = rw_repo.index +        hp = hook_path('pre-commit', index.repo.git_dir) +        hpd = osp.dirname(hp) +        if not osp.isdir(hpd): +            os.mkdir(hpd) +        with open(hp, "wt") as fp: +            fp.write("#!/usr/bin/env sh\necho stdout; echo stderr 1>&2; exit 1") +        os.chmod(hp, 0o744) +        try: +            index.commit("This should fail") +        except HookExecutionError as err: +            if is_win: +                self.assertIsInstance(err.status, OSError) +                self.assertEqual(err.command, [hp]) +                self.assertEqual(err.stdout, '') +                self.assertEqual(err.stderr, '') +                assert str(err) +            else: +                self.assertEqual(err.status, 1) +                self.assertEqual(err.command, [hp]) +                self.assertEqual(err.stdout, "\n  stdout: 'stdout\n'") +                self.assertEqual(err.stderr, "\n  stderr: 'stderr\n'") +                assert str(err) +        else: +            raise AssertionError("Should have cought a HookExecutionError") + +    @with_rw_repo('HEAD', bare=True) +    def test_commit_msg_hook_success(self, rw_repo): +        index = rw_repo.index +        commit_message = u"commit default head by Frèderic Çaufl€" +        from_hook_message = u"from commit-msg" + +        hp = hook_path('commit-msg', index.repo.git_dir) +        hpd = osp.dirname(hp) +        if not osp.isdir(hpd): +            os.mkdir(hpd) +        with open(hp, "wt") as fp: +            fp.write('#!/usr/bin/env sh\necho -n " {}" >> "$1"'.format(from_hook_message)) +        os.chmod(hp, 0o744) + +        new_commit = index.commit(commit_message) +        self.assertEqual(new_commit.message, u"{} {}".format(commit_message, from_hook_message)) + +    @with_rw_repo('HEAD', bare=True) +    def test_commit_msg_hook_fail(self, rw_repo): +        index = rw_repo.index +        hp = hook_path('commit-msg', index.repo.git_dir) +        hpd = osp.dirname(hp) +        if not osp.isdir(hpd): +            os.mkdir(hpd) +        with open(hp, "wt") as fp: +            fp.write("#!/usr/bin/env sh\necho stdout; echo stderr 1>&2; exit 1") +        os.chmod(hp, 0o744) +        try: +            index.commit("This should fail") +        except HookExecutionError as err: +            if is_win: +                self.assertIsInstance(err.status, OSError) +                self.assertEqual(err.command, [hp]) +                self.assertEqual(err.stdout, '') +                self.assertEqual(err.stderr, '') +                assert str(err) +            else: +                self.assertEqual(err.status, 1) +                self.assertEqual(err.command, [hp]) +                self.assertEqual(err.stdout, "\n  stdout: 'stdout\n'") +                self.assertEqual(err.stderr, "\n  stderr: 'stderr\n'") +                assert str(err) +        else: +            raise AssertionError("Should have cought a HookExecutionError")  | 
