Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit22129a7

Browse files
authored
Merge pull request#1776 from EliahKagan/temporary-file-swap
Fix TemporaryFileSwap regression where file_path could not be Path
2 parents4023f28 +4e91a6c commit22129a7

File tree

3 files changed

+35
-10
lines changed

3 files changed

+35
-10
lines changed

‎git/index/util.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ class TemporaryFileSwap:
4141

4242
def__init__(self,file_path:PathLike)->None:
4343
self.file_path=file_path
44-
fd,self.tmp_file_path=tempfile.mkstemp(prefix=self.file_path,dir="")
44+
dirname,basename=osp.split(file_path)
45+
fd,self.tmp_file_path=tempfile.mkstemp(prefix=basename,dir=dirname)
4546
os.close(fd)
4647
withcontextlib.suppress(OSError):# It may be that the source does not exist.
4748
os.replace(self.file_path,self.tmp_file_path)

‎test/test_blob_filter.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,15 @@
1414
fromgit.typesimportPathLike
1515

1616

17-
# fmt: off
18-
@pytest.mark.parametrize('paths, path, expected_result', [
19-
((Path("foo"),),Path("foo"),True),
20-
((Path("foo"),),Path("foo/bar"),True),
21-
((Path("foo/bar"),),Path("foo"),False),
22-
((Path("foo"),Path("bar")),Path("foo"),True),
23-
])
24-
# fmt: on
17+
@pytest.mark.parametrize(
18+
"paths, path, expected_result",
19+
[
20+
((Path("foo"),),Path("foo"),True),
21+
((Path("foo"),),Path("foo/bar"),True),
22+
((Path("foo/bar"),),Path("foo"),False),
23+
((Path("foo"),Path("bar")),Path("foo"),True),
24+
],
25+
)
2526
deftest_blob_filter(paths:Sequence[PathLike],path:PathLike,expected_result:bool)->None:
2627
"""Test the blob filter."""
2728
blob_filter=BlobFilter(paths)

‎test/test_index.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,11 @@
3434
)
3535
fromgit.index.funimporthook_path
3636
fromgit.index.typimportBaseIndexEntry,IndexEntry
37+
fromgit.index.utilimportTemporaryFileSwap
3738
fromgit.objectsimportBlob
38-
fromtest.libimportTestBase,fixture,fixture_path,with_rw_directory,with_rw_repo
3939
fromgit.utilimportActor,hex_to_bin,rmtree
4040
fromgitdb.baseimportIStream
41+
fromtest.libimportTestBase,fixture,fixture_path,with_rw_directory,with_rw_repo
4142

4243
HOOKS_SHEBANG="#!/usr/bin/env sh\n"
4344

@@ -1087,3 +1088,25 @@ def test_index_add_pathlike(self, rw_repo):
10871088
file.touch()
10881089

10891090
rw_repo.index.add(file)
1091+
1092+
1093+
classTestIndexUtils:
1094+
@pytest.mark.parametrize("file_path_type", [str,Path])
1095+
deftest_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+
withTemporaryFileSwap(file_path_type(file_path))asctx:
1100+
assertPath(ctx.file_path)==file_path
1101+
assertnotfile_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+
asserttemp_file_path.parent==file_path.parent
1108+
asserttemp_file_path.name.startswith(file_path.name)
1109+
asserttemp_file_path.read_bytes()==b"some data"
1110+
1111+
assertnottemp_file_path.exists()
1112+
assertfile_path.read_bytes()==b"some data"# Not b"other data".

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp