AkurAI Build
Menu

BifrOSt-Apps

public

Latest change a5b8f241ab7e08ba2740e980a5d90870a34b32fd - Add first-party Flatpak app template by Ólafur Búi Ólafsson

#!/usr/bin/env python3

from __future__ import annotations

import py_compile
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") -> subprocess.CompletedProcess[str]:
        return subprocess.run(
            [
                sys.executable,
                str(GENERATOR),
                app_id,
                "BifrOSt & Sample",
                "--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),
            ],
            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_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())


if __name__ == "__main__":
    unittest.main()