Tests fail w/root `pytest` but succeed if test file is run directly.
What changed, and why it matters
This commit adds a new test file to fix a test-suite quirk where tests passed when run individually but failed when run with the root `pytest` command. It is purely a testing/development change and does not alter any production code or security behavior.
No security action needed. Treat as a normal quality/test-coverage improvement.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The commit introduces tests/test_version.py containing unit tests for seedsigner.helpers.version.Version. The tests mock filesystem and git state to verify version string generation. No application code is modified; the change only expands test coverage and resolves a test-discovery/execution issue.
Changed components
tests/test_version.pyInspect captured patch +84 / −0
diff --git a/tests/test_version.py b/tests/test_version.py
new file mode 100644
index 0000000..8006966
--- /dev/null
+++ b/tests/test_version.py
@@ -0,0 +1,84 @@
+from datetime import datetime
+import os
+from unittest import mock
+
+# Must import this before any SeedSigner imports
+from base import BaseTest
+
+from seedsigner.helpers.version import Version
+
+
+
+class TestVersion(BaseTest):
+ def test_version_with_no_git_head(self):
+ """
+ If there is no .git/HEAD file, the hard-coded VERSION constant should be returned.
+ """
+ # mock out the os.path.exists call to always return False
+ with mock.patch("os.path.exists", return_value=False):
+ version = Version.get_version()
+ assert version == f"v{Version.VERSION}"
+
+
+ def test_version_with_actual_git_head(self):
+ """
+ If the .git dir exists on our actual filesystem right now, we should get a version
+ based on its contents.
+
+ NOTE: This test is potentially fragile as it depends on the test runner system's actual
+ git state. The get_version() call should be able to handle all possible git states, but
+ this does create the possibility of external variability.
+ """
+ fake_hardcoded_version = "fake.version.123"
+ with mock.patch.object(Version, 'VERSION', fake_hardcoded_version):
+ git_dot_dir = Version._get_dot_git_dir()
+ if os.path.exists(git_dot_dir):
+ assert Version.get_version() != f"v{fake_hardcoded_version}"
+ else:
+ # if there's no .git dir, mark this test as skipped
+ self.skipTest(f"No .git dir found at {git_dot_dir}, skipping test.")
+
+
+ def test_version_with_mocked_git_head(self):
+ """
+ If there is a .git/HEAD file, the version should report the current git branch
+ name, commit hash, or a matching tag.
+ """
+ with mock.patch("os.path.exists", return_value=True):
+ # Mock the HEAD file read in _read_HEAD_file to return our fake branch name
+ branch_name = "my_feature_branch"
+ git_HEAD_content = f"ref: refs/heads/{branch_name}"
+ with mock.patch("builtins.open", mock.mock_open(read_data=git_HEAD_content)):
+ version = Version.get_version()
+ assert version == branch_name
+
+ # Mock the HEAD file read in _read_HEAD_file to return a fake commit hash
+ commit_hash = "abcdef1234567890"
+ with mock.patch("builtins.open", mock.mock_open(read_data=commit_hash)):
+ # Mock that there are no matching tags for this commit hash
+ with mock.patch.object(Version, '_get_matching_tag', return_value=None):
+ version = Version.get_version()
+ assert version == commit_hash[:7] # short commit hash
+
+ # Now mock that there is a matching tag for this commit hash
+ tag_name = "v1.2.3"
+ with mock.patch.object(Version, '_get_matching_tag', return_value=tag_name):
+ version = Version.get_version()
+ assert version == tag_name
+
+
+ def test_get_last_src_edit(self):
+ """
+ Test that get_last_src_edit returns a sane datetime object. Assumes the system
+ running this test has a reasonably correct system time.
+ """
+ last_edit = Version.get_last_src_edit()
+ assert isinstance(last_edit, datetime)
+
+ # Has to be more recent than the first SeedSigner v0.0.1 release
+ known_past = datetime(2020, 12, 13)
+ assert last_edit > known_past
+
+ # Could not have happened tomorrow
+ known_future = datetime.now().replace(year=datetime.now().year + 1)
+ assert last_edit < known_future
\ No newline at end of file
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.