|
34 | 34 | )
|
35 | 35 | from git.index.fun import hook_path
|
36 | 36 | from git.index.typ import BaseIndexEntry, IndexEntry
|
| 37 | +from git.index.util import TemporaryFileSwap |
37 | 38 | from git.objects import Blob
|
38 |
| -from test.lib import TestBase, fixture, fixture_path, with_rw_directory, with_rw_repo |
39 | 39 | from git.util import Actor, hex_to_bin, rmtree
|
40 | 40 | from gitdb.base import IStream
|
| 41 | +from test.lib import TestBase, fixture, fixture_path, with_rw_directory, with_rw_repo |
41 | 42 |
|
42 | 43 | HOOKS_SHEBANG = "#!/usr/bin/env sh\n"
|
43 | 44 |
|
@@ -1087,3 +1088,25 @@ def test_index_add_pathlike(self, rw_repo):
|
1087 | 1088 | file.touch()
|
1088 | 1089 |
|
1089 | 1090 | rw_repo.index.add(file)
|
| 1091 | + |
| 1092 | + |
| 1093 | +class TestIndexUtils: |
| 1094 | + @pytest.mark.parametrize("file_path_type", [str, Path]) |
| 1095 | + def test_temporary_file_swap(self, tmp_path, file_path_type): |
| 1096 | + file_path = tmp_path / "foo" |
| 1097 | + file_path.write_bytes(b"some data") |
| 1098 | + |
| 1099 | + with TemporaryFileSwap(file_path_type(file_path)) as ctx: |
| 1100 | + assert Path(ctx.file_path) == file_path |
| 1101 | + assert not file_path.exists() |
| 1102 | + |
| 1103 | + # Recreate it with new data, so we can observe that they're really separate. |
| 1104 | + file_path.write_bytes(b"other data") |
| 1105 | + |
| 1106 | + temp_file_path = Path(ctx.tmp_file_path) |
| 1107 | + assert temp_file_path.parent == file_path.parent |
| 1108 | + assert temp_file_path.name.startswith(file_path.name) |
| 1109 | + assert temp_file_path.read_bytes() == b"some data" |
| 1110 | + |
| 1111 | + assert not temp_file_path.exists() |
| 1112 | + assert file_path.read_bytes() == b"some data" # Not b"other data". |
0 commit comments