From 7c4db269af55b000e81b162938e29f5e8f4260ba Mon Sep 17 00:00:00 2001 From: Adam Turner <9087854+AA-Turner@users.noreply.github.com> Date: Tue, 23 Jul 2024 16:55:21 +0100 Subject: [PATCH] Normalise ``sys.path`` manipulation in tests (#12665) --- tests/js/roots/titles/conf.py | 4 ++-- tests/roots/test-add_enumerable_node/conf.py | 5 +++-- .../conf.py | 6 +++--- tests/roots/test-add_source_parser/conf.py | 5 ++--- tests/roots/test-api-set-translator/conf.py | 4 ++-- tests/roots/test-api-set-translator/nonext/conf.py | 4 ++-- tests/roots/test-autosummary/conf.py | 4 ++-- tests/roots/test-environment-record-dependencies/conf.py | 5 +++-- tests/roots/test-ext-autodoc/conf.py | 4 ++-- tests/roots/test-ext-autosummary-filename-map/conf.py | 4 ++-- tests/roots/test-ext-autosummary-import_cycle/conf.py | 4 ++-- tests/roots/test-ext-autosummary-imported_members/conf.py | 4 ++-- tests/roots/test-ext-autosummary-mock_imports/conf.py | 4 ++-- tests/roots/test-ext-autosummary-module_all/conf.py | 4 ++-- tests/roots/test-ext-autosummary-module_prefix/conf.py | 4 ++-- tests/roots/test-ext-autosummary-recursive/conf.py | 4 ++-- tests/roots/test-ext-autosummary-skip-member/conf.py | 5 ++--- tests/roots/test-ext-autosummary-template/conf.py | 5 ++--- tests/roots/test-ext-autosummary/conf.py | 4 ++-- tests/roots/test-ext-coverage/conf.py | 4 ++-- tests/roots/test-ext-doctest-with-autodoc/conf.py | 4 ++-- tests/roots/test-ext-imgmockconverter/conf.py | 5 +++-- tests/roots/test-ext-inheritance_diagram/conf.py | 4 ++-- tests/roots/test-ext-napoleon-paramtype/conf.py | 7 ++++--- tests/roots/test-ext-napoleon/conf.py | 5 +++-- tests/roots/test-ext-viewcode/conf.py | 7 +++---- tests/roots/test-extensions/conf.py | 4 ++-- tests/roots/test-inheritance/conf.py | 4 ++-- tests/roots/test-latex-labels-before-module/conf.py | 4 ++-- tests/roots/test-prolog/conf.py | 5 ++--- tests/roots/test-root/conf.py | 6 +++--- tests/roots/test-util-copyasset_overwrite/conf.py | 5 +++-- tests/roots/test-warnings/conf.py | 4 ++-- tests/test_application.py | 4 ++-- tests/test_extensions/test_ext_inheritance_diagram.py | 5 +++-- tests/test_pycode/test_pycode.py | 6 +++--- tests/test_writers/test_api_translator.py | 6 +++--- 37 files changed, 87 insertions(+), 85 deletions(-) diff --git a/tests/js/roots/titles/conf.py b/tests/js/roots/titles/conf.py index e5f6bb97a2..2178c556e2 100644 --- a/tests/js/roots/titles/conf.py +++ b/tests/js/roots/titles/conf.py @@ -1,6 +1,6 @@ -import os import sys +from pathlib import Path -sys.path.insert(0, os.path.abspath('.')) +sys.path.insert(0, str(Path.cwd().resolve())) extensions = ['sphinx.ext.autodoc'] diff --git a/tests/roots/test-add_enumerable_node/conf.py b/tests/roots/test-add_enumerable_node/conf.py index 8c3a568a51..11f749dc24 100644 --- a/tests/roots/test-add_enumerable_node/conf.py +++ b/tests/roots/test-add_enumerable_node/conf.py @@ -1,7 +1,8 @@ -import os import sys +from pathlib import Path + +sys.path.insert(0, str(Path.cwd().resolve())) -sys.path.insert(0, os.path.abspath('.')) extensions = ['enumerable_node'] numfig = True diff --git a/tests/roots/test-add_source_parser-conflicts-with-users-setting/conf.py b/tests/roots/test-add_source_parser-conflicts-with-users-setting/conf.py index 5e57901692..5f979c7091 100644 --- a/tests/roots/test-add_source_parser-conflicts-with-users-setting/conf.py +++ b/tests/roots/test-add_source_parser-conflicts-with-users-setting/conf.py @@ -1,10 +1,10 @@ -import os import sys +from pathlib import Path + +sys.path.insert(0, str(Path.cwd().resolve())) from docutils.parsers import Parser -sys.path.insert(0, os.path.abspath('.')) - class DummyTestParser(Parser): supported = ('dummy',) diff --git a/tests/roots/test-add_source_parser/conf.py b/tests/roots/test-add_source_parser/conf.py index ef85560b88..9d05a25159 100644 --- a/tests/roots/test-add_source_parser/conf.py +++ b/tests/roots/test-add_source_parser/conf.py @@ -1,7 +1,6 @@ -import os import sys +from pathlib import Path -sys.path.insert(0, os.path.abspath('.')) - +sys.path.insert(0, str(Path.cwd().resolve())) extensions = ['source_parser'] diff --git a/tests/roots/test-api-set-translator/conf.py b/tests/roots/test-api-set-translator/conf.py index 3b56c39e8e..9f43f50d08 100644 --- a/tests/roots/test-api-set-translator/conf.py +++ b/tests/roots/test-api-set-translator/conf.py @@ -1,7 +1,7 @@ # set this by test -# import os # import sys -# sys.path.insert(0, os.path.abspath('.')) +# from pathlib import Path +# sys.path.insert(0, str(Path.cwd().resolve())) from docutils.writers.docutils_xml import XMLTranslator diff --git a/tests/roots/test-api-set-translator/nonext/conf.py b/tests/roots/test-api-set-translator/nonext/conf.py index f93e4da49e..803e173f7f 100644 --- a/tests/roots/test-api-set-translator/nonext/conf.py +++ b/tests/roots/test-api-set-translator/nonext/conf.py @@ -1,6 +1,6 @@ -import os import sys +from pathlib import Path -sys.path.insert(0, os.path.dirname(os.path.abspath('.'))) +sys.path.insert(0, str(Path.cwd().resolve())) project = 'test' diff --git a/tests/roots/test-autosummary/conf.py b/tests/roots/test-autosummary/conf.py index f459017f88..733702e8fa 100644 --- a/tests/roots/test-autosummary/conf.py +++ b/tests/roots/test-autosummary/conf.py @@ -1,7 +1,7 @@ -import os import sys +from pathlib import Path -sys.path.insert(0, os.path.abspath('.')) +sys.path.insert(0, str(Path.cwd().resolve())) extensions = ['sphinx.ext.autosummary'] diff --git a/tests/roots/test-environment-record-dependencies/conf.py b/tests/roots/test-environment-record-dependencies/conf.py index 107480e40c..2178c556e2 100644 --- a/tests/roots/test-environment-record-dependencies/conf.py +++ b/tests/roots/test-environment-record-dependencies/conf.py @@ -1,5 +1,6 @@ -import os import sys +from pathlib import Path + +sys.path.insert(0, str(Path.cwd().resolve())) -sys.path.insert(0, os.path.abspath('.')) extensions = ['sphinx.ext.autodoc'] diff --git a/tests/roots/test-ext-autodoc/conf.py b/tests/roots/test-ext-autodoc/conf.py index 9d1cdc7404..abaea1c996 100644 --- a/tests/roots/test-ext-autodoc/conf.py +++ b/tests/roots/test-ext-autodoc/conf.py @@ -1,7 +1,7 @@ -import os import sys +from pathlib import Path -sys.path.insert(0, os.path.abspath('.')) +sys.path.insert(0, str(Path.cwd().resolve())) extensions = ['sphinx.ext.autodoc'] diff --git a/tests/roots/test-ext-autosummary-filename-map/conf.py b/tests/roots/test-ext-autosummary-filename-map/conf.py index 17e2fa4456..ea64caec23 100644 --- a/tests/roots/test-ext-autosummary-filename-map/conf.py +++ b/tests/roots/test-ext-autosummary-filename-map/conf.py @@ -1,7 +1,7 @@ -import os import sys +from pathlib import Path -sys.path.insert(0, os.path.abspath('.')) +sys.path.insert(0, str(Path.cwd().resolve())) extensions = ['sphinx.ext.autosummary'] autosummary_generate = True diff --git a/tests/roots/test-ext-autosummary-import_cycle/conf.py b/tests/roots/test-ext-autosummary-import_cycle/conf.py index 5e889f9e36..513f506cc9 100644 --- a/tests/roots/test-ext-autosummary-import_cycle/conf.py +++ b/tests/roots/test-ext-autosummary-import_cycle/conf.py @@ -1,7 +1,7 @@ -import os import sys +from pathlib import Path -sys.path.insert(0, os.path.abspath('.')) +sys.path.insert(0, str(Path.cwd().resolve())) extensions = ['sphinx.ext.autosummary'] autosummary_generate = False diff --git a/tests/roots/test-ext-autosummary-imported_members/conf.py b/tests/roots/test-ext-autosummary-imported_members/conf.py index 77af668ab9..6ea7a55284 100644 --- a/tests/roots/test-ext-autosummary-imported_members/conf.py +++ b/tests/roots/test-ext-autosummary-imported_members/conf.py @@ -1,7 +1,7 @@ -import os import sys +from pathlib import Path -sys.path.insert(0, os.path.abspath('.')) +sys.path.insert(0, str(Path.cwd().resolve())) extensions = ['sphinx.ext.autosummary'] autosummary_generate = True diff --git a/tests/roots/test-ext-autosummary-mock_imports/conf.py b/tests/roots/test-ext-autosummary-mock_imports/conf.py index 121f814593..8a39ee590b 100644 --- a/tests/roots/test-ext-autosummary-mock_imports/conf.py +++ b/tests/roots/test-ext-autosummary-mock_imports/conf.py @@ -1,7 +1,7 @@ -import os import sys +from pathlib import Path -sys.path.insert(0, os.path.abspath('.')) +sys.path.insert(0, str(Path.cwd().resolve())) extensions = ['sphinx.ext.autosummary'] autosummary_generate = True diff --git a/tests/roots/test-ext-autosummary-module_all/conf.py b/tests/roots/test-ext-autosummary-module_all/conf.py index c6ff53419b..abdbfccced 100644 --- a/tests/roots/test-ext-autosummary-module_all/conf.py +++ b/tests/roots/test-ext-autosummary-module_all/conf.py @@ -1,7 +1,7 @@ -import os import sys +from pathlib import Path -sys.path.insert(0, os.path.abspath('.')) +sys.path.insert(0, str(Path.cwd().resolve())) extensions = ['sphinx.ext.autosummary'] autosummary_generate = True diff --git a/tests/roots/test-ext-autosummary-module_prefix/conf.py b/tests/roots/test-ext-autosummary-module_prefix/conf.py index 1065b9112e..11714a36dd 100644 --- a/tests/roots/test-ext-autosummary-module_prefix/conf.py +++ b/tests/roots/test-ext-autosummary-module_prefix/conf.py @@ -1,7 +1,7 @@ -import os import sys +from pathlib import Path -sys.path.insert(0, os.path.abspath('.')) +sys.path.insert(0, str(Path.cwd().resolve())) extensions = [ 'sphinx.ext.autosummary', diff --git a/tests/roots/test-ext-autosummary-recursive/conf.py b/tests/roots/test-ext-autosummary-recursive/conf.py index 1c0d02202c..04324a6bdf 100644 --- a/tests/roots/test-ext-autosummary-recursive/conf.py +++ b/tests/roots/test-ext-autosummary-recursive/conf.py @@ -1,7 +1,7 @@ -import os import sys +from pathlib import Path -sys.path.insert(0, os.path.abspath('.')) +sys.path.insert(0, str(Path.cwd().resolve())) extensions = ['sphinx.ext.autosummary'] autosummary_generate = True diff --git a/tests/roots/test-ext-autosummary-skip-member/conf.py b/tests/roots/test-ext-autosummary-skip-member/conf.py index 7c8f0e9cf4..f409bdc5c1 100644 --- a/tests/roots/test-ext-autosummary-skip-member/conf.py +++ b/tests/roots/test-ext-autosummary-skip-member/conf.py @@ -1,8 +1,7 @@ -import os import sys +from pathlib import Path -sys.path.insert(0, os.path.abspath('.')) - +sys.path.insert(0, str(Path.cwd().resolve())) extensions = ['sphinx.ext.autosummary'] autosummary_generate = True diff --git a/tests/roots/test-ext-autosummary-template/conf.py b/tests/roots/test-ext-autosummary-template/conf.py index cc23635276..62515c6652 100644 --- a/tests/roots/test-ext-autosummary-template/conf.py +++ b/tests/roots/test-ext-autosummary-template/conf.py @@ -1,8 +1,7 @@ -import os import sys +from pathlib import Path -sys.path.insert(0, os.path.abspath('.')) - +sys.path.insert(0, str(Path.cwd().resolve())) extensions = ['sphinx.ext.autosummary'] autosummary_generate = True diff --git a/tests/roots/test-ext-autosummary/conf.py b/tests/roots/test-ext-autosummary/conf.py index 1c0d02202c..04324a6bdf 100644 --- a/tests/roots/test-ext-autosummary/conf.py +++ b/tests/roots/test-ext-autosummary/conf.py @@ -1,7 +1,7 @@ -import os import sys +from pathlib import Path -sys.path.insert(0, os.path.abspath('.')) +sys.path.insert(0, str(Path.cwd().resolve())) extensions = ['sphinx.ext.autosummary'] autosummary_generate = True diff --git a/tests/roots/test-ext-coverage/conf.py b/tests/roots/test-ext-coverage/conf.py index 70fd03e91b..b38fec0e04 100644 --- a/tests/roots/test-ext-coverage/conf.py +++ b/tests/roots/test-ext-coverage/conf.py @@ -1,7 +1,7 @@ -import os import sys +from pathlib import Path -sys.path.insert(0, os.path.abspath('.')) +sys.path.insert(0, str(Path.cwd().resolve())) extensions = ['sphinx.ext.autodoc', 'sphinx.ext.coverage'] diff --git a/tests/roots/test-ext-doctest-with-autodoc/conf.py b/tests/roots/test-ext-doctest-with-autodoc/conf.py index 1ec1dd9948..b662b1cbaa 100644 --- a/tests/roots/test-ext-doctest-with-autodoc/conf.py +++ b/tests/roots/test-ext-doctest-with-autodoc/conf.py @@ -1,7 +1,7 @@ import sys -from os import path +from pathlib import Path -sys.path.insert(0, path.abspath(path.dirname(__file__))) +sys.path.insert(0, str(Path.cwd().resolve())) project = 'test project for doctest + autodoc reporting' extensions = ['sphinx.ext.autodoc', 'sphinx.ext.doctest'] diff --git a/tests/roots/test-ext-imgmockconverter/conf.py b/tests/roots/test-ext-imgmockconverter/conf.py index 679bb5acc5..d189ea673d 100644 --- a/tests/roots/test-ext-imgmockconverter/conf.py +++ b/tests/roots/test-ext-imgmockconverter/conf.py @@ -1,5 +1,6 @@ -import os import sys +from pathlib import Path + +sys.path.insert(0, str(Path.cwd().resolve())) -sys.path.insert(0, os.path.abspath('.')) extensions = ['mocksvgconverter'] diff --git a/tests/roots/test-ext-inheritance_diagram/conf.py b/tests/roots/test-ext-inheritance_diagram/conf.py index d3778d5cfb..6ae8a66085 100644 --- a/tests/roots/test-ext-inheritance_diagram/conf.py +++ b/tests/roots/test-ext-inheritance_diagram/conf.py @@ -1,6 +1,6 @@ -import os import sys +from pathlib import Path -sys.path.insert(0, os.path.abspath('.')) +sys.path.insert(0, str(Path.cwd().resolve())) extensions = ['sphinx.ext.inheritance_diagram', 'sphinx.ext.intersphinx'] diff --git a/tests/roots/test-ext-napoleon-paramtype/conf.py b/tests/roots/test-ext-napoleon-paramtype/conf.py index 34e2274701..70d30ce0a6 100644 --- a/tests/roots/test-ext-napoleon-paramtype/conf.py +++ b/tests/roots/test-ext-napoleon-paramtype/conf.py @@ -1,7 +1,8 @@ -import os import sys +from pathlib import Path + +sys.path.insert(0, str(Path.cwd().resolve())) -sys.path.insert(0, os.path.abspath('.')) extensions = [ 'sphinx.ext.autodoc', 'sphinx.ext.napoleon', @@ -12,4 +13,4 @@ extensions = [ # in order to avoid creating a real HTTP connection intersphinx_mapping = {} intersphinx_cache_limit = 0 -intersphinx_disabled_reftypes = [] \ No newline at end of file +intersphinx_disabled_reftypes = [] diff --git a/tests/roots/test-ext-napoleon/conf.py b/tests/roots/test-ext-napoleon/conf.py index 502fb5a9de..fd5daa8647 100644 --- a/tests/roots/test-ext-napoleon/conf.py +++ b/tests/roots/test-ext-napoleon/conf.py @@ -1,5 +1,6 @@ -import os import sys +from pathlib import Path + +sys.path.insert(0, str(Path.cwd().resolve())) -sys.path.insert(0, os.path.abspath('.')) extensions = ['sphinx.ext.napoleon'] diff --git a/tests/roots/test-ext-viewcode/conf.py b/tests/roots/test-ext-viewcode/conf.py index d6de1d9693..0a5d2fcb06 100644 --- a/tests/roots/test-ext-viewcode/conf.py +++ b/tests/roots/test-ext-viewcode/conf.py @@ -1,9 +1,8 @@ -import os import sys +from pathlib import Path + +sys.path.insert(0, str(Path.cwd().resolve())) -source_dir = os.path.abspath('.') -if source_dir not in sys.path: - sys.path.insert(0, source_dir) extensions = ['sphinx.ext.autodoc', 'sphinx.ext.viewcode'] exclude_patterns = ['_build'] diff --git a/tests/roots/test-extensions/conf.py b/tests/roots/test-extensions/conf.py index 9a3cbc844c..931f66643d 100644 --- a/tests/roots/test-extensions/conf.py +++ b/tests/roots/test-extensions/conf.py @@ -1,4 +1,4 @@ -import os import sys +from pathlib import Path -sys.path.insert(0, os.path.abspath('.')) +sys.path.insert(0, str(Path.cwd().resolve())) diff --git a/tests/roots/test-inheritance/conf.py b/tests/roots/test-inheritance/conf.py index 9953494a59..7690ee1416 100644 --- a/tests/roots/test-inheritance/conf.py +++ b/tests/roots/test-inheritance/conf.py @@ -1,6 +1,6 @@ -import os import sys +from pathlib import Path -sys.path.insert(0, os.path.abspath('.')) +sys.path.insert(0, str(Path.cwd().resolve())) extensions = ['sphinx.ext.inheritance_diagram'] diff --git a/tests/roots/test-latex-labels-before-module/conf.py b/tests/roots/test-latex-labels-before-module/conf.py index 25193b0ddb..e038492c16 100644 --- a/tests/roots/test-latex-labels-before-module/conf.py +++ b/tests/roots/test-latex-labels-before-module/conf.py @@ -1,7 +1,7 @@ -import os import sys +from pathlib import Path -sys.path.insert(0, os.path.abspath('.')) +sys.path.insert(0, str(Path.cwd().resolve())) extensions = ['sphinx.ext.autodoc'] diff --git a/tests/roots/test-prolog/conf.py b/tests/roots/test-prolog/conf.py index f6be09c5e8..c9a0a50c91 100644 --- a/tests/roots/test-prolog/conf.py +++ b/tests/roots/test-prolog/conf.py @@ -1,8 +1,7 @@ -import os import sys +from pathlib import Path -sys.path.insert(0, os.path.abspath('.')) - +sys.path.insert(0, str(Path.cwd().resolve())) extensions = ['prolog_markdown_parser'] diff --git a/tests/roots/test-root/conf.py b/tests/roots/test-root/conf.py index 25c723b8cc..1cc5c2f8fc 100644 --- a/tests/roots/test-root/conf.py +++ b/tests/roots/test-root/conf.py @@ -1,13 +1,13 @@ -import os import sys +from pathlib import Path + +sys.path.insert(0, str(Path.cwd().resolve())) from docutils import nodes from docutils.parsers.rst import Directive from sphinx import addnodes -sys.path.append(os.path.abspath('.')) - extensions = ['sphinx.ext.autodoc', 'sphinx.ext.todo', 'sphinx.ext.coverage', diff --git a/tests/roots/test-util-copyasset_overwrite/conf.py b/tests/roots/test-util-copyasset_overwrite/conf.py index bb91f31f7a..e4e70b6902 100644 --- a/tests/roots/test-util-copyasset_overwrite/conf.py +++ b/tests/roots/test-util-copyasset_overwrite/conf.py @@ -1,6 +1,7 @@ -import os import sys -sys.path.insert(0, os.path.abspath('.')) +from pathlib import Path + +sys.path.insert(0, str(Path.cwd().resolve())) extensions = ['myext'] html_static_path = ['user_static'] diff --git a/tests/roots/test-warnings/conf.py b/tests/roots/test-warnings/conf.py index 25b8aba074..2178c556e2 100644 --- a/tests/roots/test-warnings/conf.py +++ b/tests/roots/test-warnings/conf.py @@ -1,6 +1,6 @@ -import os import sys +from pathlib import Path -sys.path.append(os.path.abspath('.')) +sys.path.insert(0, str(Path.cwd().resolve())) extensions = ['sphinx.ext.autodoc'] diff --git a/tests/test_application.py b/tests/test_application.py index d39e248322..ee7e850e7c 100644 --- a/tests/test_application.py +++ b/tests/test_application.py @@ -33,7 +33,7 @@ def test_instantiation( if rootdir and not src_dir.exists(): shutil.copytree(Path(str(rootdir)) / 'test-root', src_dir) - syspath = sys.path[:] + saved_path = sys.path.copy() # When app_ = SphinxTestApp( @@ -41,7 +41,7 @@ def test_instantiation( status=StringIO(), warning=StringIO(), ) - sys.path[:] = syspath + sys.path[:] = saved_path app_.cleanup() # Then diff --git a/tests/test_extensions/test_ext_inheritance_diagram.py b/tests/test_extensions/test_ext_inheritance_diagram.py index 38cc085327..5bfa8afac8 100644 --- a/tests/test_extensions/test_ext_inheritance_diagram.py +++ b/tests/test_extensions/test_ext_inheritance_diagram.py @@ -289,8 +289,9 @@ def test_import_classes(rootdir): from sphinx.parsers import Parser, RSTParser from sphinx.util.i18n import CatalogInfo + saved_path = sys.path.copy() + sys.path.insert(0, str(rootdir / 'test-ext-inheritance_diagram')) try: - sys.path.append(str(rootdir / 'test-ext-inheritance_diagram')) from example.sphinx import DummyClass # got exception for unknown class or module @@ -339,4 +340,4 @@ def test_import_classes(rootdir): classes = import_classes('sphinx', 'example') assert classes == [DummyClass] finally: - sys.path.pop() + sys.path[:] = saved_path diff --git a/tests/test_pycode/test_pycode.py b/tests/test_pycode/test_pycode.py index 5739787fff..fae1b635c1 100644 --- a/tests/test_pycode/test_pycode.py +++ b/tests/test_pycode/test_pycode.py @@ -40,14 +40,14 @@ def test_ModuleAnalyzer_for_module(rootdir): assert analyzer.srcname in (SPHINX_MODULE_PATH, os.path.abspath(SPHINX_MODULE_PATH)) - path = str(rootdir / 'test-pycode') - sys.path.insert(0, path) + saved_path = sys.path.copy() + sys.path.insert(0, str(rootdir / 'test-pycode')) try: analyzer = ModuleAnalyzer.for_module('cp_1251_coded') docs = analyzer.find_attr_docs() assert docs == {('', 'X'): ['It MUST look like X="\u0425"', '']} finally: - sys.path.pop(0) + sys.path[:] = saved_path def test_ModuleAnalyzer_find_tags(): diff --git a/tests/test_writers/test_api_translator.py b/tests/test_writers/test_api_translator.py index eab1cd46ee..c3a144d33d 100644 --- a/tests/test_writers/test_api_translator.py +++ b/tests/test_writers/test_api_translator.py @@ -7,10 +7,10 @@ import pytest @pytest.fixture(scope='module', autouse=True) def _setup_module(rootdir): - p = rootdir / 'test-api-set-translator' - sys.path.insert(0, p) + saved_path = sys.path.copy() + sys.path.insert(0, str(rootdir / 'test-api-set-translator')) yield - sys.path.remove(p) + sys.path[:] = saved_path @pytest.mark.sphinx('html')