Skip to content

Commit d63f747

Browse files
Chore: Increase test coverage for external_needs.py
1 parent e1ec741 commit d63f747

1 file changed

Lines changed: 112 additions & 2 deletions

File tree

src/extensions/score_metamodel/tests/test_external_needs.py

Lines changed: 112 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,38 @@
1010
#
1111
# SPDX-License-Identifier: Apache-2.0
1212
# *******************************************************************************
13+
14+
# ╓ ╖
15+
# ║ Some portions generated by CoPilot ║
16+
# ╙ ╜
17+
18+
from __future__ import annotations
19+
1320
import pytest
1421

15-
from ..external_needs import ExternalNeedsSource, parse_external_needs_sources_from_DATA
22+
import json
23+
from pathlib import Path
24+
from sphinx.config import Config
25+
26+
import score_metamodel.external_needs as ext_needs
27+
28+
from score_metamodel.external_needs import (
29+
ExternalNeedsSource,
30+
parse_external_needs_sources_from_DATA,
31+
get_external_needs_source,
32+
add_external_docs_sources,
33+
add_external_needs_json,
34+
)
1635

1736

1837
def test_empty_list():
1938
assert parse_external_needs_sources_from_DATA("[]") == []
2039

2140

41+
def test_external_str_does_not_start_with_at():
42+
assert get_external_needs_source('["noatrepo//foo/bar:baz"]') == []
43+
44+
2245
def test_single_entry_with_path():
2346
result = parse_external_needs_sources_from_DATA('["@repo//foo/bar:baz"]')
2447
# IF a target has a path, it will not be reported as external needs
@@ -69,4 +92,91 @@ def test_invalid_entry():
6992
_ = parse_external_needs_sources_from_DATA('["@not_a_valid_string"]')
7093

7194

72-
def test_parser(): ...
95+
def test_add_external_needs_json_appends_entry(
96+
monkeypatch: pytest.MonkeyPatch, tmp_path: Path
97+
) -> None:
98+
"""add_external_needs_json should append one external needs mapping entry."""
99+
# Arrange
100+
e = ExternalNeedsSource(
101+
bazel_module="ext_mod", target="needs_json", path_to_target=""
102+
)
103+
config = Config()
104+
config.needs_external_needs = []
105+
106+
runfiles_dir = tmp_path
107+
rel_json = Path("ext_mod+/needs_json/_build/needs/needs.json")
108+
json_path = runfiles_dir / rel_json
109+
json_path.parent.mkdir(parents=True, exist_ok=True)
110+
json_path.write_text(
111+
json.dumps({"project_url": "https://example.test/repo"}), encoding="utf-8"
112+
)
113+
114+
monkeypatch.setattr(ext_needs, "get_runfiles_dir", lambda: runfiles_dir)
115+
116+
add_external_needs_json(e, config)
117+
118+
assert config.needs_external_needs is not None
119+
assert len(config.needs_external_needs) == 1
120+
entry = config.needs_external_needs[0]
121+
assert entry["base_url"] == "https://example.test/repo/main"
122+
assert Path(entry["json_path"]) == json_path
123+
124+
125+
def test_add_external_needs_json_missing_file_keeps_list_empty(
126+
monkeypatch: pytest.MonkeyPatch, tmp_path: Path
127+
) -> None:
128+
"""add_external_needs_json should return gracefully when JSON file is missing."""
129+
# Arrange
130+
e = ExternalNeedsSource(
131+
bazel_module="ext_mod", target="needs_json", path_to_target=""
132+
)
133+
config = Config()
134+
config.needs_external_needs = []
135+
136+
monkeypatch.setattr(ext_needs, "get_runfiles_dir", lambda: tmp_path)
137+
138+
add_external_needs_json(e, config)
139+
140+
# Assert
141+
assert config.needs_external_needs == []
142+
143+
144+
def test_add_external_docs_sources_adds_collection(
145+
monkeypatch: pytest.MonkeyPatch, tmp_path: Path
146+
) -> None:
147+
"""add_external_docs_sources should add one symlink collection entry."""
148+
e = ExternalNeedsSource(
149+
bazel_module="third_party_docs", target="docs_sources", path_to_target=""
150+
)
151+
config = Config()
152+
config.collections = {}
153+
154+
monkeypatch.setattr(ext_needs, "get_runfiles_dir", lambda: tmp_path)
155+
156+
add_external_docs_sources(e, config)
157+
158+
assert config.collections is not None
159+
assert "third_party_docs" in config.collections
160+
entry = config.collections["third_party_docs"]
161+
assert entry["driver"] == "symlink"
162+
assert entry["source"] == str(tmp_path / "third_party_docs+")
163+
assert entry["target"] == "third_party_docs"
164+
165+
166+
def test_add_external_docs_sources_ide_support_returns_without_changes(
167+
monkeypatch: pytest.MonkeyPatch,
168+
) -> None:
169+
"""add_external_docs_sources should exit early for ide_support.runfiles paths."""
170+
e = ExternalNeedsSource(
171+
bazel_module="third_party_docs", target="docs_sources", path_to_target=""
172+
)
173+
config = Config()
174+
config.collections = {}
175+
176+
monkeypatch.setattr(
177+
ext_needs, "get_runfiles_dir", lambda: Path("/tmp/ide_support.runfiles")
178+
)
179+
180+
add_external_docs_sources(e, config)
181+
182+
assert config.collections == {}

0 commit comments

Comments
 (0)