Skip to content

Commit 2f103ea

Browse files
committed
🔧 Resolve git_root paths
Relative to config file location in analyse command
1 parent 04b397c commit 2f103ea

File tree

3 files changed

+121
-1
lines changed

3 files changed

+121
-1
lines changed

‎src/sphinx_codelinks/cmd.py‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656

5757

5858
@app.command(no_args_is_help=True)
59-
def analyse(
59+
def analyse( # noqa: PLR0912 # for CLI, so it needs the branches
6060
config: Annotated[
6161
Path,
6262
typer.Argument(
@@ -141,6 +141,12 @@ def analyse(
141141
analyse_config.src_files = src_discover.source_paths
142142
analyse_config.src_dir = Path(src_discover.src_discover_config.src_dir)
143143

144+
# git_root shall be relative to the config file's location (like src_dir)
145+
if analyse_config.git_root is not None:
146+
analyse_config.git_root = (
147+
config.parent / analyse_config.git_root
148+
).resolve()
149+
144150
analyse_errors = analyse_config.check_fields_configuration()
145151
errors.extend(analyse_errors)
146152
if errors:

‎src/sphinx_codelinks/sphinx_extension/directives/src_trace.py‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,13 @@ def run(self) -> list[nodes.Node]:
116116
analyse_config = src_trace_conf["analyse_config"]
117117
analyse_config.src_dir = src_dir
118118
analyse_config.src_files = source_files
119+
# git_root shall be relative to the config file's location (if provided)
120+
if analyse_config.git_root:
121+
conf_dir = Path(self.env.app.confdir)
122+
if src_trace_sphinx_config.config_from_toml:
123+
src_trace_toml_path = Path(src_trace_sphinx_config.config_from_toml)
124+
conf_dir = conf_dir / src_trace_toml_path.parent
125+
analyse_config.git_root = (conf_dir / analyse_config.git_root).resolve()
119126
src_analyse = SourceAnalyse(analyse_config)
120127
src_analyse.run()
121128

‎tests/test_cmd.py‎

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,3 +283,110 @@ def test_write_rst_negative(json_objs: list[dict], output_lines, tmp_path) -> No
283283
assert result.exit_code != 0
284284
for line in output_lines:
285285
assert line in result.stdout
286+
287+
288+
def test_analyse_with_relative_git_root(tmp_path: Path) -> None:
289+
"""Test that relative git_root is resolved relative to the config file location."""
290+
# Create a fake git repo structure
291+
fake_git_root = tmp_path / "fake_repo"
292+
fake_git_root.mkdir()
293+
(fake_git_root / ".git").mkdir()
294+
git_config = fake_git_root / ".git" / "config"
295+
git_config.write_text(
296+
'[remote "origin"]\n url = https://github.com/test/repo.git\n'
297+
)
298+
git_head = fake_git_root / ".git" / "HEAD"
299+
git_head.write_text("ref: refs/heads/main\n")
300+
refs_dir = fake_git_root / ".git" / "refs" / "heads"
301+
refs_dir.mkdir(parents=True)
302+
(refs_dir / "main").write_text("abc123def456\n")
303+
304+
# Create source file
305+
src_dir = fake_git_root / "src"
306+
src_dir.mkdir()
307+
src_file = src_dir / "test.c"
308+
src_file.write_text("// @Test, TEST_1, test\nvoid test() {}\n")
309+
310+
# Create config in a subdirectory using a RELATIVE git_root path
311+
config_dir = tmp_path / "config"
312+
config_dir.mkdir()
313+
config_file = config_dir / "codelinks.toml"
314+
config_content = """
315+
[codelinks.projects.test_project.source_discover]
316+
src_dir = "../fake_repo/src"
317+
gitignore = false
318+
319+
[codelinks.projects.test_project.analyse]
320+
get_oneline_needs = true
321+
git_root = "../fake_repo"
322+
"""
323+
config_file.write_text(config_content)
324+
325+
outdir = tmp_path / "output"
326+
outdir.mkdir()
327+
328+
options = ["analyse", str(config_file), "--outdir", str(outdir)]
329+
result = runner.invoke(app, options)
330+
331+
assert result.exit_code == 0, f"CLI failed: {result.stdout}"
332+
output_path = outdir / "marked_content.json"
333+
assert output_path.exists()
334+
335+
with output_path.open("r") as f:
336+
marked_content = json.load(f)
337+
# Verify the content was analysed using the correct git_root
338+
assert len(marked_content["test_project"]) > 0
339+
340+
341+
def test_analyse_with_absolute_git_root(tmp_path: Path) -> None:
342+
"""Test that absolute git_root is used as-is."""
343+
# Create a fake git repo structure
344+
fake_git_root = tmp_path / "fake_repo"
345+
fake_git_root.mkdir()
346+
(fake_git_root / ".git").mkdir()
347+
git_config = fake_git_root / ".git" / "config"
348+
git_config.write_text(
349+
'[remote "origin"]\n url = https://github.com/test/repo.git\n'
350+
)
351+
git_head = fake_git_root / ".git" / "HEAD"
352+
git_head.write_text("ref: refs/heads/main\n")
353+
refs_dir = fake_git_root / ".git" / "refs" / "heads"
354+
refs_dir.mkdir(parents=True)
355+
(refs_dir / "main").write_text("abc123def456\n")
356+
357+
# Create source file
358+
src_dir = fake_git_root / "src"
359+
src_dir.mkdir()
360+
src_file = src_dir / "test.c"
361+
src_file.write_text("// @Test, TEST_2, test\nvoid test() {}\n")
362+
363+
# Create config in a different location using an ABSOLUTE git_root path
364+
config_dir = tmp_path / "config"
365+
config_dir.mkdir()
366+
config_file = config_dir / "codelinks.toml"
367+
# Use absolute path for both src_dir and git_root
368+
config_content = f"""
369+
[codelinks.projects.test_project.source_discover]
370+
src_dir = "{src_dir.as_posix()}"
371+
gitignore = false
372+
373+
[codelinks.projects.test_project.analyse]
374+
get_oneline_needs = true
375+
git_root = "{fake_git_root.as_posix()}"
376+
"""
377+
config_file.write_text(config_content)
378+
379+
outdir = tmp_path / "output"
380+
outdir.mkdir()
381+
382+
options = ["analyse", str(config_file), "--outdir", str(outdir)]
383+
result = runner.invoke(app, options)
384+
385+
assert result.exit_code == 0, f"CLI failed: {result.stdout}"
386+
output_path = outdir / "marked_content.json"
387+
assert output_path.exists()
388+
389+
with output_path.open("r") as f:
390+
marked_content = json.load(f)
391+
# Verify the content was analysed using the correct git_root
392+
assert len(marked_content["test_project"]) > 0

0 commit comments

Comments
 (0)