Skip to content

Commit d5ea516

Browse files
committed
Work around azdev test not respecting the session scope
1 parent 64a918b commit d5ea516

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

src/confcom/azext_confcom/tests/conftest.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# Licensed under the MIT License. See License.txt in the project root for license information.
44
# --------------------------------------------------------------------------------------------
55

6+
import functools
67
import importlib
78
import subprocess
89
import tempfile
@@ -13,20 +14,28 @@
1314
from pathlib import Path
1415

1516

17+
# This is required to work around the implementation of azdev test. It runs
18+
# separate pytest invocations for each test, making it impossible to respect the
19+
# session scope. Therefore, we need to manually ensure wheels are not built
20+
# separately for each test.
21+
@functools.lru_cache()
22+
def get_wheel_dir():
23+
return Path(tempfile.mkdtemp(prefix="confcom_wheels_"))
24+
25+
1626
# This fixture ensures tests are run against final built wheels of the extension
1727
# instead of the unbuilt local code, which may have breaking differences with
1828
# the thing we actually ship to users. All but the test modules themselves are
1929
# replaced with the wheel in case the tests themselves rely on unshipped code.
20-
21-
2230
@pytest.fixture(autouse=True, scope="session")
2331
def run_on_wheel(request):
2432

2533
modules_to_test = {i.module for i in request.session.items}
2634
extensions_to_build = {module.__name__.split(".")[0] for module in modules_to_test}
2735
extension_dirs = {Path(a.split("/azext_")[0]) for a in request.config.args}
2836

29-
with tempfile.TemporaryDirectory() as build_dir:
37+
build_dir = get_wheel_dir()
38+
if not any(build_dir.iterdir()):
3039

3140
# Delete the extensions build dir, as azdev extension build doesn't
3241
# reliably handle changes
@@ -37,16 +46,16 @@ def run_on_wheel(request):
3746
# Build all extensions being tested into wheels
3847
for extension in extensions_to_build:
3948
subprocess.run(
40-
["azdev", "extension", "build", extension.replace("azext_", ""), "--dist-dir", build_dir],
49+
["azdev", "extension", "build", extension.replace("azext_", ""), "--dist-dir", build_dir.as_posix()],
4150
check=True,
4251
)
4352

4453
# Add the wheel to the path and reload extension modules so the
4554
# tests pick up the wheel code over the unbuilt code
46-
sys.path.insert(0, Path(build_dir).glob("*.whl").__next__().as_posix())
55+
sys.path.insert(0, build_dir.glob("*.whl").__next__().as_posix())
4756
for module in list(sys.modules.values()):
4857
if extension in module.__name__ and module not in modules_to_test:
4958
del sys.modules[module.__name__]
5059
importlib.import_module(module.__name__)
5160

52-
yield
61+
yield

0 commit comments

Comments
 (0)