Skip to content

Commit bc3d0e7

Browse files
committed
Add end to end test
1 parent a055cc3 commit bc3d0e7

File tree

4 files changed

+82
-6
lines changed

4 files changed

+82
-6
lines changed

dirdiff/cli.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
}
2323

2424

25-
def parse_args():
25+
def parse_args(args=None) -> argparse.Namespace:
2626
parser = argparse.ArgumentParser(
2727
description="Computes the directory difference `upper = merged - lower`"
2828
)
@@ -113,7 +113,7 @@ def parse_args():
113113
const=True,
114114
help="Attempt to chown files to preserve ownership, only applies to file-based output",
115115
)
116-
return parser.parse_args()
116+
return parser.parse_args(args=args)
117117

118118

119119
def setup_logging(verbose: int) -> None:
@@ -185,8 +185,8 @@ def _get_backend(
185185
return OutputBackendFile(output, preserve_owners=preserve_owners)
186186

187187

188-
def main() -> int:
189-
args = parse_args()
188+
def main(args=None) -> int:
189+
args = parse_args(args=args)
190190
setup_logging(1 + args.verbose - args.quiet)
191191

192192
with ExitStack() as stack:
@@ -203,7 +203,7 @@ def main() -> int:
203203
args.output_type,
204204
args.output,
205205
args.force,
206-
args.preserve_owners,
206+
getattr(args, "preserve_owners", False),
207207
)
208208
)
209209

dirdiff/output_tar.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,15 @@ def __init__(self, tf: TarFile, *, archive_root=".") -> None:
1717
self.archive_root = archive_root
1818

1919
def _get_tar_info(self, name: str, st: StatInfo) -> TarInfo:
20-
ti = TarInfo(os.path.join(self.archive_root, name))
20+
if name in (".", os.path.sep):
21+
arch_name = self.archive_root
22+
elif name.startswith(f".{os.path.sep}"):
23+
arch_name = os.path.join(self.archive_root, name[2:])
24+
elif name.startswith(os.path.sep):
25+
arch_name = os.path.join(self.archive_root, name[1:])
26+
else:
27+
arch_name = os.path.join(self.archive_root, name)
28+
ti = TarInfo(arch_name)
2129
ti.mode = stat.S_IMODE(st.mode)
2230
ti.mtime = st.mtime
2331
ti.uid = st.uid

tests/cases/generic/diff.tgz

333 Bytes
Binary file not shown.

tests/test_e2e.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import hashlib
2+
import os
3+
import tarfile
4+
import tempfile
5+
6+
from dirdiff.cli import main
7+
8+
9+
def tar_summarize(tf: tarfile.TarFile):
10+
result = {}
11+
while ti := tf.next():
12+
assert ti.name == "." or ti.name.startswith(f".{os.path.sep}")
13+
assert ti.name not in result
14+
parent = os.path.dirname(ti.name)
15+
16+
if parent:
17+
assert parent in result
18+
19+
content_hash = ""
20+
if ti.isfile():
21+
hsh = hashlib.sha256()
22+
f = tf.extractfile(ti)
23+
assert f is not None
24+
with f:
25+
while data := f.read(2**16):
26+
hsh.update(data)
27+
content_hash = hsh.hexdigest()
28+
29+
result[ti.name] = dict(
30+
uid=ti.uid,
31+
gid=ti.gid,
32+
mode=ti.mode,
33+
size=ti.size,
34+
mtime=ti.mtime,
35+
typ=ti.type,
36+
content_hash=content_hash,
37+
)
38+
return result
39+
40+
41+
def run_test(name: str) -> None:
42+
test_path = os.path.join(os.path.dirname(__file__), "cases", name)
43+
with tempfile.NamedTemporaryFile(delete=False) as tmpf:
44+
tmp_name = tmpf.name
45+
46+
try:
47+
args = [
48+
os.path.join(test_path, "merged.tgz"),
49+
os.path.join(test_path, "lower.tgz"),
50+
"--output",
51+
tmp_name,
52+
]
53+
result = main(args=args)
54+
assert result == 0
55+
56+
with tarfile.open(tmp_name, mode="r|") as tf:
57+
summary = tar_summarize(tf)
58+
finally:
59+
os.remove(tmp_name)
60+
61+
with tarfile.open(os.path.join(test_path, "diff.tgz"), mode="r|gz") as tf:
62+
expected_summary = tar_summarize(tf)
63+
64+
assert summary == expected_summary
65+
66+
67+
def test_generic():
68+
run_test("generic")

0 commit comments

Comments
 (0)