diff options
author | Joris Roovers <jroovers@cisco.com> | 2015-10-02 15:21:44 +0200 |
---|---|---|
committer | Joris Roovers <jroovers@cisco.com> | 2015-10-02 15:21:44 +0200 |
commit | b3dd35c1ea8d5fb25382919a8df8eb5523bd65db (patch) | |
tree | c94dadff043ed7879b09b54463de2e42925809c6 | |
parent | 4c07cf62cb1ffc262e64dfd25edd7b350c030fa2 (diff) |
Unit tests for getting info from the local git repo
The code that queries the local git repo for the last commit message and
changed files is now also properly unit tested.
-rw-r--r-- | gitlint/git.py | 2 | ||||
-rw-r--r-- | gitlint/tests/test_git.py | 20 | ||||
-rwxr-xr-x | run_tests.sh | 2 |
3 files changed, 19 insertions, 5 deletions
diff --git a/gitlint/git.py b/gitlint/git.py index 563ec33..ef2a714 100644 --- a/gitlint/git.py +++ b/gitlint/git.py @@ -35,7 +35,7 @@ class GitContext(object): """ Sets the commit message by parsing a given string into the different parts of a commit message """ lines = [line for line in commit_msg_str.split("\n") if not line.startswith("#")] full = "\n".join(lines) - title = lines[0] + title = lines[0] if len(lines) > 0 else "" body = lines[1:] if len(lines) > 1 else [] self.commit_msg = GitCommitMessage(original=commit_msg_str, full=full, title=title, body=body) diff --git a/gitlint/tests/test_git.py b/gitlint/tests/test_git.py index 16e0955..02fcec5 100644 --- a/gitlint/tests/test_git.py +++ b/gitlint/tests/test_git.py @@ -1,11 +1,25 @@ from gitlint.tests.base import BaseTestCase from gitlint.git import GitContext +from mock import patch + class GitTests(BaseTestCase): - def test_get_latest_commit(self): - # Some issues with mocking out the 'sh' library. Need to investigate this further. - pass + @patch('gitlint.git.sh') + def test_get_latest_commit(self, sh): + sh.git.log.return_value = "commit-title\n\ncommit-body" + sh.git.return_value = "file1.txt\npath/to/file2.txt\n" + + context = GitContext.from_local_repository() + + # assert that commit message was read using git command + sh.git.log.assert_called_once_with('-1', '--pretty=%B', _tty_out=False) + self.assertEqual(context.commit_msg.title, "commit-title") + self.assertEqual(context.commit_msg.body, ["", "commit-body"]) + + # assert that changed files are read using git command + sh.git.assert_called_once_with('diff-tree', '--no-commit-id', '--name-only', '-r', 'HEAD', _tty_out=False) + self.assertListEqual(context.changed_files, ["file1.txt", "path/to/file2.txt"]) def test_set_commit_msg_full(self): gitcontext = GitContext() diff --git a/run_tests.sh b/run_tests.sh index d7c46a7..2e01efe 100755 --- a/run_tests.sh +++ b/run_tests.sh @@ -34,7 +34,7 @@ run_unit_tests(){ # this way, you can pass a test file path to the CLI which is convenient testargs="${testargs//\//.}" # replace slashes with dots testargs="${testargs/.py/}" # remove trailing .py - coverage run -m discover -v "$testargs" + coverage run -m unittest -v "$testargs" else coverage run -m discover -v fi |