Skip to content

Commit 27b6521

Browse files
authored
Merge pull request #111 from ev-br/run_after_collection
BUG: run tests in a tempdir, copy needed local files
2 parents 33e85a4 + 19f3534 commit 27b6521

File tree

4 files changed

+21
-100
lines changed

4 files changed

+21
-100
lines changed

scpdt/plugin.py

Lines changed: 8 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,10 @@
1313

1414
from scpdt.impl import DTChecker, DTParser, DebugDTRunner
1515
from scpdt.conftest import dt_config
16-
from scpdt.util import np_errstate, matplotlib_make_nongui
16+
from scpdt.util import np_errstate, matplotlib_make_nongui, temp_cwd
1717
from scpdt.frontend import find_doctests
1818

1919

20-
copied_files = []
21-
22-
2320
def pytest_configure(config):
2421
"""
2522
Perform initial configuration for the pytest plugin.
@@ -33,20 +30,6 @@ def pytest_configure(config):
3330
pydoctest.DoctestTextfile = DTTextfile
3431

3532

36-
def pytest_unconfigure(config):
37-
"""
38-
Called before exiting the test process.
39-
"""
40-
41-
# Delete all locally copied files in the current working directory
42-
if copied_files:
43-
try:
44-
for filepath in copied_files:
45-
os.remove(filepath)
46-
except FileNotFoundError:
47-
pass
48-
49-
5033
def pytest_ignore_collect(collection_path, config):
5134
"""
5235
Determine whether to ignore the specified collection path.
@@ -105,31 +88,6 @@ def pytest_collection_modifyitems(config, items):
10588

10689
# Replace the original list of test items with the unique ones
10790
items[:] = unique_items
108-
109-
110-
def copy_local_files(local_resources, destination_dir):
111-
"""
112-
Copy necessary local files for doctests to the current working directory.
113-
114-
This function copies files specified in the `local_resources` attribute of a DTConfig instance
115-
to the specified `destination_dir`.
116-
117-
Args:
118-
local_resources (dict): A dictionary of resources to be copied.
119-
destination_dir (str): The destination directory where files will be copied.
120-
121-
Returns:
122-
list: A list of paths to the copied files.
123-
"""
124-
for value in local_resources.values():
125-
for filepath in value:
126-
basename = os.path.basename(filepath)
127-
dest_path = os.path.join(destination_dir, basename)
128-
129-
if not os.path.exists(dest_path):
130-
shutil.copy(filepath, destination_dir)
131-
copied_files.append(dest_path)
132-
return copied_files
13391

13492

13593
class DTModule(DoctestModule):
@@ -164,10 +122,6 @@ def collect(self):
164122
else:
165123
raise
166124

167-
# Copy local files specified by the `local_resources` attribute to the current working directory
168-
if self.config.dt_config.local_resources:
169-
copy_local_files(self.config.dt_config.local_resources, os.getcwd())
170-
171125
optionflags = dt_config.optionflags
172126

173127
# Plug in the custom runner: `PytestDTRunner`
@@ -208,10 +162,6 @@ def collect(self):
208162

209163
optionflags = pydoctest.get_optionflags(self)
210164

211-
# Copy local files specified by the `local_resources` attribute to the current working directory
212-
if self.config.dt_config.local_resources:
213-
copy_local_files(self.config.dt_config.local_resources, os.getcwd())
214-
215165
# Plug in the custom runner: `PytestDTRunner`
216166
runner = _get_runner(self.config,
217167
verbose=False,
@@ -254,10 +204,15 @@ def run(self, test, compileflags=None, out=None, clear_globs=False):
254204
*unless* the `mpl()` context mgr has a chance to filter them out
255205
*before* they become errors in `config.user_context_mgr()`.
256206
"""
207+
dt_config = config.dt_config
208+
209+
257210
with np_errstate():
258-
with config.dt_config.user_context_mgr(test):
211+
with dt_config.user_context_mgr(test):
259212
with matplotlib_make_nongui():
260-
super().run(test, compileflags=compileflags, out=out, clear_globs=clear_globs)
213+
# XXX: local_resourses needed? they seem to be, w/o pytest
214+
with temp_cwd(test, dt_config.local_resources):
215+
super().run(test, compileflags=compileflags, out=out, clear_globs=clear_globs)
261216

262217
"""
263218
Almost verbatim copy of `_pytest.doctest.PytestDoctestRunner` except we utilize

scpdt/tests/local_file_cases.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
from scpdt.conftest import dt_config
22

3-
43
# Specify local files required by doctests
5-
dt_config.local_resources = {'scpdt.tests.local_file_cases.local_files':
6-
['scpdt/tests/local_file.txt'],
7-
'scpdt.tests.local_file_cases.sio':
8-
['scpdt/tests/octave_a.mat']
9-
}
4+
dt_config.local_resources = {
5+
'scpdt.tests.local_file_cases.local_files': ['local_file.txt'],
6+
'scpdt.tests.local_file_cases.sio': ['octave_a.mat']
7+
}
8+
109

1110
__all__ = ['local_files', 'sio']
1211

12+
1313
def local_files():
1414
"""
1515
A doctest that tries to read a local file

scpdt/tests/test_pytest_configuration.py

Lines changed: 6 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,17 @@
11
import pytest
2-
import os
32
from pathlib import PosixPath, Path
43

54
from . import module_cases, failure_cases, failure_cases_2, stopwords_cases, local_file_cases
6-
from scpdt.plugin import copy_local_files
75
from scpdt.conftest import dt_config
86

9-
pytest_plugins = ['pytester']
10-
11-
12-
@pytest.fixture(autouse=True)
13-
def copy_files():
14-
"""
15-
Copy necessary local files for doctests to the temporary directory used by pytester.
16-
The files to be copied are defined by the `local_resources` attribute of a DTConfig instance.
17-
When testing is done, all copied files are deleted.
18-
"""
19-
try:
20-
dirname = os.path.dirname(Path(__file__))
7+
# XXX: this is a bit hacky and repetetive. Can rework?
218

22-
# Update the file path of each filename
23-
for value in dt_config.local_resources.values():
24-
for i, path in enumerate(value):
25-
value[i] = os.path.join(dirname, os.path.basename(path))
26-
27-
# Copy the files
28-
copied_files = copy_local_files(dt_config.local_resources, os.getcwd())
29-
30-
yield copied_files
31-
32-
finally:
33-
# Perform clean-up
34-
for filepath in copied_files:
35-
try:
36-
os.remove(filepath)
37-
except FileNotFoundError:
38-
pass
399

10+
pytest_plugins = ['pytester']
4011

41-
"""
42-
Test that pytest uses the DTChecker for doctests
43-
"""
4412

4513
def test_module_cases(pytester):
14+
"""Test that pytest uses the DTChecker for doctests."""
4615
path_str = module_cases.__file__
4716
python_file = PosixPath(path_str)
4817
result = pytester.inline_run(python_file, "--doctest-modules")
@@ -58,20 +27,17 @@ def test_failure_cases(pytester):
5827
assert result.ret == pytest.ExitCode.TESTS_FAILED
5928

6029

61-
"""
62-
Test that pytest uses the DTParser for doctests
63-
"""
6430
def test_stopword_cases(pytester):
31+
"""Test that pytest uses the DTParser for doctests."""
6532
path_str = stopwords_cases.__file__
6633
python_file = PosixPath(path_str)
6734
result = pytester.inline_run(python_file, "--doctest-modules")
6835
assert result.ret == pytest.ExitCode.OK
6936

7037

71-
"""
72-
Test that local files are found for use in doctests
73-
"""
7438
def test_local_file_cases(pytester):
39+
"""Test that local files are found for use in doctests.
40+
"""
7541
path_str = local_file_cases.__file__
7642
python_file = PosixPath(path_str)
7743
result = pytester.inline_run(python_file, "--doctest-modules")

scpdt/util.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def temp_cwd(test, local_resources=None):
5858
# local files requested; copy the files
5959
path, _ = os.path.split(test.filename)
6060
for fname in local_resources[test.name]:
61-
shutil.copy(os.path.join(path, fname), tmpdir)
61+
shutil.copy(os.path.join(path, fname), tmpdir)
6262
try:
6363
os.chdir(tmpdir)
6464
yield tmpdir

0 commit comments

Comments
 (0)