2020-06-03 06:17:06 +02:00
|
|
|
from io import StringIO
|
2017-09-11 20:01:08 +02:00
|
|
|
import os
|
|
|
|
|
2020-05-26 07:16:25 +02:00
|
|
|
from unittest.mock import patch
|
2017-09-11 20:01:08 +02:00
|
|
|
from unittest import TestCase
|
|
|
|
|
2019-05-30 17:41:23 +02:00
|
|
|
from zulint.custom_rules import RuleList
|
|
|
|
from linter_lib.custom_check import python_rules, non_py_rules
|
2017-09-11 20:01:08 +02:00
|
|
|
|
|
|
|
ROOT_DIR = os.path.abspath(os.path.join(__file__, '..', '..', '..'))
|
2019-07-26 04:45:18 +02:00
|
|
|
CHECK_MESSAGE = "Fix the corresponding rule in `tools/linter_lib/custom_check.py`."
|
2017-09-11 20:01:08 +02:00
|
|
|
|
2019-05-30 17:41:23 +02:00
|
|
|
class TestRuleList(TestCase):
|
2017-09-13 15:31:00 +02:00
|
|
|
|
2017-12-13 06:45:46 +01:00
|
|
|
def setUp(self) -> None:
|
2019-05-30 17:41:23 +02:00
|
|
|
self.all_rules = python_rules.rules
|
|
|
|
for rule in non_py_rules:
|
|
|
|
self.all_rules.extend(rule.rules)
|
2017-09-13 15:31:00 +02:00
|
|
|
|
2017-12-13 06:45:46 +01:00
|
|
|
def test_paths_in_rules(self) -> None:
|
2017-09-13 15:28:36 +02:00
|
|
|
"""Verifies that the paths mentioned in linter rules actually exist"""
|
2017-09-13 15:31:00 +02:00
|
|
|
for rule in self.all_rules:
|
|
|
|
for path in rule.get('exclude', {}):
|
|
|
|
abs_path = os.path.abspath(os.path.join(ROOT_DIR, path))
|
|
|
|
self.assertTrue(os.path.exists(abs_path),
|
2020-06-09 00:25:09 +02:00
|
|
|
f"'{path}' is neither an existing file, nor a directory. {CHECK_MESSAGE}")
|
2017-09-13 15:31:00 +02:00
|
|
|
|
|
|
|
for line_tuple in rule.get('exclude_line', {}):
|
|
|
|
path = line_tuple[0]
|
|
|
|
abs_path = os.path.abspath(os.path.join(ROOT_DIR, path))
|
|
|
|
self.assertTrue(os.path.isfile(abs_path),
|
2020-06-09 00:25:09 +02:00
|
|
|
f"The file '{path}' doesn't exist. {CHECK_MESSAGE}")
|
2017-09-13 15:31:00 +02:00
|
|
|
|
|
|
|
for path in rule.get('include_only', {}):
|
|
|
|
if not os.path.splitext(path)[1]:
|
|
|
|
self.assertTrue(path.endswith('/'),
|
2020-06-09 00:25:09 +02:00
|
|
|
f"The path '{path}' should end with '/'. {CHECK_MESSAGE}")
|
2017-09-13 18:02:58 +02:00
|
|
|
|
2017-12-13 06:45:46 +01:00
|
|
|
def test_rule_patterns(self) -> None:
|
2017-09-13 18:02:58 +02:00
|
|
|
"""Verifies that the search regex specified in a custom rule actually matches
|
|
|
|
the expectation and doesn't throw false positives."""
|
|
|
|
for rule in self.all_rules:
|
|
|
|
pattern = rule['pattern']
|
|
|
|
for line in rule.get('good_lines', []):
|
|
|
|
# create=True is superfluous when mocking built-ins in Python >= 3.5
|
2020-06-03 06:17:06 +02:00
|
|
|
with patch('builtins.open', return_value=StringIO(line + '\n\n'), create=True, autospec=True):
|
2019-05-30 17:41:23 +02:00
|
|
|
self.assertFalse(RuleList([], [rule]).custom_check_file('foo.bar', 'baz', ''),
|
2020-06-09 00:25:09 +02:00
|
|
|
f"The pattern '{pattern}' matched the line '{line}' while it shouldn't.")
|
2017-09-13 18:02:58 +02:00
|
|
|
|
|
|
|
for line in rule.get('bad_lines', []):
|
|
|
|
# create=True is superfluous when mocking built-ins in Python >= 3.5
|
|
|
|
with patch('builtins.open',
|
2020-06-03 06:17:06 +02:00
|
|
|
return_value=StringIO(line + '\n\n'), create=True, autospec=True), patch('builtins.print'):
|
2017-11-08 19:18:15 +01:00
|
|
|
filename = list(rule.get('include_only', {'foo.bar'}))[0]
|
2019-05-30 17:41:23 +02:00
|
|
|
self.assertTrue(RuleList([], [rule]).custom_check_file(filename, 'baz', ''),
|
2020-06-09 00:25:09 +02:00
|
|
|
f"The pattern '{pattern}' didn't match the line '{line}' while it should.")
|