Luce packages
LuceLuce BaseLuciaOS

dymokomi/luc

Source of release 0.24.0

0.24.0 / tests / packages.py

#!/usr/bin/env python3
"""Anonymous registry packages: add, lock, sync, transitive graphs, offline use and tamper refusal.

The registry is a directory of static files, so this test builds one with stock Git and
serves it over loopback HTTP. No account, credential or registry server is involved."""
import functools
import hashlib
import http.server
import os
from pathlib import Path
import shutil
import subprocess
import sys
import tempfile
import threading

luc = str(Path(sys.argv[1]).resolve())
GIT_ENV = dict(os.environ, GIT_CONFIG_NOSYSTEM='1', GIT_CONFIG_GLOBAL=os.devnull,
               GIT_AUTHOR_NAME='Fixture', GIT_AUTHOR_EMAIL='fixture@example.test',
               GIT_COMMITTER_NAME='Fixture', GIT_COMMITTER_EMAIL='fixture@example.test')


class Quiet(http.server.SimpleHTTPRequestHandler):
    def log_message(self, *args): pass


def git(repo, *args, input=None):
    return subprocess.run(['git', '-C', str(repo), *args], env=GIT_ENV, input=input, check=True,
                          capture_output=True, timeout=60).stdout


def release_app(site, work, name, version, files, install=None, application=''):
    """Publish an application: an entry, optional resources and an optional install script."""
    repo = work / f'{name}-{version}'
    (repo / 'src').mkdir(parents=True)
    script = '    str install = "install.luc"\n' if install is not None else ''
    kind = 'application' if application else 'tool'
    definition = (f'#prisma 4.0\ndef package "{name}" {{\n    str owner = "acme"\n    str version = "{version}"\n'
                  f'    str kind = "{kind}"\n    str language = "luce-base"\n    str entry = "src/main.lucb"\n{script}{application}}}\n')
    (repo / 'package.prisma').write_text(definition)
    (repo / 'src' / 'main.lucb').write_text(f'pub func main(arguments: str[]) -> i32:\n    print("{name} {version} runs")\n    return 0\n')
    if install is not None: (repo / 'install.luc').write_text(install)
    for relative, content in files.items():
        (repo / relative).parent.mkdir(parents=True, exist_ok=True)
        (repo / relative).write_text(content)
    git(repo, 'init', '-q', '-b', 'main')
    git(repo, 'add', '-A')
    git(repo, 'commit', '-qm', f'{name} {version}')
    commit = git(repo, 'rev-parse', 'HEAD').decode().strip()
    pack = git(repo, 'pack-objects', '--stdout', input=git(repo, 'rev-list', '--objects', 'HEAD'))
    target = site / 'acme' / name
    target.mkdir(parents=True, exist_ok=True)
    (target / f'{version}.pack').write_bytes(pack)
    (target / f'{version}.prisma').write_text(definition)
    with (target / 'versions').open('a') as listing:
        listing.write(f'{version} {hashlib.sha256(pack).hexdigest()} {commit}\n')


def release(site, work, name, version, module_source, dependencies=''):
    """Publish one history-free release exactly as the registry does on a tag push."""
    repo = work / f'{name}-{version}'
    module = name.replace('-', '_')
    (repo / 'src').mkdir(parents=True)
    definition = (f'#prisma 4.0\ndef package "{name}" {{\n    str owner = "acme"\n    str version = "{version}"\n'
                  f'    str kind = "package"\n    str language = "luce-base"\n    str[] public = ["{module}"]\n'
                  f'{dependencies}}}\n')
    (repo / 'package.prisma').write_text(definition)
    (repo / 'src' / f'{module}.lucb').write_text(module_source)
    # Development material every release carries and no dependency checkout should.
    for relative in ('tests/vectors.txt', 'dev/notes.txt'):
        (repo / relative).parent.mkdir(parents=True, exist_ok=True)
        (repo / relative).write_text('development only')
    git(repo, 'init', '-q', '-b', 'main')
    git(repo, 'add', '-A')
    git(repo, 'commit', '-qm', f'{name} {version}')
    commit = git(repo, 'rev-parse', 'HEAD').decode().strip()
    pack = git(repo, 'pack-objects', '--stdout', input=git(repo, 'rev-list', '--objects', 'HEAD'))
    target = site / 'acme' / name
    target.mkdir(parents=True, exist_ok=True)
    (target / f'{version}.pack').write_bytes(pack)
    (target / f'{version}.prisma').write_text(definition)
    with (target / 'versions').open('a') as listing:
        listing.write(f'{version} {hashlib.sha256(pack).hexdigest()} {commit}\n')


SCRIPT = '''pub func main(arguments: list[str]) -> int!:
    let name = arguments[2]
    print(f"copy build/{name} -> bin/{name}")
    print("copy themes -> share/my themes")
    print(f"link {name} -> bin/{name}")
    print(f"link {name}-{arguments[0]} -> bin/{name}")
    return 0
'''


def applications(site, work, root, online):
    """install builds and places what the sandboxed script asks for; uninstall removes exactly that."""
    home = root / 'luce-home'
    shelf = root / 'desktop-applications'
    env = dict(online, LUC_HOME=str(home), LUC_APPLICATIONS=str(shelf))
    sandbox = 'LUCE' in os.environ

    def luc_run(*args, success=True):
        result = subprocess.run([luc, *args], cwd=root, env=env, capture_output=True, text=True, timeout=600)
        assert (result.returncode == 0) == success, (args, result.stdout, result.stderr)
        return result.stdout + result.stderr

    for flag, kind in (('--package', 'package'), ('--tool', 'tool'), ('--application', 'application')):
        luc_run('new', f'made-{kind}', flag)
        made = (root / f'made-{kind}' / 'package.prisma').read_text()
        assert f'str kind = "{kind}"' in made
        assert ('def application "bundle"' in made and 'def dependency "luce-ui"' in made and 'com.dymokomi.made-application' in made and 'str name = "Made Application"' in made) == (kind == 'application')
    release_app(site, work, 'plain-clock', '1.0.0', {})
    assert 'installed acme/plain-clock 1.0.0' in luc_run('install', 'acme/plain-clock')
    assert subprocess.check_output([str(home / 'bin/plain-clock')], text=True).strip() == 'plain-clock 1.0.0 runs'
    assert 'already installed' in luc_run('install', 'acme/plain-clock')

    # A declared application is bundled for the desktop and installed as that bundle.
    release_app(site, work, 'desk-clock', '3.0.0', {'assets/clock.icns': 'icon-bytes'}, application=(
        '    def application "bundle" {\n        str name = "Desk Clock"\n        str identifier = "com.example.desk-clock"\n'
        '        str icon = "assets/clock.icns"\n    }\n'))
    assert 'installed acme/desk-clock 3.0.0' in luc_run('install', 'acme/desk-clock')
    placed = home / 'apps/desk-clock/3.0.0'
    # The bundle is installed where the system keeps applications, not under ~/.luce, which
    # keeps only the record of what was placed.
    assert not any(path.suffix in ('.app', '.AppDir') for path in placed.iterdir())
    if sys.platform == 'darwin':
        exposed = shelf / 'Desk Clock.app'
        assert exposed.is_dir() and not exposed.is_symlink()
        bundle = exposed / 'Contents'
        plist = (bundle / 'Info.plist').read_text()
        assert '<string>com.example.desk-clock</string>' in plist and '<string>Desk Clock</string>' in plist
        assert '<string>3.0.0</string>' in plist and '<string>clock.icns</string>' in plist and '<string>APPL</string>' in plist
        assert (bundle / 'Resources/clock.icns').read_text() == 'icon-bytes'
        subprocess.run(['plutil', '-lint', str(bundle / 'Info.plist')], check=True, capture_output=True)
        assert subprocess.check_output([str(bundle / 'MacOS/desk-clock')], text=True).strip() == 'desk-clock 3.0.0 runs'
        placed_paths = [exposed]
    else:
        appdir = shelf / 'luce/apps/Desk Clock.AppDir'
        assert 'Name=Desk Clock' in (appdir / 'desk-clock.desktop').read_text()
        assert subprocess.check_output([str(appdir / 'AppRun')], text=True).strip() == 'desk-clock 3.0.0 runs'
        exposed = shelf / 'applications/desk-clock.desktop'
        entry = exposed.read_text()
        assert 'Name=Desk Clock' in entry and 'Terminal=false' in entry and str(appdir) in entry
        placed_paths = [appdir, exposed]
    assert (placed / '.luc-placed').read_text().splitlines() == [str(path) for path in placed_paths]
    assert subprocess.check_output([str(home / 'bin/desk-clock')], text=True).strip() == 'desk-clock 3.0.0 runs'
    # update brings the newest release and replaces the installed application
    desk = ('    def application "bundle" {\n        str name = "Desk Clock"\n        str identifier = "com.example.desk-clock"\n'
            '        str icon = "assets/clock.icns"\n    }\n')
    assert 'is the newest' in luc_run('update', 'acme/desk-clock')
    release_app(site, work, 'desk-clock', '3.0.1', {'assets/clock.icns': 'icon-bytes'}, application=desk)
    assert 'installed acme/desk-clock 3.0.1' in luc_run('update', 'acme/desk-clock')
    assert subprocess.check_output([str(home / 'bin/desk-clock')], text=True).strip() == 'desk-clock 3.0.1 runs'
    assert sorted(path.name for path in (home / 'apps/desk-clock').iterdir()) == ['3.0.1']
    placed = home / 'apps/desk-clock/3.0.1'
    assert (placed / '.luc-placed').read_text().splitlines() == [str(path) for path in placed_paths]
    assert all(path.exists() for path in placed_paths)
    (shelf / 'Unrelated.app').mkdir()
    assert 'uninstalled desk-clock' in luc_run('uninstall', 'desk-clock')
    assert not any(path.exists() for path in placed_paths) and (shelf / 'Unrelated.app').is_dir(), 'uninstall removes exactly what install placed'
    assert not (home / 'bin/desk-clock').exists()
    # Another application of the same name, which luc did not install, is never replaced.
    if sys.platform == 'darwin':
        foreign = shelf / 'Desk Clock.app/Contents'
        foreign.mkdir(parents=True)
        (foreign / 'Info.plist').write_text('<string>com.other.clock</string>')
        assert 'will not replace it' in luc_run('install', 'acme/desk-clock', success=False)
        assert (foreign / 'Info.plist').read_text() == '<string>com.other.clock</string>'
        shutil.rmtree(shelf / 'Desk Clock.app')
    assert 'is a package' in luc_run('install', 'acme/greeter', success=False)
    if sandbox:
        release_app(site, work, 'clock', '2.1.0', {'themes/dark.theme': 'dark\n', 'themes/nested/light.theme': 'light\n'}, SCRIPT)
        assert 'installed acme/clock 2.1.0' in luc_run('install', 'acme/clock@2.1.0')
        placed = home / 'apps/clock/2.1.0'
        assert (placed / 'share/my themes/nested/light.theme').read_text() == 'light\n'
        assert subprocess.check_output([str(home / 'bin/clock')], text=True).strip() == 'clock 2.1.0 runs'
        system = 'macos' if sys.platform == 'darwin' else 'linux'
        assert (home / f'bin/clock-{system}').is_symlink()
        assert luc_run('list').split() == ['clock', '2.1.0', 'plain-clock', '1.0.0']
        # A script is confined and its plan is validated: neither can reach outside.
        for number, hostile in enumerate(('print("copy ../../etc/passwd -> bin/x")', 'print("copy /etc/passwd -> bin/x")',
                                          'print("copy themes -> ../../escape")', 'print("run rm -rf / -> x")',
                                          'print("copy themes bin/x")', 'import files\n    print("copy themes -> share")')):
            name = f'hostile{number}'
            release_app(site, work, name, '1.0.0', {'themes/a': 'a\n'},
                        f'pub func main(arguments: list[str]) -> int!:\n    {hostile}\n    return 0\n')
            luc_run('install', f'acme/{name}', success=False)
            assert not (home / 'apps' / name / '1.0.0').exists() and not (home / 'bin/x').exists()
        assert 'uninstalled clock' in luc_run('uninstall', 'clock')
        assert not (home / 'apps/clock').exists() and not (home / 'bin/clock').exists() and not (home / f'bin/clock-{system}').exists()
    assert (home / 'bin/plain-clock').is_symlink()
    assert 'uninstalled plain-clock' in luc_run('uninstall', 'plain-clock')
    assert not (home / 'bin/plain-clock').exists() and 'not installed' in luc_run('uninstall', 'plain-clock', success=False)

    # A bare name is the one registry package of that name, found in the index; an
    # unknown name fails, and so does one several owners publish, naming each of them.
    (site / 'index').write_text('acme/plain-clock\t1.0.0\tA clock.\ttool\nacme/greeter\t0.2.0\tGreets.\tpackage\n')
    assert 'installed acme/plain-clock 1.0.0' in luc_run('install', 'plain-clock')
    assert luc_run('latest', 'plain-clock').strip() == '1.0.0'
    assert 'uninstalled plain-clock' in luc_run('uninstall', 'plain-clock')
    assert 'no registry package is called no-clock' in luc_run('install', 'no-clock', success=False)
    with (site / 'index').open('a') as index:
        index.write('zeta/plain-clock\t2.0.0\tAnother clock.\ttool\n')
    ambiguous = luc_run('install', 'plain-clock', success=False)
    assert 'acme/plain-clock' in ambiguous and 'zeta/plain-clock' in ambiguous and 'owner/name' in ambiguous, ambiguous
    (site / 'index').unlink()
    print('PASS application install/list/uninstall' + (', sandboxed install script and hostile plans' if sandbox else ' (no LUCE: scripted install skipped)'), flush=True)


def main():
    with tempfile.TemporaryDirectory(prefix='luc-packages-') as temporary:
        root = Path(temporary)
        site, work, cache = root / 'site', root / 'work', root / 'cache'
        for directory in (site, work, cache): directory.mkdir()
        release(site, work, 'greeter', '0.1.0', 'pub func greeting() -> str:\n    return "hello 0.1.0"\n')
        release(site, work, 'greeter', '0.1.1', 'pub func greeting() -> str:\n    return "hello 0.1.1"\n')
        release(site, work, 'greeter', '0.2.0', 'pub func greeting() -> str:\n    return "hello 0.2.0"\n')
        release(site, work, 'polite-kit', '1.0.0', 'import greeter.greeter\n\npub func welcome() -> str:\n    return greeter.greeting()\n',
                '    def dependency "greeter" {\n        str owner = "acme"\n        str version = "^0.1.0"\n'
                '        str path = "../author-only-checkout"\n    }\n')
        handler = functools.partial(Quiet, directory=str(site))
        server = http.server.ThreadingHTTPServer(('127.0.0.1', 0), handler)
        threading.Thread(target=server.serve_forever, daemon=True).start()
        online = dict(os.environ, LUC_REGISTRY=f'http://127.0.0.1:{server.server_port}', LUC_CACHE=str(cache))
        offline = dict(online, LUC_REGISTRY='http://127.0.0.1:1')

        def run(*args, env=online, success=True):
            result = subprocess.run([luc, *args], cwd=app, env=env, capture_output=True, text=True, timeout=300)
            assert (result.returncode == 0) == success, (args, result.stdout, result.stderr)
            return result.stdout + result.stderr

        app = root
        run('new', 'app')
        app = root / 'app'
        # a registry package is added with no version: its newest release, which the lock records
        assert 'added dependency acme/polite-kit (newest release, 1.0.0)' in run('add', 'acme/polite-kit')
        added = (app / 'package.prisma').read_text()
        assert 'def dependency "polite-kit" {\n        str owner = "acme"\n    }' in added, added
        (app / 'src' / 'main.lucb').write_text('import polite_kit.polite_kit\n\npub func main(arguments: str[]) -> i32:\n'
                                               '    print(polite_kit.welcome())\n    return 0\n')
        lock = (app / 'luc.lock').read_text()
        # ^0.1.0 takes the newest 0.1.x and never 0.2.0; the author's path override is ignored.
        assert 'def package "greeter"' in lock and 'str version = "0.1.1"' in lock and '0.2.0' not in lock
        assert hashlib.sha256((site / 'acme/greeter/0.1.1.pack').read_bytes()).hexdigest() in lock
        assert 'hello 0.1.1' in run('run').splitlines()
        # Dependencies are unpacked without their development paths.
        greeter = app / '.luc' / 'deps' / 'greeter'
        assert (greeter / 'package.prisma').exists() and (greeter / 'src').is_dir(), list(greeter.parent.iterdir())
        assert not (greeter / 'tests').exists() and not (greeter / 'dev').exists()
        assert 'no such package' in run('add', 'acme/missing', success=False)
        # The newest release, whatever the requirement: 0.2.0 over the 0.1 line.
        assert run('latest', 'acme/greeter').strip() == '0.2.0'
        assert 'no such package' in run('latest', 'acme/missing', success=False)
        # `luc add` takes a bare name as `luc install` does: the one registry package of it
        (site / 'index').write_text('acme/polite-kit\t1.0.0\tPolite.\tpackage\n')
        refused = run('add', 'missing-kit', success=False)
        assert 'no registry package is called missing-kit' in refused, refused
        before = (app / 'package.prisma').read_text()
        run('remove', 'polite-kit')
        assert 'added dependency acme/polite-kit (newest release, 1.0.0)' in run('add', 'polite-kit')
        assert (app / 'package.prisma').read_text() == before
        # a dependency without a version is the newest release, which the lock records
        app = root
        run('new', 'newest')
        app = root / 'newest'
        manifest = (app / 'package.prisma').read_text()
        (app / 'package.prisma').write_text(manifest.rstrip()[:-1] + '    def dependency "greeter" {\n        str owner = "acme"\n    }\n}\n')
        run('lock')
        assert 'str version = "0.2.0"' in (app / 'luc.lock').read_text(), (app / 'luc.lock').read_text()
        app = root / 'app'

        # A fresh checkout rebuilds from the lock and the cache alone.
        for leftover in ('.luc', 'build'): subprocess.run(['rm', '-rf', str(app / leftover)], check=True)
        assert 'synced 2' in run('sync', '--offline', env=offline)
        assert 'hello 0.1.1' in run('run', env=offline).splitlines()
        # With nothing cached, the packs download together before they are unpacked in order.
        subprocess.run(['rm', '-rf', str(app / '.luc'), str(cache)], check=True)
        fetched = run('sync')
        assert 'fetching 2 packages' in fetched and 'synced 2' in fetched, fetched
        # the ordered pass after the downloads unpacks; it does not claim a second fetch
        assert fetched.index('fetching 2 packages') < fetched.index('unpacking 2 dependencies'), fetched
        assert (cache / 'acme/greeter/0.1.1.pack').exists() and (cache / 'acme/polite-kit').is_dir()

        # A fresh clone of a project whose author keeps its packages as sibling checkouts:
        # a checkout path that is not there resolves from the registry by owner and version.
        manifest = app / 'package.prisma'
        authored = manifest.read_text()
        manifest.write_text(authored.replace('def dependency "polite-kit" {', 'def dependency "polite-kit" {\n        str path = "../polite-kit-checkout"', 1))
        subprocess.run(['rm', '-rf', str(app / '.luc'), str(app / 'build'), str(app / 'luc.lock')], check=True)
        assert 'hello 0.1.1' in run('run').splitlines()
        assert 'def package "polite-kit"' in (app / 'luc.lock').read_text()
        manifest.write_text(authored)
        assert 'synced 2' in run('lock') + run('sync')

        # The lock's SHA-256 is the integrity check: a changed cache or download is refused.
        cached = cache / 'acme/greeter/0.1.1.pack'
        cached.write_bytes(cached.read_bytes() + b'x')
        subprocess.run(['rm', '-rf', str(app / '.luc')], check=True)
        assert 'does not match luc.lock' in run('sync', '--offline', env=offline, success=False)
        assert 'synced 2' in run('sync')
        published = site / 'acme/greeter/0.1.1.pack'
        published.write_bytes(published.read_bytes() + b'x')
        cached.unlink()
        subprocess.run(['rm', '-rf', str(app / '.luc')], check=True)
        assert 'does not match the SHA-256' in run('sync', success=False)
        applications(site, work, root, online)
        server.shutdown()
    print('PASS anonymous add/lock/sync, caret selection, transitive graph, offline rebuild and SHA-256 refusal', flush=True)


if __name__ == '__main__':
    main()