Option C Tools
Loading...
Searching...
No Matches
test_config_validation.py
Go to the documentation of this file.
1#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3# tests/tests_linter/test_config_validation.py
4
5"""
6Purpose
7-------
8Validate the ``.octrc.json`` schema validator and strict-mode enforcement (OI-411).
9
10Responsibilities
11----------------
12- Confirm that ``_validate_octrc_schema()`` accepts well-formed configs.
13- Confirm that it rejects unknown profile names, wrong types, and
14 non-boolean rule values.
15- Confirm tolerance for unknown top-level keys.
16
17Diagnostics
18-----------
19Domain: LINTER-TESTS
20L2: test lifecycle
21L3: assertion details
22L4: deep tracing
23
24Contracts
25---------
26Inputs: dict configs
27Outputs: list[str] validation errors
28"""
29
30from oct.linter import oct_lint
31
32
34 """An empty dict is valid — nothing to validate."""
35 errors = oct_lint._validate_octrc_schema({})
36 assert errors == []
37
38
40 """Configs without a linter section are valid."""
41 errors = oct_lint._validate_octrc_schema({"other": "value"})
42 assert errors == []
43
44
46 errors = oct_lint._validate_octrc_schema({"linter": "bogus"})
47 assert errors
48 assert "'linter' must be an object" in errors[0]
49
50
52 errors = oct_lint._validate_octrc_schema({"linter": {"profile": "strict"}})
53 assert errors == []
54
55
57 errors = oct_lint._validate_octrc_schema({"linter": {"profile": "bogus"}})
58 assert any("unknown value 'bogus'" in e for e in errors)
59
60
62 errors = oct_lint._validate_octrc_schema({"linter": {"profile": 123}})
63 assert any("'linter.profile' must be a string" in e for e in errors)
64
65
67 errors = oct_lint._validate_octrc_schema(
68 {"linter": {"exclude_dirs_add": "not_a_list"}}
69 )
70 assert any("'linter.exclude_dirs_add' must be a list" in e for e in errors)
71
72
74 errors = oct_lint._validate_octrc_schema(
75 {"linter": {"exclude_dirs_add": ["ok", 42]}}
76 )
77 assert any("'linter.exclude_dirs_add[1]'" in e for e in errors)
78
79
81 errors = oct_lint._validate_octrc_schema({"linter": {"rules": ["header"]}})
82 assert any("'linter.rules' must be an object" in e for e in errors)
83
84
86 errors = oct_lint._validate_octrc_schema(
87 {"linter": {"rules": {"header": "yes"}}}
88 )
89 assert any("'linter.rules.header' must be a bool" in e for e in errors)
90
91
93 cfg = {
94 "linter": {
95 "profile": "compact",
96 "exclude_dirs_add": ["vendor", "third_party"],
97 "exclude_dirs_remove": ["tests"],
98 "wildcard_exclude_add": ["*_generated"],
99 "rules": {"type_hints": False, "header": True},
100 }
101 }
102 assert oct_lint._validate_octrc_schema(cfg) == []
103
104
106 monkeypatch.delenv("OCT_STRICT_CONFIG", raising=False)
107 assert not oct_lint._strict_config_requested()
108
109
111 monkeypatch.setenv("OCT_STRICT_CONFIG", "1")
112 assert oct_lint._strict_config_requested()
113 monkeypatch.setenv("OCT_STRICT_CONFIG", "true")
114 assert oct_lint._strict_config_requested()
115 monkeypatch.setenv("OCT_STRICT_CONFIG", "0")
116 assert not oct_lint._strict_config_requested()
117
118
119# =====================================================================
120# Phase 4E — G-E8: Git section validation
121# =====================================================================
122
123
125 """Well-formed git section passes validation."""
126 cfg = {
127 "git": {
128 "protected_branches": ["main", "master"],
129 "branch_naming": {"enabled": True, "patterns": [r"^main$"]},
130 "pre_commit_profile": "auto",
131 "conventional_commits": True,
132 "require_changelog_update": False,
133 "large_file_warn_mb": 10,
134 "timeout": 30,
135 "long_timeout": 120,
136 }
137 }
138 assert oct_lint._validate_octrc_schema(cfg) == []
139
140
142 errors = oct_lint._validate_octrc_schema({"git": "bogus"})
143 assert any("'git' must be an object" in e for e in errors)
144
145
147 errors = oct_lint._validate_octrc_schema(
148 {"git": {"protected_branches": "main"}}
149 )
150 assert any("'git.protected_branches' must be a list" in e for e in errors)
151
152
154 errors = oct_lint._validate_octrc_schema(
155 {"git": {"branch_naming": {"enabled": "yes"}}}
156 )
157 assert any("'git.branch_naming.enabled' must be a bool" in e for e in errors)
158
159
161 errors = oct_lint._validate_octrc_schema(
162 {"git": {"pre_commit_profile": "turbo"}}
163 )
164 assert any("unknown value 'turbo'" in e for e in errors)
165
166
168 errors = oct_lint._validate_octrc_schema(
169 {"git": {"timeout": "fast"}}
170 )
171 assert any("'git.timeout' must be a number" in e for e in errors)
172
173
175 errors = oct_lint._validate_octrc_schema(
176 {"git": {"timeout": 0}}
177 )
178 assert any("'git.timeout' must be > 0" in e for e in errors)