Skip to content

Commit 9caf3ae

Browse files
authored
Merge pull request #1825 from EliahKagan/tree-test
Keep temp files out of project dir and improve cleanup
2 parents 2613421 + b780a8c commit 9caf3ae

File tree

2 files changed

+37
-28
lines changed

2 files changed

+37
-28
lines changed

test/lib/helper.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,8 @@ def wrapper(self, *args, **kwargs):
9797
return func(self, path, *args, **kwargs)
9898
except Exception:
9999
_logger.info(
100-
"Test %s.%s failed, output is at %r\n",
100+
"%s %s.%s failed, output is at %r\n",
101+
"Test" if func.__name__.startswith("test_") else "Helper",
101102
type(self).__name__,
102103
func.__name__,
103104
path,

test/test_tree.py

+35-27
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44
# 3-Clause BSD License: https://opensource.org/license/bsd-3-clause/
55

66
from io import BytesIO
7-
8-
from git.objects import Tree, Blob
9-
from test.lib import TestBase
10-
11-
import os
127
import os.path as osp
8+
from pathlib import Path
139
import subprocess
1410

11+
from git.objects import Tree, Blob
12+
from git.util import cwd
13+
from test.lib import TestBase, with_rw_directory
14+
1515

1616
class TestTree(TestBase):
1717
def test_serializable(self):
@@ -42,28 +42,39 @@ def test_serializable(self):
4242
testtree._deserialize(stream)
4343
# END for each item in tree
4444

45-
def test_tree_modifier_ordering(self):
46-
def setup_git_repository_and_get_ordered_files():
47-
os.mkdir("tmp")
48-
os.chdir("tmp")
45+
@with_rw_directory
46+
def _get_git_ordered_files(self, rw_dir):
47+
"""Get files as git orders them, to compare in test_tree_modifier_ordering."""
48+
# Create directory contents.
49+
Path(rw_dir, "file").mkdir()
50+
for filename in (
51+
"bin",
52+
"bin.d",
53+
"file.to",
54+
"file.toml",
55+
"file.toml.bin",
56+
"file0",
57+
):
58+
Path(rw_dir, filename).touch()
59+
Path(rw_dir, "file", "a").touch()
60+
61+
with cwd(rw_dir):
62+
# Prepare the repository.
4963
subprocess.run(["git", "init", "-q"], check=True)
50-
os.mkdir("file")
51-
for filename in [
52-
"bin",
53-
"bin.d",
54-
"file.to",
55-
"file.toml",
56-
"file.toml.bin",
57-
"file0",
58-
"file/a",
59-
]:
60-
open(filename, "a").close()
61-
6264
subprocess.run(["git", "add", "."], check=True)
6365
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()
66-
return [line.split()[-1] for line in cat_file_output.split("\n") if line]
66+
67+
# Get git output from which an ordered file list can be parsed.
68+
rev_parse_command = ["git", "rev-parse", "HEAD^{tree}"]
69+
tree_hash = subprocess.check_output(rev_parse_command).decode().strip()
70+
cat_file_command = ["git", "cat-file", "-p", tree_hash]
71+
cat_file_output = subprocess.check_output(cat_file_command).decode()
72+
73+
return [line.split()[-1] for line in cat_file_output.split("\n") if line]
74+
75+
def test_tree_modifier_ordering(self):
76+
"""TreeModifier.set_done() sorts files in the same order git does."""
77+
git_file_names_in_order = self._get_git_ordered_files()
6778

6879
hexsha = "6c1faef799095f3990e9970bc2cb10aa0221cf9c"
6980
roottree = self.rorepo.tree(hexsha)
@@ -92,9 +103,6 @@ def names_in_mod_cache():
92103
here = file_names_in_order()
93104
return [e for e in a if e in here]
94105

95-
git_file_names_in_order = setup_git_repository_and_get_ordered_files()
96-
os.chdir("..")
97-
98106
mod.set_done()
99107
assert names_in_mod_cache() == git_file_names_in_order, "set_done() performs git-sorting"
100108

0 commit comments

Comments
 (0)