Skip to content

Commit 78f009c

Browse files
committed
Example Runner
1 parent 8ada5d6 commit 78f009c

File tree

4 files changed

+63
-7
lines changed

4 files changed

+63
-7
lines changed

tests/fixtures/cli.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
"""Fixtures for interfacing with the CLI."""
2+
3+
import pytest
4+
from typer.testing import CliRunner
5+
6+
7+
@pytest.fixture(
8+
name='typer_runner',
9+
scope='session',
10+
)
11+
def fixture_typer_runner() -> CliRunner:
12+
"""Returns a runner setup for the CPPython interface"""
13+
runner = CliRunner()
14+
15+
return runner

tests/fixtures/example.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
"""Fixtures for the cmake plugin"""
22

33
import os
4+
import shutil
5+
from collections.abc import Generator
46
from pathlib import Path
57
from typing import cast
68

79
import pytest
10+
from typer.testing import CliRunner
11+
12+
pytest_plugins = ['tests.fixtures.cli']
813

914

1015
def _examples() -> list[Path]:
@@ -35,3 +40,36 @@ def fixture_example_directory(
3540
"""
3641
directory = cast(Path, request.param)
3742
return directory
43+
44+
45+
@pytest.fixture(
46+
name='example_runner',
47+
)
48+
def fixture_example_runner(
49+
request: pytest.FixtureRequest, typer_runner: CliRunner, tmp_path: Path
50+
) -> Generator[CliRunner]:
51+
"""TODO"""
52+
prev_cwd = os.getcwd()
53+
54+
# Get the root directory of the project
55+
root_directory = Path(__file__).parent.parent.parent.absolute()
56+
57+
# Remove the file extension and required 'test_' prefix from the test's file name
58+
file_name = request.node.fspath.basename[:-3].replace('test_', '')
59+
60+
# Get the test function name and remove the required 'test_' prefix
61+
test_name = request.node.name.replace('test_', '')
62+
63+
# Generate the example path from the pytest file and test name
64+
example_path = root_directory / 'examples' / file_name / test_name
65+
66+
try:
67+
with typer_runner.isolated_filesystem(tmp_path) as temp_directory:
68+
os.chdir(temp_directory)
69+
70+
# Copy the example directory to the temporary directory
71+
shutil.copytree(example_path, Path(), dirs_exist_ok=True)
72+
73+
yield typer_runner
74+
finally:
75+
os.chdir(prev_cwd)

tests/integration/examples/test_examples.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
pytest_plugins = ['tests.fixtures.example']
99

1010

11-
class TestSetup:
11+
class TestExamples:
1212
"""Verification that the example directory is setup correctly"""
1313

1414
@staticmethod
Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1-
"""Example folder tests.
1+
"""TODO"""
22

3-
All examples can be run with the CPPython entry-point, and we use the examples as the test data for the CLI.
4-
"""
3+
from typer.testing import CliRunner
54

6-
pytest_plugins = ['tests.fixtures.cmake']
5+
pytest_plugins = ['tests.fixtures.example']
76

87

9-
class TestSetup:
10-
"""Verification that the example directory is setup correctly"""
8+
class TestPdmVcpkgCMake:
9+
"""TODO"""
10+
11+
@staticmethod
12+
def test_simple(example_runner: CliRunner) -> None:
13+
"""Verify that the fixture is returning the right data"""

0 commit comments

Comments
 (0)