|
| 1 | +from dataclasses import dataclass |
| 2 | + |
| 3 | +import pytest |
| 4 | +from rattler import Version |
| 5 | + |
| 6 | +from minimum_versions import environments |
| 7 | +from minimum_versions.environments.spec import Spec |
| 8 | + |
| 9 | + |
| 10 | +@dataclass |
| 11 | +class FakeRecord: |
| 12 | + version: Version | None |
| 13 | + |
| 14 | + |
| 15 | +@pytest.mark.parametrize("manifest_path", ("a/pixi.toml", "b/pyproject.toml", None)) |
| 16 | +@pytest.mark.parametrize( |
| 17 | + ["specifier", "key"], (("conda:ci/environment.yml", "conda"), ("pixi:env", "pixi")) |
| 18 | +) |
| 19 | +def test_parse_environment(specifier, manifest_path, key, monkeypatch): |
| 20 | + results = {"conda": object(), "pixi": object()} |
| 21 | + kinds = { |
| 22 | + "conda": lambda s, m: results["conda"], |
| 23 | + "pixi": lambda s, m: results["pixi"], |
| 24 | + } |
| 25 | + monkeypatch.setattr(environments, "kinds", kinds) |
| 26 | + |
| 27 | + actual = environments.parse_environment(specifier, manifest_path) |
| 28 | + expected = results[key] |
| 29 | + |
| 30 | + assert actual is expected |
| 31 | + |
| 32 | + |
| 33 | +@pytest.mark.parametrize( |
| 34 | + ["envs", "ignored_violations", "expected"], |
| 35 | + ( |
| 36 | + pytest.param( |
| 37 | + { |
| 38 | + "env1": [ |
| 39 | + Spec("a", Version("1.2")), |
| 40 | + Spec("c", Version("2024.8")), |
| 41 | + Spec("d", Version("0.6")), |
| 42 | + ] |
| 43 | + }, |
| 44 | + ["d"], |
| 45 | + {"env1": False}, |
| 46 | + id="single-violation-ignored", |
| 47 | + ), |
| 48 | + pytest.param( |
| 49 | + { |
| 50 | + "env1": [Spec("b", Version("3.2")), Spec("c", Version("2025.2"))], |
| 51 | + "env2": [Spec("b", Version("3.1"))], |
| 52 | + }, |
| 53 | + [], |
| 54 | + {"env1": True, "env2": False}, |
| 55 | + id="multiple-split-not ignored", |
| 56 | + ), |
| 57 | + pytest.param( |
| 58 | + {"env1": [Spec("d", None)]}, |
| 59 | + [], |
| 60 | + {"env1": True}, |
| 61 | + id="single-none-not ignored", |
| 62 | + ), |
| 63 | + pytest.param( |
| 64 | + {"env1": [Spec("d", None)]}, |
| 65 | + ["d"], |
| 66 | + {"env1": False}, |
| 67 | + id="single-none-ignored", |
| 68 | + ), |
| 69 | + ), |
| 70 | +) |
| 71 | +def test_compare_versions(envs, ignored_violations, expected): |
| 72 | + policy_versions = { |
| 73 | + "a": FakeRecord(version=Version("1.2")), |
| 74 | + "b": FakeRecord(version=Version("3.1")), |
| 75 | + "c": FakeRecord(version=Version("2025.1")), |
| 76 | + "d": FakeRecord(version=Version("0.5")), |
| 77 | + } |
| 78 | + |
| 79 | + actual = environments.spec.compare_versions( |
| 80 | + envs, policy_versions, ignored_violations |
| 81 | + ) |
| 82 | + assert actual == expected |
0 commit comments