Option C Tools
Loading...
Searching...
No Matches
test_dotted_keys.py
Go to the documentation of this file.
1#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3# tests/tests_deps/test_dotted_keys.py
4
5"""
6Purpose
7-------
8OI-521 regression coverage — ``oct.deps.oct_deps.build_dependency_graph``
9must key modules by their dotted import path (``pkg.sub.module``) rather
10than the bare file stem (``module``). The old behaviour silently dropped
11every occurrence of a repeated stem on top of a single graph entry,
12hiding both modules and their dependencies.
13
14Responsibilities
15----------------
16- Verify ``module_name_for`` returns the dotted import path.
17- Verify ``__init__.py`` resolves to its parent package's dotted name.
18- Verify two files with the same stem in different packages produce
19 distinct graph keys.
20
21Diagnostics
22-----------
23Domain: DEPS-TESTS
24Levels:
25 L2 — test lifecycle
26 L3 — assertion details
27 L4 — deep tracing
28
29Contracts
30---------
31- ``module_name_for(py_file, root)`` returns the dotted path.
32- ``__init__.py`` resolves to its parent package's dotted name.
33- Two files with the same stem in different packages both appear in the
34 graph with distinct dotted keys (no collision).
35"""
36
37from __future__ import annotations
38
39from pathlib import Path
40
41import pytest
42
43from oct.deps.oct_deps import build_dependency_graph, module_name_for
44
45
46def test_module_name_for_nested_module(tmp_path: Path) -> None:
47 """OI-521: a module two packages deep gets a three-segment dotted key."""
48 (tmp_path / "pkg" / "sub").mkdir(parents=True)
49 module = tmp_path / "pkg" / "sub" / "util.py"
50 module.write_text("", encoding="utf-8")
51 assert module_name_for(module, tmp_path) == "pkg.sub.util"
52
53
55 """OI-521: ``pkg/__init__.py`` resolves to the package name ``pkg``."""
56 (tmp_path / "pkg").mkdir()
57 init = tmp_path / "pkg" / "__init__.py"
58 init.write_text("", encoding="utf-8")
59 assert module_name_for(init, tmp_path) == "pkg"
60
61
63 """Two ``util.py`` files in different packages both land in the graph."""
64 src = (
65 '"""Mod.\n\n'
66 "Dependencies\n"
67 "------------\n"
68 "- some.other\n"
69 '"""\n'
70 )
71 (tmp_path / "pkg_a").mkdir()
72 (tmp_path / "pkg_a" / "__init__.py").write_text("", encoding="utf-8")
73 (tmp_path / "pkg_a" / "util.py").write_text(src, encoding="utf-8")
74
75 (tmp_path / "pkg_b").mkdir()
76 (tmp_path / "pkg_b" / "__init__.py").write_text("", encoding="utf-8")
77 (tmp_path / "pkg_b" / "util.py").write_text(src, encoding="utf-8")
78
79 graph = build_dependency_graph(tmp_path)
80
81 assert "pkg_a.util" in graph
82 assert "pkg_b.util" in graph
83 # Old stem-based key must not exist.
84 assert "util" not in graph
None test_module_name_for_nested_module(Path tmp_path)
None test_build_dependency_graph_no_stem_collision(Path tmp_path)
None test_module_name_for_init_collapses_to_package(Path tmp_path)