Option C Tools
Loading...
Searching...
No Matches
test_project_root.py
Go to the documentation of this file.
1#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3# tests/tests_core/test_project_root.py
4
5"""
6Purpose
7-------
8Validate path containment (OI-405) and ``.octrc.json`` size cap (OI-414)
9logic in ``oct.core.project_root``.
10
11Responsibilities
12----------------
13- Confirm ``validate_path_containment`` accepts paths within the project.
14- Confirm it rejects paths outside unless ``allow_outside=True``.
15- Confirm oversized ``.octrc.json`` files are skipped during root
16 discovery and a warning is emitted.
17
18Diagnostics
19-----------
20Domain: CORE-TESTS
21L2: test lifecycle
22L3: assertion details
23L4: deep tracing
24
25Contracts
26---------
27Inputs: tmp_path fixture
28Outputs: pass/fail assertions
29"""
30
31import json
32
33import click
34import pytest
35
36from oct.core import project_root
37
38
40 root = tmp_path / "proj"
41 root.mkdir()
42 inner = root / "src" / "mod"
43 inner.mkdir(parents=True)
44 result = project_root.validate_path_containment(inner, root)
45 assert result == inner.resolve()
46
47
49 root = tmp_path / "proj"
50 root.mkdir()
51 outside = tmp_path / "other"
52 outside.mkdir()
53 with pytest.raises(click.ClickException) as exc:
55 assert "outside project root" in str(exc.value.message)
56
57
59 root = tmp_path / "proj"
60 root.mkdir()
61 outside = tmp_path / "other"
62 outside.mkdir()
64 outside, root, allow_outside=True
65 )
66 assert result == outside.resolve()
67
68
69def test_octrc_size_cap_skipped(tmp_path, capsys):
70 """A ``.octrc.json`` larger than MAX_OCTRC_SIZE is skipped with a warning."""
71 # Build a valid Option C project layout at tmp_path/proj
72 proj = tmp_path / "proj"
73 (proj / "docs").mkdir(parents=True)
74 (proj / "tests").mkdir()
75 (proj / "oc_diagnostics").mkdir()
76
77 # Place an oversized .octrc.json at the project root
78 octrc = proj / ".octrc.json"
79 # Valid JSON but padded to exceed the size cap
80 padding = "x" * (project_root.MAX_OCTRC_SIZE + 1024)
81 octrc.write_text(json.dumps({"root_marker": True, "_pad": padding}))
82
83 # find_project_root should still locate the directory structure,
84 # and the size cap warning should be emitted to stderr.
85 result = project_root.find_project_root(proj / "docs")
86 captured = capsys.readouterr()
87 assert "skipping" in captured.err
88 assert str(project_root.MAX_OCTRC_SIZE) in captured.err
89 # Root must still resolve via the directory-marker path
90 assert result == proj.resolve()
91
92
94 """A small ``.octrc.json`` with root_marker=True wins over structural detection."""
95 proj = tmp_path / "proj"
96 proj.mkdir()
97 (proj / ".octrc.json").write_text('{"root_marker": true}')
98 # Nested sub-dir should still resolve up to proj
99 sub = proj / "a" / "b"
100 sub.mkdir(parents=True)
102 assert result == proj.resolve()
Path validate_path_containment(Path path, Path project_root, bool allow_outside=False)
Path find_project_root(Path start)
test_validate_path_containment_inside_passes(tmp_path)
test_validate_path_containment_allow_outside_passes(tmp_path)
test_octrc_size_cap_skipped(tmp_path, capsys)
test_validate_path_containment_outside_raises(tmp_path)