Skip to content

Commit 3ef8e13

Browse files
committed
Shared Examples Fixture
1 parent 7e83f3f commit 3ef8e13

File tree

3 files changed

+69
-47
lines changed

3 files changed

+69
-47
lines changed

tests/fixtures/example.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
"""Fixtures for the cmake plugin"""
2+
3+
import os
4+
from pathlib import Path
5+
from typing import cast
6+
7+
import pytest
8+
9+
10+
def _examples() -> list[Path]:
11+
"""Returns the examples directory"""
12+
matching_directories = []
13+
14+
for dirpath, _, filenames in os.walk('examples'):
15+
for filename in filenames:
16+
if filename == 'pyproject.toml':
17+
absolute_path = Path(dirpath).absolute()
18+
matching_directories.append(absolute_path)
19+
break
20+
21+
return matching_directories
22+
23+
24+
@pytest.fixture(
25+
name='example_directory',
26+
scope='session',
27+
params=_examples(),
28+
)
29+
def fixture_example_directory(
30+
request: pytest.FixtureRequest,
31+
) -> Path:
32+
"""Enumerates folders in the examples directory.
33+
34+
Parameterizes all directories with a pyproject.toml file within the examples directory.
35+
"""
36+
directory = cast(Path, request.param)
37+
return directory
Lines changed: 3 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,15 @@
11
"""Example folder tests"""
22

3-
from os import walk
43
from pathlib import Path
5-
from typing import cast
64

7-
import pytest
5+
pytest_plugins = ['tests.fixtures.example']
86

97

108
class TestExamples:
119
"""Tests to apply to all examples"""
1210

1311
@staticmethod
14-
def _examples() -> list[Path]:
15-
"""Returns the examples directory"""
16-
matching_directories = []
17-
18-
for dirpath, _, filenames in walk('examples'):
19-
for filename in filenames:
20-
if filename == 'pyproject.toml':
21-
matching_directories.append(Path(dirpath))
22-
break
23-
24-
return matching_directories
25-
26-
@staticmethod
27-
@pytest.fixture(
28-
name='example_directory',
29-
scope='session',
30-
params=_examples(),
31-
)
32-
def fixture_example_directory(
33-
request: pytest.FixtureRequest,
34-
) -> Path:
35-
"""Enumerates folders in the examples directory.
36-
37-
Parameterizes all directories with a pyproject.toml file within the examples directory.
38-
"""
39-
directory = cast(Path, request.param)
40-
return directory
41-
42-
@staticmethod
43-
def test_example(example_directory: Path) -> None:
44-
"""Tests the examples"""
12+
def test_example_directory(example_directory: Path) -> None:
13+
"""Verify that the fixture is returning the right data"""
4514
assert example_directory.is_dir()
4615
assert (example_directory / 'pyproject.toml').is_file()

tests/unit/test_console.py

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,51 @@
11
"""Tests the typer interface type"""
22

3+
import shutil
4+
from pathlib import Path
5+
36
from typer.testing import CliRunner
47

58
from cppython.console.entry import app
69

710
runner = CliRunner()
11+
pytest_plugins = ['tests.fixtures.example']
812

913

1014
class TestConsole:
11-
"""Various tests for the typer interface"""
15+
"""Various that all the examples are accessible to cppython"""
1216

1317
@staticmethod
14-
def test_info() -> None:
18+
def test_info(example_directory: Path) -> None:
1519
"""Verifies that the info command functions with CPPython hooks"""
16-
result = runner.invoke(app, ['info'])
17-
assert result.exit_code == 0
20+
with runner.isolated_filesystem() as temp_directory:
21+
shutil.copytree(example_directory, temp_directory, dirs_exist_ok=True)
22+
23+
result = runner.invoke(app, ['info'])
24+
assert result.exit_code == 0
1825

1926
@staticmethod
20-
def test_list() -> None:
27+
def test_list(example_directory: Path) -> None:
2128
"""Verifies that the list command functions with CPPython hooks"""
22-
result = runner.invoke(app, ['list'])
23-
assert result.exit_code == 0
29+
with runner.isolated_filesystem() as temp_directory:
30+
shutil.copytree(example_directory, temp_directory, dirs_exist_ok=True)
31+
32+
result = runner.invoke(app, ['list'])
33+
assert result.exit_code == 0
2434

2535
@staticmethod
26-
def test_update() -> None:
36+
def test_update(example_directory: Path) -> None:
2737
"""Verifies that the update command functions with CPPython hooks"""
28-
result = runner.invoke(app, ['update'])
29-
assert result.exit_code == 0
38+
with runner.isolated_filesystem() as temp_directory:
39+
shutil.copytree(example_directory, temp_directory, dirs_exist_ok=True)
40+
41+
result = runner.invoke(app, ['update'])
42+
assert result.exit_code == 0
3043

3144
@staticmethod
32-
def test_install() -> None:
45+
def test_install(example_directory: Path) -> None:
3346
"""Verifies that the install command functions with CPPython hooks"""
34-
result = runner.invoke(app, ['install'])
35-
assert result.exit_code == 0
47+
with runner.isolated_filesystem() as temp_directory:
48+
shutil.copytree(example_directory, temp_directory, dirs_exist_ok=True)
49+
50+
result = runner.invoke(app, ['install'])
51+
assert result.exit_code == 0

0 commit comments

Comments
 (0)