@@ -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\n void 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\n void 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