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