From a73e4f5e7e22dd644fcc5955c10215d3212b5047 Mon Sep 17 00:00:00 2001 From: Evgeni Burovski Date: Tue, 20 Feb 2024 19:29:30 +0300 Subject: [PATCH 1/3] MAINT: plugin: run tests in a tempdir, stop blank filtering DeprecationWarnings --- scpdt/plugin.py | 17 ++++++++---- scpdt/tests/test_pytest_configuration.py | 35 ++++++++++++++++-------- scpdt/util.py | 2 +- 3 files changed, 36 insertions(+), 18 deletions(-) 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/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 From 85e829b0fb755822879cddcbef0e091767347de4 Mon Sep 17 00:00:00 2001 From: Sheila-nk Date: Mon, 26 Feb 2024 15:13:08 +0300 Subject: [PATCH 2/3] change local file path --- scpdt/tests/local_file_cases.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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'] From 19f35342fda6d358d0c401c7cd8fdfdf7a9b1d7c Mon Sep 17 00:00:00 2001 From: Evgeni Burovski Date: Mon, 26 Feb 2024 17:14:00 +0300 Subject: [PATCH 3/3] MAINT: rm dead code, reformat --- scpdt/plugin.py | 52 ------------------------ scpdt/tests/local_file_cases.py | 12 +++--- scpdt/tests/test_pytest_configuration.py | 49 +--------------------- 3 files changed, 8 insertions(+), 105 deletions(-) diff --git a/scpdt/plugin.py b/scpdt/plugin.py index 3b857ce..e3c39e7 100644 --- a/scpdt/plugin.py +++ b/scpdt/plugin.py @@ -17,10 +17,6 @@ from scpdt.frontend import find_doctests -# XXX: unused global? -copied_files = [] - - def pytest_configure(config): """ Perform initial configuration for the pytest plugin. @@ -34,20 +30,6 @@ def pytest_configure(config): pydoctest.DoctestTextfile = DTTextfile -def pytest_unconfigure(config): - """ - Called before exiting the test process. - """ - - # Delete all locally copied files in the current working directory - if copied_files: - try: - for filepath in copied_files: - os.remove(filepath) - except FileNotFoundError: - pass - - def pytest_ignore_collect(collection_path, config): """ Determine whether to ignore the specified collection path. @@ -106,32 +88,6 @@ def pytest_collection_modifyitems(config, items): # Replace the original list of test items with the unique ones items[:] = unique_items - - -# XXX: unused? -def copy_local_files(local_resources, destination_dir): - """ - Copy necessary local files for doctests to the current working directory. - - This function copies files specified in the `local_resources` attribute of a DTConfig instance - to the specified `destination_dir`. - - Args: - local_resources (dict): A dictionary of resources to be copied. - destination_dir (str): The destination directory where files will be copied. - - Returns: - list: A list of paths to the copied files. - """ - for value in local_resources.values(): - for filepath in value: - basename = os.path.basename(filepath) - dest_path = os.path.join(destination_dir, basename) - - if not os.path.exists(dest_path): - shutil.copy(filepath, destination_dir) - copied_files.append(dest_path) - return copied_files class DTModule(DoctestModule): @@ -166,10 +122,6 @@ def collect(self): else: 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()) - optionflags = dt_config.optionflags # Plug in the custom runner: `PytestDTRunner` @@ -210,10 +162,6 @@ def collect(self): optionflags = pydoctest.get_optionflags(self) - # 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()) - # Plug in the custom runner: `PytestDTRunner` runner = _get_runner(self.config, verbose=False, diff --git a/scpdt/tests/local_file_cases.py b/scpdt/tests/local_file_cases.py index 8508213..f3584af 100644 --- a/scpdt/tests/local_file_cases.py +++ b/scpdt/tests/local_file_cases.py @@ -1,15 +1,15 @@ from scpdt.conftest import dt_config - # Specify local files required by doctests -dt_config.local_resources = {'scpdt.tests.local_file_cases.local_files': - ['local_file.txt'], - 'scpdt.tests.local_file_cases.sio': - ['octave_a.mat'] - } +dt_config.local_resources = { + 'scpdt.tests.local_file_cases.local_files': ['local_file.txt'], + 'scpdt.tests.local_file_cases.sio': ['octave_a.mat'] +} + __all__ = ['local_files', 'sio'] + def local_files(): """ A doctest that tries to read a local file diff --git a/scpdt/tests/test_pytest_configuration.py b/scpdt/tests/test_pytest_configuration.py index 91366ba..797501d 100644 --- a/scpdt/tests/test_pytest_configuration.py +++ b/scpdt/tests/test_pytest_configuration.py @@ -1,42 +1,14 @@ import pytest -import os from pathlib import PosixPath, Path from . import module_cases, failure_cases, failure_cases_2, stopwords_cases, local_file_cases -from scpdt.plugin import copy_local_files from scpdt.conftest import dt_config -pytest_plugins = ['pytester'] - -''' -@pytest.fixture(autouse=True) -def copy_files(): - """ - Copy necessary local files for doctests to the temporary directory used by pytester. - The files to be copied are defined by the `local_resources` attribute of a DTConfig instance. - When testing is done, all copied files are deleted. - """ - try: - dirname = os.path.dirname(Path(__file__)) - - # Update the file path of each filename - for value in dt_config.local_resources.values(): - for i, path in enumerate(value): - value[i] = os.path.join(dirname, os.path.basename(path)) +# XXX: this is a bit hacky and repetetive. Can rework? - # Copy the files - copied_files = copy_local_files(dt_config.local_resources, os.getcwd()) - yield copied_files +pytest_plugins = ['pytester'] - finally: - # Perform clean-up - for filepath in copied_files: - try: - os.remove(filepath) - except FileNotFoundError: - pass -''' def test_module_cases(pytester): """Test that pytest uses the DTChecker for doctests.""" @@ -63,27 +35,10 @@ def test_stopword_cases(pytester): assert result.ret == pytest.ExitCode.OK -#@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