summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorDaniƫl van Noord <13665637+DanielNoord@users.noreply.github.com>2021-10-24 12:48:19 +0200
committerGitHub <noreply@github.com>2021-10-24 12:48:19 +0200
commitbfc8d32273102495ee1cfd8daa14d9b6693b11f7 (patch)
tree60785b9a4d5981f06ce2d09a8113803417cab479 /tests
parent8eb7ff153e951f69b4d1ff3cbddc367c797c0aa4 (diff)
downloadpylint-git-bfc8d32273102495ee1cfd8daa14d9b6693b11f7.tar.gz
Normalize input of ``ignore-paths`` for all path types (#5201)
Closes #5194 Co-authored-by: Pierre Sassoulas <pierre.sassoulas@gmail.com>
Diffstat (limited to 'tests')
-rw-r--r--tests/lint/unittest_expand_modules.py104
-rw-r--r--tests/unittest_config.py34
2 files changed, 104 insertions, 34 deletions
diff --git a/tests/lint/unittest_expand_modules.py b/tests/lint/unittest_expand_modules.py
index ef98cf575..4d679ad64 100644
--- a/tests/lint/unittest_expand_modules.py
+++ b/tests/lint/unittest_expand_modules.py
@@ -4,10 +4,14 @@
import re
from pathlib import Path
+from typing import Dict, Tuple, Type
import pytest
+from pylint.checkers import BaseChecker
from pylint.lint.expand_modules import _is_in_ignore_list_re, expand_modules
+from pylint.testutils import CheckerTestCase, set_config
+from pylint.utils.utils import get_global_option
def test__is_in_ignore_list_re_match() -> None:
@@ -21,17 +25,6 @@ def test__is_in_ignore_list_re_match() -> None:
assert _is_in_ignore_list_re("src/tests/whatever.xml", patterns)
-def test__is_in_ignore_list_re_nomatch() -> None:
- patterns = [
- re.compile(".*enchilada.*"),
- re.compile("unittest_.*"),
- re.compile(".*tests/.*"),
- ]
- assert not _is_in_ignore_list_re("test_utils.py", patterns)
- assert not _is_in_ignore_list_re("enchilad.py", patterns)
- assert not _is_in_ignore_list_re("src/tests.py", patterns)
-
-
TEST_DIRECTORY = Path(__file__).parent.parent
INIT_PATH = str(TEST_DIRECTORY / "lint/__init__.py")
EXPAND_MODULES = str(TEST_DIRECTORY / "lint/unittest_expand_modules.py")
@@ -84,27 +77,70 @@ init_of_package = {
}
-@pytest.mark.parametrize(
- "files_or_modules,expected",
- [
- ([__file__], [this_file]),
- (
- [Path(__file__).parent],
- [
- init_of_package,
- test_pylinter,
- test_utils,
- this_file_from_init,
- unittest_lint,
- ],
- ),
- ],
-)
-def test_expand_modules(files_or_modules, expected):
- ignore_list, ignore_list_re, ignore_list_paths_re = [], [], []
- modules, errors = expand_modules(
- files_or_modules, ignore_list, ignore_list_re, ignore_list_paths_re
+class TestExpandModules(CheckerTestCase):
+ """Test the expand_modules function while allowing options to be set"""
+
+ class Checker(BaseChecker):
+ """This dummy checker is needed to allow options to be set"""
+
+ name = "checker"
+ msgs: Dict[str, Tuple[str, ...]] = {}
+ options = (("An option", {"An option": "dict"}),)
+
+ CHECKER_CLASS: Type = Checker
+
+ @pytest.mark.parametrize(
+ "files_or_modules,expected",
+ [
+ ([__file__], [this_file]),
+ (
+ [str(Path(__file__).parent)],
+ [
+ init_of_package,
+ test_pylinter,
+ test_utils,
+ this_file_from_init,
+ unittest_lint,
+ ],
+ ),
+ ],
+ )
+ @set_config(ignore_paths="")
+ def test_expand_modules(self, files_or_modules, expected):
+ """Test expand_modules with the default value of ignore-paths"""
+ ignore_list, ignore_list_re = [], []
+ modules, errors = expand_modules(
+ files_or_modules,
+ ignore_list,
+ ignore_list_re,
+ get_global_option(self, "ignore-paths"),
+ )
+ modules.sort(key=lambda d: d["name"])
+ assert modules == expected
+ assert not errors
+
+ @pytest.mark.parametrize(
+ "files_or_modules,expected",
+ [
+ ([__file__], []),
+ (
+ [str(Path(__file__).parent)],
+ [
+ init_of_package,
+ ],
+ ),
+ ],
)
- modules.sort(key=lambda d: d["name"])
- assert modules == expected
- assert not errors
+ @set_config(ignore_paths=".*/lint/.*")
+ def test_expand_modules_with_ignore(self, files_or_modules, expected):
+ """Test expand_modules with a non-default value of ignore-paths"""
+ ignore_list, ignore_list_re = [], []
+ modules, errors = expand_modules(
+ files_or_modules,
+ ignore_list,
+ ignore_list_re,
+ get_global_option(self.checker, "ignore-paths"),
+ )
+ modules.sort(key=lambda d: d["name"])
+ assert modules == expected
+ assert not errors
diff --git a/tests/unittest_config.py b/tests/unittest_config.py
index 9be02fc4c..9979aa41b 100644
--- a/tests/unittest_config.py
+++ b/tests/unittest_config.py
@@ -16,10 +16,14 @@
import re
import sre_constants
+from typing import Dict, Tuple, Type
import pytest
from pylint import config
+from pylint.checkers import BaseChecker
+from pylint.testutils import CheckerTestCase, set_config
+from pylint.utils.utils import get_global_option
RE_PATTERN_TYPE = getattr(re, "Pattern", getattr(re, "_pattern_type", None))
@@ -65,3 +69,33 @@ def test__regexp_csv_validator_invalid() -> None:
pattern_strings = ["test_.*", "foo\\.bar", "^baz)$"]
with pytest.raises(sre_constants.error):
config.option._regexp_csv_validator(None, None, ",".join(pattern_strings))
+
+
+class TestPyLinterOptionSetters(CheckerTestCase):
+ """Class to check the set_config decorator and get_global_option util
+ for options declared in PyLinter."""
+
+ class Checker(BaseChecker):
+ name = "checker"
+ msgs: Dict[str, Tuple[str, ...]] = {}
+ options = (("An option", {"An option": "dict"}),)
+
+ CHECKER_CLASS: Type = Checker
+
+ @set_config(ignore_paths=".*/tests/.*,.*\\ignore\\.*")
+ def test_ignore_paths_with_value(self) -> None:
+ """Test ignore-paths option with value"""
+ options = get_global_option(self.checker, "ignore-paths")
+
+ assert any(i.match("dir/tests/file.py") for i in options)
+ assert any(i.match("dir\\tests\\file.py") for i in options)
+ assert any(i.match("dir/ignore/file.py") for i in options)
+ assert any(i.match("dir\\ignore\\file.py") for i in options)
+
+ def test_ignore_paths_with_no_value(self) -> None:
+ """Test ignore-paths option with no value.
+ Compare against actual list to see if validator works."""
+ options = get_global_option(self.checker, "ignore-paths")
+
+ # pylint: disable-next=use-implicit-booleaness-not-comparison
+ assert options == []