Skip to content

Commit

Permalink
Improve test setup
Browse files Browse the repository at this point in the history
  • Loading branch information
lalten committed Apr 23, 2023
1 parent f6f6172 commit b7322bb
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 19 deletions.
3 changes: 3 additions & 0 deletions tests/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -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",
)
Expand All @@ -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
)
Expand All @@ -44,6 +46,7 @@ appimage(

py_test(
name = "test_appimage",
size = "small",
srcs = ["test_appimage.py"],
data = [":appimage_py"],
deps = [requirement("pytest")],
Expand Down
14 changes: 11 additions & 3 deletions tests/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand All @@ -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


Expand Down
33 changes: 17 additions & 16 deletions tests/test_appimage.py
Original file line number Diff line number Diff line change
@@ -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__":
Expand Down
2 changes: 2 additions & 0 deletions appimage/private/tool/tests/BUILD → tests/tool/tests/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand All @@ -12,6 +13,7 @@ py_test(

py_test(
name = "test_mkappimage",
size = "small",
srcs = ["test_mkappimage.py"],
deps = [
requirement("pytest"),
Expand Down
File renamed without changes.
File renamed without changes.

0 comments on commit b7322bb

Please sign in to comment.