|
21 | 21 | check_extra_options, |
22 | 22 | check_options, |
23 | 23 | parse_milestone, |
| 24 | + validate_links, |
24 | 25 | ) |
25 | 26 | from score_metamodel.tests import fake_check_logger, need |
26 | 27 | from sphinx.application import Sphinx # type: ignore[import-untyped] |
@@ -124,3 +125,125 @@ def test_milestone_parsing(): |
124 | 125 | assert parse_milestone("v0.5") == (0, 5, 0) |
125 | 126 | assert parse_milestone("v1.0") == (1, 0, 0) |
126 | 127 | assert parse_milestone("v1.0.1") == (1, 0, 1) |
| 128 | + |
| 129 | + |
| 130 | +class TestValidateLinks: |
| 131 | + """Tests for validate_links() — specifically the prefix-stripping bug (#588).""" |
| 132 | + |
| 133 | + NEED_TYPE_WITH_LINK_BY_NAME: ScoreNeedType = { |
| 134 | + "title": "Test Type", |
| 135 | + "prefix": "TT", |
| 136 | + "tags": [], |
| 137 | + "parts": 1, |
| 138 | + "directive": "test_type", |
| 139 | + "mandatory_options": { |
| 140 | + "id": "^test_type__.*$", |
| 141 | + "status": "^(draft|valid)$", |
| 142 | + }, |
| 143 | + "optional_options": {}, |
| 144 | + "mandatory_links": {}, |
| 145 | + "optional_links": { |
| 146 | + "satisfies": ["product"], |
| 147 | + }, |
| 148 | + } |
| 149 | + |
| 150 | + NEED_TYPE_WITH_LINK_BY_REGEX: ScoreNeedType = { |
| 151 | + "title": "Test Type", |
| 152 | + "prefix": "TT", |
| 153 | + "tags": [], |
| 154 | + "parts": 1, |
| 155 | + "directive": "test_type", |
| 156 | + "mandatory_options": { |
| 157 | + "id": "^test_type__.*$", |
| 158 | + "status": "^(draft|valid)$", |
| 159 | + }, |
| 160 | + "optional_options": {}, |
| 161 | + "mandatory_links": {}, |
| 162 | + "optional_links": { |
| 163 | + "satisfies": ["^product__.*$"], |
| 164 | + }, |
| 165 | + } |
| 166 | + |
| 167 | + def test_link_matches_by_type_name_no_prefix(self): |
| 168 | + """A link value that starts with the type name should pass validation.""" |
| 169 | + need_1 = need( |
| 170 | + id="test_type__001", |
| 171 | + type="test_type", |
| 172 | + satisfies="product__foo", |
| 173 | + ) |
| 174 | + logger = fake_check_logger() |
| 175 | + validate_links( |
| 176 | + cast(CheckLogger, logger), |
| 177 | + self.NEED_TYPE_WITH_LINK_BY_NAME, |
| 178 | + need_1, |
| 179 | + ) |
| 180 | + logger.assert_no_warnings() |
| 181 | + |
| 182 | + def test_link_with_uppercase_prefix_no_longer_stripped(self): |
| 183 | + """ |
| 184 | + After the fix for #588, uppercase prefixes are no longer stripped. |
| 185 | + P_FOO stays as P_FOO and correctly fails to match the 'product' type pattern. |
| 186 | + """ |
| 187 | + need_1 = need( |
| 188 | + id="test_type__001", |
| 189 | + type="test_type", |
| 190 | + satisfies="P_FOO", |
| 191 | + ) |
| 192 | + logger = fake_check_logger() |
| 193 | + validate_links( |
| 194 | + cast(CheckLogger, logger), |
| 195 | + self.NEED_TYPE_WITH_LINK_BY_NAME, |
| 196 | + need_1, |
| 197 | + ) |
| 198 | + # P_FOO is preserved as-is (no stripping) and doesn't match ^product__ |
| 199 | + logger.assert_warning("references 'P_FOO' as 'satisfies'") |
| 200 | + |
| 201 | + def test_link_with_uppercase_prefix_not_stripped_for_regex(self): |
| 202 | + """ |
| 203 | + After the fix for #588, uppercase prefixes are no longer stripped. |
| 204 | + P_product__foo stays as P_product__foo and does NOT match ^product__.*$ |
| 205 | + because the P_ prefix is preserved. |
| 206 | + """ |
| 207 | + need_1 = need( |
| 208 | + id="test_type__001", |
| 209 | + type="test_type", |
| 210 | + satisfies="P_product__foo", |
| 211 | + ) |
| 212 | + logger = fake_check_logger() |
| 213 | + validate_links( |
| 214 | + cast(CheckLogger, logger), |
| 215 | + self.NEED_TYPE_WITH_LINK_BY_REGEX, |
| 216 | + need_1, |
| 217 | + ) |
| 218 | + # P_product__foo is preserved as-is and doesn't match ^product__.*$ |
| 219 | + logger.assert_warning("references 'P_product__foo' as 'satisfies'") |
| 220 | + |
| 221 | + def test_link_without_prefix_matches_by_name(self): |
| 222 | + """A link value without any prefix that starts with the type name passes.""" |
| 223 | + need_1 = need( |
| 224 | + id="test_type__001", |
| 225 | + type="test_type", |
| 226 | + satisfies="product__bar", |
| 227 | + ) |
| 228 | + logger = fake_check_logger() |
| 229 | + validate_links( |
| 230 | + cast(CheckLogger, logger), |
| 231 | + self.NEED_TYPE_WITH_LINK_BY_NAME, |
| 232 | + need_1, |
| 233 | + ) |
| 234 | + logger.assert_no_warnings() |
| 235 | + |
| 236 | + def test_link_not_matching_any_type_warns(self): |
| 237 | + """A link value that doesn't match any allowed type should warn.""" |
| 238 | + need_1 = need( |
| 239 | + id="test_type__001", |
| 240 | + type="test_type", |
| 241 | + satisfies="unknown__thing", |
| 242 | + ) |
| 243 | + logger = fake_check_logger() |
| 244 | + validate_links( |
| 245 | + cast(CheckLogger, logger), |
| 246 | + self.NEED_TYPE_WITH_LINK_BY_NAME, |
| 247 | + need_1, |
| 248 | + ) |
| 249 | + logger.assert_warning("references 'unknown__thing' as 'satisfies'") |
0 commit comments