From e68d6b66f004f924c1c9472910726ab8db78a513 Mon Sep 17 00:00:00 2001 From: Sebastian Birunt Date: Tue, 1 Apr 2025 19:16:38 +0200 Subject: [PATCH 1/5] test: add `touch` test Add test for `touch` binary. --- e2e/binary/touch/BUILD.bazel | 32 ++++++++++++++++++++++++++------ e2e/binary/touch/touch.py | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 6 deletions(-) create mode 100644 e2e/binary/touch/touch.py diff --git a/e2e/binary/touch/BUILD.bazel b/e2e/binary/touch/BUILD.bazel index 2903f461..206225c3 100644 --- a/e2e/binary/touch/BUILD.bazel +++ b/e2e/binary/touch/BUILD.bazel @@ -1,11 +1,31 @@ -load("@bazel_skylib//rules:build_test.bzl", "build_test") +load("@bazel_skylib//rules:native_binary.bzl", "native_test") +load("@rules_python_pytest//python_pytest:defs.bzl", "py_pytest_test") -# TODO: write an _actual_ test for `touch` +py_pytest_test( + name = "pytest", + size = "small", + srcs = ["touch.py"], + data = [ + "@ape//ape:touch", + ], + deps = [ + "//binary:pytest", + ], +) -build_test( - name = "touch", +native_test( + name = "version", size = "small", - tags = ["stub"], - targets = ["@ape//ape:touch"], + src = "@ape//ape:touch", + args = ["--version"], + visibility = ["//:__subpackages__"], +) + +test_suite( + name = "touch", + tests = [ + "pytest", + "version", + ], visibility = ["//:__subpackages__"], ) diff --git a/e2e/binary/touch/touch.py b/e2e/binary/touch/touch.py new file mode 100644 index 00000000..3c07eed6 --- /dev/null +++ b/e2e/binary/touch/touch.py @@ -0,0 +1,33 @@ +from __future__ import annotations + +from pathlib import Path +from subprocess import PIPE, run + +from binary import Tool + + +def test_touch(tool: Tool, tmp_path: Path) -> None: + binary = tool("touch") + filepath = tmp_path / "touch.txt" + + cmd = (binary, "--date", "Sun, 29 Feb 2004 16:21:42 -0800", filepath) + run(cmd, check=True, timeout=30, stdout=PIPE) + + assert filepath.is_file() + + s = filepath.stat() + + assert s.st_atime == 1078100502 + assert s.st_mtime == 1078100502 + assert s.st_size == 0 + + ctime = s.st_ctime + + cmd = (binary, filepath) + run(cmd, check=True, timeout=30, stdout=PIPE) + + s = filepath.stat() + + assert s.st_ctime >= ctime + assert s.st_atime > 1078100502 + assert s.st_mtime > 1078100502 -- GitLab From f3ba8c9c0e3f3400acdfeb0cd71977fc57fa5726 Mon Sep 17 00:00:00 2001 From: Sebastian Birunt Date: Tue, 1 Apr 2025 19:18:38 +0200 Subject: [PATCH 2/5] test: add `tidy` test Add test for `tidy` binary. --- e2e/binary/tidy/BUILD.bazel | 35 +++++++++++++++++++++++++++++------ e2e/binary/tidy/config.txt | 6 ++++++ e2e/binary/tidy/expected.txt | 7 +++++++ e2e/binary/tidy/fixture.txt | 4 ++++ e2e/binary/tidy/pretty.py | 26 ++++++++++++++++++++++++++ 5 files changed, 72 insertions(+), 6 deletions(-) create mode 100644 e2e/binary/tidy/config.txt create mode 100644 e2e/binary/tidy/expected.txt create mode 100644 e2e/binary/tidy/fixture.txt create mode 100644 e2e/binary/tidy/pretty.py diff --git a/e2e/binary/tidy/BUILD.bazel b/e2e/binary/tidy/BUILD.bazel index 53c8ad35..34cd32d4 100644 --- a/e2e/binary/tidy/BUILD.bazel +++ b/e2e/binary/tidy/BUILD.bazel @@ -1,11 +1,34 @@ -load("@bazel_skylib//rules:build_test.bzl", "build_test") +load("@bazel_skylib//rules:native_binary.bzl", "native_test") +load("@rules_python_pytest//python_pytest:defs.bzl", "py_pytest_test") -# TODO: write an _actual_ test for `tidy` +py_pytest_test( + name = "pytest", + size = "small", + srcs = ["pretty.py"], + data = [ + "config.txt", + "expected.txt", + "fixture.txt", + "@ape//ape:tidy", + ], + deps = [ + "//binary:pytest", + ], +) -build_test( - name = "tidy", +native_test( + name = "version", size = "small", - tags = ["stub"], - targets = ["@ape//ape:tidy"], + src = "@ape//ape:tidy", + args = ["--version"], + visibility = ["//:__subpackages__"], +) + +test_suite( + name = "tidy", + tests = [ + "pytest", + "version", + ], visibility = ["//:__subpackages__"], ) diff --git a/e2e/binary/tidy/config.txt b/e2e/binary/tidy/config.txt new file mode 100644 index 00000000..9a5e5c65 --- /dev/null +++ b/e2e/binary/tidy/config.txt @@ -0,0 +1,6 @@ +indent: auto +indent-spaces: 2 +quiet: no +tidy-mark: no +output-html: yes +drop-empty-elements: no diff --git a/e2e/binary/tidy/expected.txt b/e2e/binary/tidy/expected.txt new file mode 100644 index 00000000..28b3bd81 --- /dev/null +++ b/e2e/binary/tidy/expected.txt @@ -0,0 +1,7 @@ + + + +HTML Tidy + +

+ diff --git a/e2e/binary/tidy/fixture.txt b/e2e/binary/tidy/fixture.txt new file mode 100644 index 00000000..faad8aad --- /dev/null +++ b/e2e/binary/tidy/fixture.txt @@ -0,0 +1,4 @@ + +HTML Tidy + + diff --git a/e2e/binary/tidy/pretty.py b/e2e/binary/tidy/pretty.py new file mode 100644 index 00000000..48211cb8 --- /dev/null +++ b/e2e/binary/tidy/pretty.py @@ -0,0 +1,26 @@ +from __future__ import annotations + +from pathlib import Path +from subprocess import run + +from binary import Diff, Relative, Tool + + +def test_tidy(tool: Tool, relative: Relative, tmp_path: Path) -> None: + binary = tool("tidy") + fixture = relative("fixture.txt") + expected = relative("expected.txt") + config = relative("config.txt") + output = tmp_path / "output.txt" + + args = ( + "-config", + config, + fixture, + ) + + cmd = (binary, *args) + with open(output, "w") as stream: + run(cmd, check=True, timeout=30, stdout=stream) + + assert Diff(expected) == Diff(output) -- GitLab From 493d1f17e6975836435fb0cb6afc052f3750a42d Mon Sep 17 00:00:00 2001 From: Sebastian Birunt Date: Tue, 1 Apr 2025 19:19:18 +0200 Subject: [PATCH 3/5] test: add `chmod` test Add test for `chmod` binary. --- e2e/binary/chmod/BUILD.bazel | 32 ++++++++++++++++++++++++++------ e2e/binary/chmod/mode.py | 26 ++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 6 deletions(-) create mode 100644 e2e/binary/chmod/mode.py diff --git a/e2e/binary/chmod/BUILD.bazel b/e2e/binary/chmod/BUILD.bazel index ad95dedc..1307113f 100644 --- a/e2e/binary/chmod/BUILD.bazel +++ b/e2e/binary/chmod/BUILD.bazel @@ -1,11 +1,31 @@ -load("@bazel_skylib//rules:build_test.bzl", "build_test") +load("@bazel_skylib//rules:native_binary.bzl", "native_test") +load("@rules_python_pytest//python_pytest:defs.bzl", "py_pytest_test") -# TODO: write an _actual_ test for `chmod` +py_pytest_test( + name = "pytest", + size = "small", + srcs = ["mode.py"], + data = [ + "@ape//ape:chmod", + ], + deps = [ + "//binary:pytest", + ], +) -build_test( - name = "chmod", +native_test( + name = "version", size = "small", - tags = ["stub"], - targets = ["@ape//ape:chmod"], + src = "@ape//ape:chmod", + args = ["--version"], + visibility = ["//:__subpackages__"], +) + +test_suite( + name = "chmod", + tests = [ + "pytest", + "version", + ], visibility = ["//:__subpackages__"], ) diff --git a/e2e/binary/chmod/mode.py b/e2e/binary/chmod/mode.py new file mode 100644 index 00000000..b5d41248 --- /dev/null +++ b/e2e/binary/chmod/mode.py @@ -0,0 +1,26 @@ +from __future__ import annotations + + +from pathlib import Path +from subprocess import PIPE, run + +from binary import Tool + + +def test_mode(tool: Tool, tmp_path: Path) -> None: + binary = tool("chmod") + filepath = tmp_path / "secret.txt" + + filepath.touch() + + assert 0o100644 == filepath.stat().st_mode + + cmd = (binary, "og-rw,-x,u-w", filepath) + run(cmd, check=True, timeout=30, stdout=PIPE) + + assert 0o100400 == filepath.stat().st_mode + + cmd = (binary, "777", filepath) + run(cmd, check=True, timeout=30, stdout=PIPE) + + assert 0o100777 == filepath.stat().st_mode -- GitLab From da8db3056b113548c838fa091e46271bf2b401c2 Mon Sep 17 00:00:00 2001 From: Sebastian Birunt Date: Tue, 1 Apr 2025 19:19:50 +0200 Subject: [PATCH 4/5] test: add `python` test Add test for `python` binary. --- e2e/binary/python/BUILD.bazel | 33 +++++++++++++++++++++++++++------ e2e/binary/python/hello.py | 11 +++++++++++ e2e/binary/python/python.py | 30 ++++++++++++++++++++++++++++++ 3 files changed, 68 insertions(+), 6 deletions(-) create mode 100644 e2e/binary/python/hello.py create mode 100644 e2e/binary/python/python.py diff --git a/e2e/binary/python/BUILD.bazel b/e2e/binary/python/BUILD.bazel index bd0c823c..9fbab89a 100644 --- a/e2e/binary/python/BUILD.bazel +++ b/e2e/binary/python/BUILD.bazel @@ -1,11 +1,32 @@ -load("@bazel_skylib//rules:build_test.bzl", "build_test") +load("@bazel_skylib//rules:native_binary.bzl", "native_test") +load("@rules_python_pytest//python_pytest:defs.bzl", "py_pytest_test") -# TODO: write an _actual_ test for `python` +py_pytest_test( + name = "pytest", + size = "small", + srcs = ["python.py"], + data = [ + "hello.py", + "@ape//ape:python", + ], + deps = [ + "//binary:pytest", + ], +) -build_test( - name = "python", +native_test( + name = "version", size = "small", - tags = ["stub"], - targets = ["@ape//ape:python"], + src = "@ape//ape:python", + args = ["--version"], + visibility = ["//:__subpackages__"], +) + +test_suite( + name = "python", + tests = [ + "pytest", + "version", + ], visibility = ["//:__subpackages__"], ) diff --git a/e2e/binary/python/hello.py b/e2e/binary/python/hello.py new file mode 100644 index 00000000..1d1fe875 --- /dev/null +++ b/e2e/binary/python/hello.py @@ -0,0 +1,11 @@ +import sys + + +def main() -> int: + print("Hello!", end="") + + return 0 + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/e2e/binary/python/python.py b/e2e/binary/python/python.py new file mode 100644 index 00000000..9086ecf7 --- /dev/null +++ b/e2e/binary/python/python.py @@ -0,0 +1,30 @@ +from __future__ import annotations + +from subprocess import PIPE, run + +from binary import Relative, Tool + + +def test_script(tool: Tool, relative: Relative) -> None: + binary = tool("python") + hello = relative("hello.py") + + cmd = (binary, hello) + r = run(cmd, check=True, timeout=30, stdout=PIPE, text=True) + + assert 0 == r.returncode + assert "Hello!" == r.stdout + + +def test_command(tool: Tool) -> None: + binary = tool("python") + + cmd = ( + binary, + "-c", + "print('Hello!', end='')", + ) + r = run(cmd, check=True, timeout=30, stdout=PIPE, text=True) + + assert 0 == r.returncode + assert "Hello!" == r.stdout -- GitLab From 13723d2710d590465c6487367f99bef461a264f9 Mon Sep 17 00:00:00 2001 From: Sebastian Birunt Date: Tue, 1 Apr 2025 19:20:49 +0200 Subject: [PATCH 5/5] test: add `chown` test Add test for `chown` binary. --- e2e/binary/chown/BUILD.bazel | 34 ++++++++++++++++++++++++++++------ e2e/binary/chown/fixture.txt | 1 + e2e/binary/chown/own.py | 24 ++++++++++++++++++++++++ 3 files changed, 53 insertions(+), 6 deletions(-) create mode 100644 e2e/binary/chown/fixture.txt create mode 100644 e2e/binary/chown/own.py diff --git a/e2e/binary/chown/BUILD.bazel b/e2e/binary/chown/BUILD.bazel index 0c9034ca..2327ba65 100644 --- a/e2e/binary/chown/BUILD.bazel +++ b/e2e/binary/chown/BUILD.bazel @@ -1,11 +1,33 @@ -load("@bazel_skylib//rules:build_test.bzl", "build_test") +load("@bazel_skylib//rules:native_binary.bzl", "native_test") +load("@rules_python_pytest//python_pytest:defs.bzl", "py_pytest_test") -# TODO: write an _actual_ test for `chown` +py_pytest_test( + name = "pytest", + size = "small", + srcs = ["own.py"], + data = [ + "fixture.txt", + "@ape//ape:chown", + ], + tags = ["requires-fakeroot"], + deps = [ + "//binary:pytest", + ], +) -build_test( - name = "chown", +native_test( + name = "version", size = "small", - tags = ["stub"], - targets = ["@ape//ape:chown"], + src = "@ape//ape:chown", + args = ["--version"], + visibility = ["//:__subpackages__"], +) + +test_suite( + name = "chown", + tests = [ + "pytest", + "version", + ], visibility = ["//:__subpackages__"], ) diff --git a/e2e/binary/chown/fixture.txt b/e2e/binary/chown/fixture.txt new file mode 100644 index 00000000..ce013625 --- /dev/null +++ b/e2e/binary/chown/fixture.txt @@ -0,0 +1 @@ +hello diff --git a/e2e/binary/chown/own.py b/e2e/binary/chown/own.py new file mode 100644 index 00000000..1c247d4f --- /dev/null +++ b/e2e/binary/chown/own.py @@ -0,0 +1,24 @@ +from __future__ import annotations + + +from pathlib import Path +from subprocess import run + +from binary import Relative, Tool + + +def test_owner(tool: Tool, relative: Relative, tmp_path: Path) -> None: + binary = tool("chown") + filepath = tmp_path / "random.txt" + fixture = relative("fixture.txt") + + filepath.touch() + + uid = fixture.stat().st_uid + gid = fixture.stat().st_gid + + cmd = (binary, f"{uid}:{gid}", filepath) + run(cmd, check=True, timeout=30) + + assert uid == filepath.stat().st_uid + assert gid == filepath.stat().st_gid -- GitLab