2017-09-11 20:01:08 +02:00
|
|
|
import os
|
2020-06-11 00:54:34 +02:00
|
|
|
from io import StringIO
|
2017-09-11 20:01:08 +02:00
|
|
|
from unittest import TestCase
|
2020-06-11 00:54:34 +02:00
|
|
|
from unittest.mock import patch
|
2017-09-11 20:01:08 +02:00
|
|
|
|
2019-05-30 17:41:23 +02:00
|
|
|
from zulint.custom_rules import RuleList
|
2020-06-11 00:54:34 +02:00
|
|
|
|
2021-07-03 08:22:44 +02:00
|
|
|
from tools.linter_lib.custom_check import non_py_rules, python_rules
|
2017-09-11 20:01:08 +02:00
|
|
|
|
2021-02-12 08:20:45 +01: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
|
|
|
|
2017-09-13 15:31:00 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
class TestRuleList(TestCase):
|
2017-12-13 06:45:46 +01:00
|
|
|
def setUp(self) -> None:
|
2021-07-03 08:22:44 +02:00
|
|
|
all_rules = list(python_rules.rules)
|
2019-05-30 17:41:23 +02:00
|
|
|
for rule in non_py_rules:
|
2021-07-03 08:22:44 +02:00
|
|
|
all_rules.extend(rule.rules)
|
|
|
|
self.all_rules = all_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:
|
2021-02-12 08:20:45 +01:00
|
|
|
for path in rule.get("exclude", {}):
|
2017-09-13 15:31:00 +02:00
|
|
|
abs_path = os.path.abspath(os.path.join(ROOT_DIR, path))
|
2021-02-12 08:19:30 +01:00
|
|
|
self.assertTrue(
|
|
|
|
os.path.exists(abs_path),
|
|
|
|
f"'{path}' is neither an existing file, nor a directory. {CHECK_MESSAGE}",
|
|
|
|
)
|
2017-09-13 15:31:00 +02:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
for line_tuple in rule.get("exclude_line", {}):
|
2017-09-13 15:31:00 +02:00
|
|
|
path = line_tuple[0]
|
|
|
|
abs_path = os.path.abspath(os.path.join(ROOT_DIR, path))
|
2021-02-12 08:19:30 +01:00
|
|
|
self.assertTrue(
|
|
|
|
os.path.isfile(abs_path), f"The file '{path}' doesn't exist. {CHECK_MESSAGE}"
|
|
|
|
)
|
2017-09-13 15:31:00 +02:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
for path in rule.get("include_only", {}):
|
2017-09-13 15:31:00 +02:00
|
|
|
if not os.path.splitext(path)[1]:
|
2021-02-12 08:19:30 +01:00
|
|
|
self.assertTrue(
|
2021-02-12 08:20:45 +01:00
|
|
|
path.endswith("/"),
|
2021-02-12 08:19:30 +01: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
|
2021-02-12 08:19:30 +01:00
|
|
|
the expectation and doesn't throw false positives."""
|
2017-09-13 18:02:58 +02:00
|
|
|
for rule in self.all_rules:
|
2021-02-12 08:20:45 +01:00
|
|
|
pattern = rule["pattern"]
|
|
|
|
for line in rule.get("good_lines", []):
|
2023-07-22 01:37:06 +02:00
|
|
|
with patch("builtins.open", return_value=StringIO(line + "\n\n"), autospec=True):
|
2021-02-12 08:19:30 +01:00
|
|
|
self.assertFalse(
|
2021-02-12 08:20:45 +01:00
|
|
|
RuleList([], [rule]).custom_check_file("foo.bar", "baz", ""),
|
2021-02-12 08:19:30 +01:00
|
|
|
f"The pattern '{pattern}' matched the line '{line}' while it shouldn't.",
|
|
|
|
)
|
2017-09-13 18:02:58 +02:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
for line in rule.get("bad_lines", []):
|
2021-02-12 08:19:30 +01:00
|
|
|
with patch(
|
2023-07-22 01:37:06 +02:00
|
|
|
"builtins.open", return_value=StringIO(line + "\n\n"), autospec=True
|
2021-02-12 08:20:45 +01:00
|
|
|
), patch("builtins.print"):
|
|
|
|
filename = list(rule.get("include_only", {"foo.bar"}))[0]
|
2021-02-12 08:19:30 +01:00
|
|
|
self.assertTrue(
|
2021-02-12 08:20:45 +01:00
|
|
|
RuleList([], [rule]).custom_check_file(filename, "baz", ""),
|
2021-02-12 08:19:30 +01:00
|
|
|
f"The pattern '{pattern}' didn't match the line '{line}' while it should.",
|
|
|
|
)
|