Option C Tools
Loading...
Searching...
No Matches
test_octrc_safe.py
Go to the documentation of this file.
1#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3# tests/tests_core/test_octrc_safe.py
4
5"""
6Purpose
7-------
8OI-519 regression tests — :func:`oct.core.octrc.load_octrc` and its silent
9wrapper :func:`load_octrc_safe` must be the single source of truth for
10``.octrc.json`` reads. This test exercises the contract that multiple
11historical readers (linter, formatter, health, typecheck) now rely on.
12
13Responsibilities
14----------------
15- Missing file → ``({}, [])``; ``load_octrc_safe`` → ``{}``.
16- Valid JSON object → parsed dict, no errors.
17- Invalid JSON → ``({}, [<error>])``.
18- Non-object top-level JSON (e.g. a list) → ``({}, [<error>])``.
19- OSError on read → ``({}, [<error>])``.
20
21Diagnostics
22-----------
23Domain: CORE-TESTS
24Levels:
25 L2 — test lifecycle
26 L3 — assertion details
27 L4 — deep tracing
28
29Contracts
30---------
31- All file reads and writes use ``tmp_path``; no real ``.octrc.json``
32 is touched.
33"""
34
35import json
36from pathlib import Path
37
38from oct.core.octrc import load_octrc, load_octrc_safe
39
40
42 data, errors = load_octrc(tmp_path)
43 assert data == {}
44 assert errors == []
45 assert load_octrc_safe(tmp_path) == {}
46
47
49 cfg = {"linter": {"profile": "strict"}, "health": {"test_timeout": 60}}
50 (tmp_path / ".octrc.json").write_text(json.dumps(cfg), encoding="utf-8")
51
52 data, errors = load_octrc(tmp_path)
53 assert data == cfg
54 assert errors == []
55 assert load_octrc_safe(tmp_path) == cfg
56
57
59 (tmp_path / ".octrc.json").write_text("{not valid json", encoding="utf-8")
60
61 data, errors = load_octrc(tmp_path)
62 assert data == {}
63 assert len(errors) == 1
64 assert "invalid JSON" in errors[0]
65 assert load_octrc_safe(tmp_path) == {}
66
67
69 (tmp_path / ".octrc.json").write_text("[1, 2, 3]", encoding="utf-8")
70
71 data, errors = load_octrc(tmp_path)
72 assert data == {}
73 assert len(errors) == 1
74 assert "JSON object" in errors[0]
75 assert "list" in errors[0]
test_missing_file_returns_empty(Path tmp_path)
test_non_object_top_level_returns_error(Path tmp_path)
test_valid_object_returns_dict(Path tmp_path)
test_invalid_json_returns_error(Path tmp_path)