def test_pull_is_dirty_if_target_arch_changes( self, mock_install_build_snaps, mock_install_build_packages, mock_enable_cross_compilation, ): # Set the option to error on dirty/outdated steps with snapcraft.config.CLIConfig() as cli_config: cli_config.set_outdated_step_action( snapcraft.config.OutdatedStepAction.ERROR ) mock_install_build_packages.return_value = [] project_config = self.make_snapcraft_project( textwrap.dedent( """\ parts: part1: plugin: nil """ ) ) project = Project( snapcraft_yaml_file_path=self.snapcraft_yaml_file_path, target_deb_arch="amd64", ) project_config = project_loader.load_config(project) # Pull it with amd64 lifecycle.execute(steps.PULL, project_config) # Reset logging since we only care about the following self.fake_logger = fixtures.FakeLogger(level=logging.INFO) self.useFixture(self.fake_logger) project = Project( snapcraft_yaml_file_path=self.snapcraft_yaml_file_path, target_deb_arch="armhf", ) project_config = project_loader.load_config(project) # Pull it again with armhf. Should catch that the part needs to be # re-pulled due to the change in target architecture and raise an # error. raised = self.assertRaises( errors.StepOutdatedError, lifecycle.execute, steps.PULL, project_config ) self.assertThat( self.fake_logger.output, Contains("Setting target machine to 'armhf'") ) self.assertThat(raised.step, Equals(steps.PULL)) self.assertThat(raised.part, Equals("part1")) self.assertThat( raised.report, Equals("The 'deb_arch' project option appears to have changed.\n"), )
def test_project_with_arguments(self): project = Project(target_deb_arch="armhf", debug=True) self.assertThat(project.deb_arch, Equals("armhf")) self.assertThat(project.debug, Equals(True)) # This is a backwards compatibility check self.assertThat(project.info, Is(None))
def setUp(self): super().setUp() snapcraft_yaml_path = self.make_snapcraft_yaml( dedent("""\ name: go-snap base: core18 """)) self.project = Project(target_deb_arch=self.deb_arch, snapcraft_yaml_file_path=snapcraft_yaml_path) def fake_go_build(command, cwd, *args, **kwargs): if command[0] == "go" and command[1] == "build" and "-o" in command: open(os.path.join(command[command.index("-o") + 1], "binary"), "w").close() elif command[0] == "go" and command[ 1] == "build" and "-o" not in command: # the package is -1 open(os.path.join(cwd, os.path.basename(command[-1])), "w").close() fake_run = self.useFixture( fixtures.MockPatch("snapcraft.internal.common.run", side_effect=fake_go_build)) self.run_mock = fake_run.mock fake_run_output = self.useFixture( fixtures.MockPatch("snapcraft.internal.common.run_output")) self.run_output_mock = fake_run_output.mock
def setUp(self): super().setUp() snapcraft_yaml_path = self.make_snapcraft_yaml( dedent( """\ name: gradle-snap base: core18 """ ) ) self.project = Project(snapcraft_yaml_file_path=snapcraft_yaml_path) class Options: gradle_options = [] gradle_output_dir = "build/libs" gradle_version = gradle._DEFAULT_GRADLE_VERSION gradle_version_checksum = gradle._DEFAULT_GRADLE_CHECKSUM gradle_openjdk_version = "11" self.options = Options() # unset http and https proxies. self.useFixture(fixtures.EnvironmentVariable("http_proxy", None)) self.useFixture(fixtures.EnvironmentVariable("https_proxy", None)) patcher = mock.patch("snapcraft.internal.common.run") self.run_mock = patcher.start() self.addCleanup(patcher.stop) patcher = mock.patch("snapcraft.internal.sources.Zip") self.zip_mock = patcher.start() self.addCleanup(patcher.stop)
def get_project(base: str = "") -> Project: with open("snapcraft.yaml", "w") as snapcraft_file: print("name: project-name", file=snapcraft_file) if base: print("base: {}".format(base), file=snapcraft_file) return Project(snapcraft_yaml_file_path="snapcraft.yaml")
def setUp(self): super().setUp() snapcraft_yaml_path = self.make_snapcraft_yaml( dedent("""\ name: test-snap base: {base} """).format(base=self.base)) self.project = Project(snapcraft_yaml_file_path=snapcraft_yaml_path) class Options: ant_properties = {} ant_build_targets = None ant_version = ant._DEFAULT_ANT_VERSION ant_version_checksum = ant._DEFAULT_ANT_CHECKSUM ant_openjdk_version = self.java_version self.options = Options() patcher = mock.patch("snapcraft.internal.common.run") self.run_mock = patcher.start() self.addCleanup(patcher.stop) patcher = mock.patch("snapcraft.internal.sources.Tar") self.tar_mock = patcher.start() self.addCleanup(patcher.stop)
def get_project(*, is_managed_host: bool = False, skip_snapcraft_yaml: bool = False, **kwargs): # We need to do this here until we can get_snapcraft_yaml as part of Project. if is_managed_host: os.chdir(os.path.expanduser(os.path.join("~", "project"))) if skip_snapcraft_yaml: snapcraft_yaml_file_path = None else: snapcraft_yaml_file_path = get_snapcraft_yaml() ctx = click.get_current_context() for key, value in ctx.parent.params.items(): if not kwargs.get(key): kwargs[key] = value project = Project( debug=kwargs.pop("debug"), use_geoip=kwargs.pop("enable_geoip"), parallel_builds=not kwargs.pop("no_parallel_builds"), target_deb_arch=kwargs.pop("target_arch"), snapcraft_yaml_file_path=snapcraft_yaml_file_path, is_managed_host=is_managed_host, ) return project
def project(snapcraft_yaml_path, request): """Return a project in host and managed-host modes.""" snapcraft_project = Project( is_managed_host=request.param, snapcraft_yaml_file_path=snapcraft_yaml_path.as_posix(), ) return snapcraft_project
def setUp(self): super().setUp() snapcraft_yaml_path = self.make_snapcraft_yaml( dedent("""\ name: python-snap base: core18 """)) self.project = Project(snapcraft_yaml_file_path=snapcraft_yaml_path) class Options: source = "." source_subdir = "" requirements = [] constraints = "" python_version = "python3" python_packages = [] process_dependency_links = False self.options = Options() patcher = mock.patch("snapcraft.plugins.v1._python.Pip") self.mock_pip = patcher.start() self.addCleanup(patcher.stop) patcher = mock.patch.object(python.PythonPlugin, "_setup_tools_install") self.mock_setup_tools = patcher.start() self.addCleanup(patcher.stop)
def get_project(base: str = "core20") -> Project: project = Project() project._snap_meta = Snap(name="project-name", base=base, version="1.0", confinement="strict") return project
def get_project_config(snapcraft_yaml_content): snapcraft_yaml_path = pathlib.Path("Snapcraft.yaml") with snapcraft_yaml_path.open("w") as snapcraft_yaml_file: print(snapcraft_yaml_content, file=snapcraft_yaml_file) project = Project(snapcraft_yaml_file_path=snapcraft_yaml_path.as_posix()) return project_loader.load_config(project)
def __init__(self): self.project = Project() self.project._snap_meta = Snap(name="project-name", base="core20", version="1.0", confinement="strict") self.parts = Parts()
def test_static_base_with_libc6_stage_packaged(self): # The "bare" base is a static base. snapcraft_yaml = fixture_setup.SnapcraftYaml( self.path, base="bare", confinement=self.confinement) snapcraft_yaml.update_part("part1", dict(plugin="nil")) self.useFixture(snapcraft_yaml) project = Project( snapcraft_yaml_file_path=snapcraft_yaml.snapcraft_yaml_file_path) patcher = PartPatcher( elf_files=frozenset(["foo"]), project=project, confinement="strict", core_base="bare", snap_base_path="/snap/test-snap/current", stage_packages=["libc6"], stagedir="stage", primedir="prime", ) patcher.patch() self.fake_patchelf.mock.assert_called_once_with( dynamic_linker= "/snap/test-snap/current/lib/x86_64-linux-gnu/ld-2.27.so", preferred_patchelf_path=None, root_path="prime", )
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, ) # 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 setUp(self): super().setUp() snapcraft_yaml_path = self.make_snapcraft_yaml( dedent("""\ name: test-snap base: {base} """).format(base=self.base)) self.project = Project(snapcraft_yaml_file_path=snapcraft_yaml_path) class Options: ant_properties = {} ant_build_targets = None ant_channel = None ant_version = "1.10.5" ant_version_checksum = "sha512/a7f1e0cec9d5ed1b3ab6cddbb9364f127305a997bbc88ecd734f9ef142ec0332375e01ace3592759bb5c3307cd9c1ac0a78a30053f304c7030ea459498e4ce4e" ant_openjdk_version = self.java_version ant_buildfile = None self.options = Options() patcher = mock.patch("snapcraft.internal.common.run") self.run_mock = patcher.start() self.addCleanup(patcher.stop) patcher = mock.patch("snapcraft.internal.sources.Tar") self.tar_mock = patcher.start() self.addCleanup(patcher.stop)
def setUp(self): super().setUp() snapcraft_yaml_path = self.make_snapcraft_yaml( dedent("""\ name: test-snap base: core18 """)) self.project = Project(snapcraft_yaml_file_path=snapcraft_yaml_path) class Options: ant_properties = {} ant_build_targets = None ant_channel = None ant_version = None ant_version_checksum = None ant_openjdk_version = None ant_buildfile = None self.options = Options() self.run_mock = self.useFixture( fixtures.MockPatch("snapcraft.internal.common.run")).mock self.tar_mock = self.useFixture( fixtures.MockPatch("snapcraft.internal.sources.Tar")).mock
def setUp(self): super().setUp() snapcraft_yaml_path = self.make_snapcraft_yaml( dedent( """\ name: maven-snap base: core18 """ ) ) self.project = Project(snapcraft_yaml_file_path=snapcraft_yaml_path) class Options: maven_options = [] maven_targets = [""] maven_version = maven._DEFAULT_MAVEN_VERSION maven_version_checksum = maven._DEFAULT_MAVEN_CHECKSUM maven_openjdk_version = "11" self.options = Options() patcher = mock.patch("snapcraft.internal.common.run") self.run_mock = patcher.start() self.addCleanup(patcher.stop) patcher = mock.patch("snapcraft.sources.Tar") self.tar_mock = patcher.start() self.addCleanup(patcher.stop)
def generate_meta_yaml( self, *, build=False, actual_prime_dir=None, snapcraft_yaml_file_path=None ): if snapcraft_yaml_file_path is None: snapcraft_yaml_file_path = self.snapcraft_yaml_file_path os.makedirs("snap", exist_ok=True) with open(snapcraft_yaml_file_path, "w") as f: f.write(yaml_utils.dump(self.config_data)) self.project = Project(snapcraft_yaml_file_path=snapcraft_yaml_file_path) if actual_prime_dir is not None: self.project._prime_dir = actual_prime_dir self.meta_dir = os.path.join(self.project.prime_dir, "meta") self.hooks_dir = os.path.join(self.meta_dir, "hooks") self.snap_yaml = os.path.join(self.meta_dir, "snap.yaml") self.config = project_loader.load_config(project=self.project) if build: for part in self.config.parts.all_parts: part.pull() part.build() part.stage() part.prime() _snap_packaging.create_snap_packaging(self.config) self.assertTrue(os.path.exists(self.snap_yaml), "snap.yaml was not created") with open(self.snap_yaml) as f: return yaml_utils.load(f)
def make_snapcraft_project(self, common_id): yaml = textwrap.dedent( """\ name: test base: core18 version: "1.0" summary: test description: test confinement: strict grade: stable adopt-info: part apps: test-app: command: echo common-id: {common_id} parts: part: plugin: nil parse-info: ["1.metainfo.xml", "2.metainfo.xml"] """ ) yaml_path = self.make_snapcraft_yaml(yaml.format(common_id=common_id)) project = Project(snapcraft_yaml_file_path=yaml_path) return project_loader.load_config(project)
def test_setup_environment_content_x86(tmp_work_path, monkeypatch, machine_platform, distro): snapcraft_project = Project() snapcraft_project._snap_meta = Snap(name="test-snap", base=distro[0]) monkeypatch.setattr(platform, "machine", lambda: machine_platform[0]) recorded_files = dict() @contextlib.contextmanager def fake_namedtempfile(*, suffix: str, **kwargs): # Usage hides the file basename in the suffix. tmp_path = os.path.join("tmpfile") with open(tmp_path, "wb") as f_write: yield f_write with open(tmp_path, "r") as f_read: recorded_files[suffix] = f_read.read() monkeypatch.setattr(tempfile, "NamedTemporaryFile", fake_namedtempfile) provider = ProviderImpl(project=snapcraft_project, echoer=Mock()) provider._setup_environment() assert recorded_files == { ".bashrc": '#!/bin/bash\nexport PS1="\\h \\$(/bin/_snapcraft_prompt)# "\n', "00-snapcraft": 'Apt::Install-Recommends "false";\n', "_snapcraft_prompt": dedent("""\ #!/bin/bash if [[ "$PWD" =~ ^$HOME.* ]]; then path="${PWD/#$HOME/\\ ..}" if [[ "$path" == " .." ]]; then ps1="" else ps1="$path" fi else ps1="$PWD" fi echo -n $ps1 """), "default.sources": dedent(f"""\ Types: deb URIs: {machine_platform[1]["main"]} Suites: {distro[1]} {distro[1]}-updates Components: main multiverse restricted universe """), "default-security.sources": dedent(f"""\ Types: deb URIs: {machine_platform[1]["security"]} Suites: {distro[1]}-security Components: main multiverse restricted universe """), "sources.list": "", }
def test_project_with_arguments(): project = Project(target_deb_arch="armhf", debug=True) assert project.deb_arch == "armhf" assert project.debug is True # This is a backwards compatibility check assert project.info is None
def test_unsupported_base_raises(flutter_options): project = Project() project._snap_meta = Snap(name="test-snap", base="bad-base", confinement="strict") with pytest.raises(errors.PluginBaseError): flutter.FlutterPlugin("test-part", flutter_options, project)
def project(monkeypatch, tmp_work_path, request): """Return project variants for core and core18""" monkeypatch.setattr(Project, "parallel_build_count", 2) snapcraft_project = Project() snapcraft_project._snap_meta = Snap( name="test-snap", base=request.param, confinement="strict" ) return snapcraft_project
def project_core18(monkeypatch, tmp_work_path): """Return project for core18""" monkeypatch.setattr(Project, "parallel_build_count", 2) snapcraft_project = Project() snapcraft_project._snap_meta = Snap( name="test-snap", base="core18", confinement="strict" ) return snapcraft_project
def make_snapcraft_project(self, *, confinement: str): snapcraft_yaml = fixture_setup.SnapcraftYaml(self.path) snapcraft_yaml.data["confinement"] = confinement snapcraft_yaml.update_part("test-part", dict(plugin="nil")) self.useFixture(snapcraft_yaml) project = Project( snapcraft_yaml_file_path=snapcraft_yaml.snapcraft_yaml_file_path) return project_loader.load_config(project)
def setUp(self): super().setUp() snapcraft_yaml_path = self.make_snapcraft_yaml( dedent("""\ name: python-snap base: {base} """).format(base=self.base)) self.project = Project(snapcraft_yaml_file_path=snapcraft_yaml_path)
def setUp(self): super().setUp() snapcraft_yaml_path = self.make_snapcraft_yaml( dedent("""\ name: python-snap base: core16 """)) self.project = Project(snapcraft_yaml_file_path=snapcraft_yaml_path)
def make_snapcraft_project(self, parts): snapcraft_yaml = fixture_setup.SnapcraftYaml(self.path) snapcraft_yaml.update_part("part1", dict(plugin="nil")) for part_name, part in parts: snapcraft_yaml.update_part(part_name, part) self.useFixture(snapcraft_yaml) project = Project( snapcraft_yaml_file_path=snapcraft_yaml.snapcraft_yaml_file_path) return project_loader.load_config(project)
def setUp(self): super().setUp() self.project = Project(target_deb_arch=self.deb_arch) self.project._snap_meta = meta.snap.Snap(name="test-snap", base="core18", confinement="strict") patcher = mock.patch("snapcraft.ProjectOptions.is_cross_compiling") patcher.start() self.addCleanup(patcher.stop)
def get_project_options(**kwargs): ctx = click.get_current_context() for key, value in ctx.parent.params.items(): if not kwargs.get(key): kwargs[key] = value project = Project(debug=kwargs.pop('debug'), use_geoip=kwargs.pop('enable_geoip'), parallel_builds=not kwargs.pop('no_parallel_builds'), target_deb_arch=kwargs.pop('target_arch')) return project