mirror of
https://github.com/storytold/cadcraft.git
synced 2026-12-02 20:22:15 -05:00
Renders the plain nightly tar's launcher from craft-common-actions' shared template. Independent check: dylanmtaylor compared all 14 app launcher PRs byte for byte with the template rendered from the registry (https://github.com/storytold/craft-common-actions/pull/367#issuecomment-7172085284); this head was re-checked against craft-common-actions main 88148a5.
239 lines
13 KiB
Python
239 lines
13 KiB
Python
#!/usr/bin/env python3
|
|
"""Every Linux Nightly launcher starts the GUI the flavor's way; no build, no window.
|
|
|
|
The plain tar's `cadcraft` is the canonical shared template
|
|
(craft-common-actions scripts/nightly-launcher.sh.in), which the nightly producer accepts byte for
|
|
byte: it always starts `cadcraft-bin` beside itself on the nightly-v1 profile under the app's
|
|
package identity, answers a version query with one plain `--version` and wraps everything else. The
|
|
deb/rpm wrapper, the AppImage `AppRun`, the Flatpak wrapper and the Arch handoff are generated by
|
|
this app's own scripts and keep their own profile folder; each must hand the binary the one protocol
|
|
the flavor accepts -- `--launch-profile nightly-v1 --profile-root <dir> -- files...` -- and pass a
|
|
version query through untouched.
|
|
"""
|
|
from pathlib import Path
|
|
import shutil
|
|
import subprocess
|
|
import tempfile
|
|
import unittest
|
|
|
|
HERE = Path(__file__).resolve().parent
|
|
LINUX = HERE / "linux"
|
|
PROFILE = "cadcraft-nightly"
|
|
# The tar launcher is the canonical template, so its profile folder is the nightly package identity.
|
|
TEMPLATE_IDENTITY = "ai.storyteller.cadcraft.nightly"
|
|
RECORDER = '#!/bin/sh\nfor arg in "$@"; do printf "<%s>\\n" "$arg"; done\n'
|
|
CONFIG_RECORDER = (
|
|
'#!/bin/sh\nprintf "config=%s\\n" "${CADCRAFT_CONFIG_DIR+<$CADCRAFT_CONFIG_DIR>}"\n'
|
|
'for arg in "$@"; do printf "<%s>\\n" "$arg"; done\n'
|
|
)
|
|
|
|
|
|
def heredoc(script, marker, needle):
|
|
"""The one heredoc body in `script` delimited by `marker` that contains `needle`."""
|
|
text = Path(script).read_text(encoding="utf-8")
|
|
bodies = []
|
|
start = 0
|
|
while (opening := text.find("<<'%s'" % marker, start)) != -1:
|
|
body_start = text.index("\n", opening) + 1
|
|
body_end = text.index("\n%s\n" % marker, body_start)
|
|
bodies.append(text[body_start:body_end] + "\n")
|
|
start = body_end + 1
|
|
matched = [body for body in bodies if needle in body]
|
|
if len(matched) != 1:
|
|
raise AssertionError(f"expected one {marker} heredoc with {needle!r} in {script}, found {len(matched)}")
|
|
return matched[0]
|
|
|
|
|
|
class Launcher(unittest.TestCase):
|
|
def setUp(self):
|
|
scratch = tempfile.TemporaryDirectory()
|
|
self.addCleanup(scratch.cleanup)
|
|
self.root = Path(scratch.name).resolve()
|
|
self.home = self.root / "home with space"
|
|
self.home.mkdir()
|
|
self.real = self.root / "cadcraft-real"
|
|
self.real.write_text(RECORDER, encoding="utf-8")
|
|
self.real.chmod(0o755)
|
|
|
|
def launcher(self, name, body):
|
|
path = self.root / name
|
|
path.write_text(body, encoding="utf-8")
|
|
path.chmod(0o755)
|
|
return path
|
|
|
|
def generated(self, script, marker, needle, replacement):
|
|
body = heredoc(script, marker, needle)
|
|
return self.launcher("cadcraft-nightly", body.replace(needle, str(replacement)))
|
|
|
|
def recording(self, launcher, *args, env=None):
|
|
environment = {"PATH": "/usr/bin:/bin", "HOME": str(self.home), **(env or {})}
|
|
result = subprocess.run([str(launcher), *args], capture_output=True, text=True, env=environment, timeout=60)
|
|
self.assertEqual(result.returncode, 0, result.stderr)
|
|
return [line[1:-1] for line in result.stdout.splitlines() if line.startswith("<")]
|
|
|
|
def profile_base(self, env=None, fallback=(".local", "share")):
|
|
return Path((env or {}).get("XDG_DATA_HOME") or self.home.joinpath(*fallback)) / PROFILE
|
|
|
|
def assert_starts_the_profile(self, launcher, env=None, fallback=(".local", "share")):
|
|
root = str(self.profile_base(env, fallback))
|
|
files = ["drawing.dxf", "with spaces and -- dashes.dxf"]
|
|
self.assertEqual(
|
|
self.recording(launcher, *files, env=env),
|
|
["--launch-profile", "nightly-v1", "--profile-root", root, "--", *files],
|
|
launcher,
|
|
)
|
|
self.assertEqual(self.recording(launcher, env=env), ["--launch-profile", "nightly-v1", "--profile-root", root, "--"])
|
|
for flag in ("--version", "-V"):
|
|
self.assertEqual(self.recording(launcher, flag, env=env), [flag], launcher)
|
|
query = ["--launch-profile", "nightly-v1", "--profile-root", str(self.root / "launcher root"), "--version"]
|
|
self.assertEqual(self.recording(launcher, *query, env=env), query, "a Launcher query is passed through once")
|
|
|
|
# -- the flat tar: the canonical shared launcher template -----------------------------------
|
|
def template(self):
|
|
"""The tar's launcher as the shared template renders it for this app: CADCraft Nightly's
|
|
package identity is its profile folder, and `cadcraft-bin` sits beside the script."""
|
|
(self.root / "cadcraft-bin").write_text(RECORDER, encoding="utf-8")
|
|
(self.root / "cadcraft-bin").chmod(0o755)
|
|
return self.launcher("cadcraft", (LINUX / "nightly-launch.sh").read_text(encoding="utf-8"))
|
|
|
|
def template_profile(self, env=None):
|
|
return Path((env or {}).get("XDG_DATA_HOME") or self.home / ".local" / "share") / TEMPLATE_IDENTITY
|
|
|
|
def test_the_plain_tar_launcher_is_the_shared_template(self):
|
|
launcher = LINUX / "nightly-launch.sh"
|
|
self.assertIn("# Nightly launcher generated by craft-common-actions scripts/nightly-launcher.sh.in.",
|
|
launcher.read_text(encoding="utf-8"))
|
|
self.assertIn('target="$dir/cadcraft-bin"', launcher.read_text(encoding="utf-8"))
|
|
self.assertIn(' --launch-profile) exec "$target" "$@" ;;', launcher.read_text(encoding="utf-8"))
|
|
self.assertIn('}/ai.storyteller.cadcraft.nightly"', launcher.read_text(encoding="utf-8"))
|
|
|
|
def test_the_plain_tar_launcher_starts_the_profile(self):
|
|
command = self.template()
|
|
files = ["drawing.dxf", "with spaces and -- dashes.dxf"]
|
|
root = str(self.template_profile())
|
|
self.assertEqual(self.recording(command, *files),
|
|
["--launch-profile", "nightly-v1", "--profile-root", root, "--", *files])
|
|
self.assertEqual(self.recording(command), ["--launch-profile", "nightly-v1", "--profile-root", root, "--"])
|
|
xdg = {"XDG_DATA_HOME": str(self.root / "xdg with space")}
|
|
self.assertEqual(self.recording(command, "drawing.dxf", env=xdg),
|
|
["--launch-profile", "nightly-v1", "--profile-root", str(self.template_profile(xdg)),
|
|
"--", "drawing.dxf"])
|
|
|
|
def test_the_template_answers_its_version_query_with_one_plain_argument(self):
|
|
# Both spellings reach the GUI as `--version`: the template never forwards `-V`.
|
|
command = self.template()
|
|
env = {"XDG_DATA_HOME": str(self.root / "xdg")}
|
|
root = str(self.template_profile(env))
|
|
for flag in ("--version", "-V"):
|
|
with self.subTest(flag=flag):
|
|
self.assertEqual(self.recording(command, flag, env=env),
|
|
["--launch-profile", "nightly-v1", "--profile-root", root, "--version"])
|
|
|
|
def test_the_launchers_own_profile_line_passes_through_untouched(self):
|
|
# The ArtCraft Launcher starts the tar's entry point with its own profile pair and per-install
|
|
# root; the template hands that line to the GUI unchanged.
|
|
command = self.template()
|
|
managed = str(self.root / "launcher root")
|
|
for supplied in (["--launch-profile", "nightly-v1", "--profile-root", managed, "--"],
|
|
["--launch-profile", "nightly-v1", "--profile-root", managed, "--", "drawing.dxf"],
|
|
["--launch-profile"]):
|
|
with self.subTest(supplied=supplied):
|
|
self.assertEqual(self.recording(command, *supplied), supplied)
|
|
|
|
def test_every_other_line_still_gets_the_templates_own_profile(self):
|
|
# Only a line that opens with `--launch-profile` exactly is the Launcher's: a `--profile-root`
|
|
# first, the `=` spelling, or any other argument is wrapped in the fixed profile.
|
|
command = self.template()
|
|
root = str(self.template_profile())
|
|
for arguments in (["--profile-root", str(self.root / "launcher root"), "--"],
|
|
["--launch-profile=nightly-v1"], ["--help"]):
|
|
with self.subTest(arguments=arguments):
|
|
self.assertEqual(self.recording(command, *arguments),
|
|
["--launch-profile", "nightly-v1", "--profile-root", root, "--", *arguments])
|
|
|
|
def test_the_template_refuses_a_missing_gui(self):
|
|
command = self.template()
|
|
(self.root / "cadcraft-bin").unlink()
|
|
for args in ([], ["--version"]):
|
|
with self.subTest(args=args):
|
|
result = subprocess.run([str(command), *args], capture_output=True, text=True, timeout=60,
|
|
env={"PATH": "/usr/bin:/bin", "HOME": str(self.home)})
|
|
self.assertNotEqual(result.returncode, 0, result.stdout)
|
|
self.assertEqual(result.stdout, "")
|
|
|
|
def test_the_template_resolves_its_own_folder_physically(self):
|
|
# `dir` is the directory the script is reached through, resolved with `pwd -P`: a symlinked
|
|
# folder path still finds the GUI beside the real file.
|
|
command = self.template()
|
|
link = self.root / "linked" / "bin"
|
|
link.parent.mkdir()
|
|
link.symlink_to(command.parent)
|
|
root = str(self.template_profile())
|
|
self.assertEqual(self.recording(link / "cadcraft", "drawing.dxf"),
|
|
["--launch-profile", "nightly-v1", "--profile-root", root, "--", "drawing.dxf"])
|
|
|
|
def test_the_template_needs_a_data_folder_before_it_starts_anything(self):
|
|
command = self.template()
|
|
result = subprocess.run([str(command)], capture_output=True, text=True, timeout=60,
|
|
env={"PATH": "/usr/bin:/bin"})
|
|
self.assertNotEqual(result.returncode, 0)
|
|
self.assertEqual(result.stdout, "")
|
|
self.assertIn("HOME", result.stderr)
|
|
|
|
def test_the_arch_launcher_starts_the_profile(self):
|
|
(self.root / ".cadcraft-nightly").mkdir()
|
|
shutil.copy(self.real, self.root / ".cadcraft-nightly" / "cadcraft")
|
|
installed = self.launcher("cadcraft-nightly", (LINUX / "nightly-arch-launch.sh").read_text(encoding="utf-8"))
|
|
self.assert_starts_the_profile(installed)
|
|
self.assert_starts_the_profile(installed, {"XDG_DATA_HOME": str(self.root / "xdg with space")})
|
|
|
|
def test_the_deb_and_rpm_wrapper_starts_the_profile(self):
|
|
generated = self.generated(LINUX / "package.sh", "WRAPPER", "/usr/lib/cadcraft-nightly/cadcraft", self.real)
|
|
self.assert_starts_the_profile(generated)
|
|
self.assert_starts_the_profile(generated, {"XDG_DATA_HOME": str(self.root / "xdg with space")})
|
|
|
|
def test_the_appimage_apprun_starts_the_profile(self):
|
|
appdir = self.root / "CADCraft.AppDir"
|
|
(appdir / "usr/bin").mkdir(parents=True)
|
|
shutil.copy(self.real, appdir / "usr/bin" / "cadcraft")
|
|
apprun = self.launcher("AppRun", heredoc(LINUX / "package.sh", "APPRUN", "$APPDIR/usr/bin/cadcraft"))
|
|
shutil.move(str(apprun), str(appdir / "AppRun"))
|
|
self.assert_starts_the_profile(appdir / "AppRun")
|
|
self.assert_starts_the_profile(appdir / "AppRun", {"XDG_DATA_HOME": str(self.root / "xdg with space")})
|
|
|
|
def test_the_nightly_cli_keeps_its_settings_in_the_profile(self):
|
|
body = heredoc(LINUX / "package.sh", "CLI_WRAPPER", "/usr/lib/cadcraft-nightly/cadcraft-cli")
|
|
cli = self.launcher("cadcraft-nightly-cli", body.replace("/usr/lib/cadcraft-nightly/cadcraft-cli", str(self.real)))
|
|
self.real.write_text(CONFIG_RECORDER, encoding="utf-8")
|
|
self.real.chmod(0o755)
|
|
# A downstream process sees the variable the CLI wrapper exported.
|
|
def recorded(*args, config=None, xdg=None):
|
|
environment = {"PATH": "/usr/bin:/bin", "HOME": str(self.home)}
|
|
if xdg is not None:
|
|
environment["XDG_DATA_HOME"] = xdg
|
|
if config is not None:
|
|
environment["CADCRAFT_CONFIG_DIR"] = config
|
|
result = subprocess.run([str(cli), *args], capture_output=True, text=True, env=environment, timeout=60)
|
|
self.assertEqual(result.returncode, 0, result.stderr)
|
|
lines = result.stdout.splitlines()
|
|
return lines[0], lines[1:]
|
|
|
|
base = Path(str(self.home / ".local" / "share")) / PROFILE
|
|
self.assertEqual(recorded("convert", "in.dxf"), (f"config=<{base}>", ["<convert>", "<in.dxf>"]))
|
|
self.assertEqual(recorded("convert", xdg=str(self.root / "xdg with space"))[0], f"config=<{self.root}/xdg with space/{PROFILE}>")
|
|
# An explicitly empty value still means "no settings folder", and an explicit one is kept.
|
|
self.assertEqual(recorded("run")[0], f"config=<{base}>")
|
|
self.assertEqual(recorded("run", config="")[0], "config=<>")
|
|
self.assertEqual(recorded("run", config=str(self.root / "explicit"))[0], f"config=<{self.root}/explicit>")
|
|
|
|
def test_the_flatpak_wrapper_starts_the_profile(self):
|
|
generated = self.generated(LINUX / "flatpak-bundle.sh", "WRAPPER", "/app/libexec/cadcraft", self.real)
|
|
# Inside the bundle XDG_DATA_HOME is the nightly app's own sandbox folder.
|
|
sandbox = self.home / ".var/app/ai.storyteller.cadcraft.nightly/data"
|
|
self.assert_starts_the_profile(generated, {"XDG_DATA_HOME": str(sandbox)})
|
|
self.assert_starts_the_profile(generated, fallback=(".var", "app", "ai.storyteller.cadcraft.nightly", "data"))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|