def test_cross_compile_with_rust_toolchain_file(self, mock_download): plugin = rust.RustPlugin("test-part", self.options, self.project) os.makedirs(plugin.sourcedir) open(os.path.join(plugin.sourcedir, "rust-toolchain"), "w").close() plugin.pull() self.assertThat(self.run_mock.call_count, Equals(3)) self.run_mock.assert_has_calls([ mock.call( [ os.path.join(plugin._rust_dir, "rustup.sh"), "-y", "--no-modify-path", ], cwd=os.path.join(plugin.partdir, "build"), env=plugin._build_env(), ), mock.call( [plugin._rustup_cmd, "target", "add", self.target], cwd=plugin.builddir, env=plugin._build_env(), ), mock.call( [ plugin._cargo_cmd, "fetch", "--manifest-path", os.path.join(plugin.sourcedir, "Cargo.toml"), ], cwd=plugin.builddir, env=plugin._build_env(), ), ]) self.run_mock.reset_mock() plugin.build() self.assertThat(os.path.join(plugin.builddir, ".cargo", "config"), FileExists()) self.assertThat(self.run_mock.call_count, Equals(1)) self.run_mock.assert_has_calls([ mock.call( [ plugin._cargo_cmd, "install", "--path", plugin.builddir, "--root", plugin.installdir, "--force", "--target", self.target, ], cwd=os.path.join(plugin.partdir, "build"), env=plugin._build_env(), ) ])
def test_build_with_conditional_compilation(self, _, run_mock): plugin = rust.RustPlugin("test-part", self.options, self.project) plugin.options.rust_features = ["conditional-compilation"] os.makedirs(plugin.sourcedir) plugin.build() self.assertThat(run_mock.call_count, Equals(2)) run_mock.assert_has_calls([ mock.call( [ plugin._cargo, "test", "-j{}".format(plugin.project.parallel_build_count), "--features", "conditional-compilation", ], env=plugin._build_env(), ), mock.call( [ plugin._cargo, "install", "-j{}".format(plugin.project.parallel_build_count), "--root", plugin.installdir, "--path", plugin.builddir, "--features", "conditional-compilation", ], env=plugin._build_env(), ), ])
def test_pull_with_rust_toolchain_file(self, script_mock): plugin = rust.RustPlugin("test-part", self.options, self.project) os.makedirs(plugin.sourcedir) open(os.path.join(plugin.sourcedir, "rust-toolchain"), "w").close() plugin.options.rust_revision = [] plugin.options.rust_channel = [] plugin.pull() self.assertThat(self.run_mock.call_count, Equals(2)) self.run_mock.assert_has_calls( [ mock.call( [ os.path.join(plugin._rust_dir, "rustup.sh"), "-y", "--no-modify-path", ], cwd=os.path.join(plugin.partdir, "build"), env=plugin._build_env(), ), mock.call( [ plugin._cargo_cmd, "fetch", "--manifest-path", os.path.join(plugin.sourcedir, "Cargo.toml"), ], cwd=plugin.builddir, env=plugin._build_env(), ), ] )
def test_pull_with_revision(self, run_mock, script_mock): plugin = rust.RustPlugin("test-part", self.options, self.project_options) os.makedirs(plugin.sourcedir) plugin.options.rust_revision = "1.13.0" plugin.options.rust_channel = "" plugin.pull() self.assertThat(run_mock.call_count, Equals(2)) rustdir = os.path.join(plugin.partdir, "rust") run_mock.assert_has_calls([ mock.call([ os.path.join(rustdir, "rustup.sh"), "--prefix={}".format(rustdir), "--disable-sudo", "--save", "--revision=1.13.0", ]), mock.call( [ plugin._cargo, "fetch", "--manifest-path", os.path.join(plugin.sourcedir, "Cargo.toml"), ], env=plugin._build_env(), ), ])
def test_build(self, _, run_mock): plugin = rust.RustPlugin("test-part", self.options, self.project_options) os.makedirs(plugin.sourcedir) plugin.build() self.assertThat(run_mock.call_count, Equals(2)) run_mock.assert_has_calls([ mock.call( [ plugin._cargo, "test", "-j{}".format(plugin.project.parallel_build_count), ], env=plugin._build_env(), ), mock.call( [ plugin._cargo, "install", "-j{}".format(plugin.project.parallel_build_count), "--root", plugin.installdir, "--path", plugin.builddir, ], env=plugin._build_env(), ), ])
def test_get_manifest_with_unexisting_cargo_lock(self): plugin = rust.RustPlugin("test-part", self.options, self.project) os.makedirs(plugin.sourcedir) os.makedirs(plugin.builddir) plugin.build() self.assertThat(plugin.get_manifest(), Not(Contains("cargo-lock-contents")))
def test_get_manifest_with_cargo_lock_dir(self, *_): plugin = rust.RustPlugin("test-part", self.options, self.project) os.makedirs(plugin.sourcedir) os.makedirs(plugin.builddir) os.mkdir(os.path.join(plugin.builddir, "Cargo.lock")) plugin.build() self.assertThat(plugin.get_manifest(), Not(Contains("cargo-lock-contents")))
def test_get_manifest_with_cargo_lock_file(self): plugin = rust.RustPlugin("test-part", self.options, self.project) os.makedirs(plugin.sourcedir) os.makedirs(plugin.builddir) with open(os.path.join(plugin.builddir, "Cargo.lock"), "w") as cargo_lock_file: cargo_lock_file.write("test cargo lock contents") plugin.build() self.assertThat( plugin.get_manifest()["cargo-lock-contents"], Equals("test cargo lock contents"), )
def test_get_manifest_with_cargo_lock_file(self, *_): plugin = rust.RustPlugin('test-part', self.options, self.project_options) os.makedirs(plugin.sourcedir) os.makedirs(plugin.builddir) with open(os.path.join(plugin.builddir, 'Cargo.lock'), 'w') as cargo_lock_file: cargo_lock_file.write('test cargo lock contents') plugin.build() self.assertThat(plugin.get_manifest()['cargo-lock-contents'], Equals('test cargo lock contents'))
def test_pull_with_source_and_source_subdir(self, run_mock, script_mock): plugin = rust.RustPlugin('test-part', self.options, self.project_options) os.makedirs(plugin.sourcedir) plugin.options.source_subdir = 'test-subdir' plugin.pull() run_mock.assert_has_calls([ mock.ANY, mock.call([ plugin._cargo, 'fetch', '--manifest-path', os.path.join(plugin.sourcedir, 'test-subdir', 'Cargo.toml') ]) ])
def test_cross_compile(self, mock_download): plugin = rust.RustPlugin('test-part', self.options, self.project_options) os.makedirs(plugin.sourcedir) plugin.enable_cross_compilation() self.assertThat(plugin._target, Equals(self.target)) plugin.pull() mock_download.assert_called_once_with() self.assertThat(self.run_mock.call_count, Equals(2)) self.run_mock.assert_has_calls([ mock.call( [plugin._rustup, '--prefix={}'.format(os.path.join(plugin._rustpath)), '--disable-sudo', '--save', '--with-target={}'.format(self.target)], cwd=os.path.join(plugin.partdir, 'build')), mock.call( [plugin._cargo, 'fetch', '--manifest-path', os.path.join(plugin.sourcedir, 'Cargo.toml')], cwd=os.path.join(plugin.partdir, 'build')), ]) plugin.build() self.assertThat(os.path.join(plugin._cargo_dir, 'config'), FileExists()) self.assertThat(self.run_mock.call_count, Equals(3)) self.run_mock.assert_has_calls([ mock.call( [plugin._cargo, 'install', '-j{}'.format(plugin.project.parallel_build_count), '--root', plugin.installdir, '--path', plugin.builddir], cwd=os.path.join(plugin.partdir, 'build'), env=plugin._build_env()) ]) plugin.clean_build() self.assertThat(plugin._cargo_dir, Not(DirExists())) plugin.clean_pull() self.assertThat(plugin._rustpath, Not(DirExists())) # Cleaning again shouldn't raise an exception plugin.clean_build() plugin.clean_pull()
def test_pull(self, run_mock): plugin = rust.RustPlugin('test-part', self.options, self.project_options) os.makedirs(plugin.sourcedir) plugin.options.rust_revision = [] plugin.options.rust_channel = [] plugin.pull() self.assertEqual(1, run_mock.call_count) run_mock.assert_has_calls([ mock.call([ "%s" % plugin._rustup, "--prefix=%s" % plugin._rustpath, "--disable-sudo", "--save" ]) ])
def test_build(self, _, run_mock): plugin = rust.RustPlugin('test-part', self.options, self.project_options) os.makedirs(plugin.sourcedir) plugin.build() self.assertThat(run_mock.call_count, Equals(1)) run_mock.assert_has_calls([ mock.call([ plugin._cargo, 'install', '-j{}'.format( plugin.project.parallel_build_count), '--root', plugin.installdir, '--path', plugin.builddir ], env=plugin._build_env()) ])
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.project = snapcraft.project.Project( snapcraft_yaml_file_path=self.make_snapcraft_yaml( textwrap.dedent("""\ name: test-snap base: core16 """))) 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_pull(self, run_mock, script_mock): plugin = rust.RustPlugin('test-part', self.options, self.project_options) os.makedirs(plugin.sourcedir) plugin.options.rust_revision = [] plugin.options.rust_channel = [] plugin.pull() self.assertThat(run_mock.call_count, Equals(2)) rustdir = os.path.join(plugin.partdir, 'rust') run_mock.assert_has_calls([mock.call([ os.path.join(rustdir, 'rustup.sh'), '--prefix={}'.format(rustdir), '--disable-sudo', '--save']), mock.call([plugin._cargo, 'fetch', '--manifest-path', os.path.join(plugin.sourcedir, 'Cargo.toml')])])
def test_build_with_conditional_compilation(self, run_mock): plugin = rust.RustPlugin('test-part', self.options, self.project_options) plugin.options.rust_features = ['conditional-compilation'] os.makedirs(plugin.sourcedir) plugin.build() self.assertThat(run_mock.call_count, Equals(1)) run_mock.assert_has_calls([ mock.call([ plugin._cargo, 'install', '-j{}'.format( plugin.project.parallel_build_count), '--root', plugin.installdir, '--path', plugin.builddir, '--features', 'conditional-compilation' ], env=plugin._build_env()) ])
def test_build(self): plugin = rust.RustPlugin("test-part", self.options, self.project) os.makedirs(plugin.sourcedir) plugin.build() self.run_mock.assert_called_once_with( [ plugin._cargo_cmd, "+stable", "install", "--path", plugin.builddir, "--root", plugin.installdir, ], cwd=os.path.join(plugin.partdir, "build"), env=plugin._build_env(), )
def test_build_with_rust_toolchain_file(self): plugin = rust.RustPlugin("test-part", self.options, self.project) os.makedirs(plugin.sourcedir) open(os.path.join(plugin.sourcedir, "rust-toolchain"), "w").close() plugin.build() self.run_mock.assert_called_once_with( [ plugin._cargo_cmd, "install", "--path", plugin.builddir, "--root", plugin.installdir, ], cwd=os.path.join(plugin.partdir, "build"), env=plugin._build_env(), )
def test_pull_with_source_and_source_subdir(self, run_mock, script_mock): plugin = rust.RustPlugin("test-part", self.options, self.project) os.makedirs(plugin.sourcedir) plugin.options.source_subdir = "test-subdir" plugin.pull() run_mock.assert_has_calls([ mock.ANY, mock.call( [ plugin._cargo, "fetch", "--manifest-path", os.path.join(plugin.sourcedir, "test-subdir", "Cargo.toml"), ], env=plugin._build_env(), ), ])
def test_pull_with_revision(self, script_mock): plugin = rust.RustPlugin("test-part", self.options, self.project) os.makedirs(plugin.sourcedir) plugin.options.rust_revision = "1.13.0" plugin.options.rust_channel = "" plugin.pull() self.assertThat(self.run_mock.call_count, Equals(3)) self.run_mock.assert_has_calls( [ mock.call( [ os.path.join(plugin._rust_dir, "rustup.sh"), "-y", "--no-modify-path", "--default-toolchain", "none", ], cwd=os.path.join(plugin.partdir, "build"), env=plugin._build_env(), ), mock.call( [plugin._rustup_cmd, "install", "1.13.0"], cwd=plugin.builddir, env=plugin._build_env(), ), mock.call( [ plugin._cargo_cmd, "+1.13.0", "fetch", "--manifest-path", os.path.join(plugin.sourcedir, "Cargo.toml"), ], cwd=plugin.builddir, env=plugin._build_env(), ), ] )
def test_write_cargo_config(self): plugin = rust.RustPlugin("test-part", self.options, self.project) config_toml_path = "config.toml" plugin._write_cargo_config(cargo_config_path=config_toml_path) self.assertThat(config_toml_path, FileExists()) config = toml.load(config_toml_path) self.assertThat( config, Equals( dict( rustdoc_cmd=plugin._rustdoc_cmd, rustc_cmd=plugin._rustc_cmd, arch_triplet=plugin.project.arch_triplet, jobs=plugin.parallel_build_count, target={plugin._get_target(): dict(linker=plugin._get_linker())}, ) ), )
def test_get_manifest_with_versions(self): plugin = rust.RustPlugin("test-part", self.options, self.project) os.makedirs(plugin.sourcedir) original_check_output = subprocess.check_output def side_effect(cmd, *args, **kwargs): if cmd[-1] == "--version": binary = os.path.basename(cmd[0]) return "test {} version".format(binary) return original_check_output(cmd, *args, **kwargs) with mock.patch.object(rust.RustPlugin, "run_output") as run_output_mock: run_output_mock.side_effect = side_effect plugin.build() expected_manifest = collections.OrderedDict() expected_manifest["rustup-version"] = "test rustup version" expected_manifest["rustc-version"] = "test rustc version" expected_manifest["cargo-version"] = "test cargo version" self.assertThat(plugin.get_manifest(), Equals(expected_manifest))
def test_build_with_conditional_compilation(self): plugin = rust.RustPlugin("test-part", self.options, self.project) plugin.options.rust_features = ["conditional-compilation"] os.makedirs(plugin.sourcedir) plugin.build() self.run_mock.assert_called_once_with( [ plugin._cargo_cmd, "+stable", "install", "--path", plugin.builddir, "--root", plugin.installdir, "--features", "conditional-compilation", ], cwd=os.path.join(plugin.partdir, "build"), env=plugin._build_env(), )
def test_pull(self, run_mock, script_mock): plugin = rust.RustPlugin('test-part', self.options, self.project_options) os.makedirs(plugin.sourcedir) plugin.options.rust_revision = [] plugin.options.rust_channel = [] plugin.pull() self.assertEqual(1, run_mock.call_count) run_mock.assert_has_calls([ mock.call([ "%s" % plugin._rustup, "--prefix=%s" % plugin._rustpath, "--disable-sudo", "--save" ]) ]) rustdir = os.path.join(plugin.partdir, 'rust') run_mock.assert_called_once_with([ os.path.join(rustdir, 'rustup.sh'), '--prefix={}'.format(rustdir), '--disable-sudo', '--save' ])
def test_pull_with_source_and_source_subdir(self, script_mock): plugin = rust.RustPlugin("test-part", self.options, self.project) os.makedirs(plugin.sourcedir) plugin.options.source_subdir = "test-subdir" plugin.pull() self.assertThat(self.run_mock.call_count, Equals(3)) self.run_mock.assert_has_calls([ mock.call( [ os.path.join(plugin._rust_dir, "rustup.sh"), "-y", "--no-modify-path", ], cwd=os.path.join(plugin.partdir, "build"), env=plugin._build_env(), ), mock.call( [plugin._rustup_cmd, "install", "stable"], cwd=plugin.builddir, env=plugin._build_env(), ), mock.call( [ plugin._cargo_cmd, "+stable", "fetch", "--manifest-path", os.path.join(plugin.sourcedir, "test-subdir", "Cargo.toml"), ], cwd=plugin.builddir, env=plugin._build_env(), ), ])
def test_get_manifest_with_versions(self, _): plugin = rust.RustPlugin('test-part', self.options, self.project_options) os.makedirs(plugin.sourcedir) original_check_output = subprocess.check_output def side_effect(cmd, *args, **kwargs): if cmd[-1] == '--version': binary = os.path.basename(cmd[-2]) return 'test {} version'.format(binary) return original_check_output(cmd, *args, **kwargs) with mock.patch.object(rust.RustPlugin, 'run_output') as run_output_mock: run_output_mock.side_effect = side_effect plugin.build() expected_manifest = collections.OrderedDict() expected_manifest['rustup-version'] = 'test rustup.sh version' expected_manifest['rustc-version'] = 'test rustc version' expected_manifest['cargo-version'] = 'test cargo version' self.assertThat(plugin.get_manifest(), Equals(expected_manifest))
def test_unsupported_target_arch_raises_exception(self): plugin = rust.RustPlugin("test-part", self.options, self.project) os.makedirs(plugin.sourcedir) self.assertRaises(errors.SnapcraftEnvironmentError, plugin._get_target)
def test_cross_compiling_unsupported_arch_raises_exception(self): plugin = rust.RustPlugin("test-part", self.options, self.project_options) os.makedirs(plugin.sourcedir) self.assertRaises(NotImplementedError, plugin.enable_cross_compilation)
def test_cross_compile(self, mock_download): plugin = rust.RustPlugin("test-part", self.options, self.project) os.makedirs(plugin.sourcedir) plugin.enable_cross_compilation() self.assertThat(plugin._target, Equals(self.target)) plugin.pull() mock_download.assert_called_once_with() self.assertThat(self.run_mock.call_count, Equals(2)) self.run_mock.assert_has_calls([ mock.call( [ plugin._rustup, "--prefix={}".format(os.path.join(plugin._rustpath)), "--disable-sudo", "--save", "--with-target={}".format(self.target), ], cwd=os.path.join(plugin.partdir, "build"), ), mock.call( [ plugin._cargo, "fetch", "--manifest-path", os.path.join(plugin.sourcedir, "Cargo.toml"), ], cwd=os.path.join(plugin.partdir, "build"), env=plugin._build_env(), ), ]) plugin.build() self.assertThat(os.path.join(plugin._cargo_dir, "config"), FileExists()) self.assertThat(self.run_mock.call_count, Equals(3)) self.run_mock.assert_has_calls([ mock.call( [ plugin._cargo, "install", "-j{}".format(plugin.project.parallel_build_count), "--root", plugin.installdir, "--path", plugin.builddir, "--target", plugin._target, ], cwd=os.path.join(plugin.partdir, "build"), env=plugin._build_env(), ) ]) plugin.clean_build() self.assertThat(plugin._cargo_dir, Not(DirExists())) plugin.clean_pull() self.assertThat(plugin._rustpath, Not(DirExists())) # Cleaning again shouldn't raise an exception plugin.clean_build() plugin.clean_pull()
def test_cross_compile_with_rust_toolchain_file(self, mock_download): self.project = snapcraft.project.Project( target_deb_arch=self.deb_arch, snapcraft_yaml_file_path=self.make_snapcraft_yaml( textwrap.dedent("""\ name: test-snap base: core16 """)), ) 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(), ) ])