diff --git a/src/sphinx_autobuild/cli.py b/src/sphinx_autobuild/cli.py index 114650e..54bb9b3 100644 --- a/src/sphinx_autobuild/cli.py +++ b/src/sphinx_autobuild/cli.py @@ -44,6 +44,22 @@ 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 + + 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 + pass + return get_ignore(regular, regex_based) 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)