diff --git a/.github/workflows/pip.yml b/.github/workflows/pip.yml index 0da483c..55852ed 100644 --- a/.github/workflows/pip.yml +++ b/.github/workflows/pip.yml @@ -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 diff --git a/scpdt/impl.py b/scpdt/impl.py index 9379b67..5a7e015 100644 --- a/scpdt/impl.py +++ b/scpdt/impl.py @@ -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: @@ -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: @@ -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. diff --git a/scpdt/plugin.py b/scpdt/plugin.py index bdd0994..3b857ce 100644 --- a/scpdt/plugin.py +++ b/scpdt/plugin.py @@ -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 = [] @@ -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. @@ -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 @@ -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 diff --git a/scpdt/tests/local_file_cases.py b/scpdt/tests/local_file_cases.py index cb45aed..8508213 100644 --- a/scpdt/tests/local_file_cases.py +++ b/scpdt/tests/local_file_cases.py @@ -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'] diff --git a/scpdt/tests/test_pytest_configuration.py b/scpdt/tests/test_pytest_configuration.py index d43f416..91366ba 100644 --- a/scpdt/tests/test_pytest_configuration.py +++ b/scpdt/tests/test_pytest_configuration.py @@ -8,7 +8,7 @@ pytest_plugins = ['pytester'] - +''' @pytest.fixture(autouse=True) def copy_files(): """ @@ -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") @@ -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 diff --git a/scpdt/util.py b/scpdt/util.py index c82eb93..2eeef60 100644 --- a/scpdt/util.py +++ b/scpdt/util.py @@ -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