Skip to content

Commit

Permalink
Fix validate config (#481)
Browse files Browse the repository at this point in the history
* Fixed path issue in validate-config

* Made paths relative to config

* Make union optional

* Updated parse_rule to use root of config
  • Loading branch information
surge119 authored Aug 15, 2024
1 parent f1ea091 commit 4a85c0a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
11 changes: 5 additions & 6 deletions src/fixit/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -602,12 +602,13 @@ def validate_config(path: Path) -> List[str]:
"""
exceptions: List[str] = []
try:
root = path.parent
configs = read_configs([path])[0]

def validate_rules(rules: List[str], path: Path, context: str) -> None:
def validate_rules(rules: List[str], context: str) -> None:
for rule in rules:
try:
qualified_rule = parse_rule(rule, path, configs)
qualified_rule = parse_rule(rule, root, configs)
try:
for _ in find_rules(qualified_rule):
pass
Expand All @@ -621,19 +622,17 @@ def validate_rules(rules: List[str], path: Path, context: str) -> None:
)

data = configs.data
validate_rules(data.get("enable", []), path, "global enable")
validate_rules(data.get("disable", []), path, "global disable")
validate_rules(data.get("enable", []), "global enable")
validate_rules(data.get("disable", []), "global disable")

for override in data.get("overrides", []):
override_path = Path(override.get("path", path))
validate_rules(
override.get("enable", []),
override_path,
f"override enable: `{override_path}`",
)
validate_rules(
override.get("disable", []),
override_path,
f"override disable: `{override_path}`",
)

Expand Down
29 changes: 29 additions & 0 deletions src/fixit/tests/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,35 @@ def test_validate_config(self) -> None:

self.assertEqual(results, [])

def test_validate_config_with_override(self) -> None:
with self.subTest("validate-config valid with overrides"):
with TemporaryDirectory() as td:
tdp = Path(td).resolve()
path = tdp / ".fixit.toml"
(tdp / "rule/ruledir").mkdir(parents=True, exist_ok=True)

(tdp / "rule/rule.py").write_text("# Rule")
(tdp / "rule/ruledir/rule.py").write_text("# Rule")
path.write_text(
"""
[tool.fixit]
disable = ["fixit.rules"]
root = true
[[tool.fixit.overrides]]
path = "SUPER_REAL_PATH"
enable = [".rule.rule"]
[[tool.fixit.overrides]]
path = "SUPER_REAL_PATH/BUT_ACTUALLY_REAL"
enable = [".rule.ruledir.rule"]
"""
)

results = config.validate_config(path)

self.assertEqual(results, [])

with self.subTest("validate-config invalid config"):
with TemporaryDirectory() as td:
tdp = Path(td).resolve()
Expand Down

0 comments on commit 4a85c0a

Please sign in to comment.