def run(self, parsed_args): """Run the command.""" if not self.name: raise RuntimeError("command name not specified") emit.debug( f"lifecycle command: {self.name!r}, arguments: {parsed_args!r}") parts_lifecycle.run(self.name, parsed_args)
def test_config_not_found(new_dir): """If snapcraft.yaml is not found, raise an error.""" with pytest.raises(errors.SnapcraftError) as raised: parts_lifecycle.run("pull", argparse.Namespace()) assert str(raised.value) == ( "Could not find snap/snapcraft.yaml. Are you sure you are in the right " "directory?") assert raised.value.resolution == "To start a new project, use `snapcraft init`"
def test_lifecycle_run_expand_craft_vars(new_dir, mocker): mocker.patch("platform.machine", return_value="aarch64") mocker.patch( "snapcraft.parts.lifecycle.get_build_plan", return_value=[("arm64", "arm64")], ) content = textwrap.dedent( """\ name: mytest version: "0.1" base: core22 summary: Environment expansion test description: Environment expansion test grade: stable confinement: strict apps: app: command: usr/$CRAFT_ARCH_TRIPLET/foo parts: part1: plugin: nil override-build: | touch $CRAFT_PART_INSTALL/foo chmod +x $CRAFT_PART_INSTALL/foo organize: foo: usr/$CRAFT_ARCH_TRIPLET/foo """ ) yaml_path = Path("snapcraft.yaml") yaml_path.write_text(content) parts_lifecycle.run( "prime", argparse.Namespace( parts=[], destructive_mode=True, use_lxd=False, provider=None, enable_manifest=False, manifest_image_information=None, bind_ssh=False, build_for=None, debug=False, ), ) assert Path(new_dir / "prime/usr/aarch64-linux-gnu/foo").is_file() meta_yaml = Path(new_dir / "prime/meta/snap.yaml").read_text() assert "command: usr/aarch64-linux-gnu/foo" in meta_yaml
def test_config_loading_error(new_dir, mocker, snapcraft_yaml): """Catch OSErrors when loading snapcraft.yaml""" mocker.patch( "builtins.open", side_effect=OSError(2, "test-message", "test-filename"), ) snapcraft_yaml(base="core22") with pytest.raises(errors.SnapcraftError) as raised: parts_lifecycle.run("pull", argparse.Namespace()) assert str(raised.value) == ("test-message: 'test-filename'.")
def test_snapcraft_yaml_load(new_dir, snapcraft_yaml, filename, mocker): """Snapcraft.yaml should be parsed as a valid yaml file.""" yaml_data = snapcraft_yaml(base="core22", filename=filename) run_command_mock = mocker.patch("snapcraft.parts.lifecycle._run_command") mocker.patch("snapcraft.utils.get_parallel_build_count", return_value=5) parts_lifecycle.run( "pull", argparse.Namespace( parts=["part1"], destructive_mode=True, use_lxd=False, provider=None, enable_manifest=False, manifest_image_information=None, bind_ssh=False, build_for=None, ), ) project = Project.unmarshal(yaml_data) if filename == "build-aux/snap/snapcraft.yaml": assets_dir = Path("build-aux/snap") else: assets_dir = Path("snap") assert run_command_mock.mock_calls == [ call( "pull", project=project, parse_info={}, assets_dir=assets_dir, parallel_build_count=5, start_time=mocker.ANY, parsed_args=argparse.Namespace( parts=["part1"], destructive_mode=True, use_lxd=False, provider=None, enable_manifest=False, manifest_image_information=None, bind_ssh=False, build_for=None, ), ), ]
def test_lifecycle_legacy_run_provider(cmd, snapcraft_yaml, new_dir, mocker): """Option --provider is supported by legacy.""" snapcraft_yaml(base="core20") run_mock = mocker.patch("snapcraft.parts.PartsLifecycle.run") with pytest.raises(errors.LegacyFallback) as raised: parts_lifecycle.run( cmd, parsed_args=argparse.Namespace( destructive_mode=False, use_lxd=False, provider="some", ), ) assert run_mock.mock_calls == [] assert str(raised.value) == "base is not core22"
def test_lifecycle_run_permission_denied(new_dir): content = textwrap.dedent( """\ name: mytest version: "0.1" base: core22 summary: Permission error test description: Permission error test grade: stable confinement: strict parts: part1: plugin: nil """ ) yaml_path = Path("snapcraft.yaml") yaml_path.write_text(content) Path("prime/meta/").mkdir(parents=True) Path("prime/meta/snap.yaml").touch() Path("prime/meta/snap.yaml").chmod(0o000) with pytest.raises(errors.FilePermissionError) as raised: parts_lifecycle.run( "prime", argparse.Namespace( parts=[], destructive_mode=True, use_lxd=False, provider=None, enable_manifest=False, manifest_image_information=None, bind_ssh=False, build_for=None, debug=False, ), ) error = raised.value assert str(error) == f"Permission denied in file {new_dir!s}/prime/meta/snap.yaml" assert error.resolution == ( "Make sure the file is part of the current project " "and its permissions and ownership are correct." )
def test_lifecycle_run_provider(cmd, snapcraft_yaml, new_dir, mocker): """Option --provider is not supported in core22.""" snapcraft_yaml(base="core22") run_mock = mocker.patch("snapcraft.parts.PartsLifecycle.run") mocker.patch( "snapcraft.providers.Provider.is_base_available", return_value=(True, None) ) with pytest.raises(errors.SnapcraftError) as raised: parts_lifecycle.run( cmd, parsed_args=argparse.Namespace( destructive_mode=False, use_lxd=False, provider="some", build_for=get_host_architecture(), ), ) assert run_mock.mock_calls == [] assert str(raised.value) == "Option --provider is not supported."