Option C Tools
Loading...
Searching...
No Matches
conftest.py
Go to the documentation of this file.
1#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3# tests/tests_health/conftest.py
4
5"""
6Shared fixtures for health dashboard tests.
7
8Purpose
9-------
10Provide reusable test fixtures for health check unit and integration tests.
11
12Responsibilities
13----------------
14- Create temporary Option C projects with required directory structure.
15- Provide LinterContext for health check functions.
16- Create compliant and non-compliant test files.
17
18Diagnostics
19-----------
20Domain: TESTS.HEALTH
21Levels:
22 L2 — test lifecycle and setup
23 L3 — fixture creation and teardown
24 L4 — file operations and structure verification
25
26Contracts
27---------
28- temp_project_root fixture provides a clean, isolated test project.
29- LinterContext is properly configured for the temporary project.
30"""
31
32import pytest
33from pathlib import Path
34
35from oct.linter.oct_lint import LinterContext
36
37
38@pytest.fixture
39def temp_project_root(tmp_path: Path) -> Path:
40 """
41 Create a temporary Option C project root with required directory structure.
42
43 Returns the project root path. All required directories are created.
44 """
45 root = tmp_path / "test-project"
46 root.mkdir()
47
48 # Create required directories
49 (root / "docs").mkdir()
50 (root / "tests").mkdir()
51 (root / "oc_diagnostics").mkdir()
52 (root / "logs").mkdir()
53
54 return root
55
56
57@pytest.fixture
58def linter_ctx(temp_project_root: Path) -> LinterContext:
59 """
60 Return a LinterContext configured for the temporary test project.
61 """
62 return LinterContext(
63 project_root=temp_project_root,
64 project_name=temp_project_root.name,
65 diagnostics_dir=temp_project_root / "oc_diagnostics",
66 tests_dir=temp_project_root / "tests",
67 docs_dir=temp_project_root / "docs",
68 )
69
70
71def make_compliant_file(filename: str) -> str:
72 """Generate a fully compliant Option C file with the given filename."""
73 return (
74 "#!/usr/bin/env python3\n"
75 "# -*- coding: utf-8 -*-\n"
76 f"# {filename}\n"
77 "\n"
78 '"""\n'
79 "Example module.\n"
80 "\n"
81 "Purpose\n"
82 "-------\n"
83 "Demonstrate a fully compliant Option C file.\n"
84 "\n"
85 "Responsibilities\n"
86 "----------------\n"
87 "- Serve as a test fixture.\n"
88 "\n"
89 "Diagnostics\n"
90 "-----------\n"
91 "Domain: TEST\n"
92 "Levels:\n"
93 " L2 -- lifecycle\n"
94 " L3 -- details\n"
95 " L4 -- tracing\n"
96 "\n"
97 "Contracts\n"
98 "---------\n"
99 "- Must be valid Python.\n"
100 "\n"
101 "Dependencies\n"
102 "------------\n"
103 "- oc_diagnostics (structured debug logger)\n"
104 '"""\n'
105 "\n"
106 "from oc_diagnostics import _dbg\n"
107 "\n"
108 "\n"
109 "def example():\n"
110 ' func = "example"\n'
111 ' _dbg("TEST", func, "start", 2)\n'
112 " return 42\n"
113 )
114
115
116NON_COMPLIANT_FILE = '''\
117# Missing header
118def broken():
119 return 1
120'''
121
122
123def make_func_pattern_violation_file(filename: str) -> str:
124 """Generate a file that is compliant except for a missing func pattern.
125
126 The file has a function that calls ``_dbg()`` without first assigning
127 ``func = "..."``. The file is long enough (>= 20 lines) to avoid
128 the trivial-file exemption.
129 """
130 return (
131 "#!/usr/bin/env python3\n"
132 "# -*- coding: utf-8 -*-\n"
133 f"# {filename}\n"
134 "\n"
135 '"""\n'
136 "Example module with func_pattern violation.\n"
137 "\n"
138 "Purpose\n"
139 "-------\n"
140 "Test fixture for fixable func_pattern violations.\n"
141 "\n"
142 "Responsibilities\n"
143 "----------------\n"
144 "- Serve as a test fixture.\n"
145 "\n"
146 "Diagnostics\n"
147 "-----------\n"
148 "Domain: TEST\n"
149 "Levels:\n"
150 " L2 -- lifecycle\n"
151 " L3 -- details\n"
152 " L4 -- tracing\n"
153 "\n"
154 "Contracts\n"
155 "---------\n"
156 "- Must be valid Python.\n"
157 "\n"
158 "Dependencies\n"
159 "------------\n"
160 "- oc_diagnostics (structured debug logger)\n"
161 '"""\n'
162 "\n"
163 "from oc_diagnostics import _dbg\n"
164 "\n"
165 "\n"
166 "def missing_func_var():\n"
167 ' _dbg("TEST", "missing_func_var", "start", 2)\n'
168 " return 42\n"
169 )
Path temp_project_root(Path tmp_path)
Definition conftest.py:39
str make_func_pattern_violation_file(str filename)
Definition conftest.py:123
str make_compliant_file(str filename)
Definition conftest.py:71
LinterContext linter_ctx(Path temp_project_root)
Definition conftest.py:58