Skip to content

Commit 7cb145f

Browse files
committed
Add a variantlib plugin validate-variant command
1 parent 853a304 commit 7cb145f

File tree

3 files changed

+72
-0
lines changed

3 files changed

+72
-0
lines changed

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ show = "variantlib.commands.config.show:show"
7777
[project.entry-points."variantlib.actions.plugins"]
7878
list = "variantlib.commands.plugins.list_plugins:list_plugins"
7979
get-supported-configs = "variantlib.commands.plugins.get_supported_configs:get_supported_configs"
80+
validate-property = "variantlib.commands.plugins.validate_property:validate_property"
8081

8182
[tool.pytest.ini_options]
8283
testpaths = ["tests/"]

tests/commands/test_plugins.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,33 @@ def test_plugins_get_supported_configs_filter_feature(
6666
test_namespace :: name1 :: val1b
6767
"""
6868
)
69+
70+
71+
def test_plugins_validate_property(
72+
capsys: pytest.CaptureFixture[str], mocked_entry_points: None
73+
) -> None:
74+
main(
75+
[
76+
"plugins",
77+
"validate-property",
78+
"test_namespace :: name1 :: val1a",
79+
"test_namespace :: name1::val1b",
80+
"test_namespace :: name2 :: val2a",
81+
"second_namespace:: name1:: val1a",
82+
"second_namespace::name3 :: val3a",
83+
"test_namespace :: name3 :: val3a",
84+
"unknown_namespace::foo::bar",
85+
]
86+
)
87+
assert (
88+
capsys.readouterr().out
89+
== """\
90+
test_namespace :: name1 :: val1a : valid
91+
test_namespace :: name1 :: val1b : valid
92+
test_namespace :: name2 :: val2a : valid
93+
second_namespace :: name1 :: val1a : invalid
94+
second_namespace :: name3 :: val3a : valid
95+
test_namespace :: name3 :: val3a : invalid
96+
unknown_namespace :: foo :: bar : no-plugin
97+
"""
98+
)
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
from __future__ import annotations
2+
3+
import argparse
4+
import logging
5+
import sys
6+
from typing import TYPE_CHECKING
7+
8+
from variantlib import __package_name__
9+
from variantlib.models.variant import VariantProperty
10+
11+
if TYPE_CHECKING:
12+
from variantlib.plugins.loader import BasePluginLoader
13+
14+
15+
logger = logging.getLogger(__name__)
16+
17+
res_map = {
18+
False: "invalid",
19+
True: "valid",
20+
None: "no-plugin",
21+
}
22+
23+
24+
def validate_property(args: list[str], plugin_loader: BasePluginLoader) -> None:
25+
parser = argparse.ArgumentParser(
26+
prog=f"{__package_name__} plugins validate-property",
27+
description="CLI interface to validate properties",
28+
)
29+
parser.add_argument(
30+
"property",
31+
nargs="+",
32+
type=VariantProperty.from_str,
33+
help="One or more properties to validate",
34+
)
35+
36+
parsed_args = parser.parse_args(args)
37+
validation_result = plugin_loader.validate_properties(parsed_args.property)
38+
for vprop in parsed_args.property:
39+
sys.stdout.write(
40+
f"{vprop.to_str()} : {res_map[validation_result.results[vprop]]}\n"
41+
)

0 commit comments

Comments
 (0)