Skip to content

Commit

Permalink
Tweak @with_rw_directory and go back to using it
Browse files Browse the repository at this point in the history
The situation with test_tree_modifier_ordering's helper, where it
makes sense to wrap the helper itself with `@with_rw_directory`
while still deleting the temporary directory as soon as the helper
itself finishes, in order to make clear that the test itself does
not use the temporary directory that the helper used, only occurs
in one place in GitPython's tests as far as I know.

In particular, all other places where `@with_rw_directory` was
actually in use are test methods, not helper methods, and the way
the decorator would have its wrapper log on failure documented the
decorated method as a test. Mainly for that reason, I had backed
away from using `@with_rw_directory` here.

But the test code seems much easier to understand when it is used
compared to other approaches. While it also has the disadvantage of
doing a gc.collect that is unnecessary here, this is not the only
place what that is done.

This commit brings back the test_tree_modifier_ordering helper
implementation that uses `@with_rw_directory`, while also modifying
the logging logic in `@with_rw_directory` slightly, so that when
the decorated method is not named as a test, it is referred to as a
"Helper" rather than a "Test" in the failure log message.
  • Loading branch information
EliahKagan committed Feb 15, 2024
1 parent 0114a99 commit b780a8c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 36 deletions.
3 changes: 2 additions & 1 deletion test/lib/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ def wrapper(self, *args, **kwargs):
return func(self, path, *args, **kwargs)
except Exception:
_logger.info(
"Test %s.%s failed, output is at %r\n",
"%s %s.%s failed, output is at %r\n",
"Test" if func.__name__.startswith("test_") else "Helper",
type(self).__name__,
func.__name__,
path,
Expand Down
65 changes: 30 additions & 35 deletions test/test_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@
import os.path as osp
from pathlib import Path
import subprocess
import tempfile

from git.objects import Tree, Blob
from git.util import cwd, rmtree
from test.lib import TestBase
from git.util import cwd
from test.lib import TestBase, with_rw_directory


class TestTree(TestBase):
Expand Down Expand Up @@ -43,39 +42,35 @@ def test_serializable(self):
testtree._deserialize(stream)
# END for each item in tree

@staticmethod
def _get_git_ordered_files():
@with_rw_directory
def _get_git_ordered_files(self, rw_dir):
"""Get files as git orders them, to compare in test_tree_modifier_ordering."""
with tempfile.TemporaryDirectory() as tdir:
# Create directory contents.
Path(tdir, "file").mkdir()
for filename in (
"bin",
"bin.d",
"file.to",
"file.toml",
"file.toml.bin",
"file0",
):
Path(tdir, filename).touch()
Path(tdir, "file", "a").touch()

try:
with cwd(tdir):
# Prepare the repository.
subprocess.run(["git", "init", "-q"], check=True)
subprocess.run(["git", "add", "."], check=True)
subprocess.run(["git", "commit", "-m", "c1"], check=True)

# Get git output from which an ordered file list can be parsed.
rev_parse_command = ["git", "rev-parse", "HEAD^{tree}"]
tree_hash = subprocess.check_output(rev_parse_command).decode().strip()
cat_file_command = ["git", "cat-file", "-p", tree_hash]
cat_file_output = subprocess.check_output(cat_file_command).decode()
finally:
rmtree(Path(tdir, ".git"))

return [line.split()[-1] for line in cat_file_output.split("\n") if line]
# Create directory contents.
Path(rw_dir, "file").mkdir()
for filename in (
"bin",
"bin.d",
"file.to",
"file.toml",
"file.toml.bin",
"file0",
):
Path(rw_dir, filename).touch()
Path(rw_dir, "file", "a").touch()

with cwd(rw_dir):
# Prepare the repository.
subprocess.run(["git", "init", "-q"], check=True)
subprocess.run(["git", "add", "."], check=True)
subprocess.run(["git", "commit", "-m", "c1"], check=True)

# Get git output from which an ordered file list can be parsed.
rev_parse_command = ["git", "rev-parse", "HEAD^{tree}"]
tree_hash = subprocess.check_output(rev_parse_command).decode().strip()
cat_file_command = ["git", "cat-file", "-p", tree_hash]
cat_file_output = subprocess.check_output(cat_file_command).decode()

return [line.split()[-1] for line in cat_file_output.split("\n") if line]

def test_tree_modifier_ordering(self):
"""TreeModifier.set_done() sorts files in the same order git does."""
Expand Down

0 comments on commit b780a8c

Please sign in to comment.