|
32 | 32 |
|
33 | 33 | # Standard Library Imports |
34 | 34 | import unittest |
| 35 | +from unittest.mock import patch |
| 36 | +import subprocess |
35 | 37 |
|
36 | 38 | # Third Party Imports |
37 | 39 |
|
38 | 40 | # Local Imports |
39 | | -from navigate._commit import get_git_revision_hash |
| 41 | +from navigate._commit import get_git_revision_hash, get_version_from_file |
40 | 42 |
|
41 | 43 |
|
42 | 44 | class TestGetGitRevisionHash(unittest.TestCase): |
43 | 45 | def test_return_type(self): |
44 | 46 | """Test that the function returns a string.""" |
45 | 47 | result = get_git_revision_hash() |
46 | 48 | self.assertIsInstance(result, str) |
| 49 | + |
| 50 | + @patch("navigate._commit.subprocess.check_output") |
| 51 | + def test_if_not_git_repo(self, mock_check_output): |
| 52 | + mock_check_output.side_effect = [ |
| 53 | + b"true", |
| 54 | + # Mock the return value for ["git", "rev-parse", "--is-inside-work-tree"] |
| 55 | + b"dummy_commit_hash" |
| 56 | + # Mock the return value for ["git", "rev-parse", "HEAD"] |
| 57 | + ] |
| 58 | + result = get_git_revision_hash() |
| 59 | + |
| 60 | + # Verify the correct calls were made to subprocess.check_output |
| 61 | + mock_check_output.assert_any_call( |
| 62 | + ["git", "rev-parse", "--is-inside-work-tree"], stderr=subprocess.DEVNULL |
| 63 | + ) |
| 64 | + mock_check_output.assert_any_call(["git", "rev-parse", "HEAD"]) |
| 65 | + |
| 66 | + # Assert the returned commit hash |
| 67 | + self.assertEqual(result, "dummy_commit_hash") |
| 68 | + |
| 69 | + @patch("navigate._commit.subprocess.check_output") |
| 70 | + def test_not_git_repo(self, mock_check_output): |
| 71 | + # throw subprocess.CalledProcessError |
| 72 | + mock_check_output.side_effect = subprocess.CalledProcessError(1, "git") |
| 73 | + |
| 74 | + result = get_git_revision_hash() |
| 75 | + |
| 76 | + # Assert that None is returned |
| 77 | + self.assertIsNone(result) |
| 78 | + |
| 79 | + |
| 80 | +class TestGetVersionFromFile(unittest.TestCase): |
| 81 | + def test_file_found(self): |
| 82 | + result = get_version_from_file() |
| 83 | + self.assertIsInstance(result, str) |
| 84 | + self.assertIsNot(result, "unknown") |
| 85 | + |
| 86 | + @patch("builtins.open") |
| 87 | + def test_file_not_found(self, mock_open): |
| 88 | + mock_open.side_effect = FileNotFoundError |
| 89 | + result = get_version_from_file() |
| 90 | + self.assertEqual(result, "unknown") |
0 commit comments