Option C Tools
Loading...
Searching...
No Matches
test_validate_config_strict.py
Go to the documentation of this file.
1#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3# tests/tests_diag/test_validate_config_strict.py
4
5"""
6Purpose
7-------
8OI-534 / FS-513 — ``oct diag validate-config --strict`` promotes
9WARNING-level issues to failures and returns exit code 2 so CI can
10distinguish a strict-mode config audit failure from the generic exit 1
11used by the non-strict path (reserved for ERROR-level schema problems).
12
13The tests use a temporary ``oc_diagnostics/debug_config.json`` layout so
14we never touch the real project config.
15
16Responsibilities
17----------------
18- Verify non-strict mode: ERROR exits 1, WARNING exits 0.
19- Verify strict mode: both ERROR and WARNING exit 2, clean exits 0.
20
21Diagnostics
22-----------
23Domain: DIAG-TESTS
24Levels:
25 L2 — test lifecycle
26 L3 — assertion details
27 L4 — deep tracing
28
29Contracts
30---------
31- Non-strict: ERROR → exit 1, WARNING → exit 0.
32- Strict: ERROR → exit 2, WARNING → exit 2, clean → exit 0.
33"""
34
35from __future__ import annotations
36
37import json
38import os
39from pathlib import Path
40
41import pytest
42from click.testing import CliRunner
43
44from oct.cli import cli
45
46
47def _write_config(project_root: Path, payload: dict) -> Path:
48 """Drop a debug_config.json at ``<root>/oc_diagnostics/`` and return it.
49
50 ``find_project_root`` anchors on ``.octrc.json`` with ``root_marker``
51 set — use that rather than simulating the full ``docs/tests/...``
52 layout so the fixture stays lean.
53 """
54 cfg_dir = project_root / "oc_diagnostics"
55 cfg_dir.mkdir(parents=True, exist_ok=True)
56 cfg = cfg_dir / "debug_config.json"
57 cfg.write_text(json.dumps(payload, indent=2), encoding="utf-8")
58 (project_root / ".octrc.json").write_text(
59 json.dumps({"root_marker": True}), encoding="utf-8",
60 )
61 return cfg
62
63
64def _invoke(cwd: Path, *args: str):
65 """Run ``oct diag validate-config ...`` with cwd pinned to ``cwd``."""
66 runner = CliRunner()
67 prev = os.getcwd()
68 try:
69 os.chdir(cwd)
70 return runner.invoke(cli, ["diag", "validate-config", *args])
71 finally:
72 os.chdir(prev)
73
74
76 """OI-534: a fully-valid config exits 0 regardless of --strict."""
77 _write_config(tmp_path, {
78 "_schema_version": "2.0",
79 "DEBUG_LEVELS": {"GENERAL": {"level": 2, "color": "default"}},
80 "global_settings": {"output_mode": "both", "mode": "dev"},
81 })
82 for args in ([], ["--strict"]):
83 result = _invoke(tmp_path, *args)
84 assert result.exit_code == 0, (args, result.output)
85
86
88 """OI-534: WARNING issues alone exit 0 when --strict is absent."""
89 # Missing _schema_version, missing global_settings → WARNINGs only.
90 _write_config(tmp_path, {"domains": {"GENERAL": {"level": 2}}})
91 result = _invoke(tmp_path)
92 assert result.exit_code == 0, result.output
93 assert "WARNING" in result.output
94
95
97 """OI-534: --strict promotes WARNING to exit 2."""
98 _write_config(tmp_path, {"domains": {"GENERAL": {"level": 2}}})
99 result = _invoke(tmp_path, "--strict")
100 assert result.exit_code == 2, result.output
101 assert "WARNING" in result.output
102
103
105 """OI-534: non-strict ERROR → exit 1 (preserves legacy behaviour)."""
106 # level=99 is out of the 0-4 range → ERROR.
107 _write_config(tmp_path, {
108 "_schema_version": "2.0",
109 "DEBUG_LEVELS": {"GENERAL": {"level": 99, "color": "default"}},
110 "global_settings": {"output_mode": "both", "mode": "dev"},
111 })
112 result = _invoke(tmp_path)
113 assert result.exit_code == 1, result.output
114 assert "ERROR" in result.output
115
116
118 """OI-534: --strict upgrades ERROR exit from 1 to 2."""
119 _write_config(tmp_path, {
120 "_schema_version": "2.0",
121 "DEBUG_LEVELS": {"GENERAL": {"level": 99, "color": "default"}},
122 "global_settings": {"output_mode": "both", "mode": "dev"},
123 })
124 result = _invoke(tmp_path, "--strict")
125 assert result.exit_code == 2, result.output
126 assert "ERROR" in result.output
Definition cli.py:1
Path _write_config(Path project_root, dict payload)