Skip to content

Commit 9302ce3

Browse files
Merge pull request #935 from TheDeanLab/2024-07-10
Update test_commit.py
2 parents 2972303 + 814a606 commit 9302ce3

File tree

1 file changed

+45
-1
lines changed

1 file changed

+45
-1
lines changed

test/test_commit.py

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,59 @@
3232

3333
# Standard Library Imports
3434
import unittest
35+
from unittest.mock import patch
36+
import subprocess
3537

3638
# Third Party Imports
3739

3840
# Local Imports
39-
from navigate._commit import get_git_revision_hash
41+
from navigate._commit import get_git_revision_hash, get_version_from_file
4042

4143

4244
class TestGetGitRevisionHash(unittest.TestCase):
4345
def test_return_type(self):
4446
"""Test that the function returns a string."""
4547
result = get_git_revision_hash()
4648
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

Comments
 (0)