Skip to content

Commit 382129c

Browse files
committed
Use build to handle installing providers in make-variant
Since we are going to be removing provider installation code in favor of doing it directly inside the package manager, modify `make-variant` command implementation to use the code from `build` directly for that purpose.
1 parent 526132a commit 382129c

File tree

3 files changed

+56
-23
lines changed

3 files changed

+56
-23
lines changed

pyproject.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ dynamic = ["version"]
2929

3030
[project.optional-dependencies]
3131
cli = [
32+
"build>=1.2.0,<1.3",
3233
"tomlkit>=0.13,<0.14",
3334
"tzlocal>=3.0,<6",
3435
]
@@ -41,6 +42,7 @@ dev = [
4142
"ruff>=0.10,<1.0",
4243
]
4344
test = [
45+
"build>=1.2.0,<1.3",
4446
"deepdiff>=8.0,<9.0",
4547
"hypothesis>=6.0.0,<7",
4648
"parameterized>=0.9.0,<0.10",

tests/commands/test_make_variant.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
if TYPE_CHECKING:
1212
from pathlib import Path
1313

14+
from pytest_mock import MockerFixture
15+
1416

1517
@pytest.fixture
1618
def non_variant_wheel(test_artifact_path: Path) -> Path:
@@ -20,6 +22,16 @@ def non_variant_wheel(test_artifact_path: Path) -> Path:
2022
return whl_f
2123

2224

25+
@pytest.fixture
26+
def mocked_plugin_reqs(
27+
mocker: MockerFixture,
28+
test_plugin_package_req: str,
29+
) -> None:
30+
mocker.patch(
31+
"variantlib.models.variant_info.VariantInfo.get_provider_requires"
32+
).return_value = [test_plugin_package_req]
33+
34+
2335
@pytest.mark.parametrize(
2436
("vhash", "properties"),
2537
[
@@ -51,6 +63,7 @@ def test_make_variant(
5163
non_variant_wheel: Path,
5264
test_artifact_path: Path,
5365
tmp_path: Path,
66+
mocked_plugin_reqs: None,
5467
) -> None:
5568
cmd_args = [
5669
"make-variant",
@@ -69,12 +82,7 @@ def test_make_variant(
6982
itertools.chain.from_iterable(["-p", vprop] for vprop in properties)
7083
)
7184

72-
if properties is not None:
73-
with pytest.raises(RuntimeError, match="No module named 'test_plugin_package'"):
74-
# This should fail because the plugin is not installed
75-
main(cmd_args)
76-
77-
main([*cmd_args, "--skip-plugin-validation"])
85+
main([*cmd_args])
7886

7987
target_variant_wheel = (
8088
non_variant_wheel.parent / f"test_package-0-py3-none-any-{vhash}.whl"

variantlib/commands/make_variant.py

Lines changed: 40 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
import logging
77
import pathlib
88
import shutil
9+
import sys
910
import zipfile
11+
from subprocess import CalledProcessError
1012

1113
from variantlib import __package_name__
1214
from variantlib.api import VariantDescription
@@ -129,24 +131,45 @@ def _make_variant(
129131
vdesc = VariantDescription(properties=properties)
130132

131133
if validate_properties:
132-
# Verify whether the variant properties are valid
133-
vdesc_valid = validate_variant(vdesc, variant_info=variant_info)
134-
if vdesc_valid.invalid_properties:
135-
invalid_str = ", ".join(
136-
x.to_str() for x in vdesc_valid.invalid_properties
137-
)
138-
raise ValidationError(
139-
"The following variant properties are invalid according to the "
140-
f"plugins: {invalid_str}"
141-
)
142-
if vdesc_valid.unknown_properties:
143-
unknown_str = ", ".join(
144-
x.to_str() for x in vdesc_valid.unknown_properties
145-
)
146-
raise ValidationError(
147-
"The following variant properties use namespaces that are not "
148-
f"provided by any installed plugin: {unknown_str}"
134+
from build.env import DefaultIsolatedEnv
135+
136+
with DefaultIsolatedEnv() as venv:
137+
try:
138+
venv.install(
139+
variant_info.get_provider_requires(
140+
{vprop.namespace for vprop in vdesc.properties}
141+
)
142+
)
143+
except CalledProcessError as err:
144+
sys.stderr.write(
145+
"Installing variant provider dependencies failed:\n"
146+
f"{err.stderr.decode()}"
147+
)
148+
raise
149+
150+
# Verify whether the variant properties are valid
151+
vdesc_valid = validate_variant(
152+
vdesc,
153+
variant_info=variant_info,
154+
use_auto_install=False,
155+
venv_path=venv.path,
149156
)
157+
if vdesc_valid.invalid_properties:
158+
invalid_str = ", ".join(
159+
x.to_str() for x in vdesc_valid.invalid_properties
160+
)
161+
raise ValidationError(
162+
"The following variant properties are invalid according to the "
163+
f"plugins: {invalid_str}"
164+
)
165+
if vdesc_valid.unknown_properties:
166+
unknown_str = ", ".join(
167+
x.to_str() for x in vdesc_valid.unknown_properties
168+
)
169+
raise ValidationError(
170+
"The following variant properties use namespaces that are not "
171+
f"provided by any installed plugin: {unknown_str}"
172+
)
150173
else:
151174
# Create a null variant
152175
vdesc = VariantDescription()

0 commit comments

Comments
 (0)