Skip to content

Commit 54eb35b

Browse files
committed
create conda env in pytest fixture
1 parent 397f94c commit 54eb35b

File tree

2 files changed

+28
-34
lines changed

2 files changed

+28
-34
lines changed

tests/conftest.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,26 @@
11
import json
2+
import subprocess
23
from pathlib import Path
34

45
import matplotlib
56
import pytest
67

78

9+
@pytest.fixture(scope="function")
10+
def conda_env(tmp_path):
11+
env_dir = tmp_path / "fake_env"
12+
env_dir_str = env_dir.as_posix()
13+
subprocess.run(
14+
["conda", "create", "-y", "-p", env_dir_str],
15+
check=True,
16+
capture_output=True,
17+
)
18+
yield env_dir_str
19+
subprocess.run(
20+
["conda", "env", "remove", "-p", env_dir_str, "-y"], check=True
21+
)
22+
23+
824
@pytest.fixture(scope="function")
925
def example_cases(tmp_path_factory):
1026
"""Copy the entire examples tree into a temp directory once per test

tests/test_packsmanager.py

Lines changed: 12 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import os
22
import re
3-
import shutil
43
import subprocess
54
from pathlib import Path
65

@@ -373,44 +372,23 @@ def test_copy_examples_force(example_cases, expected_paths, force):
373372

374373
@pytest.mark.parametrize("packs_to_install,expected", install_params)
375374
def test_print_packs_and_examples(
376-
packs_to_install, expected, example_cases, capsys
375+
packs_to_install, expected, example_cases, capsys, conda_env
377376
):
378-
case5dir = example_cases / "case5"
379-
env_dir = case5dir / "fake_env"
380-
req_dir = case5dir / "requirements" / "packs"
381-
# Handle Windows path format
382-
env_dir_str = env_dir.as_posix()
377+
env_dir_str = Path(conda_env).as_posix()
383378
shell = os.name == "nt"
384-
try:
379+
req_dir = example_cases / "case5" / "requirements" / "packs"
380+
for pack in packs_to_install:
381+
req_file = (req_dir / f"{pack}.txt").as_posix()
385382
subprocess.run(
386-
["conda", "create", "-y", "-p", env_dir_str],
383+
["conda", "install", "-y", "--file", req_file, "-p", env_dir_str],
387384
check=True,
388385
capture_output=True,
389386
text=True,
390387
shell=shell,
391388
)
392-
for pack in packs_to_install:
393-
req_file = (req_dir / f"{pack}.txt").as_posix()
394-
subprocess.run(
395-
[
396-
"conda",
397-
"install",
398-
"-y",
399-
"--file",
400-
req_file,
401-
"-p",
402-
env_dir_str,
403-
],
404-
check=True,
405-
capture_output=True,
406-
text=True,
407-
shell=shell,
408-
)
409-
pm = PacksManager(root_path=case5dir)
410-
pm.print_packs()
411-
pm.print_examples()
412-
captured = capsys.readouterr()
413-
actual = captured.out
414-
assert actual.strip() == expected.strip()
415-
finally:
416-
shutil.rmtree(env_dir, ignore_errors=True)
389+
pm = PacksManager(root_path=example_cases / "case5")
390+
pm.print_packs()
391+
pm.print_examples()
392+
captured = capsys.readouterr()
393+
actual = captured.out
394+
assert actual.strip() == expected.strip()

0 commit comments

Comments
 (0)