summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAshley Whetter <ashley@awhetter.co.uk>2018-02-17 15:38:11 -0800
committerAshley Whetter <ashley@awhetter.co.uk>2019-02-09 13:25:20 -0800
commite0901156f8a7537378a5f3e80c5a9dab333cca7e (patch)
tree0a83588ed3006fb6b3833fd685e1b1c9697d7b24
parent80104b1d8b257b3f711c77d7214d1fccb8bc5329 (diff)
downloadpylint-git-e0901156f8a7537378a5f3e80c5a9dab333cca7e.tar.gz
Fixed some tests
-rw-r--r--pylint/checkers/__init__.py2
-rw-r--r--pylint/config.py3
-rw-r--r--pylint/lint.py57
-rw-r--r--pylint/test/test_functional.py15
-rw-r--r--pylint/test/test_self.py7
-rw-r--r--pylint/test/unittest_config.py12
-rw-r--r--pylint/test/unittest_lint.py48
-rw-r--r--pylint/test/unittest_reporters_json.py10
-rw-r--r--pylint/test/unittest_reporting.py12
-rw-r--r--pylint/testutils.py14
-rw-r--r--pylint/utils.py2
11 files changed, 73 insertions, 109 deletions
diff --git a/pylint/checkers/__init__.py b/pylint/checkers/__init__.py
index ed649707d..95c225440 100644
--- a/pylint/checkers/__init__.py
+++ b/pylint/checkers/__init__.py
@@ -124,7 +124,7 @@ def initialize(registry):
"""Register the checkers in this package.
:param registry: The registry to register checkers with.
- :type registry: CheckerRegistry
+ :type registry: PluginRegistry
"""
register_plugins(registry, __path__[0])
diff --git a/pylint/config.py b/pylint/config.py
index d70e1123b..1b4c4435b 100644
--- a/pylint/config.py
+++ b/pylint/config.py
@@ -255,6 +255,9 @@ class Configuration(object):
raise exceptions.ConfigurationError('Option "{0}" already exists.')
self._option_definitions[name] = definition
+ if "default" in definition:
+ dest = definition.get("dest", name)
+ self.set_option(dest, definition["default"])
def add_options(self, option_definitions):
for option_definition in option_definitions:
diff --git a/pylint/lint.py b/pylint/lint.py
index 34b935064..eabae89eb 100644
--- a/pylint/lint.py
+++ b/pylint/lint.py
@@ -1001,45 +1001,6 @@ def report_messages_by_module_stats(sect, stats, _):
# utilities ###################################################################
-class ArgumentPreprocessingError(Exception):
- """Raised if an error occurs during argument preprocessing."""
-
-
-def preprocess_options(args, search_for):
- """look for some options (keys of <search_for>) which have to be processed
- before others
-
- values of <search_for> are callback functions to call when the option is
- found
- """
- i = 0
- while i < len(args):
- arg = args[i]
- if arg.startswith("--"):
- try:
- option, val = arg[2:].split("=", 1)
- except ValueError:
- option, val = arg[2:], None
- try:
- cb, takearg = search_for[option]
- except KeyError:
- i += 1
- else:
- del args[i]
- if takearg and val is None:
- if i >= len(args) or args[i].startswith("-"):
- msg = "Option %s expects a value" % option
- raise ArgumentPreprocessingError(msg)
- val = args[i]
- del args[i]
- elif not takearg and val is not None:
- msg = "Option %s doesn't expects a value" % option
- raise ArgumentPreprocessingError(msg)
- cb(option, val)
- else:
- i += 1
-
-
@contextlib.contextmanager
def fix_import_path(args):
"""Prepare sys.path for running the linter checks.
@@ -1083,12 +1044,12 @@ def guess_lint_path(args):
return value
-class CheckerRegistry(object):
+class PluginRegistry(object):
"""A class to register checkers to."""
- def __init__(self, linter):
- super(CheckerRegistry, self).__init__()
- self.register_options = lambda options: None
+ def __init__(self, linter, register_options=(lambda options: None)):
+ super(PluginRegistry, self).__init__()
+ self.register_options = register_options
self._checkers = collections.defaultdict(list)
# TODO: Remove. This is needed for the MessagesHandlerMixIn for now.
linter._checkers = self._checkers
@@ -1289,7 +1250,7 @@ group are mutually exclusive.",
def __init__(self):
super(CLIRunner, self).__init__()
self._linter = PyLinter()
- self._checker_registry = CheckerRegistry(self._linter)
+ self._plugin_registry = PluginRegistry(self._linter)
self._loaded_plugins = set()
def run(self, args):
@@ -1354,9 +1315,9 @@ group are mutually exclusive.",
parser.add_option_definitions(options)
file_parser.add_option_definitions(options)
- self._checker_registry.register_options = register_options
+ self._plugin_registry.register_options = register_options
- checkers.initialize(self._checker_registry)
+ checkers.initialize(self._plugin_registry)
# Load plugins from CLI
plugins = parsed.load_plugins or []
@@ -1411,7 +1372,7 @@ group are mutually exclusive.",
self._linter.disable("I")
self._linter.enable("c-extension-no-member")
- for checker in self._checker_registry.for_all_checkers():
+ for checker in self._plugin_registry.for_all_checkers():
checker.config = global_config
with fix_import_path(global_config.module_or_package):
@@ -1431,7 +1392,7 @@ group are mutually exclusive.",
warnings.warn(msg)
else:
module = astroid.modutils.load_module_from_name(module_name)
- module.register(self._checker_registry)
+ module.register(self._plugin_registry)
def load_plugins(self, module_names):
"""Load a plugin.
diff --git a/pylint/test/test_functional.py b/pylint/test/test_functional.py
index 576cd0d56..05d401dc3 100644
--- a/pylint/test/test_functional.py
+++ b/pylint/test/test_functional.py
@@ -30,6 +30,7 @@ from pylint import checkers
from pylint import interfaces
from pylint import lint
from pylint import reporters
+import pylint.config
class test_dialect(csv.excel):
if sys.version_info[0] < 3:
@@ -232,15 +233,17 @@ class LintModuleTest(object):
def __init__(self, test_file):
test_reporter = FunctionalTestReporter()
self._linter = lint.PyLinter()
+ global_config = pylint.config.Configuration()
+ self._linter.config = global_config
+ global_config.add_options(self._linter.options)
+ registry = lint.PluginRegistry(self._linter)
+ file_parser = pylint.config.IniFileParser()
+ file_parser.add_option_definitions(self._linter.options)
self._linter.set_reporter(test_reporter)
self._linter.config.persistent = 0
- checkers.initialize(self._linter)
+ checkers.initialize(registry)
self._linter.disable('I')
- try:
- self._linter.read_config_file(test_file.option_file)
- self._linter.load_config_file()
- except NoFileError:
- pass
+ file_parser.parse(test_file.option_file, global_config)
self._test_file = test_file
def setUp(self):
diff --git a/pylint/test/test_self.py b/pylint/test/test_self.py
index 2c3907e05..e63eb9f5b 100644
--- a/pylint/test/test_self.py
+++ b/pylint/test/test_self.py
@@ -31,7 +31,7 @@ import textwrap
import configparser
from io import StringIO
-from pylint.lint import Run
+from pylint.lint import CLIRunner
from pylint.reporters import BaseReporter
from pylint.reporters.text import *
from pylint.reporters.json import JSONReporter
@@ -117,7 +117,8 @@ class TestRunTC(object):
with pytest.raises(SystemExit) as cm:
with warnings.catch_warnings():
warnings.simplefilter("ignore")
- Run(args, reporter=reporter)
+ runner = CLIRunner()
+ runner.run(args)
return cm.value.code
def _clean_paths(self, output):
@@ -227,6 +228,7 @@ class TestRunTC(object):
code=28,
)
+ @pytest.mark.xfail(reason="Removed parallel execution for now")
def test_parallel_execution(self):
self._runtest(
[
@@ -237,6 +239,7 @@ class TestRunTC(object):
code=2,
)
+ @pytest.mark.xfail(reason="Removed parallel execution for now")
def test_parallel_execution_missing_arguments(self):
self._runtest(["-j 2", "not_here", "not_here_too"], code=1)
diff --git a/pylint/test/unittest_config.py b/pylint/test/unittest_config.py
index 17e48d5a8..327870640 100644
--- a/pylint/test/unittest_config.py
+++ b/pylint/test/unittest_config.py
@@ -20,19 +20,19 @@ RE_PATTERN_TYPE = getattr(re, "Pattern", getattr(re, "_pattern_type", None))
def test__regexp_validator_valid():
- result = config._regexp_validator(None, None, "test_.*")
+ result = config.VALIDATORS["regex"]("test_.*")
assert isinstance(result, RE_PATTERN_TYPE)
assert result.pattern == "test_.*"
def test__regexp_validator_invalid():
with pytest.raises(sre_constants.error):
- config._regexp_validator(None, None, "test_)")
+ config.VALIDATORS["regex"]("test_)")
def test__csv_validator_no_spaces():
values = ["One", "Two", "Three"]
- result = config._csv_validator(None, None, ",".join(values))
+ result = config.VALIDATORS["csv"](",".join(values))
assert isinstance(result, list)
assert len(result) == 3
for i, value in enumerate(values):
@@ -41,7 +41,7 @@ def test__csv_validator_no_spaces():
def test__csv_validator_spaces():
values = ["One", "Two", "Three"]
- result = config._csv_validator(None, None, ", ".join(values))
+ result = config.VALIDATORS["csv"](", ".join(values))
assert isinstance(result, list)
assert len(result) == 3
for i, value in enumerate(values):
@@ -50,7 +50,7 @@ def test__csv_validator_spaces():
def test__regexp_csv_validator_valid():
pattern_strings = ["test_.*", "foo\\.bar", "^baz$"]
- result = config._regexp_csv_validator(None, None, ",".join(pattern_strings))
+ result = config.VALIDATORS["regexp_csv"](",".join(pattern_strings))
for i, regex in enumerate(result):
assert isinstance(regex, RE_PATTERN_TYPE)
assert regex.pattern == pattern_strings[i]
@@ -59,4 +59,4 @@ def test__regexp_csv_validator_valid():
def test__regexp_csv_validator_invalid():
pattern_strings = ["test_.*", "foo\\.bar", "^baz)$"]
with pytest.raises(sre_constants.error):
- config._regexp_csv_validator(None, None, ",".join(pattern_strings))
+ config.VALIDATORS["regexp_csv"](",".join(pattern_strings))
diff --git a/pylint/test/unittest_lint.py b/pylint/test/unittest_lint.py
index 4d985192e..072df1744 100644
--- a/pylint/test/unittest_lint.py
+++ b/pylint/test/unittest_lint.py
@@ -38,7 +38,7 @@ from importlib import reload
from io import StringIO
from pylint import config, lint
-from pylint.lint import PyLinter, Run, preprocess_options, ArgumentPreprocessingError
+from pylint.lint import CLIRunner, PluginRegistry, PyLinter
from pylint.utils import (
MSG_STATE_SCOPE_CONFIG,
MSG_STATE_SCOPE_MODULE,
@@ -562,10 +562,15 @@ def test_load_plugin_configuration():
def test_init_hooks_called_before_load_plugins():
+ runner = CLIRunner()
with pytest.raises(RuntimeError):
- Run(["--load-plugins", "unexistant", "--init-hook", "raise RuntimeError"])
+ runner.run(
+ ["--load-plugins", "unexistant", "--init-hook", "raise RuntimeError"]
+ )
with pytest.raises(RuntimeError):
- Run(["--init-hook", "raise RuntimeError", "--load-plugins", "unexistant"])
+ runner.run(
+ ["--init-hook", "raise RuntimeError", "--load-plugins", "unexistant"]
+ )
def test_analyze_explicit_script(linter):
@@ -696,36 +701,6 @@ def test_pylintrc_parentdir_no_package():
assert config.find_pylintrc() == expected
-class TestPreprocessOptions(object):
- def _callback(self, name, value):
- self.args.append((name, value))
-
- def test_value_equal(self):
- self.args = []
- preprocess_options(
- ["--foo", "--bar=baz", "--qu=ux"],
- {"foo": (self._callback, False), "qu": (self._callback, True)},
- )
- assert [("foo", None), ("qu", "ux")] == self.args
-
- def test_value_space(self):
- self.args = []
- preprocess_options(["--qu", "ux"], {"qu": (self._callback, True)})
- assert [("qu", "ux")] == self.args
-
- def test_error_missing_expected_value(self):
- with pytest.raises(ArgumentPreprocessingError):
- preprocess_options(["--foo", "--bar", "--qu=ux"], {"bar": (None, True)})
- with pytest.raises(ArgumentPreprocessingError):
- preprocess_options(["--foo", "--bar"], {"bar": (None, True)})
-
- def test_error_unexpected_value(self):
- with pytest.raises(ArgumentPreprocessingError):
- preprocess_options(
- ["--foo", "--bar=spam", "--qu=ux"], {"bar": (None, False)}
- )
-
-
@pytest.fixture
def store():
store = MessagesStore()
@@ -839,6 +814,13 @@ def test_custom_should_analyze_file():
package_dir = os.path.join(HERE, "regrtest_data", "bad_package")
wrong_file = os.path.join(package_dir, "wrong.py")
+ reporter = testutils.TestReporter()
+ global_config = config.Configuration()
+ linter = CustomPyLinter(global_config)
+ registry = PluginRegistry(linter, register_options=global_config.add_options)
+ linter.config.persistent = 0
+ linter.open()
+ linter.set_reporter(reporter)
for jobs in [1, 2]:
reporter = testutils.TestReporter()
diff --git a/pylint/test/unittest_reporters_json.py b/pylint/test/unittest_reporters_json.py
index ba7853887..083acc5d5 100644
--- a/pylint/test/unittest_reporters_json.py
+++ b/pylint/test/unittest_reporters_json.py
@@ -14,7 +14,8 @@ import json
from io import StringIO
-from pylint.lint import PyLinter
+import pylint.config
+from pylint.lint import PluginRegistry, PyLinter
from pylint import checkers
from pylint.reporters.json import JSONReporter
@@ -23,8 +24,11 @@ def test_simple_json_output():
output = StringIO()
reporter = JSONReporter()
- linter = PyLinter(reporter=reporter)
- checkers.initialize(linter)
+ global_config = pylint.config.Configuration()
+ linter = PyLinter(global_config)
+ registry = PluginRegistry(linter, register_options=global_config.add_options)
+ checkers.initialize(registry)
+ linter.set_reporter(reporter)
linter.config.persistent = 0
linter.reporter.set_output(output)
diff --git a/pylint/test/unittest_reporting.py b/pylint/test/unittest_reporting.py
index 88f2ce4d5..14b05f0fc 100644
--- a/pylint/test/unittest_reporting.py
+++ b/pylint/test/unittest_reporting.py
@@ -14,7 +14,8 @@ import warnings
from io import StringIO
-from pylint.lint import PyLinter
+import pylint.config
+from pylint.lint import PluginRegistry, PyLinter
from pylint import checkers
from pylint.reporters.text import TextReporter, ParseableTextReporter
import pytest
@@ -55,12 +56,15 @@ def test_parseable_output_deprecated():
def test_parseable_output_regression():
output = StringIO()
with warnings.catch_warnings(record=True):
- linter = PyLinter(reporter=ParseableTextReporter())
+ global_config = pylint.config.Configuration()
+ linter = PyLinter(global_config)
+ registry = PluginRegistry(linter, register_options=global_config.add_options)
+ linter.set_reporter(ParseableTextReporter())
- checkers.initialize(linter)
+ checkers.initialize(registry)
linter.config.persistent = 0
linter.reporter.set_output(output)
- linter.set_option("output-format", "parseable")
+ linter.config.output_format = "parseable"
linter.open()
linter.set_current_module("0123")
linter.add_message("line-too-long", line=1, args=(1, 2))
diff --git a/pylint/testutils.py b/pylint/testutils.py
index f22b897e8..8b802cf16 100644
--- a/pylint/testutils.py
+++ b/pylint/testutils.py
@@ -37,10 +37,11 @@ from io import StringIO
import astroid
from pylint import checkers
+import pylint.config
from pylint.utils import PyLintASTWalker
from pylint.reporters import BaseReporter
from pylint.interfaces import IReporter
-from pylint.lint import PyLinter
+from pylint.lint import PluginRegistry, PyLinter
# Utils
@@ -173,6 +174,7 @@ class UnittestLinter:
def __init__(self):
self._messages = []
self.stats = {}
+ self.config = pylint.config.Configuration()
def release_messages(self):
try:
@@ -225,8 +227,10 @@ class CheckerTestCase:
def setup_method(self):
self.linter = UnittestLinter()
- self.checker = self.CHECKER_CLASS(self.linter) # pylint: disable=not-callable
- for key, value in self.CONFIG.items():
+ registry = pylint.config.PluginRegistry(self.linter)
+ registry.register_options = self.linter.config.add_options
+ self.checker = self.CHECKER_CLASS(registry) # pylint: disable=not-callable
+ for key, value in six.iteritems(self.CONFIG):
setattr(self.checker.config, key, value)
self.checker.open()
@@ -261,10 +265,10 @@ class CheckerTestCase:
# Init
test_reporter = TestReporter()
-linter = PyLinter()
+linter = PyLinter(pylint.config.Configuration())
linter.set_reporter(test_reporter)
linter.config.persistent = 0
-checkers.initialize(linter)
+checkers.initialize(PluginRegistry(linter))
def _tokenize_str(code):
diff --git a/pylint/utils.py b/pylint/utils.py
index 513ed4666..417ccbcb5 100644
--- a/pylint/utils.py
+++ b/pylint/utils.py
@@ -1344,7 +1344,7 @@ def register_plugins(registry, directory):
"""Load plugins from all modules and packages in the given directory.
Args:
- registry (CheckerRegistry): The registry to register the checkers with.
+ registry (PluginRegistry): The registry to register the checkers with.
directory (str): The directory to search for plugins.
"""
imported = {"__init__", "__pycache__"}