def setUp(self): super().setUp() self.useFixture(fixture_setup.CleanEnvironment()) class Options: makefile = None make_parameters = [] rust_features = [] rust_revision = "" rust_channel = "" source_subdir = "" self.options = Options() patcher = mock.patch("snapcraft.internal.common.run") self.run_mock = patcher.start() self.addCleanup(patcher.stop) patcher = mock.patch("snapcraft.internal.common.run_output") patcher.start() self.addCleanup(patcher.stop) original_exists = os.path.exists def exists_mock(*args, **kwargs): if args[0].endswith("rustup"): return False else: return original_exists(args[0]) patcher = mock.patch("os.path.exists", side_effect=exists_mock) patcher.start() self.addCleanup(patcher.stop) self.plugin = rust.RustPlugin("test-part", self.options, self.project) os.makedirs(self.plugin.sourcedir) os.makedirs(self.plugin.builddir) open(Path(self.plugin.builddir, "Cargo.toml"), "w").write("")
def test_cross_compile( self, mock_download, monkeypatch, tmp_work_path, mock_run, mock_run_output, options, deb_arch, target, ): monkeypatch.setattr(snapcraft.project.Project, "is_cross_compiling", True) project = snapcraft.project.Project(target_deb_arch=deb_arch) project._snap_meta = meta.snap.Snap(name="test-snap", base="core18") plugin = rust.RustPlugin("test-part", options, project) plugin.pull() assert mock_run.call_count == 4 mock_run.assert_has_calls([ mock.call( [ os.path.join(plugin._rustup_dir, "rustup.sh"), "-y", "--no-modify-path", "--profile=minimal", "--default-toolchain", "none", ], env=mock.ANY, ), mock.call([plugin._rustup_cmd, "install", "stable"], env=mock.ANY), mock.call( [ plugin._rustup_cmd, "target", "add", "--toolchain", "stable", target, ], env=mock.ANY, ), mock.call( [ plugin._cargo_cmd, "+stable", "fetch", "--manifest-path", os.path.join(plugin.sourcedir, "Cargo.toml"), ], env=mock.ANY, ), ]) mock_run.reset_mock() cargo_path = Path(plugin.builddir) / "Cargo.toml" cargo_path.parent.mkdir(parents=True) cargo_path.touch() plugin.build() assert os.path.exists(os.path.join(plugin.builddir, ".cargo", "config")) mock_run.assert_called_once_with( [ plugin._cargo_cmd, "+stable", "install", "--path", plugin.builddir, "--root", plugin.installdir, "--force", "--target", target, ], cwd=plugin.builddir, env=plugin._build_env(), )
def test_cross_compile_with_rust_toolchain_file(self, mock_download): self.plugin = rust.RustPlugin("test-part", self.options, self.project) open(os.path.join(self.plugin.sourcedir, "rust-toolchain"), "w").close() self.plugin.pull() self.assertThat(self.run_mock.call_count, Equals(3)) self.run_mock.assert_has_calls([ mock.call( [ os.path.join(self.plugin._rustup_dir, "rustup.sh"), "-y", "--no-modify-path", "--profile=minimal", ], cwd=os.path.join(self.plugin.partdir, "build"), env=self.plugin._build_env(), ), mock.call( [self.plugin._rustup_cmd, "target", "add", self.target], cwd=self.plugin.builddir, env=self.plugin._build_env(), ), mock.call( [ self.plugin._cargo_cmd, "fetch", "--manifest-path", os.path.join(self.plugin.sourcedir, "Cargo.toml"), ], cwd=self.plugin.builddir, env=self.plugin._build_env(), ), ]) self.run_mock.reset_mock() self.plugin.build() self.assertThat(os.path.join(self.plugin.builddir, ".cargo", "config"), FileExists()) self.assertThat(self.run_mock.call_count, Equals(1)) self.run_mock.assert_has_calls([ mock.call( [ self.plugin._cargo_cmd, "install", "--path", self.plugin.builddir, "--root", self.plugin.installdir, "--force", "--target", self.target, ], cwd=os.path.join(self.plugin.partdir, "build"), env=self.plugin._build_env(), ) ])