Skip to content

Commit

Permalink
fix(plugins/rust): fix snap dependency handling
Browse files Browse the repository at this point in the history
This changes the following:

- If 'rust-channel' is explicitly set to the string 'none', don't
  install rustup.
- If the rust-using part has a 'rust-deps' plugin, don't install rustup.
  • Loading branch information
lengau committed Oct 31, 2024
1 parent 394ede9 commit aa3f1b1
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 19 deletions.
11 changes: 9 additions & 2 deletions craft_parts/plugins/rust_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,15 @@ class RustPlugin(Plugin):
def get_build_snaps(self) -> set[str]:
"""Return a set of required snaps to install in the build environment."""
options = cast(RustPluginProperties, self._options)
if not options.rust_channel and self._check_system_rust():
logger.info("Rust is installed on the system, skipping rustup")
if options.rust_channel == "none":
logger.info(
"Not installing rustup because rust-channel 'none' was specified."
)
return set()
if options.after and "rust-deps" in options.after:
logger.info(
"Not installing rustup because a 'rust-deps' dependent part is specified."
)
return set()
return {"rustup"}

Expand Down
56 changes: 39 additions & 17 deletions tests/unit/plugins/test_rust_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,17 @@
from craft_parts.plugins.rust_plugin import RustPlugin, RustPluginProperties
from pydantic import ValidationError

SAMPLE_RUST_CHANNELS = [
"stable",
None, # synonym for "stable"
"beta",
"nightly",
"stable-x86_64-unknown-linux-gnu",
"1.0.0",
"1.68.2-x86_64-unknown-linux-gnu",
]
NO_INSTALL_RUST_CHANNEL = "none"


@pytest.fixture
def part_info(new_dir):
Expand All @@ -33,29 +44,40 @@ def part_info(new_dir):


@pytest.mark.parametrize(
"rust_channel",
"rust_channel", [*SAMPLE_RUST_CHANNELS, NO_INSTALL_RUST_CHANNEL]
)
def test_validate_rust_channel(rust_channel: str):
RustPluginProperties.validate_rust_channel(rust_channel)


@pytest.mark.parametrize(
("rust_channel", "expected"),
[
"stable",
"beta",
"nightly",
"stable-x86_64-unknown-linux-gnu",
"1.0.0",
"1.68.2-x86_64-unknown-linux-gnu",
*((channel, {"rustup"}) for channel in SAMPLE_RUST_CHANNELS),
(NO_INSTALL_RUST_CHANNEL, set()),
],
)
def test_validate_rust_channel(rust_channel):
RustPluginProperties.validate_rust_channel(
rust_channel
) # pyright: ignore[reportCallIssue]

def test_get_build_snaps_by_channel(part_info, rust_channel, expected):
properties = RustPlugin.properties_class.unmarshal(
{"source": ".", "rust-channel": rust_channel}
)
plugin = RustPlugin(properties=properties, part_info=part_info)
assert plugin.get_build_snaps() == expected

def test_get_build_snaps(fake_process: pytest_subprocess.FakeProcess, part_info):
fake_process.register(["rustc", "--version"], stdout="Not installed")
fake_process.register(["cargo", "--version"], stdout="Not installed")

properties = RustPlugin.properties_class.unmarshal({"source": "."})
@pytest.mark.parametrize(
("after", "expected"),
[
(None, {"rustup"}),
([], {"rustup"}),
(["not-rust-deps", "also-not-rust-deps"], {"rustup"}),
(["rust-deps"], set()),
],
)
def test_get_build_snaps_with_rust_deps(part_info, after, expected):
properties = RustPlugin.properties_class.unmarshal({"source": ".", "after": after})
plugin = RustPlugin(properties=properties, part_info=part_info)
assert plugin.get_build_snaps() == {"rustup"}
assert plugin.get_build_snaps() == expected


def test_get_build_packages(part_info):
Expand Down

0 comments on commit aa3f1b1

Please sign in to comment.