@@ -289,7 +289,7 @@ def test_init_dry_run_plans_agents_file_creation_without_writing(self) -> None:
289289 self .assertIn ("No files will be modified." , text )
290290 self .assertIn ("- AGENTS.md [create]" , text )
291291
292- def test_init_dry_run_skips_existing_agents_file_without_writing (self ) -> None :
292+ def test_init_dry_run_plans_backup_before_replace_without_writing (self ) -> None :
293293 output = io .StringIO ()
294294
295295 with tempfile .TemporaryDirectory () as temporary_directory :
@@ -308,17 +308,21 @@ def test_init_dry_run_skips_existing_agents_file_without_writing(self) -> None:
308308
309309 text = output .getvalue ()
310310
311- self .assertIn ("- AGENTS.md [skip-existing] - file already exists" , text )
311+ self .assertIn ("- AGENTS.md [backup-and-replace]" , text )
312+ self .assertIn ("existing file would be backed up before replacement" , text )
312313
313- def test_init_requires_dry_run_until_write_mode_exists (self ) -> None :
314+ def test_init_requires_explicit_mode (self ) -> None :
314315 output = io .StringIO ()
315316
316317 with tempfile .TemporaryDirectory () as temporary_directory :
317318 with redirect_stderr (output ):
318319 exit_code = main (["init" , temporary_directory ])
319320
320321 self .assertEqual (exit_code , 2 )
321- self .assertIn ("ERROR: init currently requires --dry-run." , output .getvalue ())
322+ self .assertIn (
323+ "ERROR: init currently requires --dry-run or --write." ,
324+ output .getvalue (),
325+ )
322326
323327 def test_init_dry_run_returns_two_for_invalid_repository_root (self ) -> None :
324328 output = io .StringIO ()
@@ -348,6 +352,73 @@ def test_init_dry_run_redacts_secret_like_repository_values(self) -> None:
348352 self .assertIn ("[REDACTED]" , text )
349353 self .assertNotIn (secret_like_path .name , text )
350354
355+ def test_init_rejects_dry_run_and_write_together (self ) -> None :
356+ output = io .StringIO ()
357+
358+ with tempfile .TemporaryDirectory () as temporary_directory :
359+ with redirect_stderr (output ):
360+ exit_code = main (
361+ [
362+ "init" ,
363+ temporary_directory ,
364+ "--dry-run" ,
365+ "--write" ,
366+ ]
367+ )
368+
369+ self .assertEqual (exit_code , 2 )
370+ self .assertIn (
371+ "ERROR: init accepts only one mode: --dry-run or --write." ,
372+ output .getvalue (),
373+ )
374+
375+ def test_init_write_creates_agents_file (self ) -> None :
376+ output = io .StringIO ()
377+
378+ with tempfile .TemporaryDirectory () as temporary_directory :
379+ repository = Path (temporary_directory )
380+
381+ with redirect_stdout (output ):
382+ exit_code = main (["init" , str (repository ), "--write" ])
383+
384+ agents_file = repository / "AGENTS.md"
385+
386+ self .assertEqual (exit_code , 0 )
387+ self .assertTrue (agents_file .exists ())
388+ self .assertIn ("# Agent Instructions" , agents_file .read_text (encoding = "utf-8" ))
389+
390+ text = output .getvalue ()
391+
392+ self .assertIn ("Mode: write" , text )
393+ self .assertIn ("- AGENTS.md [create]" , text )
394+
395+ def test_init_write_backs_up_existing_agents_file_before_replacing (self ) -> None :
396+ output = io .StringIO ()
397+
398+ with tempfile .TemporaryDirectory () as temporary_directory :
399+ repository = Path (temporary_directory )
400+ agents_file = repository / "AGENTS.md"
401+ agents_file .write_text ("existing instructions\n " , encoding = "utf-8" )
402+
403+ with redirect_stdout (output ):
404+ exit_code = main (["init" , str (repository ), "--write" ])
405+
406+ backup_file = repository / "AGENTS.md.agent-rules-kit.bak"
407+
408+ self .assertEqual (exit_code , 0 )
409+ self .assertTrue (backup_file .exists ())
410+ self .assertEqual (
411+ backup_file .read_text (encoding = "utf-8" ),
412+ "existing instructions\n " ,
413+ )
414+ self .assertIn ("# Agent Instructions" , agents_file .read_text (encoding = "utf-8" ))
415+
416+ text = output .getvalue ()
417+
418+ self .assertIn ("Mode: write" , text )
419+ self .assertIn ("- AGENTS.md [backup-and-replace]" , text )
420+ self .assertIn ("backup: AGENTS.md.agent-rules-kit.bak" , text )
421+
351422
352423if __name__ == "__main__" :
353424 unittest .main ()
0 commit comments