|
9 | 9 |
|
10 | 10 | class TestGit(unittest.TestCase):
|
11 | 11 | @mock.patch('subprocess.check_output', return_value=GIT_HASH_FULL)
|
12 |
| - @mock.patch('os.path.exists', return_value=True) |
13 |
| - def test_get_hash_with_default_argument(self, exists_mock, check_output_mock): |
| 12 | + @mock.patch('git.is_git_repository', return_value=True) |
| 13 | + def test_get_hash_with_default_argument(self, is_git_repository_mock, check_output_mock): |
14 | 14 | git_hash = git.get_hash()
|
15 |
| - exists_mock.assert_called_once_with('.git') |
| 15 | + is_git_repository_mock.assert_called_once() |
16 | 16 | check_output_mock.assert_called_once_with(['git', 'rev-parse', 'HEAD'])
|
17 | 17 | self.assertEqual(git_hash, GIT_HASH_FULL.decode('utf-8'))
|
18 | 18 |
|
19 | 19 | @mock.patch('subprocess.check_output', return_value=GIT_HASH_FULL)
|
20 |
| - @mock.patch('os.path.exists', return_value=True) |
21 |
| - def test_get_full_hash(self, exists_mock, check_output_mock): |
| 20 | + @mock.patch('git.is_git_repository', return_value=True) |
| 21 | + def test_get_full_hash(self, is_git_repository_mock, check_output_mock): |
22 | 22 | git_hash = git.get_hash(short=False)
|
23 |
| - exists_mock.assert_called_once_with('.git') |
| 23 | + is_git_repository_mock.assert_called_once() |
24 | 24 | check_output_mock.assert_called_once_with(['git', 'rev-parse', 'HEAD'])
|
25 | 25 | self.assertEqual(git_hash, GIT_HASH_FULL.decode('utf-8'))
|
26 | 26 |
|
27 | 27 | @mock.patch('subprocess.check_output', return_value=GIT_HASH_SHORT)
|
28 |
| - @mock.patch('os.path.exists', return_value=True) |
29 |
| - def test_get_short_hash(self, exists_mock, check_output_mock): |
| 28 | + @mock.patch('git.is_git_repository', return_value=True) |
| 29 | + def test_get_short_hash(self, is_git_repository_mock, check_output_mock): |
30 | 30 | git_hash = git.get_hash(short=True)
|
31 |
| - exists_mock.assert_called_once_with('.git') |
| 31 | + is_git_repository_mock.assert_called_once() |
32 | 32 | check_output_mock.assert_called_once_with(['git', 'rev-parse', '--short', 'HEAD'])
|
33 | 33 | self.assertEqual(git_hash, GIT_HASH_SHORT.decode('utf-8'))
|
34 | 34 |
|
35 |
| - @mock.patch('subprocess.check_output') |
| 35 | + @mock.patch('git.is_git_repository', return_value=False) |
| 36 | + def test_not_in_git_project(self, is_git_repository_mock): |
| 37 | + self.assertEqual(git.get_hash(), 'none') |
| 38 | + is_git_repository_mock.assert_called_once() |
| 39 | + |
| 40 | + @mock.patch('subprocess.call', return_value=0) |
36 | 41 | @mock.patch('os.path.exists', return_value=False)
|
37 |
| - def test_not_in_git_project(self, exists_mock, check_output_mock): |
38 |
| - git_hash = git.get_hash() |
| 42 | + def test_should_be_in_git_project(self, call_mock): |
| 43 | + self.assertTrue(git.is_git_repository()) |
| 44 | + call_mock.assert_called_once_with(['git', 'rev-parse', '--is-inside-work-tree']) |
| 45 | + |
| 46 | + @mock.patch('subprocess.call', return_value=1) |
| 47 | + @mock.patch('os.path.exists', return_value=False) |
| 48 | + def test_should_not_be_in_git_project(self, exists_mock, call_mock): |
| 49 | + self.assertFalse(git.is_git_repository()) |
39 | 50 | exists_mock.assert_called_once_with('.git')
|
40 |
| - check_output_mock.assert_not_called() |
41 |
| - self.assertEqual(git_hash, 'none') |
| 51 | + call_mock.assert_called_once_with(['git', 'rev-parse', '--is-inside-work-tree']) |
0 commit comments