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

Commitdd42e38

Browse files
committed
Keep temp files out of project dir and improve cleanup
This fixes a test bug in TestTree.test_tree_modifier_orderingwhere:- A `tmp` subdirectory of the directory the tests are run from (almost always the root of the GitPython source tree) was created, instead of creating it in /tmp or elsewhere configured.- That directory was never deleted afterward, creating new untracked files in GitPython's own working tree, and also making it so running the tests twice would always fail the second time, unless a manual intervening deletion was performed. (This had not broken CI, since CI checks clone the repository anew each time.)- The directory was changed into to set up the test, but not deterministically changed back out of. It would typically be exited, but this was not guaranteed to occur if some subprocess commands were to fail unexpectedly.In addition to fixing all three aspects of that bug, this also:- Remains in the temporary directory only as long as necessary to execute the subprocesses in it that produce the expected value for the test (including not entering it until running them).- Deletes the temporary directory immediately after exiting it, since it is only used to produce a file list to compare to.- Avoids interleaving file creation and git subprocess commands, since doing so made it harder to understand what was being set up and since the difference is not relevant to the test.- Does the work in that temporary directory before performing any operations with the code under test, because it is conceptually an "arrange" step, and because doing so makes its limited purpose clearer on reading the tests.- Some other refactoring to support the fixes and accompanying changes, including extracting the temporary directory logic to a helper method.This deliberately does not change the order in which any files arecreated. It also keeps the approach of using subprocess functionsdirectly to operate on the temporary git repository (and changingdirectory while doing so, keeping each of the commands the same),since there might be good reasons to do this, such as to make veryclear that the file order being obtained to compare to is reallycoming from git itself. Even if this is to be changed, it seemsoutside the scope of this bugfix.The test still fails if the fix to git/objects/tree.py from365d44f(#1799) is absent. This was verified by running: git revert --no-commit365d44f pytest --no-cov -vv test/test_tree.py git revert --abort
1 parent7ba3fd2 commitdd42e38

File tree

1 file changed

+32
-22
lines changed

1 file changed

+32
-22
lines changed

‎test/test_tree.py

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@
44
# 3-Clause BSD License: https://opensource.org/license/bsd-3-clause/
55

66
fromioimportBytesIO
7+
importos.pathasosp
8+
frompathlibimportPath
9+
importsubprocess
10+
importtempfile
711

812
fromgit.objectsimportTree,Blob
13+
fromgit.utilimportcwd
914
fromtest.libimportTestBase
1015

11-
importos
12-
importos.pathasosp
13-
importsubprocess
14-
1516

1617
classTestTree(TestBase):
1718
deftest_serializable(self):
@@ -42,29 +43,41 @@ def test_serializable(self):
4243
testtree._deserialize(stream)
4344
# END for each item in tree
4445

45-
deftest_tree_modifier_ordering(self):
46-
defsetup_git_repository_and_get_ordered_files():
47-
os.mkdir("tmp")
48-
os.chdir("tmp")
49-
subprocess.run(["git","init","-q"],check=True)
50-
os.mkdir("file")
51-
forfilenamein[
46+
@staticmethod
47+
def_get_git_ordered_files():
48+
"""Get files as git orders them, to compare in test_tree_modifier_ordering."""
49+
withtempfile.TemporaryDirectory()astdir:
50+
# Create directory contents.
51+
Path(tdir,"file").mkdir()
52+
forfilenamein(
5253
"bin",
5354
"bin.d",
5455
"file.to",
5556
"file.toml",
5657
"file.toml.bin",
5758
"file0",
58-
"file/a",
59-
]:
60-
open(filename,"a").close()
61-
62-
subprocess.run(["git","add","."],check=True)
63-
subprocess.run(["git","commit","-m","c1"],check=True)
64-
tree_hash=subprocess.check_output(["git","rev-parse","HEAD^{tree}"]).decode().strip()
65-
cat_file_output=subprocess.check_output(["git","cat-file","-p",tree_hash]).decode()
59+
):
60+
Path(tdir,filename).touch()
61+
Path(tdir,"file","a").touch()
62+
63+
withcwd(tdir):
64+
# Prepare the repository.
65+
subprocess.run(["git","init","-q"],check=True)
66+
subprocess.run(["git","add","."],check=True)
67+
subprocess.run(["git","commit","-m","c1"],check=True)
68+
69+
# Get git output from which an ordered file list can be parsed.
70+
rev_parse_command= ["git","rev-parse","HEAD^{tree}"]
71+
tree_hash=subprocess.check_output(rev_parse_command).decode().strip()
72+
cat_file_command= ["git","cat-file","-p",tree_hash]
73+
cat_file_output=subprocess.check_output(cat_file_command).decode()
74+
6675
return [line.split()[-1]forlineincat_file_output.split("\n")ifline]
6776

77+
deftest_tree_modifier_ordering(self):
78+
"""TreeModifier.set_done() sorts files in the same order git does."""
79+
git_file_names_in_order=self._get_git_ordered_files()
80+
6881
hexsha="6c1faef799095f3990e9970bc2cb10aa0221cf9c"
6982
roottree=self.rorepo.tree(hexsha)
7083
blob_mode=Tree.blob_id<<12
@@ -92,9 +105,6 @@ def names_in_mod_cache():
92105
here=file_names_in_order()
93106
return [eforeinaifeinhere]
94107

95-
git_file_names_in_order=setup_git_repository_and_get_ordered_files()
96-
os.chdir("..")
97-
98108
mod.set_done()
99109
assertnames_in_mod_cache()==git_file_names_in_order,"set_done() performs git-sorting"
100110

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp