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

Multi-line ignore parsing in config_file #429

Merged
Merged
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
22 changes: 19 additions & 3 deletions pep8.py
Original file line number Diff line number Diff line change
Expand Up @@ -2081,10 +2081,10 @@ def process_options(arglist=None, parse_argv=False, config_file=None,
options = read_config(options, args, arglist, parser)
options.reporter = parse_argv and options.quiet == 1 and FileReport

options.filename = options.filename and options.filename.split(',')
options.filename = _parse_multi_options(options.filename.split(','))
options.exclude = normalize_paths(options.exclude)
options.select = options.select and options.select.split(',')
options.ignore = options.ignore and options.ignore.split(',')
options.select = _parse_multi_options(options.select.split(','))
options.ignore = _parse_multi_options(options.ignore.split(','))

if options.diff:
options.reporter = DiffReport
Expand All @@ -2095,6 +2095,22 @@ def process_options(arglist=None, parse_argv=False, config_file=None,
return options, args


def _parse_multi_options(options):
r"""Split and strip and discard empties.

Turns the following:

A,
B,

into ["A", "B"]
"""
if options:
return [o.strip() for o in options if o.strip()]
else:
return options


def _main():
"""Parse options and run checks on Python source."""
import signal
Expand Down
3 changes: 2 additions & 1 deletion testsuite/test_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,12 @@ def test_own_dog_food(self):


def suite():
from testsuite import test_api, test_shell, test_util
from testsuite import test_api, test_parser, test_shell, test_util

suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(Pep8TestCase))
suite.addTest(unittest.makeSuite(test_api.APITestCase))
suite.addTest(unittest.makeSuite(test_parser.ParserTestCase))
suite.addTest(unittest.makeSuite(test_shell.ShellTestCase))
suite.addTest(unittest.makeSuite(test_util.UtilTestCase))
return suite
Expand Down
61 changes: 61 additions & 0 deletions testsuite/test_parser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import os
import tempfile
import unittest

import pep8


def _process_file(contents):
with tempfile.NamedTemporaryFile(delete=False) as f:
f.write(contents)

options, args = pep8.process_options(config_file=f.name)
os.remove(f.name)

return options, args


class ParserTestCase(unittest.TestCase):

def test_vanilla_ignore_parsing(self):
contents = b"""
[pep8]
ignore = E226,E24
"""
options, args = _process_file(contents)

self.assertEqual(options.ignore, ["E226", "E24"])

def test_multiline_ignore_parsing(self):
contents = b"""
[pep8]
ignore =
E226,
E24
"""

options, args = _process_file(contents)

self.assertEqual(options.ignore, ["E226", "E24"])

def test_trailing_comma_ignore_parsing(self):
contents = b"""
[pep8]
ignore = E226,
"""

options, args = _process_file(contents)

self.assertEqual(options.ignore, ["E226"])

def test_multiline_trailing_comma_ignore_parsing(self):
contents = b"""
[pep8]
ignore =
E226,
E24,
"""

options, args = _process_file(contents)

self.assertEqual(options.ignore, ["E226", "E24"])