@@ -499,6 +499,7 @@ def test_exclude_file(
499499 bad_name .write_bytes (
500500 (combinations + "5 abandonned 5\n 6 abandonned 6" ).encode ("utf-8" )
501501 )
502+
502503 assert cs .main (bad_name ) == 18
503504 fname = tmp_path / "tmp.txt"
504505 fname .write_bytes (
@@ -519,6 +520,87 @@ def test_exclude_file(
519520 assert cs .main ("-x" , f"{ fname_dummy1 } ,{ fname } ,{ fname_dummy2 } " , bad_name ) == 1
520521
521522
523+ def test_git_only_exclude_file (
524+ tmp_path : Path ,
525+ capsys : pytest .CaptureFixture [str ],
526+ monkeypatch : pytest .MonkeyPatch
527+ ) -> None :
528+ monkeypatch .chdir (tmp_path )
529+ """Test exclude file functionality."""
530+ bad_name = tmp_path / "bad.txt"
531+ # check all possible combinations of lines to ignore and ignores
532+ combinations = "" .join (
533+ f"{ n } abandonned { n } \n "
534+ f"{ n } abandonned { n } \r \n "
535+ f"{ n } abandonned { n } \n "
536+ f"{ n } abandonned { n } \r \n "
537+ for n in range (1 , 5 )
538+ )
539+ bad_name .write_bytes (
540+ (combinations + "5 abandonned 5\n 6 abandonned 6" ).encode ("utf-8" )
541+ )
542+
543+ subprocess .run (
544+ ['git' , '-C' , tmp_path , 'init' ],
545+ capture_output = False ,
546+ check = True ,
547+ text = True ,
548+ )
549+ subprocess .run (
550+ ['git' , '-C' , tmp_path , 'add' , bad_name ],
551+ capture_output = False ,
552+ check = True ,
553+ text = True ,
554+ )
555+
556+ assert cs .main (bad_name ) == 18
557+ fname = tmp_path / "tmp.txt"
558+ fname .write_bytes (
559+ b"1 abandonned 1\n "
560+ b"2 abandonned 2\r \n "
561+ b"3 abandonned 3 \n "
562+ b"4 abandonned 4 \r \n "
563+ b"6 abandonned 6\n "
564+ )
565+
566+ result = subprocess .run (
567+ ['git' , '-C' , tmp_path , 'ls-files' ],
568+ capture_output = True ,
569+ check = True ,
570+ text = True ,
571+ )
572+
573+ # Should have 23 total errors (bad_name + fname)
574+ assert cs .main (tmp_path ) == 23
575+
576+ # Before adding to git, should not report on fname, only 18 error in bad.txt
577+ assert cs .main ("--git-only" , tmp_path ) == 18
578+ subprocess .run (
579+ ['git' , '-C' , tmp_path , 'add' , fname ],
580+ capture_output = False ,
581+ check = True ,
582+ text = True ,
583+ )
584+ assert cs .main (tmp_path ) == 23
585+ # After adding to git, should report on fname
586+ assert cs .main ("--git-only" , tmp_path ) == 23
587+ # After adding to git, should not report on excluded file
588+ assert cs .main ("--git-only" , "-x" , fname , tmp_path ) == 1
589+ # comma-separated list of files
590+ fname_dummy1 = tmp_path / "dummy1.txt"
591+ fname_dummy1 .touch ()
592+ fname_dummy2 = tmp_path / "dummy2.txt"
593+ fname_dummy2 .touch ()
594+ subprocess .run (
595+ ['git' , '-C' , tmp_path , 'add' , fname_dummy1 , fname_dummy2 ],
596+ capture_output = False ,
597+ check = True ,
598+ text = True ,
599+ )
600+ assert cs .main ("--git-only" , "-x" , fname_dummy1 , "-x" , fname , "-x" , fname_dummy2 , bad_name ) == 1
601+ assert cs .main ("--git-only" , "-x" , f"{ fname_dummy1 } ,{ fname } ,{ fname_dummy2 } " , bad_name ) == 1
602+
603+
522604def test_encoding (
523605 tmp_path : Path ,
524606 capsys : pytest .CaptureFixture [str ],
0 commit comments