From b7322bbb33d368304c92bd75d2d0f22521ee2bca Mon Sep 17 00:00:00 2001 From: Laurenz Date: Sun, 23 Apr 2023 19:07:57 +0200 Subject: [PATCH] Improve test setup --- tests/BUILD | 3 ++ tests/test.py | 14 ++++++-- tests/test_appimage.py | 33 ++++++++++--------- {appimage/private => tests}/tool/tests/BUILD | 2 ++ .../private => tests}/tool/tests/test_cli.py | 0 .../tool/tests/test_mkappimage.py | 0 6 files changed, 33 insertions(+), 19 deletions(-) rename {appimage/private => tests}/tool/tests/BUILD (91%) rename {appimage/private => tests}/tool/tests/test_cli.py (100%) rename {appimage/private => tests}/tool/tests/test_mkappimage.py (100%) diff --git a/tests/BUILD b/tests/BUILD index c9e621e..c3ec846 100644 --- a/tests/BUILD +++ b/tests/BUILD @@ -10,6 +10,7 @@ sh_binary( appimage_test( name = "appimage_test_sh", + size = "small", args = ["--appimage-extract-and-run"], # One way to run if no libfuse2 is available binary = ":test_sh", ) @@ -28,6 +29,7 @@ py_binary( appimage_test( name = "appimage_test_py", + size = "small", binary = ":test_py", env = {"APPIMAGE_EXTRACT_AND_RUN": "1"}, # Another way to run if no libfuse2 is available ) @@ -44,6 +46,7 @@ appimage( py_test( name = "test_appimage", + size = "small", srcs = ["test_appimage.py"], data = [":appimage_py"], deps = [requirement("pytest")], diff --git a/tests/test.py b/tests/test.py index 0b370e4..1198300 100644 --- a/tests/test.py +++ b/tests/test.py @@ -5,6 +5,11 @@ import subprocess from pathlib import Path +_TMPDIR = os.environ.get("TEST_TMPDIR", "") +assert _TMPDIR +_ENV = os.environ.copy() +_ENV.update({"TMPDIR": _TMPDIR}) + def test_datadep() -> None: """Test that the data dependency is bundled.""" @@ -15,10 +20,13 @@ def test_datadep() -> None: def test_external_bin() -> None: """Test that the external binary is bundled.""" - external_bin_appimage = Path("tests/external_bin.appimage") + external_bin_appimage = Path.cwd() / "tests/external_bin.appimage" assert external_bin_appimage.is_file() - cmd = [os.fspath(external_bin_appimage), "--appimage-extract-and-run", "-h"] - proc = subprocess.run(cmd, text=True, check=False, stderr=subprocess.STDOUT, stdout=subprocess.PIPE) + assert os.access(external_bin_appimage, os.X_OK) + cmd = [os.fspath(external_bin_appimage), "--appimage-extract-and-run", "--help"] + proc = subprocess.run( + cmd, text=True, check=False, stderr=subprocess.STDOUT, stdout=subprocess.PIPE, cwd=_TMPDIR, env=_ENV + ) assert "Builds a python wheel" in proc.stdout, proc.stdout diff --git a/tests/test_appimage.py b/tests/test_appimage.py index 6d39ec7..e2ada76 100644 --- a/tests/test_appimage.py +++ b/tests/test_appimage.py @@ -1,44 +1,45 @@ """Test appimages as data deps.""" -import shutil import subprocess import sys from pathlib import Path +import os import pytest -APPIMAGE = "tests/appimage_py" +APPIMAGE = str(Path.cwd() / "tests/appimage_py") +_TMPDIR = os.environ.get("TEST_TMPDIR", "") +assert _TMPDIR +_ENV = os.environ.copy() +_ENV.update({"TMPDIR": _TMPDIR}) def test_file() -> None: """Test that the appimage has the expected magic.""" cmd = ["file", "--dereference", APPIMAGE] out = subprocess.run(cmd, check=True, text=True, stdout=subprocess.PIPE).stdout - assert out.startswith( - "tests/appimage_py: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, stripped" - ) + expected = "tests/appimage_py: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, stripped" + assert expected in out def test_run() -> None: """Test that the appimage can be run.""" cmd = [APPIMAGE, "--appimage-extract-and-run", "--name", "Simon Peter"] - output = subprocess.run(cmd, check=True, text=True, stdout=subprocess.PIPE).stdout + output = subprocess.run(cmd, check=True, text=True, stdout=subprocess.PIPE, cwd=_TMPDIR, env=_ENV).stdout assert output == "Hello, Simon Peter!\n" def test_symlinks() -> None: """Test that the appimage has the expected symlinks and none are broken.""" cmd = [APPIMAGE, "--appimage-extract"] - try: - subprocess.run(cmd, check=True, text=True, stdout=subprocess.PIPE) - symlinks_present = False - for file in Path("squashfs-root").glob("**/*"): - if file.is_symlink() and file.name != "invalid_link": - assert file.resolve().exists(), f"{file} resolves to {file.resolve()}, which does not exist!" - symlinks_present = True - assert symlinks_present - finally: - shutil.rmtree("squashfs-root") + subprocess.run(cmd, check=True, text=True, stdout=subprocess.PIPE, cwd=_TMPDIR, env=_ENV) + extracted_path = Path(_TMPDIR) / "squashfs-root" + symlinks_present = False + for file in extracted_path.glob("**/*"): + if file.is_symlink() and file.name != "invalid_link": + assert file.resolve().exists(), f"{file} resolves to {file.resolve()}, which does not exist!" + symlinks_present = True + assert symlinks_present if __name__ == "__main__": diff --git a/appimage/private/tool/tests/BUILD b/tests/tool/tests/BUILD similarity index 91% rename from appimage/private/tool/tests/BUILD rename to tests/tool/tests/BUILD index 4f91630..3e1eca6 100644 --- a/appimage/private/tool/tests/BUILD +++ b/tests/tool/tests/BUILD @@ -3,6 +3,7 @@ load("@rules_python//python:defs.bzl", "py_test") py_test( name = "test_cli", + size = "small", srcs = ["test_cli.py"], deps = [ requirement("pytest"), @@ -12,6 +13,7 @@ py_test( py_test( name = "test_mkappimage", + size = "small", srcs = ["test_mkappimage.py"], deps = [ requirement("pytest"), diff --git a/appimage/private/tool/tests/test_cli.py b/tests/tool/tests/test_cli.py similarity index 100% rename from appimage/private/tool/tests/test_cli.py rename to tests/tool/tests/test_cli.py diff --git a/appimage/private/tool/tests/test_mkappimage.py b/tests/tool/tests/test_mkappimage.py similarity index 100% rename from appimage/private/tool/tests/test_mkappimage.py rename to tests/tool/tests/test_mkappimage.py