|
| 1 | +"""Tests for bakery ci matrix with images that have both matrix: and devVersions:. |
| 2 | +
|
| 3 | +Covers two regression scenarios where dev versions from a matrix image were |
| 4 | +silently dropped from the output: |
| 5 | +
|
| 6 | + Issue 1: --matrix-versions only --dev-versions only → returned [] |
| 7 | + Issue 2: --matrix-versions include --dev-versions only → excluded matrix images with dev versions |
| 8 | +""" |
| 9 | + |
| 10 | +import json |
| 11 | +from pathlib import Path |
| 12 | +from unittest.mock import MagicMock, patch |
| 13 | + |
| 14 | +import pytest |
| 15 | +from typer.testing import CliRunner |
| 16 | + |
| 17 | +from posit_bakery.cli.main import app |
| 18 | +from posit_bakery.config.image.posit_product.const import ReleaseChannelEnum |
| 19 | +from posit_bakery.const import DevVersionInclusionEnum |
| 20 | + |
| 21 | +runner = CliRunner() |
| 22 | +BASIC_CONTEXT = str(Path(__file__).parent.parent / "resources" / "basic") |
| 23 | + |
| 24 | + |
| 25 | +def _make_version(name: str, *, is_dev: bool, channel: ReleaseChannelEnum | None = None): |
| 26 | + """Return a minimal MagicMock ImageVersion with working matches_dev_filter.""" |
| 27 | + ver = MagicMock() |
| 28 | + ver.name = name |
| 29 | + ver.isDevelopmentVersion = is_dev |
| 30 | + ver.metadata = {"release_channel": channel} if channel else {} |
| 31 | + ver.supported_platforms = ["linux/amd64"] |
| 32 | + |
| 33 | + def matches_dev_filter(dev_versions, dev_channel=None): |
| 34 | + if is_dev and dev_versions == DevVersionInclusionEnum.EXCLUDE: |
| 35 | + return False, "excluded by --dev-versions exclude" |
| 36 | + if not is_dev and dev_versions == DevVersionInclusionEnum.ONLY: |
| 37 | + return False, "not a development version (excluded by --dev-versions only)" |
| 38 | + if dev_channel is not None and is_dev: |
| 39 | + if channel != dev_channel: |
| 40 | + return False, f"channel mismatch" |
| 41 | + return True, None |
| 42 | + |
| 43 | + ver.matches_dev_filter = matches_dev_filter |
| 44 | + return ver |
| 45 | + |
| 46 | + |
| 47 | +def _make_matrix_image(name: str, dev_versions: list, prod_versions: list): |
| 48 | + """Return a mock Image with both a matrix and pre-loaded dev versions.""" |
| 49 | + img = MagicMock() |
| 50 | + img.name = name |
| 51 | + |
| 52 | + matrix = MagicMock() |
| 53 | + matrix.to_image_versions.return_value = prod_versions |
| 54 | + img.matrix = matrix |
| 55 | + |
| 56 | + # img.versions simulates the state *after* load_dev_versions() has been called. |
| 57 | + # In practice bakery loads dev versions into image.versions; prod matrix versions |
| 58 | + # come from img.matrix.to_image_versions() at filter time. |
| 59 | + img.versions = dev_versions |
| 60 | + |
| 61 | + return img |
| 62 | + |
| 63 | + |
| 64 | +@pytest.fixture |
| 65 | +def mock_config_with_matrix_dev_image(): |
| 66 | + """Patch BakeryConfig to return a single matrix image with a dev version loaded.""" |
| 67 | + dev_ver = _make_version("2026.99.0-dev+1", is_dev=True, channel=ReleaseChannelEnum.DAILY) |
| 68 | + prod_ver1 = _make_version("2026.1.0", is_dev=False) |
| 69 | + prod_ver2 = _make_version("2026.2.0", is_dev=False) |
| 70 | + img = _make_matrix_image("positron-session", [dev_ver], [prod_ver1, prod_ver2]) |
| 71 | + |
| 72 | + with patch("posit_bakery.cli.ci.BakeryConfig") as mock: |
| 73 | + instance = MagicMock() |
| 74 | + instance.model.images = [img] |
| 75 | + mock.from_context.return_value = instance |
| 76 | + yield mock, dev_ver, prod_ver1, prod_ver2 |
| 77 | + |
| 78 | + |
| 79 | +class TestCiMatrixDevVersionsOnly: |
| 80 | + """Issue 1: --matrix-versions only --dev-versions only returned [].""" |
| 81 | + |
| 82 | + def test_dev_version_included(self, mock_config_with_matrix_dev_image): |
| 83 | + _, dev_ver, _, _ = mock_config_with_matrix_dev_image |
| 84 | + result = runner.invoke( |
| 85 | + app, |
| 86 | + [ |
| 87 | + "ci", |
| 88 | + "matrix", |
| 89 | + "--context", |
| 90 | + BASIC_CONTEXT, |
| 91 | + "--matrix-versions", |
| 92 | + "only", |
| 93 | + "--dev-versions", |
| 94 | + "only", |
| 95 | + ], |
| 96 | + catch_exceptions=False, |
| 97 | + ) |
| 98 | + assert result.exit_code == 0, result.output |
| 99 | + data = json.loads(result.stdout.strip()) |
| 100 | + assert len(data) == 1 |
| 101 | + assert data[0]["version"] == dev_ver.name |
| 102 | + assert data[0]["dev"] is True |
| 103 | + |
| 104 | + def test_prod_versions_excluded(self, mock_config_with_matrix_dev_image): |
| 105 | + _, _, prod_ver1, prod_ver2 = mock_config_with_matrix_dev_image |
| 106 | + result = runner.invoke( |
| 107 | + app, |
| 108 | + [ |
| 109 | + "ci", |
| 110 | + "matrix", |
| 111 | + "--context", |
| 112 | + BASIC_CONTEXT, |
| 113 | + "--matrix-versions", |
| 114 | + "only", |
| 115 | + "--dev-versions", |
| 116 | + "only", |
| 117 | + ], |
| 118 | + catch_exceptions=False, |
| 119 | + ) |
| 120 | + assert result.exit_code == 0, result.output |
| 121 | + data = json.loads(result.stdout.strip()) |
| 122 | + versions_in_output = {e["version"] for e in data} |
| 123 | + assert prod_ver1.name not in versions_in_output |
| 124 | + assert prod_ver2.name not in versions_in_output |
| 125 | + |
| 126 | + |
| 127 | +class TestCiMatrixDevVersionsInclude: |
| 128 | + """Issue 2: --matrix-versions include --dev-versions only omitted matrix images' dev versions.""" |
| 129 | + |
| 130 | + def test_dev_version_included(self, mock_config_with_matrix_dev_image): |
| 131 | + _, dev_ver, _, _ = mock_config_with_matrix_dev_image |
| 132 | + result = runner.invoke( |
| 133 | + app, |
| 134 | + [ |
| 135 | + "ci", |
| 136 | + "matrix", |
| 137 | + "--context", |
| 138 | + BASIC_CONTEXT, |
| 139 | + "--matrix-versions", |
| 140 | + "include", |
| 141 | + "--dev-versions", |
| 142 | + "only", |
| 143 | + ], |
| 144 | + catch_exceptions=False, |
| 145 | + ) |
| 146 | + assert result.exit_code == 0, result.output |
| 147 | + data = json.loads(result.stdout.strip()) |
| 148 | + dev_entries = [e for e in data if e["dev"]] |
| 149 | + assert len(dev_entries) == 1 |
| 150 | + assert dev_entries[0]["version"] == dev_ver.name |
| 151 | + |
| 152 | + def test_prod_versions_excluded(self, mock_config_with_matrix_dev_image): |
| 153 | + """With --dev-versions only, production matrix versions are filtered out.""" |
| 154 | + _, _, prod_ver1, prod_ver2 = mock_config_with_matrix_dev_image |
| 155 | + result = runner.invoke( |
| 156 | + app, |
| 157 | + [ |
| 158 | + "ci", |
| 159 | + "matrix", |
| 160 | + "--context", |
| 161 | + BASIC_CONTEXT, |
| 162 | + "--matrix-versions", |
| 163 | + "include", |
| 164 | + "--dev-versions", |
| 165 | + "only", |
| 166 | + ], |
| 167 | + catch_exceptions=False, |
| 168 | + ) |
| 169 | + assert result.exit_code == 0, result.output |
| 170 | + data = json.loads(result.stdout.strip()) |
| 171 | + versions_in_output = {e["version"] for e in data} |
| 172 | + assert prod_ver1.name not in versions_in_output |
| 173 | + assert prod_ver2.name not in versions_in_output |
0 commit comments