From 596dfc2521c925ee5d0cdbd9e2fb8df2c3f38f4c Mon Sep 17 00:00:00 2001 From: Doug Hellmann Date: Thu, 19 Dec 2024 11:31:22 -0500 Subject: [PATCH] switch linter to ruff --- requirements-dev.txt | 8 +- tests/test_builder.py | 386 +++++++++++++++++++++++++++--------------- tests/test_checker.py | 62 +++---- tox.ini | 9 +- 4 files changed, 288 insertions(+), 177 deletions(-) diff --git a/requirements-dev.txt b/requirements-dev.txt index f821fcc..af3ee57 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -1,7 +1 @@ -astpretty==2.1.0 -flake8==7.1.1 -flake8-debug==0.2.0 -isort==5.13.2 -mccabe==0.7.0 -pycodestyle==2.12.1 -pyflakes==3.2.0 +ruff diff --git a/tests/test_builder.py b/tests/test_builder.py index d76c305..25c4155 100644 --- a/tests/test_builder.py +++ b/tests/test_builder.py @@ -1,8 +1,8 @@ # # Copyright (c) 2010 Doug Hellmann. All rights reserved. # -"""Tests for SpellingBuilder -""" +"""Tests for SpellingBuilder""" + import contextlib import io import os @@ -10,18 +10,21 @@ import textwrap import pytest -import sphinx from sphinx.application import Sphinx from tests import helpers # isort:skip def _make_sphinx_project(tmpdir): - srcdir = tmpdir.mkdir('src') - outdir = tmpdir.mkdir('out') - add_file(srcdir, 'conf.py', ''' + srcdir = tmpdir.mkdir("src") + outdir = tmpdir.mkdir("out") + add_file( + srcdir, + "conf.py", + """ extensions = [ 'sphinxcontrib.spelling' ] - ''') + """, + ) return (srcdir, outdir) @@ -53,27 +56,32 @@ def import_path(new_path): def add_file(thedir, filename, content): - with open(thedir.join(filename), 'w') as f: + with open(thedir.join(filename), "w") as f: f.write(textwrap.dedent(content)) -def get_sphinx_app(srcdir, outdir, docname, builder='spelling'): +def get_sphinx_app(srcdir, outdir, docname, builder="spelling"): stdout = io.StringIO() stderr = io.StringIO() app = Sphinx( - srcdir, srcdir, outdir, outdir, builder, - status=stdout, warning=stderr, + srcdir, + srcdir, + outdir, + outdir, + builder, + status=stdout, + warning=stderr, freshenv=True, ) return (stdout, stderr, app) -def get_sphinx_output(srcdir, outdir, docname, builder='spelling'): +def get_sphinx_output(srcdir, outdir, docname, builder="spelling"): stdout, stderr, app = get_sphinx_app(srcdir, outdir, docname, builder) app.build() - path = os.path.join(outdir, f'{docname}.spelling') + path = os.path.join(outdir, f"{docname}.spelling") try: - with open(path, 'r') as f: + with open(path, "r") as f: output_text = f.read() except FileNotFoundError: output_text = None @@ -88,38 +96,54 @@ def test_setup(sphinx_project): # trying to use it with the Sphinx app class will # generate an exception. Sphinx( - str(srcdir), str(srcdir), str(outdir), str(outdir), 'spelling', - status=stdout, warning=stderr, + str(srcdir), + str(srcdir), + str(outdir), + str(outdir), + "spelling", + status=stdout, + warning=stderr, freshenv=True, ) def test_title(sphinx_project): srcdir, outdir = sphinx_project - add_file(srcdir, 'contents.rst', ''' + add_file( + srcdir, + "contents.rst", + """ Welcome to Speeling Checker documentation! ========================================== - ''') - stdout, stderr, output_text = get_sphinx_output(srcdir, outdir, 'contents') - assert '(Speeling)' in output_text + """, + ) + stdout, stderr, output_text = get_sphinx_output(srcdir, outdir, "contents") + assert "(Speeling)" in output_text def test_body(sphinx_project): srcdir, outdir = sphinx_project - add_file(srcdir, 'contents.rst', ''' + add_file( + srcdir, + "contents.rst", + """ Welcome to Spelling Checker documentation! ========================================== There are several mispelled words in this txt. - ''') - stdout, stderr, output_text = get_sphinx_output(srcdir, outdir, 'contents') - assert '(mispelled)' in output_text - assert '(txt)' in output_text + """, + ) + stdout, stderr, output_text = get_sphinx_output(srcdir, outdir, "contents") + assert "(mispelled)" in output_text + assert "(txt)" in output_text def test_ignore_literals(sphinx_project): srcdir, outdir = sphinx_project - add_file(srcdir, 'contents.rst', ''' + add_file( + srcdir, + "contents.rst", + """ Welcome to Spelling Checker documentation! ========================================== @@ -131,8 +155,9 @@ def test_ignore_literals(sphinx_project): Inline ``litterals`` are ignored, too. - ''') - stdout, stderr, output_text = get_sphinx_output(srcdir, outdir, 'contents') + """, + ) + stdout, stderr, output_text = get_sphinx_output(srcdir, outdir, "contents") # The 'contents.spelling' output file should not have been # created, because the errors are ignored. assert output_text is None @@ -140,69 +165,93 @@ def test_ignore_literals(sphinx_project): def test_several_word_lists(sphinx_project): srcdir, outdir = sphinx_project - add_file(srcdir, 'conf.py', ''' + add_file( + srcdir, + "conf.py", + """ extensions = ['sphinxcontrib.spelling'] spelling_word_list_filename=['test_wordlist.txt','test_wordlist2.txt'] - ''') + """, + ) - add_file(srcdir, 'contents.rst', ''' + add_file( + srcdir, + "contents.rst", + """ Welcome to Spelling Checker documentation! ========================================== There are several mispelled words in tihs txt. - ''') + """, + ) - add_file(srcdir, 'test_wordlist.txt', ''' + add_file( + srcdir, + "test_wordlist.txt", + """ txt - ''') + """, + ) - add_file(srcdir, 'test_wordlist2.txt', ''' + add_file( + srcdir, + "test_wordlist2.txt", + """ mispelled - ''') - stdout, stderr, output_text = get_sphinx_output(srcdir, outdir, 'contents') + """, + ) + stdout, stderr, output_text = get_sphinx_output(srcdir, outdir, "contents") # Both of these should be fine now - assert '(mispelled)' not in output_text - assert '(txt)' not in output_text + assert "(mispelled)" not in output_text + assert "(txt)" not in output_text # But not this one - assert '(tihs)' in output_text + 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', ''' + add_file(srcdir, "conf.py", conf_contents) + add_file( + srcdir, + "test_wordlist.txt", + """ txt - ''') - add_file(srcdir, 'test_wordlist2.txt', ''' + """, + ) + add_file( + srcdir, + "test_wordlist2.txt", + """ mispelled - ''') - stdout, stderr, app = get_sphinx_app(srcdir, outdir, 'contents') + """, + ) + 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' + 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' + assert os.path.basename(results[0]) == "test_wordlist.txt" def test_multiple_word_list_str(tmpdir): @@ -211,44 +260,52 @@ def test_multiple_word_list_str(tmpdir): # 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' + 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' + 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', ''' + add_file( + srcdir, + "conf.py", + """ extensions = ['sphinxcontrib.spelling'] spelling_exclude_patterns=['con*'] - ''') + """, + ) - add_file(srcdir, 'contents.rst', ''' + add_file( + srcdir, + "contents.rst", + """ Welcome to Speeling Checker documentation! ========================================== - ''') + """, + ) - stdout, stderr, output_text = get_sphinx_output(srcdir, outdir, 'contents') + stdout, stderr, output_text = get_sphinx_output(srcdir, outdir, "contents") # The 'contents.spelling' output file should not have been # created, because the file is ignored. assert output_text is None @@ -258,11 +315,18 @@ def test_ignore_file(sphinx_project): def test_docstrings(sphinx_project): srcdir, outdir = sphinx_project - add_file(srcdir, 'conf.py', ''' + add_file( + srcdir, + "conf.py", + """ extensions = ['sphinxcontrib.spelling', 'sphinx.ext.autodoc'] - ''') + """, + ) - add_file(srcdir / '..', 'the_source.py', ''' + add_file( + srcdir / "..", + "the_source.py", + ''' #!/usr/bin/env python3 def public_function(arg_name): @@ -271,81 +335,100 @@ def public_function(arg_name): :param arg_name: Pass a vaule """ return 1 - ''') + ''', + ) - add_file(srcdir, 'contents.rst', ''' + add_file( + srcdir, + "contents.rst", + """ The Module ========== .. automodule:: the_source :members: - ''') + """, + ) - with working_dir(srcdir / '..'): - with import_path(['.'] + sys.path): + with working_dir(srcdir / ".."): + with import_path(["."] + sys.path): stdout, stderr, output_text = get_sphinx_output( srcdir, outdir, - 'contents', + "contents", ) # Sphinx 5.1.0 and later reports line numbers for docstring # content. - line_num = 'None:' + line_num = "None:" if sphinx.version_info[:3] >= (5, 1, 0): - line_num = '1:' + line_num = "1:" # Expected string is too long for one line expected = ( - 'the_source.py:' - 'docstring of the_source.public_function:' - ) + line_num + ( - ' (vaule)' + ("the_source.py:" "docstring of the_source.public_function:") + + line_num + + (" (vaule)") ) assert expected in output_text def test_get_suggestions_to_show_all(sphinx_project): srcdir, outdir = sphinx_project - add_file(srcdir, 'conf.py', ''' + add_file( + srcdir, + "conf.py", + """ extensions = ['sphinxcontrib.spelling'] spelling_show_suggestions = True spelling_suggestion_limit = 0 - ''') - stdout, stderr, app = get_sphinx_app(srcdir, outdir, 'contents') - results = app.builder.get_suggestions_to_show(['a', 'b', 'c']) + """, + ) + stdout, stderr, app = get_sphinx_app(srcdir, outdir, "contents") + results = app.builder.get_suggestions_to_show(["a", "b", "c"]) assert len(results) == 3 def test_get_suggestions_to_show_limit(sphinx_project): srcdir, outdir = sphinx_project - add_file(srcdir, 'conf.py', ''' + add_file( + srcdir, + "conf.py", + """ extensions = ['sphinxcontrib.spelling'] spelling_show_suggestions = True spelling_suggestion_limit = 1 - ''') - stdout, stderr, app = get_sphinx_app(srcdir, outdir, 'contents') - results = app.builder.get_suggestions_to_show(['a', 'b', 'c']) + """, + ) + stdout, stderr, app = get_sphinx_app(srcdir, outdir, "contents") + results = app.builder.get_suggestions_to_show(["a", "b", "c"]) assert len(results) == 1 def test_get_suggestions_to_show_disabled(sphinx_project): srcdir, outdir = sphinx_project - add_file(srcdir, 'conf.py', ''' + add_file( + srcdir, + "conf.py", + """ extensions = ['sphinxcontrib.spelling'] spelling_show_suggestions = False spelling_suggestion_limit = 0 - ''') - stdout, stderr, app = get_sphinx_app(srcdir, outdir, 'contents') - results = app.builder.get_suggestions_to_show(['a', 'b', 'c']) + """, + ) + stdout, stderr, app = get_sphinx_app(srcdir, outdir, "contents") + results = app.builder.get_suggestions_to_show(["a", "b", "c"]) assert len(results) == 0 def test_captions(sphinx_project): srcdir, outdir = sphinx_project - add_file(srcdir, 'contents.rst', ''' + add_file( + srcdir, + "contents.rst", + """ The Module ========== @@ -353,20 +436,24 @@ def test_captions(sphinx_project): Teh caption - ''') + """, + ) stdout, stderr, output_text = get_sphinx_output( srcdir, outdir, - 'contents', + "contents", ) - assert '(Teh)' in output_text + assert "(Teh)" in output_text def test_legacy_directive(sphinx_project): srcdir, outdir = sphinx_project - add_file(srcdir, 'contents.rst', ''' + add_file( + srcdir, + "contents.rst", + """ The Module ========== @@ -376,12 +463,13 @@ def test_legacy_directive(sphinx_project): teh is OK - ''') + """, + ) stdout, stderr, output_text = get_sphinx_output( srcdir, outdir, - 'contents', + "contents", ) assert output_text is None @@ -389,7 +477,10 @@ def test_legacy_directive(sphinx_project): def test_domain_directive(sphinx_project): srcdir, outdir = sphinx_project - add_file(srcdir, 'contents.rst', ''' + add_file( + srcdir, + "contents.rst", + """ The Module ========== @@ -399,12 +490,13 @@ def test_domain_directive(sphinx_project): teh is OK - ''') + """, + ) stdout, stderr, output_text = get_sphinx_output( srcdir, outdir, - 'contents', + "contents", ) assert output_text is None @@ -412,18 +504,22 @@ def test_domain_directive(sphinx_project): def test_domain_role(sphinx_project): srcdir, outdir = sphinx_project - add_file(srcdir, 'contents.rst', ''' + add_file( + srcdir, + "contents.rst", + """ The Module ========== :spelling:word:`teh` is OK - ''') + """, + ) stdout, stderr, output_text = get_sphinx_output( srcdir, outdir, - 'contents', + "contents", ) assert output_text is None @@ -431,18 +527,22 @@ def test_domain_role(sphinx_project): def test_domain_role_multiple_words(sphinx_project): srcdir, outdir = sphinx_project - add_file(srcdir, 'contents.rst', ''' + add_file( + srcdir, + "contents.rst", + """ The Module ========== :spelling:word:`teh is KO` - ''') + """, + ) stdout, stderr, output_text = get_sphinx_output( srcdir, outdir, - 'contents', + "contents", ) assert output_text is None @@ -450,24 +550,28 @@ def test_domain_role_multiple_words(sphinx_project): def test_domain_role_output(sphinx_project): srcdir, outdir = sphinx_project - add_file(srcdir, 'contents.rst', ''' + add_file( + srcdir, + "contents.rst", + """ The Module ========== :spelling:word:`teh` is OK - ''') + """, + ) stdout, stderr, output_text = get_sphinx_output( srcdir, outdir, - 'contents', - 'text', + "contents", + "text", ) - path = os.path.join(outdir, 'contents.txt') + path = os.path.join(outdir, "contents.txt") try: - with open(path, 'r') as f: + with open(path, "r") as f: output_text = f.read() except FileNotFoundError: output_text = None @@ -478,18 +582,22 @@ def test_domain_role_output(sphinx_project): def test_domain_ignore(sphinx_project): srcdir, outdir = sphinx_project - add_file(srcdir, 'contents.rst', ''' + add_file( + srcdir, + "contents.rst", + """ The Module ========== :spelling:ignore:`baddddd` is OK - ''') + """, + ) stdout, stderr, output_text = get_sphinx_output( srcdir, outdir, - 'contents', + "contents", ) assert output_text is None @@ -497,7 +605,10 @@ def test_domain_ignore(sphinx_project): def test_domain_ignore_multiple_words(sphinx_project): srcdir, outdir = sphinx_project - add_file(srcdir, 'contents.rst', ''' + add_file( + srcdir, + "contents.rst", + """ The Module ========== @@ -506,38 +617,43 @@ def test_domain_ignore_multiple_words(sphinx_project): But, baddddd is not OK here. Nor, here baddddd. - ''') + """, + ) stdout, stderr, output_text = get_sphinx_output( srcdir, outdir, - 'contents', + "contents", ) - assert '(baddddd)' in output_text - assert output_text.count('\n') == 2 # Only expect 2 errors, not 3. + assert "(baddddd)" in output_text + assert output_text.count("\n") == 2 # Only expect 2 errors, not 3. def test_domain_ignore_output(sphinx_project): srcdir, outdir = sphinx_project - add_file(srcdir, 'contents.rst', ''' + add_file( + srcdir, + "contents.rst", + """ The Module ========== :spelling:ignore:`teh` is OK - ''') + """, + ) stdout, stderr, output_text = get_sphinx_output( srcdir, outdir, - 'contents', - 'text', + "contents", + "text", ) - path = os.path.join(outdir, 'contents.txt') + path = os.path.join(outdir, "contents.txt") try: - with open(path, 'r') as f: + with open(path, "r") as f: output_text = f.read() except FileNotFoundError: output_text = None @@ -550,7 +666,10 @@ def test_only_directive(sphinx_project): # https://github.com/sphinx-contrib/spelling/issues/204 srcdir, outdir = sphinx_project - add_file(srcdir, 'contents.rst', ''' + add_file( + srcdir, + "contents.rst", + """ The Module ========== @@ -559,12 +678,13 @@ def test_only_directive(sphinx_project): teh is ok whaat is not ok - ''') + """, + ) stdout, stderr, output_text = get_sphinx_output( srcdir, outdir, - 'contents', + "contents", ) - assert '(whaat)' in output_text - assert '(teh)' not in output_text + assert "(whaat)" in output_text + assert "(teh)" not in output_text diff --git a/tests/test_checker.py b/tests/test_checker.py index 49b63e5..eeb48af 100644 --- a/tests/test_checker.py +++ b/tests/test_checker.py @@ -1,8 +1,7 @@ # # Copyright (c) 2010 Doug Hellmann. All rights reserved. # -"""Tests for SpellingChecker. -""" +"""Tests for SpellingChecker.""" import os @@ -10,50 +9,55 @@ def test_errors_only(): - checker = SpellingChecker(lang='en_US', - suggest=False, - word_list_filename=None, - ) - for word, suggestions, line, offset in checker.check('This txt is wrong'): - assert not suggestions, 'Suggesting' - assert word == 'txt' + checker = SpellingChecker( + lang="en_US", + suggest=False, + word_list_filename=None, + ) + for word, suggestions, line, offset in checker.check("This txt is wrong"): + assert not suggestions, "Suggesting" + assert word == "txt" assert line == "" assert offset == 0 def test_with_suggestions(): - checker = SpellingChecker(lang='en_US', - suggest=True, - word_list_filename=None, - ) - for word, suggestions, line, offset in checker.check('This txt is wrong'): - assert suggestions, 'Not suggesting' - assert word == 'txt' + checker = SpellingChecker( + lang="en_US", + suggest=True, + word_list_filename=None, + ) + for word, suggestions, line, offset in checker.check("This txt is wrong"): + assert suggestions, "Not suggesting" + assert word == "txt" assert line == "" assert offset == 0 + def test_with_wordlist(): checker = SpellingChecker( - lang='en_US', + lang="en_US", suggest=False, - word_list_filename=os.path.join(os.path.dirname(__file__), - 'test_wordlist.txt') + word_list_filename=os.path.join(os.path.dirname(__file__), "test_wordlist.txt"), ) - words = [w for w, s, l, o in checker.check('This txt is wrong')] - assert not words, 'Did not use personal word list file' + words = [ + word for word, suggestions, line, offset in checker.check("This txt is wrong") + ] + assert not words, "Did not use personal word list file" def test_with_context_line(): - checker = SpellingChecker(lang='en_US', - suggest=False, - word_list_filename=None, - context_line=True, - ) + checker = SpellingChecker( + lang="en_US", + suggest=False, + word_list_filename=None, + context_line=True, + ) - text = 'Line one\nThis txt is wrong\nLine two' + text = "Line one\nThis txt is wrong\nLine two" for word, suggestions, line, offset in checker.check(text): - assert not suggestions, 'Suggesting' - assert word == 'txt' + assert not suggestions, "Suggesting" + assert word == "txt" assert line == "This txt is wrong" assert offset == 1 diff --git a/tox.ini b/tox.ini index 76edfd6..f323c92 100644 --- a/tox.ini +++ b/tox.ini @@ -33,19 +33,12 @@ commands= --cov-report term-missing \ --log-level DEBUG -[testenv:style] -deps = -r requirements-dev.txt -commands = - isort . -skip_install = true - [testenv:linter] deps = -r requirements-dev.txt setenv = BUILD=linter commands = - flake8 sphinxcontrib integration_tests - isort --check --diff . + ruff check sphinxcontrib integration_tests tests skip_install = true [testenv:pkglint]