Skip to content

Keep temp files out of project dir and improve cleanup #1825

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 15, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Use more ligtweight approach to guarantee deletion
This changes from `@with_rw_directory` back to TemporaryDirectory,
but calls git.util.rmtree on the repsitory's .git directory where
some read-only files otherwise cause TemporaryDirectory's cleanup
to raise PermissionError on Windows in Python 3.7.

This avoids the gc.collect that is known not to be necessary in
this speciifc situation, as well as the problem that, if operating
in the temporary directory did fail, then name of the helper would
be logged as the name of the test where the failure occurred. But
this has the disadvantage of making the helper more complex and
harder to understand. So this may not be the best approach either.
  • Loading branch information
EliahKagan committed Feb 15, 2024
commit 0114a997bf56eec86f217f99553c859613f1c9a4
65 changes: 35 additions & 30 deletions test/test_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
import os.path as osp
from pathlib import Path
import subprocess
import tempfile

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


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

@with_rw_directory
def _get_git_ordered_files(self, rw_dir):
@staticmethod
def _get_git_ordered_files():
"""Get files as git orders them, to compare in test_tree_modifier_ordering."""
# 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]
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]

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