Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change values of local resources config variable to file names #127

Closed
wants to merge 10 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/pip.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ jobs:
- name: Test pytest plugin
# This test will fail in a venv where scpdt has not been installed and the plugin has not been activated
run: |
#ls -l /home/runner/work/scpdt/scpdt/scpdt/tests/scpdt/tests/
pwd
ls -l
test_files=("scpdt/tests/module_cases.py" "scpdt/tests/stopwords_cases.py" "scpdt/tests/local_file_cases.py")
for file in "${test_files[@]}"; do
python -m pytest "${file}" --doctest-modules
Expand Down
10 changes: 4 additions & 6 deletions scpdt/impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,8 +334,7 @@ def __init__(self, checker=None, verbose=None, optionflags=None, config=None):
self.nameerror_after_exception = config.nameerror_after_exception
if optionflags is None:
optionflags = config.optionflags
doctest.DocTestRunner.__init__(self, checker=checker, verbose=verbose,
optionflags=optionflags)
super().__init__(checker=checker, verbose=verbose, optionflags=optionflags)

def _report_item_name(self, out, item_name, new_line=False):
if item_name is not None:
Expand All @@ -344,12 +343,12 @@ def _report_item_name(self, out, item_name, new_line=False):
out("\n")

def report_start(self, out, test, example):
return doctest.DocTestRunner.report_start(self, out, test, example)
return super().report_start(out, test, example)

def report_success(self, out, test, example, got):
if self._verbose:
self._report_item_name(out, test.name, new_line=True)
return doctest.DocTestRunner.report_success(self, out, test, example, got)
return super().report_success(out, test, example, got)

def report_unexpected_exception(self, out, test, example, exc_info):
if not self.nameerror_after_exception:
Expand All @@ -367,8 +366,7 @@ def report_unexpected_exception(self, out, test, example, exc_info):

def report_failure(self, out, test, example, got):
self._report_item_name(out, test.name)
return doctest.DocTestRunner.report_failure(self, out, test,
example, got)
return super().report_failure(out, test, example, got)

def get_history(self):
"""Return a dict with names of items which were run.
Expand Down
17 changes: 12 additions & 5 deletions scpdt/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@

from scpdt.impl import DTChecker, DTParser, DebugDTRunner
from scpdt.conftest import dt_config
from scpdt.util import np_errstate, matplotlib_make_nongui
from scpdt.util import np_errstate, matplotlib_make_nongui, temp_cwd
from scpdt.frontend import find_doctests


# XXX: unused global?
copied_files = []


Expand Down Expand Up @@ -107,6 +108,7 @@ def pytest_collection_modifyitems(config, items):
items[:] = unique_items


# XXX: unused?
def copy_local_files(local_resources, destination_dir):
"""
Copy necessary local files for doctests to the current working directory.
Expand Down Expand Up @@ -165,8 +167,8 @@ def collect(self):
raise

# Copy local files specified by the `local_resources` attribute to the current working directory
if self.config.dt_config.local_resources:
copy_local_files(self.config.dt_config.local_resources, os.getcwd())
# if self.config.dt_config.local_resources:
# copy_local_files(self.config.dt_config.local_resources, os.getcwd())

optionflags = dt_config.optionflags

Expand Down Expand Up @@ -254,10 +256,15 @@ def run(self, test, compileflags=None, out=None, clear_globs=False):
*unless* the `mpl()` context mgr has a chance to filter them out
*before* they become errors in `config.user_context_mgr()`.
"""
dt_config = config.dt_config


with np_errstate():
with config.dt_config.user_context_mgr(test):
with dt_config.user_context_mgr(test):
with matplotlib_make_nongui():
super().run(test, compileflags=compileflags, out=out, clear_globs=clear_globs)
# XXX: local_resourses needed? they seem to be, w/o pytest
with temp_cwd(test, dt_config.local_resources):
super().run(test, compileflags=compileflags, out=out, clear_globs=clear_globs)

"""
Almost verbatim copy of `_pytest.doctest.PytestDoctestRunner` except we utilize
Expand Down
4 changes: 2 additions & 2 deletions scpdt/tests/local_file_cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

# Specify local files required by doctests
dt_config.local_resources = {'scpdt.tests.local_file_cases.local_files':
['scpdt/tests/local_file.txt'],
['local_file.txt'],
'scpdt.tests.local_file_cases.sio':
['scpdt/tests/octave_a.mat']
['octave_a.mat']
}

__all__ = ['local_files', 'sio']
Expand Down
35 changes: 23 additions & 12 deletions scpdt/tests/test_pytest_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

pytest_plugins = ['pytester']


'''
@pytest.fixture(autouse=True)
def copy_files():
"""
Expand Down Expand Up @@ -36,13 +36,10 @@ def copy_files():
os.remove(filepath)
except FileNotFoundError:
pass


"""
Test that pytest uses the DTChecker for doctests
"""
'''

def test_module_cases(pytester):
"""Test that pytest uses the DTChecker for doctests."""
path_str = module_cases.__file__
python_file = PosixPath(path_str)
result = pytester.inline_run(python_file, "--doctest-modules")
Expand All @@ -58,21 +55,35 @@ def test_failure_cases(pytester):
assert result.ret == pytest.ExitCode.TESTS_FAILED


"""
Test that pytest uses the DTParser for doctests
"""
def test_stopword_cases(pytester):
"""Test that pytest uses the DTParser for doctests."""
path_str = stopwords_cases.__file__
python_file = PosixPath(path_str)
result = pytester.inline_run(python_file, "--doctest-modules")
assert result.ret == pytest.ExitCode.OK


"""
Test that local files are found for use in doctests
"""
#@pytest.mark.xfail
def test_local_file_cases(pytester):
"""Test that local files are found for use in doctests.

XXX: this one fails because nobody told pytest how to find those local files.
cf test_testmod.py::TestLocalFiles
"""
path_str = local_file_cases.__file__
python_file = PosixPath(path_str)

# pytester.makeconftest(
# """
# import pytest
# from scpdt.conftest import dt_config
#
# dt_config.local_resources = {'scpdt.tests.local_file_cases.local_files':
# ['local_file.txt']}
# """
# )

# breakpoint()

result = pytester.inline_run(python_file, "--doctest-modules")
assert result.ret == pytest.ExitCode.OK
2 changes: 1 addition & 1 deletion scpdt/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def temp_cwd(test, local_resources=None):
# local files requested; copy the files
path, _ = os.path.split(test.filename)
for fname in local_resources[test.name]:
shutil.copy(os.path.join(path, fname), tmpdir)
shutil.copy(os.path.join(path, fname), tmpdir)
try:
os.chdir(tmpdir)
yield tmpdir
Expand Down
Loading