From 87c98d1914a691e9333d163a4dfd4b3d1fb90baf Mon Sep 17 00:00:00 2001 From: maxdup Date: Wed, 26 Jul 2023 19:43:01 -0400 Subject: [PATCH 1/2] ignore conf.py's exclude_patterns --- src/sphinx_autobuild/cli.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/sphinx_autobuild/cli.py b/src/sphinx_autobuild/cli.py index 114650e..2f9896e 100644 --- a/src/sphinx_autobuild/cli.py +++ b/src/sphinx_autobuild/cli.py @@ -44,6 +44,21 @@ def _get_ignore_handler(args): regular.append(os.path.realpath(args.d[0])) regex_based = args.re_ignore + + try: + import types + from importlib.machinery import SourceFileLoader + + confPath = os.path.join(args.sourcedir, "conf.py") + confLoader = SourceFileLoader("conf", confPath) + conf = types.ModuleType(confLoader.name) + confLoader.exec_module(conf) + regular = regular + conf.exclude_patterns + except Exception as e: + # if either conf.py or exclude_patterns are invalid, + # simply defer error reporting to sphinx-build + pass + return get_ignore(regular, regex_based) From 4700e7a3bbd759c4a4e5c25ad5e2f8c5c8982b01 Mon Sep 17 00:00:00 2001 From: maxdup Date: Wed, 26 Jul 2023 22:10:56 -0400 Subject: [PATCH 2/2] testing for cli._get_ignore_handler --- src/sphinx_autobuild/cli.py | 11 ++++---- tests/test_cli.py | 55 +++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 5 deletions(-) create mode 100644 tests/test_cli.py diff --git a/src/sphinx_autobuild/cli.py b/src/sphinx_autobuild/cli.py index 2f9896e..54bb9b3 100644 --- a/src/sphinx_autobuild/cli.py +++ b/src/sphinx_autobuild/cli.py @@ -49,11 +49,12 @@ def _get_ignore_handler(args): import types from importlib.machinery import SourceFileLoader - confPath = os.path.join(args.sourcedir, "conf.py") - confLoader = SourceFileLoader("conf", confPath) - conf = types.ModuleType(confLoader.name) - confLoader.exec_module(conf) - regular = regular + conf.exclude_patterns + conf_path = os.path.join(args.sourcedir, "conf.py") + conf_loader = SourceFileLoader("conf", conf_path) + conf = types.ModuleType(conf_loader.name) + conf_loader.exec_module(conf) + conf_regular = [os.path.realpath(path) for path in conf.exclude_patterns] + regular = regular + conf_regular except Exception as e: # if either conf.py or exclude_patterns are invalid, # simply defer error reporting to sphinx-build diff --git a/tests/test_cli.py b/tests/test_cli.py new file mode 100644 index 0000000..82348a5 --- /dev/null +++ b/tests/test_cli.py @@ -0,0 +1,55 @@ +from argparse import Namespace +from unittest import mock +import tempfile +import os + + +def test__get_ignore_handler(): + args_ignore = ["ignore.pyc"] + args_re_ignore = ["^REGEX1$", "^REGEX2$"] + + with mock.patch("sphinx_autobuild.ignore.get_ignore") as mock_get_ignore: + from sphinx_autobuild.cli import _get_ignore_handler + + with tempfile.TemporaryDirectory() as tmpdir: + args = Namespace( + sourcedir=tmpdir, + outdir="output/directory", + ignore=args_ignore, + re_ignore=args_re_ignore, + w=['error.log'], + d=['doctrees-cache'], + ) + + # without conf.py + _get_ignore_handler(args) + + expected_ignore = [ + os.path.realpath("ignore.pyc"), + os.path.realpath("output/directory"), + os.path.realpath("error.log"), + os.path.realpath("doctrees-cache"), + ] + expected_re_ignore = args_re_ignore + + mock_get_ignore.assert_called_once_with(expected_ignore, expected_re_ignore) + + mock_get_ignore.reset_mock() + + # with conf.py + with open(os.path.join(tmpdir, "conf.py"), mode="w") as f: + f.write("exclude_patterns = ['drafts/*.rst', 'drafts/*.md']") + + _get_ignore_handler(args) + + expected_ignore = [ + os.path.realpath("ignore.pyc"), + os.path.realpath("output/directory"), + os.path.realpath("error.log"), + os.path.realpath("doctrees-cache"), + os.path.realpath("drafts/*.rst"), + os.path.realpath("drafts/*.md"), + ] + expected_re_ignore = args_re_ignore + + mock_get_ignore.assert_called_once_with(expected_ignore, expected_re_ignore)