@@ -333,6 +333,85 @@ def test_cli_scan_output_to_file(tmp_path: Path) -> None:
333333 assert "out-test" in content or "risk_assessment" in content
334334
335335
336+ @pytest .mark .parametrize ("format" , list (FormatChoice ))
337+ @pytest .mark .parametrize (
338+ "alias" , ["same-path" , "relative-path" , "parent-path" , "symlink" , "symlink-parent" , "hard-link" ]
339+ )
340+ def test_cli_scan_rejects_output_alias_of_input_file (
341+ monkeypatch : pytest .MonkeyPatch , tmp_path : Path , format : FormatChoice , alias : str
342+ ) -> None :
343+ """An output alias must not replace the original skill with its report."""
344+ source = tmp_path / "SKILL.md"
345+ original = b"---\n name: protected\n description: Say hello.\n ---\n # Hello\n "
346+ source .write_bytes (original )
347+ output = source
348+ monkeypatch .chdir (tmp_path )
349+ if alias == "relative-path" :
350+ output = Path ("SKILL.md" )
351+ elif alias == "parent-path" :
352+ nested = tmp_path / "nested"
353+ nested .mkdir ()
354+ output = nested / ".." / source .name
355+ elif alias == "symlink" :
356+ output = tmp_path / "report.txt"
357+ try :
358+ output .symlink_to (source )
359+ except OSError :
360+ pytest .skip ("symlinks are not supported on this filesystem" )
361+ elif alias == "symlink-parent" :
362+ linked = tmp_path / "linked"
363+ try :
364+ linked .symlink_to (tmp_path , target_is_directory = True )
365+ except OSError :
366+ pytest .skip ("symlinks are not supported on this filesystem" )
367+ output = linked / source .name
368+ elif alias == "hard-link" :
369+ output = tmp_path / "report.txt"
370+ try :
371+ os .link (source , output )
372+ except OSError :
373+ pytest .skip ("hard links are not supported on this filesystem" )
374+ scan_skill = MagicMock (return_value = {"report_body" : "Scan report" , "risk_score" : 0 })
375+ monkeypatch .setattr (cli , "_scan_skill" , scan_skill )
376+
377+ result = runner .invoke (
378+ app , ["scan" , str (source ), "--no-llm" , "--format" , format .value , "--output" , str (output )]
379+ )
380+
381+ assert result .exit_code == 2
382+ assert "--output points to the input file" in result .output
383+ assert source .read_bytes () == original
384+ assert output .read_bytes () == original
385+ scan_skill .assert_not_called ()
386+
387+
388+ @pytest .mark .parametrize ("format" , list (FormatChoice ))
389+ @pytest .mark .parametrize ("destination" , ["new" , "existing" , "same-name" ])
390+ def test_cli_scan_preserves_input_when_writing_a_separate_report (
391+ monkeypatch : pytest .MonkeyPatch , tmp_path : Path , format : FormatChoice , destination : str
392+ ) -> None :
393+ """Separate reports still work, including existing files and matching names."""
394+ source = tmp_path / "SKILL.md"
395+ original = b"# Original skill\n "
396+ source .write_bytes (original )
397+ reports = tmp_path / "reports"
398+ reports .mkdir ()
399+ output = reports / (source .name if destination == "same-name" else "report.txt" )
400+ if destination == "existing" :
401+ output .write_text ("Previous report" , encoding = "utf-8" )
402+ scan_skill = MagicMock (return_value = {"report_body" : "Scan report" , "risk_score" : 0 })
403+ monkeypatch .setattr (cli , "_scan_skill" , scan_skill )
404+
405+ result = runner .invoke (
406+ app , ["scan" , str (source ), "--no-llm" , "--format" , format .value , "--output" , str (output )]
407+ )
408+
409+ assert result .exit_code == 0
410+ assert source .read_bytes () == original
411+ assert output .read_text (encoding = "utf-8" ) == "Scan report"
412+ scan_skill .assert_called_once ()
413+
414+
336415def test_cli_scan_no_llm (tmp_path : Path ) -> None :
337416 """scan with --no-llm runs without requiring an LLM API key (uses fallback)."""
338417 (tmp_path / "SKILL.md" ).write_text ("# No LLM test" , encoding = "utf-8" )
0 commit comments