Option C Tools
Loading...
Searching...
No Matches
test_log_writer.py
Go to the documentation of this file.
1#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3# tests/tests_core/test_log_writer.py
4
5"""
6Purpose
7-------
8Regression tests for :func:`oct.core.log_writer.write_command_log`.
9
10Responsibilities
11----------------
12- Verify JSON file creation, naming convention, and content fidelity.
13- Verify directory auto-creation and UTF-8 encoding.
14
15Diagnostics
16-----------
17Domain: CORE-TESTS
18Levels:
19 L2 — test lifecycle
20 L3 — assertion details
21 L4 — deep tracing
22
23Contracts
24---------
25- All file writes are confined to ``tmp_path``.
26"""
27
28import json
29from pathlib import Path
30
31from oct.core.log_writer import write_command_log
32
33
34def _setup_option_c(tmp_path: Path) -> Path:
35 (tmp_path / ".option_c").mkdir()
36 return tmp_path
37
38
39def test_writes_json_file(tmp_path: Path):
40 root = _setup_option_c(tmp_path)
41 data = {"status": "ok", "count": 42}
42 path = write_command_log(root, "health", data)
43 assert path.exists()
44 content = json.loads(path.read_text(encoding="utf-8"))
45 assert content == data
46
47
48def test_filename_convention(tmp_path: Path):
49 root = _setup_option_c(tmp_path)
50 path = write_command_log(root, "health", {})
51 assert path.name.startswith("oct-health-")
52 assert path.suffix == ".json"
53
54
55def test_creates_directory(tmp_path: Path):
56 root = _setup_option_c(tmp_path)
57 logs_dir = tmp_path / ".option_c" / "logs"
58 assert not logs_dir.exists()
59 write_command_log(root, "lint", {"ok": True})
60 assert logs_dir.is_dir()
61
62
63def test_report_content_matches(tmp_path: Path):
64 root = _setup_option_c(tmp_path)
65 data = {"project": "demo", "checks": [1, 2, 3], "nested": {"a": "b"}}
66 path = write_command_log(root, "audit", data)
67 assert json.loads(path.read_text(encoding="utf-8")) == data
68
69
70def test_returns_path(tmp_path: Path):
71 root = _setup_option_c(tmp_path)
72 result = write_command_log(root, "test", {})
73 assert isinstance(result, Path)
74 assert result.exists()
75
76
77def test_utf8_encoding(tmp_path: Path):
78 root = _setup_option_c(tmp_path)
79 data = {"message": "café résumé naïve — ñ ü ö"}
80 path = write_command_log(root, "health", data)
81 content = json.loads(path.read_text(encoding="utf-8"))
82 assert content["message"] == data["message"]
test_report_content_matches(Path tmp_path)
test_writes_json_file(Path tmp_path)
test_creates_directory(Path tmp_path)
Path _setup_option_c(Path tmp_path)
test_filename_convention(Path tmp_path)
test_utf8_encoding(Path tmp_path)