Skip to content

Commit 4f83aba

Browse files
authored
Null variant labels (#110)
* Add a `get_variant_label()` helper * Use 'null' label for the null variant * Use `sha256("")` for null variant hash Since we are no longer using it as a label, we do not need a recognizable special value here. Instead, just use hash of an empty string, so we can remove the special casing in code. * Restore explicit checks for `--variant-label` use * Remove `NULL_VARIANT_HASH` Remove `NULL_VARIANT_HASH` since it resembles magic constant too much. In the few places where we need it, either call hash directly, or get it from VariantDescription. Signed-off-by: Michał Górny <[email protected]>
1 parent 008ba22 commit 4f83aba

19 files changed

+190
-69
lines changed
Binary file not shown.
Binary file not shown.

tests/artifacts/test-package/dist/test_package-0-variants.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
}
2121
},
2222
"variants": {
23-
"00000000": {},
23+
"null": {},
2424
"5d8be4b9": {
2525
"installable_plugin": {
2626
"feat1": [

tests/artifacts/variant_json_files/dummy_project-1.0.0-variants.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@
7575
]
7676
}
7777
},
78-
"00000000": {},
78+
"null": {},
7979
"3f7188c1": {
8080
"fictional_hw": {
8181
"architecture": [

tests/artifacts/variant_json_files/sandbox_project-1.0.0-variants.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
}
2424
},
2525
"variants": {
26-
"00000000": {},
26+
"null": {},
2727
"38ea48ce": {
2828
"fictional_hw": {
2929
"architecture": [
@@ -69,4 +69,4 @@
6969
}
7070
}
7171
}
72-
}
72+
}

tests/commands/test_analyze_wheel.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from typing import TYPE_CHECKING
44

55
from variantlib.commands.main import main
6-
from variantlib.constants import NULL_VARIANT_HASH
6+
from variantlib.constants import NULL_VARIANT_LABEL
77

88
if TYPE_CHECKING:
99
import pytest
@@ -34,13 +34,13 @@ def test_analyze_wheel_null_variant(
3434
"analyze-wheel",
3535
"-i",
3636
"tests/artifacts/test-package/dist/test_package-0-py3-none-any-"
37-
f"{NULL_VARIANT_HASH}.whl",
37+
f"{NULL_VARIANT_LABEL}.whl",
3838
]
3939
)
4040
assert (
4141
capsys.readouterr().out
4242
== f"""\
43-
############################## Variant: `{NULL_VARIANT_HASH}` \
43+
############################## Variant: `{NULL_VARIANT_LABEL}` \
4444
#############################
4545
################################################################################
4646
"""

tests/commands/test_generate_index_json.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from typing import TYPE_CHECKING
77

88
from variantlib.commands.main import main
9-
from variantlib.constants import NULL_VARIANT_HASH
9+
from variantlib.constants import NULL_VARIANT_LABEL
1010
from variantlib.constants import VARIANT_INFO_DEFAULT_PRIO_KEY
1111
from variantlib.constants import VARIANT_INFO_NAMESPACE_KEY
1212
from variantlib.constants import VARIANT_INFO_PROVIDER_DATA_KEY
@@ -25,7 +25,7 @@ def test_generate_index_json(
2525
) -> None:
2626
filenames = [
2727
"test_package-0-py3-none-any.whl",
28-
f"test_package-0-py3-none-any-{NULL_VARIANT_HASH}.whl",
28+
f"test_package-0-py3-none-any-{NULL_VARIANT_LABEL}.whl",
2929
"test_package-0-py3-none-any-5d8be4b9.whl",
3030
]
3131
artifact_dir = Path("tests/artifacts/test-package/dist")
@@ -51,7 +51,7 @@ def test_generate_index_json(
5151
},
5252
},
5353
VARIANTS_JSON_VARIANT_DATA_KEY: {
54-
NULL_VARIANT_HASH: {},
54+
NULL_VARIANT_LABEL: {},
5555
"5d8be4b9": {
5656
"installable_plugin": {
5757
"feat1": ["val1c"],

tests/commands/test_get_variant_hash.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
from __future__ import annotations
22

3+
import hashlib
34
from itertools import chain
45

56
import pytest
67

78
from variantlib.commands.main import main
8-
from variantlib.constants import NULL_VARIANT_HASH
9+
from variantlib.constants import VARIANT_HASH_LEN
910

1011

1112
@pytest.mark.parametrize(
1213
("properties", "expected"),
1314
[
14-
([], NULL_VARIANT_HASH),
15+
([], hashlib.sha256(b"").hexdigest()[:VARIANT_HASH_LEN]),
1516
(["a::b::c"], "01a9783a"),
1617
(["d::e::f"], "41665eee"),
1718
(["a::b::c", "d::e::f"], "eb9a66a7"),

tests/commands/test_make_variant.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
from tests.utils import assert_zips_equal
99
from variantlib.commands.main import main
10-
from variantlib.constants import NULL_VARIANT_HASH
10+
from variantlib.constants import NULL_VARIANT_LABEL
1111

1212
if TYPE_CHECKING:
1313
from pathlib import Path
@@ -37,7 +37,7 @@ def mocked_plugin_reqs(
3737
("label", "properties"),
3838
[
3939
# Null Variant
40-
(NULL_VARIANT_HASH, None),
40+
(NULL_VARIANT_LABEL, None),
4141
# Variant 1
4242
(
4343
"5d8be4b9",
@@ -86,7 +86,7 @@ def test_make_variant(
8686
itertools.chain.from_iterable(["-p", vprop] for vprop in properties)
8787
)
8888

89-
if len(label) != 8:
89+
if len(label) != 8 and label != "null":
9090
cmd_args.append(f"--variant-label={label}")
9191

9292
main([*cmd_args])
@@ -112,9 +112,13 @@ def test_make_variant(
112112
["--property=x::y::z", "--variant-label=123456789"],
113113
"error: invalid variant label",
114114
),
115+
(
116+
["--property=x::y::z", "--variant-label=null"],
117+
"error: invalid variant label",
118+
),
115119
(
116120
["--null-variant", "--variant-label=null"],
117-
"error: --variant-label cannot be usedwith --null-variant",
121+
"error: --variant-label cannot be used with --null-variant",
118122
),
119123
],
120124
)

tests/commands/test_unmake_variant.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66

77
from tests.utils import assert_zips_equal
88
from variantlib.commands.main import main
9-
from variantlib.constants import NULL_VARIANT_HASH
9+
from variantlib.constants import NULL_VARIANT_LABEL
1010

1111

1212
@pytest.mark.parametrize(
1313
"filename",
1414
[
15-
f"test_package-0-py3-none-any-{NULL_VARIANT_HASH}.whl",
15+
f"test_package-0-py3-none-any-{NULL_VARIANT_LABEL}.whl",
1616
"test_package-0-py3-none-any-5d8be4b9.whl",
1717
"test_package-0-py3-none-any-60567bd9.whl",
1818
"test_package-0-py3-none-any-fbe82642.whl",

0 commit comments

Comments
 (0)