Skip to content

Commit 15f412b

Browse files
chore(test): increase test coverage for external_needs.py (#593)
1 parent 71a0b6b commit 15f412b

1 file changed

Lines changed: 111 additions & 3 deletions

File tree

src/extensions/score_metamodel/tests/test_external_needs.py

Lines changed: 111 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,36 @@
1010
#
1111
# SPDX-License-Identifier: Apache-2.0
1212
# *******************************************************************************
13-
import pytest
1413

15-
from ..external_needs import ExternalNeedsSource, parse_external_needs_sources_from_DATA
14+
# ╓ ╖
15+
# ║ Some portions generated by CoPilot ║
16+
# ╙ ╜
17+
18+
from __future__ import annotations
19+
20+
import json
21+
from pathlib import Path
22+
23+
import pytest
24+
import score_metamodel.external_needs as ext_needs
25+
from score_metamodel.external_needs import (
26+
ExternalNeedsSource,
27+
add_external_docs_sources,
28+
add_external_needs_json,
29+
get_external_needs_source,
30+
parse_external_needs_sources_from_DATA,
31+
)
32+
from sphinx.config import Config
1633

1734

1835
def test_empty_list():
1936
assert parse_external_needs_sources_from_DATA("[]") == []
2037

2138

39+
def test_external_str_does_not_start_with_at():
40+
assert get_external_needs_source('["noatrepo//foo/bar:baz"]') == []
41+
42+
2243
def test_single_entry_with_path():
2344
result = parse_external_needs_sources_from_DATA('["@repo//foo/bar:baz"]')
2445
# IF a target has a path, it will not be reported as external needs
@@ -69,4 +90,91 @@ def test_invalid_entry():
6990
_ = parse_external_needs_sources_from_DATA('["@not_a_valid_string"]')
7091

7192

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

0 commit comments

Comments
 (0)