Skip to content

Commit

Permalink
Fix #2055: Add support for PEP 518 (#2290)
Browse files Browse the repository at this point in the history
Co-authored-by: Eric Larson <larson.eric.d@gmail.com>
  • Loading branch information
Freed-Wu and larsoner authored Oct 5, 2022
1 parent 214255a commit 2a0ee4b
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 29 deletions.
12 changes: 8 additions & 4 deletions .github/workflows/codespell-private.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
# For general usage in your repo, see the example in codespell.yml
# https://github.com/codespell-project/codespell
name: codespell Private Actions
concurrency:
group: ${{ github.workflow }}-${{ github.event.number }}-${{ github.event.ref }}
cancel-in-progress: true
on: [push, pull_request]
jobs:
test:
Expand All @@ -10,12 +13,13 @@ jobs:
# Make sure we're using the latest aspell dictionary
runs-on: ubuntu-20.04
strategy:
fail-fast: false
matrix:
python-version:
- 3.6
- 3.7
- 3.8
- 3.9
- '3.7'
- '3.8'
- '3.9'
- '3.10'
name: Python ${{ matrix.python-version }} test
steps:
- uses: actions/checkout@v3
Expand Down
9 changes: 8 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Useful links
Requirements
------------

Python 3.6 or above.
Python 3.7 or above.

Installation
------------
Expand Down Expand Up @@ -107,6 +107,13 @@ This is equivalent to running::

codespell --quiet-level 3 --count --skip "*.po,*.ts,./src/3rdParty,./src/Test"

Now codespell also support ``pyproject.toml``::

[tool.codespell]
skip = '*.po,*.ts,./src/3rdParty,./src/Test'
count = ''
quiet-level = 3

Any options specified in the command line will *override* options from the
config file.

Expand Down
15 changes: 14 additions & 1 deletion codespell_lib/_codespell.py
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,8 @@ def parse_options(args):
help='print LINES of surrounding context')
parser.add_argument('--config', type=str,
help='path to config file.')

parser.add_argument('--toml', type=str,
help='path to a pyproject.toml file.')
parser.add_argument('files', nargs='*',
help='files or directories to check')

Expand All @@ -404,6 +405,18 @@ def parse_options(args):
if options.config:
cfg_files.append(options.config)
config = configparser.ConfigParser()

# Read toml before other config files.
if options.toml:
try:
import tomli
except Exception as exc:
raise ImportError(
f'tomli is required to read pyproject.toml but could not be '
f'imported, got: {exc}') from None
with open(options.toml, 'rb') as f:
data = tomli.load(f).get('tool', {})
config.read_dict(data)
config.read(cfg_files)

if config.has_section('codespell'):
Expand Down
50 changes: 30 additions & 20 deletions codespell_lib/tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -791,35 +791,45 @@ def test_uri_regex_def():
assert not uri_regex.findall(boilerplate % uri), uri


def test_config(tmpdir, capsys):
"""
Tests loading options from a config file.
"""
d = str(tmpdir)

# Create sample files.
with open(op.join(d, 'bad.txt'), 'w') as f:
@pytest.mark.parametrize('kind', ('toml', 'cfg'))
def test_config_toml(tmp_path, capsys, kind):
"""Test loading options from a config file or toml."""
d = tmp_path / 'files'
d.mkdir()
with open(d / 'bad.txt', 'w') as f:
f.write('abandonned donn\n')
with open(op.join(d, 'good.txt'), 'w') as f:
with open(d / 'good.txt', 'w') as f:
f.write("good")

# Create a config file.
conffile = op.join(d, 'config.cfg')
with open(conffile, 'w') as f:
f.write(
'[codespell]\n'
'skip = bad.txt\n'
'count = \n'
)

# Should fail when checking both.
code, stdout, _ = cs.main(d, count=True, std=True)
code, stdout, _ = cs.main(str(d), count=True, std=True)
# Code in this case is not exit code, but count of misspellings.
assert code == 2
assert 'bad.txt' in stdout

if kind == 'cfg':
conffile = str(tmp_path / 'config.cfg')
args = ('--config', conffile)
with open(conffile, 'w') as f:
f.write("""\
[codespell]
skip = bad.txt, whatever.txt
count =
""")
else:
assert kind == 'toml'
pytest.importorskip('tomli')
tomlfile = str(tmp_path / 'pyproject.toml')
args = ('--toml', tomlfile)
with open(tomlfile, 'w') as f:
f.write("""\
[tool.codespell]
skip = 'bad.txt,whatever.txt'
count = false
""")

# Should pass when skipping bad.txt
code, stdout, _ = cs.main('--config', conffile, d, count=True, std=True)
code, stdout, _ = cs.main(str(d), *args, count=True, std=True)
assert code == 0
assert 'bad.txt' not in stdout

Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[tool:pytest]
addopts = --cov=codespell_lib --showlocals -rs --cov-report=
addopts = --cov=codespell_lib -rs --cov-report= --tb=short

[flake8]
exclude = build, ci-helpers
Expand Down
7 changes: 5 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
'Operating System :: Unix',
'Operating System :: MacOS'],
platforms='any',
python_requires='>=3.6',
python_requires='>=3.7',
packages=[
'codespell_lib',
'codespell_lib.tests',
Expand All @@ -58,9 +58,12 @@
'codespell = codespell_lib:_script_main'
],
},
# TODO: toml will need to be updated when 3.11 comes out as it's a
# CPython module there
extras_require={
"dev": ["check-manifest", "flake8", "pytest", "pytest-cov",
"pytest-dependency"],
"pytest-dependency", "tomli"],
"hard-encoding-detection": ["chardet"],
"toml": ["tomli"],
}
)

0 comments on commit 2a0ee4b

Please sign in to comment.