Skip to content

Commit

Permalink
Merge pull request #179 from dhellmann/word-list-filenames-tests
Browse files Browse the repository at this point in the history
builder: add tests for multiple wordlist filenames
  • Loading branch information
mergify[bot] authored May 30, 2022
2 parents 3ab4a13 + c5dc4d4 commit e0d9254
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 18 deletions.
10 changes: 6 additions & 4 deletions docs/source/customize.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@ Input Options
String specifying a file containing a list of words known to be
spelled correctly but that do not appear in the language dictionary
selected by ``spelling_lang``. The file should contain one word per
line. Refer to the `PyEnchant tutorial`_ for details. To add multiple
files use a list, or a comma separated string. This is useful when
calling sphinx with ``-D spelling_word_list_filename=...`` which will
not accept a list and will only accept a string parameter.
line. Refer to the `PyEnchant tutorial`_ for details.

To add multiple files use a list, or a comma separated string. This
is useful when calling sphinx with ``-D
spelling_word_list_filename=...`` which will not accept a list and
will only accept a string parameter.

``spelling_word_list_filename=['spelling_wordlist.txt','another_list.txt']``

Expand Down
1 change: 0 additions & 1 deletion docs/source/history.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ Features
Added configuration option to limit the number of suggestions
output. See :doc:`/customize` for more details. Idea contributed by
Trevor Gross.

- `#169 <https://github.com/sphinx-contrib/spelling/issues/169>`__
Adds the ability to pass in multiple wordlists via the sphinx
command line as ``-D spelling_word_list_filename=file1,file2``.
Expand Down
26 changes: 16 additions & 10 deletions sphinxcontrib/spelling/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,19 +101,27 @@ def _load_filter_classes(self, filters):
mod = importlib.import_module(module_name)
yield getattr(mod, class_name)

def get_wordlist_filename(self):
def get_configured_wordlist_filenames(self):
"Returns the configured wordlist filenames."
word_list = self.config.spelling_word_list_filename
if word_list is None:
word_list = 'spelling_wordlist.txt'
word_list = ['spelling_wordlist.txt']

if isinstance(word_list, str) and word_list.find(',') > 0:
# Wordlist is a list, formatted as a str.
# Split it back into a list
if isinstance(word_list, str):
# Wordlist is a string. Split on comma in case it came
# from the command line, via -D, and has multiple values.
word_list = word_list.split(',')

if not isinstance(word_list, list):
return os.path.join(self.srcdir, word_list)
return [
os.path.join(self.srcdir, p)
for p in word_list
]

def get_wordlist_filename(self):
"Returns the filename of the wordlist to use when checking content."
filenames = self.get_configured_wordlist_filenames()
if len(filenames) == 1:
return filenames[0]
# In case the user has multiple word lists, we combine them
# into one large list that we pass on to the checker.
return self._build_combined_wordlist()
Expand All @@ -126,10 +134,8 @@ def _build_combined_wordlist(self):
combined_word_list = os.path.join(temp_dir,
'spelling_wordlist.txt')

word_list = self.config.spelling_word_list_filename

with open(combined_word_list, 'w', encoding='UTF-8') as outfile:
for word_file in word_list:
for word_file in self.get_configured_wordlist_filenames():
# Paths are relative
long_word_file = os.path.join(self.srcdir, word_file)
logger.info('Adding contents of %s to custom word list',
Expand Down
79 changes: 76 additions & 3 deletions tests/test_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,18 @@
from sphinx.application import Sphinx


@pytest.fixture
def sphinx_project(tmpdir):
def _make_sphinx_project(tmpdir):
srcdir = tmpdir.mkdir('src')
outdir = tmpdir.mkdir('out')
add_file(srcdir, 'conf.py', '''
extensions = [ 'sphinxcontrib.spelling' ]
''')
yield (srcdir, outdir)
return (srcdir, outdir)


@pytest.fixture
def sphinx_project(tmpdir):
yield _make_sphinx_project(tmpdir)


@contextlib.contextmanager
Expand Down Expand Up @@ -160,6 +164,75 @@ def test_several_word_lists(sphinx_project):
assert '(tihs)' in output_text


def _wordlist_sphinx_project(tmpdir, conf_contents):
srcdir, outdir = _make_sphinx_project(tmpdir)
add_file(srcdir, 'conf.py', conf_contents)
add_file(srcdir, 'test_wordlist.txt', '''
txt
''')
add_file(srcdir, 'test_wordlist2.txt', '''
mispelled
''')
stdout, stderr, app = get_sphinx_app(srcdir, outdir, 'contents')
return (srcdir, outdir, stdout, stderr, app)


def test_word_list_default(tmpdir):
srcdir, outdir, stdout, stderr, app = _wordlist_sphinx_project(
tmpdir,
'''
extensions = ['sphinxcontrib.spelling']
''',
)
results = app.builder.get_configured_wordlist_filenames()
assert len(results) == 1
assert os.path.basename(results[0]) == 'spelling_wordlist.txt'


def test_one_word_list_str(tmpdir):
srcdir, outdir, stdout, stderr, app = _wordlist_sphinx_project(
tmpdir,
'''
extensions = ['sphinxcontrib.spelling']
spelling_word_list_filename='test_wordlist.txt'
''',
)
results = app.builder.get_configured_wordlist_filenames()
assert len(results) == 1
assert os.path.basename(results[0]) == 'test_wordlist.txt'


def test_multiple_word_list_str(tmpdir):
# We don't expect anyone to set up their conf.py this way but it
# simulates passing the configuration option from the command line
# using -D.
srcdir, outdir, stdout, stderr, app = _wordlist_sphinx_project(
tmpdir,
'''
extensions = ['sphinxcontrib.spelling']
spelling_word_list_filename='test_wordlist.txt,test_wordlist2.txt'
''',
)
results = app.builder.get_configured_wordlist_filenames()
assert len(results) == 2
assert os.path.basename(results[0]) == 'test_wordlist.txt'
assert os.path.basename(results[1]) == 'test_wordlist2.txt'


def test_multiple_word_list_list(tmpdir):
srcdir, outdir, stdout, stderr, app = _wordlist_sphinx_project(
tmpdir,
'''
extensions = ['sphinxcontrib.spelling']
spelling_word_list_filename=['test_wordlist.txt', 'test_wordlist2.txt']
''',
)
results = app.builder.get_configured_wordlist_filenames()
assert len(results) == 2
assert os.path.basename(results[0]) == 'test_wordlist.txt'
assert os.path.basename(results[1]) == 'test_wordlist2.txt'


def test_ignore_file(sphinx_project):
srcdir, outdir = sphinx_project
add_file(srcdir, 'conf.py', '''
Expand Down

0 comments on commit e0d9254

Please sign in to comment.