Menu
BifrOSt-Apps
publicLatest change f35619cfe0419c596645328bf0e1b682ad3a4473 - Harden RÚV for the 0.1.1 candidate by Ólafur Búi Ólafsson
#!/usr/bin/env python3
from __future__ import annotations
import py_compile
import shutil
from pathlib import Path
import subprocess
import sys
import tempfile
import unittest
import xml.etree.ElementTree as ElementTree
ROOT = Path(__file__).resolve().parents[1]
GENERATOR = ROOT / "scripts/new-app.py"
class NewAppTest(unittest.TestCase):
def run_generator(
self,
destination: Path,
app_id: str = "org.bifrost.SampleApp",
name: str = "BifrOSt & Sample",
stack: str | None = None,
) -> subprocess.CompletedProcess[str]:
return subprocess.run(
[
sys.executable,
str(GENERATOR),
app_id,
name,
"--name-is",
"BifrOSt-sýnishorn",
"--summary",
"Exercise the app template",
"--summary-is",
"Prófar forritasniðmátið",
"--description",
"A complete generated application that exercises identity, metadata, translation, and build validation.",
"--description-is",
"Fullgert forrit sem prófar auðkenni, lýsigögn, þýðingar og sannprófun byggingarferlisins.",
"--output",
str(destination),
]
+ (["--stack", stack] if stack else []),
text=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
check=False,
)
def test_generates_complete_application_with_consistent_identity(self) -> None:
with tempfile.TemporaryDirectory() as temporary:
destination = Path(temporary) / "sample"
completed = self.run_generator(destination)
self.assertEqual(completed.returncode, 0, completed.stderr)
expected = {
"README.md",
"org.bifrost.SampleApp.yml",
"src/main.py",
"po/is.po",
"data/org.bifrost.SampleApp.desktop",
"data/org.bifrost.SampleApp.metainfo.xml",
"data/icons/scalable/apps/org.bifrost.SampleApp.svg",
}
generated = {
str(path.relative_to(destination))
for path in destination.rglob("*")
if path.is_file()
}
self.assertEqual(generated, expected)
py_compile.compile(destination / "src/main.py", doraise=True)
metadata = ElementTree.parse(destination / "data/org.bifrost.SampleApp.metainfo.xml").getroot()
self.assertEqual(metadata.findtext("id"), "org.bifrost.SampleApp")
self.assertEqual(metadata.findtext("name"), "BifrOSt & Sample")
manifest = (destination / "org.bifrost.SampleApp.yml").read_text(encoding="utf-8")
readme = (destination / "README.md").read_text(encoding="utf-8")
self.assertIn('runtime-version: "50"', manifest)
self.assertIn("--default-branch=stable", readme)
self.assertNotIn("--share=network", manifest)
self.assertNotIn("--filesystem=home", manifest)
for path in destination.rglob("*"):
if path.is_file():
self.assertNotIn("@@", path.read_text(encoding="utf-8", errors="ignore"))
def test_generates_dioxus_application_with_consistent_identity(self) -> None:
with tempfile.TemporaryDirectory() as temporary:
destination = Path(temporary) / "sample-dioxus"
completed = self.run_generator(destination, stack="dioxus")
self.assertEqual(completed.returncode, 0, completed.stderr)
expected = {
"README.md",
"org.bifrost.SampleApp.yml",
"Cargo.toml",
"src/main.rs",
"data/org.bifrost.SampleApp.desktop",
"data/org.bifrost.SampleApp.metainfo.xml",
"data/icons/scalable/apps/org.bifrost.SampleApp.svg",
}
generated = {
str(path.relative_to(destination))
for path in destination.rglob("*")
if path.is_file()
}
self.assertEqual(generated, expected)
metadata = ElementTree.parse(destination / "data/org.bifrost.SampleApp.metainfo.xml").getroot()
self.assertEqual(metadata.findtext("id"), "org.bifrost.SampleApp")
cargo = (destination / "Cargo.toml").read_text(encoding="utf-8")
self.assertIn('name = "bifrost-sample-app"', cargo)
manifest = (destination / "org.bifrost.SampleApp.yml").read_text(encoding="utf-8")
self.assertIn("cargo --offline --locked build --release --bin bifrost-sample-app", manifest)
self.assertIn("org.freedesktop.Sdk.Extension.rust-stable", manifest)
self.assertNotIn("--share=network", manifest)
self.assertNotIn("--filesystem=home", manifest)
main_rs = (destination / "src/main.rs").read_text(encoding="utf-8")
self.assertIn("BifrOSt & Sample", main_rs)
for path in destination.rglob("*"):
if path.is_file():
self.assertNotIn("@@", path.read_text(encoding="utf-8", errors="ignore"))
def test_rejects_identity_outside_bifrost_namespace(self) -> None:
with tempfile.TemporaryDirectory() as temporary:
destination = Path(temporary) / "invalid"
completed = self.run_generator(destination, "com.example.Sample")
self.assertNotEqual(completed.returncode, 0)
self.assertIn("org.bifrost.<PascalCaseName>", completed.stderr)
self.assertFalse(destination.exists())
@unittest.skipUnless(shutil.which("desktop-file-validate"), "desktop-file-validate not installed")
def test_escapes_adversarial_desktop_entry_values(self) -> None:
adversarial = "BifrOSt \\ Adversarial\tSample"
with tempfile.TemporaryDirectory() as temporary:
destination = Path(temporary) / "adversarial"
completed = self.run_generator(destination, name=adversarial)
self.assertEqual(completed.returncode, 0, completed.stderr)
desktop_file = destination / "data/org.bifrost.SampleApp.desktop"
desktop_text = desktop_file.read_text(encoding="utf-8")
self.assertIn("Name=BifrOSt \\\\ Adversarial\\tSample\n", desktop_text)
self.assertNotIn("\t", desktop_text)
validated = subprocess.run(
["desktop-file-validate", str(desktop_file)],
text=True,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
check=False,
)
self.assertEqual(validated.returncode, 0, validated.stdout)
if __name__ == "__main__":
unittest.main()