Option C Tools
Loading...
Searching...
No Matches
test_oct_lint_schema_warning.py
Go to the documentation of this file.
1#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3# tests/tests_linter/test_oct_lint_schema_warning.py
4
5"""
6Purpose
7-------
8OI-501 regression tests — ``.octrc.json`` schema errors must *warn* (not crash)
9in non-strict mode, and *raise* ``SystemExit(2)`` only when strict-config is
10enabled. Prior to the fix, ``raise SystemExit(2)`` was indented at the wrong
11level, so every schema error crashed the linter unconditionally.
12
13Responsibilities
14----------------
15- Non-strict mode + schema error → warning printed to stderr, no SystemExit(2).
16- Strict mode + schema error → SystemExit(2).
17- Strict mode + malformed JSON → SystemExit(2) (unchanged behaviour).
18
19Diagnostics
20-----------
21Domain: LINTER-TESTS
22Levels:
23 L2 — test lifecycle
24 L3 — assertion details
25 L4 — deep tracing
26
27Contracts
28---------
29- ``.octrc.json`` handling must not raise in non-strict mode.
30"""
31
32import io
33import sys
34from pathlib import Path
35
36import pytest
37
38from oct.linter import oct_lint
39
40
41def _write_octrc(root: Path, content: str) -> None:
42 (root / ".octrc.json").write_text(content, encoding="utf-8")
43
44
46 temp_project_root: Path, monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture
47):
48 monkeypatch.delenv("OCT_STRICT_CONFIG", raising=False)
49 # profile="bogus" → recognised schema error, not a JSON error.
50 _write_octrc(temp_project_root, '{"linter": {"profile": "bogus"}}')
51
52 # Must not raise SystemExit(2); linter may exit 0/1 based on lint results.
53 try:
54 oct_lint.run_linter(temp_project_root, argv=[])
55 except SystemExit as e:
56 assert e.code != 2, f"non-strict mode must not SystemExit(2); got {e.code}"
57
58 err = capsys.readouterr().err
59 assert "Warning" in err
60 assert ".octrc.json schema errors" in err
61
62
64 temp_project_root: Path, monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture
65):
66 monkeypatch.setenv("OCT_STRICT_CONFIG", "1")
67 _write_octrc(temp_project_root, '{"linter": {"profile": "bogus"}}')
68
69 with pytest.raises(SystemExit) as exc_info:
70 oct_lint.run_linter(temp_project_root, argv=[])
71
72 assert exc_info.value.code == 2
73 err = capsys.readouterr().err
74 assert "Error" in err
75 assert ".octrc.json schema errors" in err
76
77
79 temp_project_root: Path, monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture
80):
81 monkeypatch.setenv("OCT_STRICT_CONFIG", "1")
82 _write_octrc(temp_project_root, "{ not valid json")
83
84 with pytest.raises(SystemExit) as exc_info:
85 oct_lint.run_linter(temp_project_root, argv=[])
86
87 assert exc_info.value.code == 2
test_schema_error_non_strict_warns_but_does_not_exit(Path temp_project_root, pytest.MonkeyPatch monkeypatch, pytest.CaptureFixture capsys)
test_schema_error_strict_mode_exits_2(Path temp_project_root, pytest.MonkeyPatch monkeypatch, pytest.CaptureFixture capsys)
test_malformed_json_strict_mode_exits_2(Path temp_project_root, pytest.MonkeyPatch monkeypatch, pytest.CaptureFixture capsys)