Skip to content

Commit

Permalink
tests/test_rust_plugin.py: add two more tests to test ...
Browse files Browse the repository at this point in the history
... dependency validation logic
  • Loading branch information
liushuyu committed Nov 19, 2024
1 parent 7fab1f4 commit ac4bdfe
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions tests/unit/plugins/test_rust_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from craft_parts.infos import PartInfo, ProjectInfo
from craft_parts.parts import Part
from craft_parts.plugins.rust_plugin import RustPlugin, RustPluginProperties
from craft_parts.plugins.validator import PluginEnvironmentValidator
from pydantic import ValidationError


Expand All @@ -32,6 +33,17 @@ def part_info(new_dir):
)


@pytest.fixture
def mock_validator(monkeypatch):
def fake_execute(self, cmd: str):
return subprocess.check_output( # noqa: S602
cmd,
shell=True,
)

monkeypatch.setattr(PluginEnvironmentValidator, "_execute", fake_execute)


@pytest.mark.parametrize(
"rust_channel",
[
Expand Down Expand Up @@ -262,6 +274,56 @@ def callback_fail():
]


@pytest.mark.parametrize("after", [["something-else"], []])
def test_validate_environment_should_have_rustup(
after, fake_process: pytest_subprocess.FakeProcess, mock_validator
):
def exec_fail(x):
raise subprocess.CalledProcessError(127, x)

fake_process.register(
["cargo", "--version"], stdout="cargo 1.84.0-nightly (69e595908 2024-11-16)"
)
fake_process.register(
["rustc", "--version"], stdout="rustc 1.84.0-nightly (5ec7d6eee 2024-11-17)"
)
fake_process.register(["rustup", "dump-testament"], callback=exec_fail)

properties = RustPlugin.properties_class.unmarshal({"source": ".", "after": after})
validator = RustPlugin.validator_class(
part_name="my-part", properties=properties, env=""
)
# positive test
validator.validate_environment(part_dependencies=["rust-deps"])
# negative test
with pytest.raises(PluginEnvironmentValidationError):
validator.validate_environment(part_dependencies=after)


def test_validate_environment_should_not_have_rustup(
fake_process: pytest_subprocess.FakeProcess, mock_validator
):
def exec_fail(x):
raise subprocess.CalledProcessError(127, x)

fake_process.register(["cargo", "--version"], callback=exec_fail)
fake_process.register(["rustc", "--version"], callback=exec_fail)
fake_process.register(
["rustup", "dump-testament"],
stdout="Rustup version renders as: 1.27.1 (2024-05-07)",
)

properties = RustPlugin.properties_class.unmarshal(
{"source": ".", "after": ["rust-deps"]}
)
validator = RustPlugin.validator_class(
part_name="my-part", properties=properties, env=""
)
# positive test
validator.validate_environment(part_dependencies=[])
validator.validate_environment(part_dependencies=["rust-deps"])


def test_get_out_of_source_build(part_info):
properties = RustPlugin.properties_class.unmarshal({"source": "."})
plugin = RustPlugin(properties=properties, part_info=part_info)
Expand Down

0 comments on commit ac4bdfe

Please sign in to comment.