Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions scripts/lint_configs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import os
import argparse
import yaml

def lint_directory(config_dir):
# --- FIX 1: Path Validation ---
if not os.path.isdir(config_dir):
print(f"🚨 Error: The path '{config_dir}' does not exist or is not a directory.")
exit(1)

print(f"🔍 Linting YAML files in '{config_dir}/'...\n")

error_count = 0
file_count = 0

for root, _, files in os.walk(config_dir):
for file in files:
if file.endswith((".yaml", ".yml")):
file_count += 1
filepath = os.path.join(root, file)

# --- FIX 2: File Read Robustness ---
try:
with open(filepath, 'r', encoding='utf-8') as f:
yaml.safe_load(f)
except OSError as e:
error_count += 1
print(f"❌ IO Error in: {filepath}\n Details: {e}\n")
except yaml.YAMLError as exc:
error_count += 1
print(f"❌ Syntax Error in: {filepath}")
if hasattr(exc, 'problem_mark'):
mark = exc.problem_mark
print(f" Hint: Check line {mark.line + 1}, column {mark.column + 1}.\n")
else:
print(f" Details: {exc}\n")

if error_count == 0:
print(f"✅ Success! Checked {file_count} files and found no errors.")
else:
print(f"🚨 Failed: Found {error_count} error(s).")
exit(1)

if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Lint YAML configuration files.")
parser.add_argument("--path", type=str, default="config", help="Path to config directory")
args = parser.parse_args()
lint_directory(args.path)
17 changes: 17 additions & 0 deletions tests/test_lint_configs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import pytest
import subprocess
import os

def test_lint_success():
# Tests a valid directory (the default 'config' folder)
result = subprocess.run(["python", "scripts/lint_configs.py", "--path", "config"], capture_output=True, text=True)
assert result.returncode == 0
assert "Success" in result.stdout

def test_invalid_path():
# Tests a non-existent directory
result = subprocess.run(["python", "scripts/lint_configs.py", "--path", "does-not-exist"], capture_output=True, text=True)
assert result.returncode == 1
assert "Error: The path" in result.stdout

# You can add more complex tests here later, but this covers the 'fail fast' requirement!
Loading