Skip to content

Commit 1de2fdc

Browse files
committed
Test that version_info caches
This begins to test the established version_info caching behavior.
1 parent e40fc2c commit 1de2fdc

File tree

1 file changed

+30
-2
lines changed

1 file changed

+30
-2
lines changed

test/test_git.py

+30-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import shutil
1515
import subprocess
1616
import sys
17-
from tempfile import TemporaryFile
17+
import tempfile
1818
from unittest import skipUnless
1919

2020
if sys.version_info >= (3, 8):
@@ -67,6 +67,24 @@ def _rollback_refresh():
6767
refresh()
6868

6969

70+
@contextlib.contextmanager
71+
def _fake_git():
72+
fake_output = "git version 123.456.789 (fake)"
73+
74+
with tempfile.TemporaryDirectory() as tdir:
75+
if os.name == "nt":
76+
fake_git = Path(tdir, "fake-git.cmd")
77+
script = f"@echo {fake_output}\n"
78+
fake_git.write_text(script, encoding="utf-8")
79+
else:
80+
fake_git = Path(tdir, "fake-git")
81+
script = f"#!/bin/sh\necho '{fake_output}'\n"
82+
fake_git.write_text(script, encoding="utf-8")
83+
fake_git.chmod(0o755)
84+
85+
yield str(fake_git.absolute())
86+
87+
7088
@ddt.ddt
7189
class TestGit(TestBase):
7290
@classmethod
@@ -260,7 +278,7 @@ def test_it_ignores_false_kwargs(self, git):
260278
self.assertTrue("pass_this_kwarg" not in git.call_args[1])
261279

262280
def test_it_raises_proper_exception_with_output_stream(self):
263-
with TemporaryFile() as tmp_file:
281+
with tempfile.TemporaryFile() as tmp_file:
264282
with self.assertRaises(GitCommandError):
265283
self.git.checkout("non-existent-branch", output_stream=tmp_file)
266284

@@ -483,6 +501,16 @@ def test_refresh_with_good_relative_git_path_arg(self):
483501
refresh(basename)
484502
self.assertEqual(self.git.GIT_PYTHON_GIT_EXECUTABLE, absolute_path)
485503

504+
def test_version_info_is_cached(self):
505+
with _rollback_refresh():
506+
with _fake_git() as path:
507+
new_git = Git() # Not cached yet.
508+
refresh(path)
509+
version_info = new_git.version_info # Caches the value.
510+
self.assertEqual(version_info, (123, 456, 789))
511+
os.remove(path) # Arrange that reading a second time would fail.
512+
self.assertEqual(new_git.version_info, version_info) # Cached value.
513+
486514
def test_options_are_passed_to_git(self):
487515
# This works because any command after git --version is ignored.
488516
git_version = self.git(version=True).NoOp()

0 commit comments

Comments
 (0)