From e10c7de4a1217edbb40ae8fe173987b24a891789 Mon Sep 17 00:00:00 2001 From: Matt Clarkson Date: Tue, 4 Feb 2025 15:51:29 +0000 Subject: [PATCH] test(tar): add version and codec test --- e2e/binary/tar/BUILD.bazel | 31 ++++++++++++++---- e2e/binary/tar/create.py | 65 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+), 6 deletions(-) create mode 100644 e2e/binary/tar/create.py diff --git a/e2e/binary/tar/BUILD.bazel b/e2e/binary/tar/BUILD.bazel index c10bdaab..14abdad3 100644 --- a/e2e/binary/tar/BUILD.bazel +++ b/e2e/binary/tar/BUILD.bazel @@ -1,11 +1,30 @@ -load("@bazel_skylib//rules:build_test.bzl", "build_test") +load("@rules_python_pytest//python_pytest:defs.bzl", "py_pytest_test") +load("@bazel_skylib//rules:native_binary.bzl", "native_test") -# TODO: write an _actual_ test for `tar` +py_pytest_test( + name = "pytest", + size = "small", + srcs = ["create.py"], + data = [ + "@ape//ape:tar", + ], + deps = [ + "//binary:pytest", + ], +) -build_test( - name = "tar", +native_test( + name = "version", size = "small", - tags = ["stub"], - targets = ["@ape//ape:tar"], + src = "@ape//ape:tar", + args = ["--version"], +) + +test_suite( + name = "tar", + tests = [ + ":pytest", + ":version", + ], visibility = ["//:__subpackages__"], ) diff --git a/e2e/binary/tar/create.py b/e2e/binary/tar/create.py new file mode 100644 index 00000000..f7888782 --- /dev/null +++ b/e2e/binary/tar/create.py @@ -0,0 +1,65 @@ +from __future__ import annotations + +from pathlib import Path +from subprocess import run +from tarfile import REGTYPE, TarFile + +from binary import Relative, Tool + + +def test_codec(tool: Tool, relative: Relative, tmp_path: Path) -> None: + binary = tool("tar") + text = tmp_path / "fixture.txt" + shell = tmp_path / "hello.sh" + output = tmp_path / "output.tar" + + with open(text, "w", encoding="utf-8") as stream: + stream.write("a" * 2048) + text.chmod(0o644) + + with open(shell, "w", encoding="utf-8") as stream: + stream.write("builtin echo 'Hello, world!'") + shell.chmod(0o744) + + cmd = ( + binary, + "--sort=name", + "--mtime=2000-01-01T00:00Z", + "--owner=0", + "--group=0", + "--numeric-owner", + "--pax-option=exthdr.name=%d/PaxHeaders/%f,delete=atime,delete=ctime", + "-cf", + str(output), + str(text.relative_to(tmp_path)), + str(shell.relative_to(tmp_path)), + ) + env = { + "LC_ALL": "C", + } + result = run(cmd, timeout=30, env=env, capture_output=True, text=True, cwd=tmp_path) + assert 0 == result.returncode, result.stderr + + with TarFile(str(output)) as archive: + assert archive.getnames() == [ + str(text.relative_to(tmp_path)), + str(shell.relative_to(tmp_path)), + ] + + m = archive.getmember(str(text.relative_to(tmp_path))) + assert m.size == 2048 + assert m.mode == 0o644, oct(m.mode) + assert m.type == REGTYPE + assert m.uid == 0 + assert m.gid == 0 + assert m.mtime == 946684800 + assert m.pax_headers == {} + + m = archive.getmember(str(shell.relative_to(tmp_path))) + assert m.size == 28 + assert m.mode == 0o744, oct(m.mode) + assert m.type == REGTYPE + assert m.uid == 0 + assert m.gid == 0 + assert m.mtime == 946684800 + assert m.pax_headers == {} -- GitLab