Skip to content

Commit e19476f

Browse files
committed
Update Tests with Fresh Py Environment
1 parent b27046d commit e19476f

File tree

3 files changed

+44
-35
lines changed

3 files changed

+44
-35
lines changed

tests/fixtures/cli.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
"""Fixtures for interfacing with the CLI."""
22

3+
import os
4+
import platform
5+
36
import pytest
47
from typer.testing import CliRunner
58

@@ -13,3 +16,21 @@ def fixture_typer_runner() -> CliRunner:
1316
runner = CliRunner()
1417

1518
return runner
19+
20+
21+
@pytest.fixture(
22+
name='fresh_environment',
23+
scope='session',
24+
)
25+
def fixture_fresh_environment() -> dict[str, str]:
26+
"""Create a fresh environment for subprocess calls."""
27+
# Start with a minimal environment
28+
new_env = {}
29+
30+
# Copy only variables you need
31+
if platform.system() == 'Windows':
32+
new_env['SystemRoot'] = os.environ['SystemRoot'] # noqa: SIM112
33+
34+
# Provide a PATH that doesn't contain venv references
35+
new_env['PATH'] = os.environ['PATH']
36+
return new_env

tests/integration/examples/test_conan_cmake.py

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,50 +9,44 @@
99

1010
from typer.testing import CliRunner
1111

12-
from cppython.console.entry import app
13-
1412
pytest_plugins = ['tests.fixtures.example']
1513

1614

1715
class TestConanCMake:
1816
"""Test project variation of conan and CMake"""
1917

2018
@staticmethod
21-
def test_simple(example_runner: CliRunner) -> None:
19+
def test_simple(example_runner: CliRunner, fresh_environment: dict[str, str]) -> None:
2220
"""Simple project"""
23-
result = example_runner.invoke(
24-
app,
25-
[
26-
'install',
27-
],
28-
)
21+
# By nature of running the test, we require PDM to develop the project and so it will be installed
22+
result = subprocess.run(['pdm', 'install'], capture_output=True, text=True, env=fresh_environment, check=False)
2923

30-
assert result.exit_code == 0, result.output
24+
assert result.returncode == 0, f'PDM install failed: {result.stderr}'
3125

3226
# Run the CMake configuration command
33-
cmake_result = subprocess.run(['cmake', '--preset=default'], capture_output=True, text=True, check=False)
27+
result = subprocess.run(
28+
['cmake', '--preset=default'], capture_output=True, text=True, env=fresh_environment, check=False
29+
)
3430

35-
assert cmake_result.returncode == 0, f'CMake configuration failed: {cmake_result.stderr}'
31+
assert result.returncode == 0, f'Cmake failed: {result.stderr}'
3632

3733
# Verify that the build directory contains the expected files
3834
assert (Path('build') / 'CMakeCache.txt').exists(), 'build/CMakeCache.txt not found'
3935

4036
@staticmethod
41-
def test_inject(example_runner: CliRunner) -> None:
37+
def test_inject(example_runner: CliRunner, fresh_environment: dict[str, str]) -> None:
4238
"""Inject"""
43-
result = example_runner.invoke(
44-
app,
45-
[
46-
'install',
47-
],
48-
)
39+
# By nature of running the test, we require PDM to develop the project and so it will be installed
40+
result = subprocess.run(['pdm', 'install'], capture_output=True, text=True, env=fresh_environment, check=False)
4941

50-
assert result.exit_code == 0, result.output
42+
assert result.returncode == 0, f'PDM install failed: {result.stderr}'
5143

5244
# Run the CMake configuration command
53-
cmake_result = subprocess.run(['cmake', '--preset=default'], capture_output=True, text=True, check=False)
45+
result = subprocess.run(
46+
['cmake', '--preset=default'], capture_output=True, text=True, env=fresh_environment, check=False
47+
)
5448

55-
assert cmake_result.returncode == 0, f'CMake configuration failed: {cmake_result.stderr}'
49+
assert result.returncode == 0, f'Cmake failed: {result.stderr}'
5650

5751
# Verify that the build directory contains the expected files
5852
assert (Path('build') / 'CMakeCache.txt').exists(), 'build/CMakeCache.txt not found'

tests/integration/examples/test_vcpkg_cmake.py

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010
import pytest
1111
from typer.testing import CliRunner
1212

13-
from cppython.console.entry import app
14-
1513
pytest_plugins = ['tests.fixtures.example']
1614

1715

@@ -20,23 +18,19 @@ class TestVcpkgCMake:
2018

2119
@staticmethod
2220
@pytest.mark.skip(reason='TODO')
23-
def test_simple(example_runner: CliRunner) -> None:
21+
def test_simple(example_runner: CliRunner, fresh_environment: dict[str, str]) -> None:
2422
"""Simple project"""
25-
result = example_runner.invoke(
26-
app,
27-
[
28-
'install',
29-
],
30-
)
23+
# By nature of running the test, we require PDM to develop the project and so it will be installed
24+
result = subprocess.run(['pdm', 'install'], capture_output=True, text=True, env=fresh_environment, check=False)
3125

32-
assert result.exit_code == 0, result.output
26+
assert result.returncode == 0, f'PDM install failed: {result.stderr}'
3327

3428
# Run the CMake configuration command
35-
cmake_result = subprocess.run(
36-
['cmake', '--preset=default', '-B', 'build'], capture_output=True, text=True, check=False
29+
result = subprocess.run(
30+
['cmake', '--preset=default'], capture_output=True, text=True, env=fresh_environment, check=False
3731
)
3832

39-
assert cmake_result.returncode == 0, f'CMake configuration failed: {cmake_result.stderr}'
33+
assert result.returncode == 0, f'Cmake failed: {result.stderr}'
4034

4135
# Verify that the build directory contains the expected files
4236
assert (Path('build') / 'CMakeCache.txt').exists(), 'build/CMakeCache.txt not found'

0 commit comments

Comments
 (0)