def __init__( self, project_config: _config.Config, extracted_metadata: Optional[_metadata.ExtractedMetadata], ) -> None: self._project_config = project_config self._extracted_metadata = extracted_metadata self._snapcraft_yaml_path = project_config.project.info.snapcraft_yaml_file_path self._prime_dir = project_config.project.prime_dir self._parts_dir = project_config.project.parts_dir self._arch_triplet = project_config.project.arch_triplet self.meta_dir = os.path.join(self._prime_dir, "meta") self.meta_gui_dir = os.path.join(self.meta_dir, "gui") self._config_data = project_config.data.copy() self._original_snapcraft_yaml = project_config.project.info.get_raw_snapcraft( ) self._install_path_pattern = re.compile( r"{}/[a-z0-9][a-z0-9+-]*/install".format(re.escape( self._parts_dir))) os.makedirs(self.meta_dir, exist_ok=True) # TODO: create_snap_packaging managles config data, so we create # a new private instance of snap_meta. Longer term, this needs # to converge with project's snap_meta. self._snap_meta = Snap.from_dict(project_config.data)
def test_snap_yaml_passthrough(self): snap_dict = OrderedDict({ "name": "snap-test", "version": "snap-version", "summary": "snap-summary", "description": "snap-description", "passthrough": { "otherkey": "othervalue" }, "grade": "stable", }) snap = Snap.from_dict(snap_dict=snap_dict) snap.validate() transformed_dict = snap_dict.copy() passthrough = transformed_dict.pop("passthrough") transformed_dict.update(passthrough) self.assertEqual(transformed_dict, snap.to_snap_yaml_dict()) self.assertEqual(True, snap.is_passthrough_enabled) self.assertEqual(passthrough, snap.passthrough) self.assertEqual(snap_dict["name"], snap.name) self.assertEqual(snap_dict["version"], snap.version) self.assertEqual(snap_dict["summary"], snap.summary) self.assertEqual(snap_dict["description"], snap.description)
def __init__(self, project: project.Project) -> None: self.build_snaps: Set[str] = set() self.project = project # raw_snapcraft_yaml is read only, create a new copy snapcraft_yaml = apply_extensions(project.info.get_raw_snapcraft()) self.validator = Validator(snapcraft_yaml) self.validator.validate() snapcraft_yaml = self._expand_filesets(snapcraft_yaml) self.data = self._expand_env(snapcraft_yaml) self.data["architectures"] = _process_architectures( self.data.get("architectures"), project.deb_arch) self._ensure_no_duplicate_app_aliases() self._global_grammar_processor = grammar_processing.GlobalGrammarProcessor( properties=self.data, arch=project.host_deb_arch, target_arch=project.target_arch, ) # XXX: Resetting snap_meta due to above mangling of data. # Convergence to operating on snap_meta will remove this requirement... project._snap_meta = Snap.from_dict(self.data) self.parts = PartsConfig(parts=self.data, project=project, validator=self.validator)
def test_missing_keys(self): snap_dict = OrderedDict({"name": "snap-test", "grade": "stable"}) snap = Snap.from_dict(snap_dict=snap_dict) self.assertEqual(snap_dict, snap.to_dict()) self.assertRaises(errors.MissingSnapcraftYamlKeysError, snap.validate)
def test_system_usernames_longform_scope(self): snap_dict = OrderedDict({ "name": "snap-test", "version": "test-version", "summary": "test-summary", "description": "test-description", "system-usernames": { "snap_daemon": { "scope": "shared" }, "lxd": { "scope": "shared" }, }, }) snap = Snap.from_dict(snap_dict=snap_dict) snap.validate() self.assertThat(snap.system_usernames["snap_daemon"].name, Equals("snap_daemon")) self.assertThat(snap.system_usernames["snap_daemon"].scope, Equals(SystemUserScope.SHARED)) self.assertThat(snap.system_usernames["lxd"].name, Equals("lxd")) self.assertThat(snap.system_usernames["lxd"].scope, Equals(SystemUserScope.SHARED))
def get_project(*, is_managed_host: bool = False, **kwargs): # We need to do this here until we can get_snapcraft_yaml as part of Project. if is_managed_host: try: os.chdir(os.path.expanduser(os.path.join("~", "project"))) except FileNotFoundError: # No project found (fresh environment). raise errors.ProjectNotFoundError() snapcraft_yaml_file_path = get_snapcraft_yaml() # This method may be called from a click.Command with no parent. ctx = click.get_current_context() if ctx.parent is not None: for key, value in ctx.parent.params.items(): if not kwargs.get(key): kwargs[key] = value project = Project( debug=kwargs.pop("debug", False), target_deb_arch=kwargs.pop("target_arch", None), snapcraft_yaml_file_path=snapcraft_yaml_file_path, is_managed_host=is_managed_host, ) # Validate yaml info from schema prior to consumption. if project.info is not None: project.info.validate_raw_snapcraft() # TODO: this should be automatic on get_project(). # This is not the complete meta parsed by the project loader. project._snap_meta = Snap.from_dict(project.info.get_raw_snapcraft()) return project
def test_get_provider_content_directories_with_content_plugs(self): snap_dict = OrderedDict({ "name": "snap-test", "version": "test-version", "summary": "test-summary", "description": "test-description", "plugs": { "test-plug": { "interface": "content", "content": "content", "target": "target", "default-provider": "gtk-common-themes:gtk-3-themes", } }, }) meta_snap_yaml = dedent(""" name: test-content-snap-meta-snap-yaml version: "1.0" summary: test-summary description: test-description base: core18 architectures: - all confinement: strict grade: stable slots: test-slot-name: interface: content source: read: - $SNAP/dir1 - $SNAP/dir2 """) snap = Snap.from_dict(snap_dict=snap_dict) snap.validate() patcher = mock.patch( "snapcraft_legacy.internal.common.get_installed_snap_path") mock_core_path = patcher.start() mock_core_path.return_value = self.path self.addCleanup(patcher.stop) meta_path = os.path.join(self.path, "meta") os.makedirs(meta_path) snap_yaml_path = os.path.join(meta_path, "snap.yaml") open(snap_yaml_path, "w").write(meta_snap_yaml) expected_content_dirs = set( [os.path.join(self.path, "dir1"), os.path.join(self.path, "dir2")]) self.assertEqual(expected_content_dirs, snap.get_provider_content_directories())
def test_simple(self): snap_dict = OrderedDict({ "name": "snap-test", "version": "snap-version", "summary": "snap-summary", "description": "snap-description", "grade": "stable", }) snap = Snap.from_dict(snap_dict=snap_dict) snap.validate() self.assertEqual(snap_dict, snap.to_dict()) self.assertEqual(False, snap.is_passthrough_enabled) self.assertEqual(snap_dict["name"], snap.name) self.assertEqual(snap_dict["version"], snap.version) self.assertEqual(snap_dict["summary"], snap.summary) self.assertEqual(snap_dict["description"], snap.description)
def test_ensure_command_chain_assumption(self): snap_dict = OrderedDict({ "name": "snap-test", "version": "snap-version", "summary": "snap-summary", "description": "snap-description", "apps": { "test-app": { "command": "test-app", "command-chain": ["test-command-chain"], } }, }) snap = Snap.from_dict(snap_dict=snap_dict) snap._ensure_command_chain_assumption() snap.validate() self.assertEqual({"command-chain"}, snap.assumes)
def test_grade_devel_statisfies_required_grade(self): self.fake_snapd.snaps_result = [{ "name": "fake-base", "channel": "edge", "revision": "fake-revision" }] snap_dict = OrderedDict({ "name": "snap-test", "base": "fake-base", "version": "snap-version", "summary": "snap-summary", "description": "snap-description", "grade": "devel", }) snap = Snap.from_dict(snap_dict=snap_dict) snap.validate()
def test_is_passthrough_enabled_hook(self): snap_dict = OrderedDict({ "name": "snap-test", "version": "test-version", "summary": "test-summary", "description": "test-description", "hooks": { "test-hook": { "passthrough": { "some-key": "some-value" } } }, }) snap = Snap.from_dict(snap_dict=snap_dict) snap.validate() self.assertEqual(True, snap.is_passthrough_enabled)
def test_snapcraft_yaml_links(self): snap_dict = { "name": "snap-test", "version": "test-version", "summary": "test-summary", "description": "test-description", "issues": "https://bug_url.org", "donation": [ "https://paypal.com", "https://cafecito.app/", "https://ko-fi.com/", ], "contact": ["mailto:[email protected]", "*****@*****.**"], "source-code": "https://github.com/org/source", "website": "https://webfront.org", } snap = Snap.from_dict(snap_dict=snap_dict) snap.validate() self.assertEqual( { "issues": ["https://bug_url.org"], "donation": [ "https://paypal.com", "https://cafecito.app/", "https://ko-fi.com/", ], "contact": ["mailto:[email protected]", "*****@*****.**"], "source-code": ["https://github.com/org/source"], "website": ["https://webfront.org"], }, snap.to_snap_yaml_dict()["links"], )
def test_build_base_and_write_snap_yaml_type_base(self): snap_dict = OrderedDict({ "name": "core18", "version": "snap-version", "summary": "snap-summary", "description": "snap-description", "grade": "devel", "type": "base", }) snap = Snap.from_dict(snap_dict=snap_dict) snap.validate() # Write snap yaml. snap_yaml_path = os.path.join(self.path, "snap.yaml") snap.write_snap_yaml(path=snap_yaml_path) # Read snap yaml. written_snap_yaml = open(snap_yaml_path, "r").read() self.assertEqual(snap_dict, snap.to_dict()) self.assertTrue("base" in written_snap_yaml) self.assertEqual(snap.get_build_base(), "core18")
def test_empty_from_dict(self): snap_dict = OrderedDict({}) snap = Snap.from_dict(snap_dict=snap_dict) self.assertEqual(snap_dict, snap.to_dict())
def test_all_keys(self): snap_dict = { "name": "snap-test", "version": "test-version", "summary": "test-summary", "description": "test-description", "adopt-info": "some-part", "apps": { "test-app": { "command": "test-app" } }, "architectures": ["all"], "assumes": ["command-chain"], "base": "core18", "confinement": "strict", "compression": "lzo", "environment": { "TESTING": "1" }, "epoch": 0, "grade": "devel", "hooks": { "test-hook": { "command-chain": ["cmd1"], "environment": { "FOO": "BAR" }, "plugs": ["network"], } }, "layout": { "/target": { "bind": "$SNAP/foo" } }, "license": "GPL", "passthrough": { "test": "value" }, "plugs": { "test-plug": { "interface": "some-value" } }, "slots": { "test-slot": { "interface": "some-value" } }, "system-usernames": { "snap_daemon": { "scope": "shared" } }, "title": "test-title", "type": "base", } snap = Snap.from_dict(snap_dict=snap_dict) snap.validate() self.assertEqual(snap_dict, snap.to_dict()) self.assertEqual(True, snap.is_passthrough_enabled)
def test_snap_yaml_all_keys(self): snap_dict = { "name": "snap-test", "version": "test-version", "summary": "test-summary", "description": "test-description", "adopt-info": "some-part", "apps": { "test-app": { "command": "test-app" } }, "architectures": ["all"], "assumes": ["command-chain"], "base": "core18", "confinement": "strict", "compression": "xz", "environment": { "TESTING": "1" }, "epoch": 0, "grade": "devel", "hooks": { "test-hook": { "command-chain": ["cmd1"], "environment": { "FOO": "BAR" }, "plugs": ["network"], } }, "layout": { "/target": { "bind": "$SNAP/foo" } }, "license": "GPL", "links": { "issues": ["https://bug_url.org"], "donation": [ "https://paypal.com", "https://cafecito.app/", "https://ko-fi.com/", ], "contact": ["mailto:[email protected]", "*****@*****.**"], "source-code": ["https://github.com/org/source"], "website": ["https://webfront.org"], }, "package-repositories": [ { "type": "apt", "ppa": "test/ppa" }, { "type": "apt", "architectures": ["amd64", "i386"], "components": ["main"], "formats": ["deb"], "key-id": "A" * 40, "key-server": "test-key-server.com", "suites": ["xenial"], "url": "http://archive.ubuntu.com", }, ], "passthrough": { "test": "value" }, "plugs": { "test-plug": { "interface": "some-value" } }, "slots": { "test-slot": { "interface": "some-value" } }, "system-usernames": { "snap_daemon": { "scope": "shared" } }, "title": "test-title", "type": "base", } snap = Snap.from_dict(snap_dict=snap_dict) snap.validate() expected_dict = snap_dict.copy() expected_dict.pop("adopt-info") expected_dict.pop("compression") expected_dict.pop("package-repositories") expected_dict.update(expected_dict.pop("passthrough")) self.assertEqual(expected_dict, snap.to_snap_yaml_dict()) self.assertEqual(True, snap.is_passthrough_enabled)