Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ENH] Sphinx's exclude_patterns as ignore patterns #135

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/sphinx_autobuild/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down
55 changes: 55 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
@@ -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)