Skip to content

Commit 17282d1

Browse files
committed
Fix remaining test failures and enable label validation
Signed-off-by: Michał Górny <mgorny@quansight.com>
1 parent 71602f8 commit 17282d1

5 files changed

Lines changed: 23 additions & 21 deletions

File tree

tests/models/test_variant.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,6 @@ def test_random_hexdigest(vdesc: VariantDescription) -> None:
427427
assert len(vdesc.hexdigest) == VARIANT_HASH_LENGTH
428428

429429

430-
@pytest.mark.xfail(reason="Validation is disabled for porting")
431430
def test_null_variant_label():
432431
with pytest.raises(
433432
ValidationError,

tests/test_variants_json.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -317,16 +317,18 @@ def test_to_str() -> None:
317317
[
318318
VariantProperty("ns1", "f1", "v1"),
319319
VariantProperty("ns2", "f2", "v1"),
320-
]
320+
],
321+
label="a",
321322
)
322323
vdesc2 = VariantDescription(
323324
[
324325
VariantProperty("ns2", "f2", "v2"),
325-
]
326+
],
327+
label="b",
326328
)
327329
variants_json.variants = {
328-
vdesc1.hexdigest: vdesc1,
329-
vdesc2.hexdigest: vdesc2,
330+
"a": vdesc1,
331+
"b": vdesc2,
330332
}
331333
assert json.loads(variants_json.to_str()) == {
332334
VARIANTS_JSON_SCHEMA_KEY: VARIANTS_JSON_SCHEMA_URL,
@@ -347,8 +349,8 @@ def test_to_str() -> None:
347349
},
348350
},
349351
VARIANTS_JSON_VARIANT_DATA_KEY: {
350-
"b3b0305c": {"ns1": {"f1": ["v1"]}, "ns2": {"f2": ["v1"]}},
351-
"9177ff3f": {"ns2": {"f2": ["v2"]}},
352+
"a": {"ns1": {"f1": ["v1"]}, "ns2": {"f2": ["v1"]}},
353+
"b": {"ns2": {"f2": ["v2"]}},
352354
},
353355
}
354356

@@ -501,12 +503,13 @@ def test_merge_variants() -> None:
501503
def test_null_variant_label():
502504
with pytest.raises(
503505
ValidationError,
504-
match=rf"{NULL_VARIANT_LABEL!r} label can only be used for the null variant",
506+
match=rf"{NULL_VARIANT_LABEL!r} label can be used only for the null variant",
505507
):
506508
VariantsJson(
507509
{VARIANTS_JSON_VARIANT_DATA_KEY: {NULL_VARIANT_LABEL: {"x": {"y": ["z"]}}}}
508510
)
509511
with pytest.raises(
510-
ValidationError, match=rf"Null variant must use {NULL_VARIANT_LABEL!r} label"
512+
ValidationError,
513+
match=rf"Null variant must always use {NULL_VARIANT_LABEL!r} label",
511514
):
512515
VariantsJson({VARIANTS_JSON_VARIANT_DATA_KEY: {"zuul": {}}})

variantlib/commands/get_variant_hash.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from variantlib import __package_name__
77
from variantlib.api import VariantDescription
88
from variantlib.api import VariantProperty
9+
from variantlib.constants import NULL_VARIANT_LABEL
910

1011

1112
def get_variant_hash(args: list[str]) -> None:
@@ -35,6 +36,8 @@ def get_variant_hash(args: list[str]) -> None:
3536

3637
def _print_variant_hash(properties: list[VariantProperty]) -> None:
3738
# Transform properties into a VariantDescription
38-
vdesc = VariantDescription(properties=properties)
39+
vdesc = VariantDescription(
40+
properties=properties, label="na" if properties else NULL_VARIANT_LABEL
41+
)
3942

4043
sys.stdout.write(f"{vdesc.hexdigest}\n")

variantlib/commands/make_variant.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ def _make_variant(
169169

170170
if not is_null_variant:
171171
# Transform properties into a VariantDescription
172-
vdesc = VariantDescription(properties=properties)
172+
vdesc = VariantDescription(properties=properties, label="na")
173173

174174
if validate_properties:
175175
env_factory: DefaultIsolatedEnv | nullcontext[None]

variantlib/models/variant.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -187,18 +187,15 @@ def __post_init__(self) -> None:
187187
# Execute the validator
188188
super().__post_init__()
189189

190-
# TODO: enable once we're done porting
191-
if False:
192-
if self.is_null_variant():
193-
if self.label != NULL_VARIANT_LABEL:
194-
raise ValidationError(
195-
f"Null variant must always use {NULL_VARIANT_LABEL!r} label"
196-
)
197-
elif self.label == NULL_VARIANT_LABEL:
190+
if self.is_null_variant():
191+
if self.label != NULL_VARIANT_LABEL:
198192
raise ValidationError(
199-
f"{NULL_VARIANT_LABEL!r} label can be used only for the null "
200-
"variant"
193+
f"Null variant must always use {NULL_VARIANT_LABEL!r} label"
201194
)
195+
elif self.label == NULL_VARIANT_LABEL:
196+
raise ValidationError(
197+
f"{NULL_VARIANT_LABEL!r} label can be used only for the null variant"
198+
)
202199

203200
def is_null_variant(self) -> bool:
204201
"""

0 commit comments

Comments
 (0)