Skip to content

The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.

4.3 KB
Raw Blame
import copy
import importlib.util
import json
from pathlib import Path
import unittest
import test_runtime_update

spec = importlib.util.spec_from_file_location('publish_update', Path(__file__).parents[1] / 'tools/publish-update.py')
publish = importlib.util.module_from_spec(spec)
spec.loader.exec_module(publish)


class PublicationGuards(unittest.TestCase):
    def setUp(self):
        self.fixture = test_runtime_update.BundleGuards()
        self.fixture.setUp()
        self.addCleanup(self.fixture.doCleanups)
        self.fixture.runtime['versions'] = {'hn': '0.1.12', 'cli': '0.3.57'}
        self.fixture.make_package()
        self.bundle = self.fixture.root
        for name in publish.bootstrap_files():
            (self.bundle / name).write_text('# fixture bootstrap\n')
        (self.bundle / 'SHA256SUMS').write_text(''.join(publish.digest(path) + '  ' + path.name + '\n' for path in
            [self.fixture.package, self.bundle / 'package-manifest.json', *[self.bundle / name for name in publish.bootstrap_files()]]))
        self.run = dict(status='completed', conclusion='success', path='.github/workflows/os.yml', head_sha='a' * 40)
        self.receipt = dict(status='passed', update=dict(status='passed', source_commit='a' * 40, candidate=self.fixture.manifest['package']),
            fast_updates=dict(status='passed'), system_channel=dict(status='passed', package=self.fixture.manifest['package'],
                                                                   reboot_keyboard={'confirmed_seconds_since_boot': 12}))

    def test_exact_package_and_complete_receipt_can_be_prepared(self):
        self.assertEqual(publish.validate(self.bundle, self.receipt, self.run)['source_commit'], 'a' * 40)

    def test_bootstrap_cannot_omit_a_platform_helper(self):
        path = self.bundle / 'SHA256SUMS'
        path.write_text(''.join(line for line in path.read_text().splitlines(keepends=True)
                                if not line.endswith('  boot_profile.py\n')))
        with self.assertRaisesRegex(ValueError, 'complete bootstrap bundle'):
            publish.validate(self.bundle, self.receipt, self.run)

    def test_official_bundle_can_migrate_a_preview_through_its_existing_feed(self):
        self.fixture.manifest['requires_os_version'] = '0.1.0'
        self.fixture.manifest['upgrades_from'] = [self.fixture.base]
        self.fixture.make_package()
        (self.bundle / 'SHA256SUMS').write_text(''.join(publish.digest(path) + '  ' + path.name + '\n' for path in
            [self.fixture.package, self.bundle / 'package-manifest.json', *[self.bundle / name for name in publish.bootstrap_files()]]))
        self.assertEqual(publish.validate(self.bundle, self.receipt, self.run)['requires_os_version'], '0.1.0')
        # The original installed updater must accept the explicit migration too.
        test_runtime_update.update.validate_bundle(self.bundle, self.fixture.base)

    def test_failed_stale_or_missing_native_scopes_cannot_advance_the_feed(self):
        for field in ['update', 'fast_updates', 'system_channel']:
            bad = copy.deepcopy(self.receipt)
            bad[field]['status'] = 'failed'
            with self.subTest(field=field), self.assertRaises(ValueError):
                publish.validate(self.bundle, bad, self.run)
        bad = copy.deepcopy(self.receipt)
        del bad['system_channel']['reboot_keyboard']
        with self.assertRaisesRegex(ValueError, 'encrypted reboot'):
            publish.validate(self.bundle, bad, self.run)
        for change in [{'conclusion': 'failure'}, {'status': 'in_progress'}, {'head_sha': 'b' * 40}, {'path': '.github/workflows/ci.yml'}]:
            with self.subTest(change=change), self.assertRaises(ValueError):
                publish.validate(self.bundle, self.receipt, dict(self.run, **change))

    def test_private_runtime_fixture_and_changed_bootstrap_are_never_published(self):
        (self.bundle / 'apply-update.py').write_text('# changed after packaging\n')
        with self.assertRaisesRegex(ValueError, 'bootstrap bundle'):
            publish.validate(self.bundle, self.receipt, self.run)
        self.fixture.runtime['versions']['hn'] = '999.0.1'
        self.fixture.make_package()
        with self.assertRaisesRegex(ValueError, 'private fixtures'):
            publish.validate(self.bundle, self.receipt, self.run)


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