Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@
"""


@pytest.fixture(scope="session")
@pytest.fixture(scope="module")
def sphinx_base_dir(tmp_path_factory: TempPathFactory) -> Path:
return tmp_path_factory.mktemp("test_module_links_repo")


@pytest.fixture(scope="session")
@pytest.fixture(scope="module")
def git_repo_setup(sphinx_base_dir: Path) -> Path:
"""Creating git repo, to make testing possible"""
repo_path = sphinx_base_dir
Expand All @@ -66,11 +66,10 @@ def git_repo_setup(sphinx_base_dir: Path) -> Path:
cwd=repo_path,
check=True,
)
os.environ["BUILD_WORKSPACE_DIRECTORY"] = str(repo_path)
return repo_path


@pytest.fixture(scope="session")
@pytest.fixture(scope="module")
def create_demo_files(sphinx_base_dir: Path, git_repo_setup: Path):
repo_path = sphinx_base_dir

Expand Down Expand Up @@ -249,11 +248,12 @@ def test_repo_grouped_cache_generated(
sphinx_base_dir: Path,
git_repo_setup: Path,
create_demo_files: None,
monkeypatch: pytest.MonkeyPatch,
):
"""Happy path: Repo grouped cache file is generated after Sphinx build"""
monkeypatch.setenv("BUILD_WORKSPACE_DIRECTORY", str(sphinx_base_dir))
app = sphinx_app_setup()
try:
os.environ["BUILD_WORKSPACE_DIRECTORY"] = str(sphinx_base_dir)
app.build()

repo_cache = app.outdir / "score_repo_grouped_scl_cache.json"
Expand All @@ -279,11 +279,12 @@ def test_repo_grouping_preserves_metadata(
sphinx_base_dir: Path,
git_repo_setup: Path,
create_demo_files: None,
monkeypatch: pytest.MonkeyPatch,
):
"""Happy path: Repo metadata (name, hash, url) is preserved in cache"""
monkeypatch.setenv("BUILD_WORKSPACE_DIRECTORY", str(sphinx_base_dir))
app = sphinx_app_setup()
try:
os.environ["BUILD_WORKSPACE_DIRECTORY"] = str(sphinx_base_dir)
app.build()

repo_cache = app.outdir / "score_repo_grouped_scl_cache.json"
Expand All @@ -306,11 +307,12 @@ def test_repo_grouping_multiple_needs_per_repo(
sphinx_base_dir: Path,
git_repo_setup: Path,
create_demo_files: None,
monkeypatch: pytest.MonkeyPatch,
):
"""Happy path: Multiple needs from same repo are grouped together"""
monkeypatch.setenv("BUILD_WORKSPACE_DIRECTORY", str(sphinx_base_dir))
app = sphinx_app_setup()
try:
os.environ["BUILD_WORKSPACE_DIRECTORY"] = str(sphinx_base_dir)
app.build()

repo_cache = app.outdir / "score_repo_grouped_scl_cache.json"
Expand Down Expand Up @@ -338,14 +340,15 @@ def test_repo_cache_json_format(
sphinx_base_dir: Path,
git_repo_setup: Path,
create_demo_files: None,
monkeypatch: pytest.MonkeyPatch,
):
"""
Repo cache JSON has correct
structure and excludes metadata from links
"""
monkeypatch.setenv("BUILD_WORKSPACE_DIRECTORY", str(sphinx_base_dir))
app = sphinx_app_setup()
try:
os.environ["BUILD_WORKSPACE_DIRECTORY"] = str(sphinx_base_dir)
app.build()

repo_cache = app.outdir / "score_repo_grouped_scl_cache.json"
Expand Down Expand Up @@ -389,11 +392,12 @@ def test_repo_cache_rebuilds_when_missing(
sphinx_base_dir: Path,
git_repo_setup: Path,
create_demo_files: None,
monkeypatch: pytest.MonkeyPatch,
):
"""Edge case: Repo cache is regenerated if deleted"""
monkeypatch.setenv("BUILD_WORKSPACE_DIRECTORY", str(sphinx_base_dir))
app = sphinx_app_setup()
try:
os.environ["BUILD_WORKSPACE_DIRECTORY"] = str(sphinx_base_dir)
app.build()

repo_cache = app.outdir / "score_repo_grouped_scl_cache.json"
Expand Down Expand Up @@ -423,11 +427,12 @@ def test_repo_grouping_with_golden_file(
sphinx_base_dir: Path,
git_repo_setup: Path,
create_demo_files: None,
monkeypatch: pytest.MonkeyPatch,
):
"""Happy path: Generated repo cache matches expected golden file"""
monkeypatch.setenv("BUILD_WORKSPACE_DIRECTORY", str(sphinx_base_dir))
app = sphinx_app_setup()
try:
os.environ["BUILD_WORKSPACE_DIRECTORY"] = str(sphinx_base_dir)
app.build()

repo_cache = app.outdir / "score_repo_grouped_scl_cache.json"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@
from src.helper_lib import find_ws_root


@pytest.fixture(scope="session")
@pytest.fixture(scope="module")
def sphinx_base_dir(tmp_path_factory: TempPathFactory) -> Path:
return tmp_path_factory.mktemp("test_git_repo")


@pytest.fixture(scope="session")
@pytest.fixture(scope="module")
def git_repo_setup(sphinx_base_dir: Path) -> Path:
"""Creating git repo, to make testing possible"""

Expand All @@ -64,11 +64,10 @@ def git_repo_setup(sphinx_base_dir: Path) -> Path:
cwd=repo_path,
check=True,
)
os.environ["BUILD_WORKSPACE_DIRECTORY"] = str(repo_path)
return repo_path


@pytest.fixture(scope="session")
@pytest.fixture(scope="module")
def create_demo_files(sphinx_base_dir: Path, git_repo_setup: Path):
repo_path = sphinx_base_dir

Expand Down Expand Up @@ -511,11 +510,12 @@ def test_source_link_integration_ok(
sphinx_base_dir: Path,
git_repo_setup: Path,
create_demo_files: None,
monkeypatch: pytest.MonkeyPatch,
):
"""This is a test description"""
monkeypatch.setenv("BUILD_WORKSPACE_DIRECTORY", str(sphinx_base_dir))
app = sphinx_app_setup()
try:
os.environ["BUILD_WORKSPACE_DIRECTORY"] = str(sphinx_base_dir)
app.build()
ws_root = find_ws_root()
assert ws_root is not None
Expand Down Expand Up @@ -574,8 +574,10 @@ def test_source_link_integration_non_existent_id(
sphinx_base_dir: Path,
git_repo_setup: Path,
create_demo_files: None,
monkeypatch: pytest.MonkeyPatch,
):
"""Asserting warning if need not found"""
monkeypatch.setenv("BUILD_WORKSPACE_DIRECTORY", str(sphinx_base_dir))
app = sphinx_app_setup()
try:
app.build()
Expand Down
35 changes: 10 additions & 25 deletions src/extensions/score_source_code_linker/tests/test_xml_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
"""

import json
import os
import xml.etree.ElementTree as ET
from collections.abc import Callable
from pathlib import Path
Expand Down Expand Up @@ -467,41 +466,27 @@ def test_get_metadata_from_test_path_local():
assert md["url"] == ""


def test_get_metadata_from_test_path_combo_with_hash(tmp_path: Path):
def test_get_metadata_from_test_path_combo_with_hash(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
):
"""Combo builds with 'hash' in known_good.json populate metadata correctly."""
json_file = tmp_path / "known_good.json"
json_file.write_text(json.dumps(_KNOWN_GOOD_WITH_HASH))

old = os.environ.get("KNOWN_GOOD_JSON")
try:
os.environ["KNOWN_GOOD_JSON"] = str(json_file)
md = xml_parser.get_metadata_from_test_path(_COMBO_TEST_PATH)
finally:
if old is None:
os.environ.pop("KNOWN_GOOD_JSON", None)
else:
os.environ["KNOWN_GOOD_JSON"] = old

monkeypatch.setenv("KNOWN_GOOD_JSON", str(json_file))
md = xml_parser.get_metadata_from_test_path(_COMBO_TEST_PATH)
assert md["repo_name"] == "score_docs_as_code"
assert md["hash"] == "abc123hashvalue"
assert md["url"] == "https://github.com/eclipse-score/docs-as-code"


def test_get_metadata_from_test_path_combo_with_version(tmp_path: Path):
def test_get_metadata_from_test_path_combo_with_version(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
):
"""Combo builds with 'version' in known_good.json populate metadata correctly."""
json_file = tmp_path / "known_good.json"
json_file.write_text(json.dumps(_KNOWN_GOOD_WITH_VERSION))

old = os.environ.get("KNOWN_GOOD_JSON")
try:
os.environ["KNOWN_GOOD_JSON"] = str(json_file)
md = xml_parser.get_metadata_from_test_path(_COMBO_TEST_PATH)
finally:
if old is None:
os.environ.pop("KNOWN_GOOD_JSON", None)
else:
os.environ["KNOWN_GOOD_JSON"] = old

monkeypatch.setenv("KNOWN_GOOD_JSON", str(json_file))
md = xml_parser.get_metadata_from_test_path(_COMBO_TEST_PATH)
assert md["repo_name"] == "score_docs_as_code"
assert md["hash"] == "v2.1.0"
assert md["url"] == "https://github.com/eclipse-score/docs-as-code"
15 changes: 8 additions & 7 deletions src/helper_lib/test_helper_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,25 +263,26 @@ def test_get_current_git_hash_invalid_repo(temp_dir: Path):
get_current_git_hash(temp_dir)


def test_runfiles_dir_found(temp_dir: Path):
def test_runfiles_dir_found(temp_dir: Path, monkeypatch: pytest.MonkeyPatch):
"""Test Runfiles dir found when provided and it's actually there"""
runfiles_dir = temp_dir / "runfiles_here"
runfiles_dir.mkdir(parents=True)
os.environ["RUNFILES_DIR"] = str(runfiles_dir)
os.chdir(runfiles_dir)
monkeypatch.setenv("RUNFILES_DIR", str(runfiles_dir))
# No chdir needed: get_runfiles_dir() reads the env var directly and never consults CWD.
result = get_runfiles_dir()
assert Path(result) == runfiles_dir
os.environ.pop("RUNFILES_DIR", None)


def test_runfiles_dir_missing_triggers_exit(temp_dir: Path):
def test_runfiles_dir_missing_triggers_exit(
temp_dir: Path, monkeypatch: pytest.MonkeyPatch
):
"""Testing if the runfiles exit via sys.exit if runfiles are set but don't exist"""
runfiles_dir = temp_dir / "does_not_exist"
os.environ["RUNFILES_DIR"] = str(runfiles_dir)
monkeypatch.setenv("RUNFILES_DIR", str(runfiles_dir))
with pytest.raises(SystemExit) as e:
# No chdir needed: get_runfiles_dir() reads the env var directly and never consults CWD.
get_runfiles_dir()
assert "Could not find runfiles_dir" in str(e.value)
os.environ.pop("RUNFILES_DIR", None)


def test_git_root_search_success(git_repo: Path, monkeypatch: pytest.MonkeyPatch):
Expand Down
Loading