|
14 | 14 | importshutil
|
15 | 15 | importsubprocess
|
16 | 16 | importsys
|
17 |
| -fromtempfileimportTemporaryFile |
| 17 | +importtempfile |
18 | 18 | fromunittestimportskipUnless
|
19 | 19 |
|
20 | 20 | ifsys.version_info>= (3,8):
|
@@ -67,6 +67,24 @@ def _rollback_refresh():
|
67 | 67 | refresh()
|
68 | 68 |
|
69 | 69 |
|
| 70 | +@contextlib.contextmanager |
| 71 | +def_fake_git(): |
| 72 | +fake_output="git version 123.456.789 (fake)" |
| 73 | + |
| 74 | +withtempfile.TemporaryDirectory()astdir: |
| 75 | +ifos.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 | +yieldstr(fake_git.absolute()) |
| 86 | + |
| 87 | + |
70 | 88 | @ddt.ddt
|
71 | 89 | classTestGit(TestBase):
|
72 | 90 | @classmethod
|
@@ -260,7 +278,7 @@ def test_it_ignores_false_kwargs(self, git):
|
260 | 278 | self.assertTrue("pass_this_kwarg"notingit.call_args[1])
|
261 | 279 |
|
262 | 280 | deftest_it_raises_proper_exception_with_output_stream(self):
|
263 |
| -withTemporaryFile()astmp_file: |
| 281 | +withtempfile.TemporaryFile()astmp_file: |
264 | 282 | withself.assertRaises(GitCommandError):
|
265 | 283 | self.git.checkout("non-existent-branch",output_stream=tmp_file)
|
266 | 284 |
|
@@ -483,6 +501,16 @@ def test_refresh_with_good_relative_git_path_arg(self):
|
483 | 501 | refresh(basename)
|
484 | 502 | self.assertEqual(self.git.GIT_PYTHON_GIT_EXECUTABLE,absolute_path)
|
485 | 503 |
|
| 504 | +deftest_version_info_is_cached(self): |
| 505 | +with_rollback_refresh(): |
| 506 | +with_fake_git()aspath: |
| 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 | + |
486 | 514 | deftest_options_are_passed_to_git(self):
|
487 | 515 | # This works because any command after git --version is ignored.
|
488 | 516 | git_version=self.git(version=True).NoOp()
|
|