@@ -525,6 +525,7 @@ def test_exclude_file(
525525 bad_name .write_bytes (
526526 (combinations + "5 abandonned 5\n 6 abandonned 6" ).encode ("utf-8" )
527527 )
528+
528529 assert cs .main (bad_name ) == 18
529530 fname = tmp_path / "tmp.txt"
530531 fname .write_bytes (
@@ -545,6 +546,77 @@ def test_exclude_file(
545546 assert cs .main ("-x" , f"{ fname_dummy1 } ,{ fname } ,{ fname_dummy2 } " , bad_name ) == 1
546547
547548
549+ def run_git (path : Path , * args : Union [Path , str ]) -> None :
550+ subprocess .run ( # noqa: S603
551+ ["git" , "-C" , path , * list (args )], # noqa: S607
552+ capture_output = False ,
553+ check = True ,
554+ text = True ,
555+ )
556+
557+
558+ def test_git_only_exclude_file (
559+ tmp_path : Path , capsys : pytest .CaptureFixture [str ], monkeypatch : pytest .MonkeyPatch
560+ ) -> None :
561+ monkeypatch .chdir (tmp_path )
562+ """Test exclude file functionality."""
563+ bad_name = tmp_path / "bad.txt"
564+ # check all possible combinations of lines to ignore and ignores
565+ combinations = "" .join (
566+ f"{ n } abandonned { n } \n "
567+ f"{ n } abandonned { n } \r \n "
568+ f"{ n } abandonned { n } \n "
569+ f"{ n } abandonned { n } \r \n "
570+ for n in range (1 , 5 )
571+ )
572+ bad_name .write_bytes (
573+ (combinations + "5 abandonned 5\n 6 abandonned 6" ).encode ("utf-8" )
574+ )
575+
576+ run_git (tmp_path , "init" )
577+ run_git (tmp_path , "add" , bad_name )
578+
579+ assert cs .main (bad_name ) == 18
580+ fname = tmp_path / "tmp.txt"
581+ fname .write_bytes (
582+ b"1 abandonned 1\n "
583+ b"2 abandonned 2\r \n "
584+ b"3 abandonned 3 \n "
585+ b"4 abandonned 4 \r \n "
586+ b"6 abandonned 6\n "
587+ )
588+
589+ # Not adding fname to git to exclude it
590+
591+ # Should have 23 total errors (bad_name + fname)
592+ assert cs .main (tmp_path ) == 23
593+
594+ # Before adding to git, should not report on fname, only 18 error in bad.txt
595+ assert cs .main ("--git-only" , tmp_path ) == 18
596+ run_git (tmp_path , "add" , fname )
597+ assert cs .main (tmp_path ) == 23
598+ # After adding to git, should report on fname
599+ assert cs .main ("--git-only" , tmp_path ) == 23
600+ # After adding to git, should not report on excluded file
601+ assert cs .main ("--git-only" , "-x" , fname , tmp_path ) == 1
602+ # comma-separated list of files
603+ fname_dummy1 = tmp_path / "dummy1.txt"
604+ fname_dummy1 .touch ()
605+ fname_dummy2 = tmp_path / "dummy2.txt"
606+ fname_dummy2 .touch ()
607+ run_git (tmp_path , "add" , fname_dummy1 , fname_dummy2 )
608+ assert (
609+ cs .main (
610+ "--git-only" , "-x" , fname_dummy1 , "-x" , fname , "-x" , fname_dummy2 , bad_name
611+ )
612+ == 1
613+ )
614+ assert (
615+ cs .main ("--git-only" , "-x" , f"{ fname_dummy1 } ,{ fname } ,{ fname_dummy2 } " , bad_name )
616+ == 1
617+ )
618+
619+
548620def test_encoding (
549621 tmp_path : Path ,
550622 capsys : pytest .CaptureFixture [str ],
@@ -662,6 +734,108 @@ def test_check_filename_irregular_file(
662734 assert cs .main ("-f" , tmp_path ) == 1
663735
664736
737+ def test_check_hidden_git (
738+ tmp_path : Path ,
739+ capsys : pytest .CaptureFixture [str ],
740+ monkeypatch : pytest .MonkeyPatch ,
741+ ) -> None :
742+ """Test ignoring of hidden files."""
743+ monkeypatch .chdir (tmp_path )
744+ run_git (tmp_path , "init" )
745+ # visible file
746+ #
747+ # tmp_path
748+ # └── test.txt
749+ #
750+ fname = tmp_path / "test.txt"
751+ fname .write_text ("erorr\n " )
752+ run_git (tmp_path , "add" , "." )
753+ assert cs .main ("--git-only" , fname ) == 1
754+ assert cs .main ("--git-only" , tmp_path ) == 1
755+
756+ # hidden file
757+ #
758+ # tmp_path
759+ # └── .test.txt
760+ #
761+ hidden_file = tmp_path / ".test.txt"
762+ fname .rename (hidden_file )
763+ run_git (tmp_path , "add" , "." )
764+ assert cs .main ("--git-only" , hidden_file ) == 0
765+ assert cs .main ("--git-only" , tmp_path ) == 0
766+ assert cs .main ("--git-only" , "--check-hidden" , hidden_file ) == 1
767+ assert cs .main ("--git-only" , "--check-hidden" , tmp_path ) == 1
768+
769+ # hidden file with typo in name
770+ #
771+ # tmp_path
772+ # └── .abandonned.txt
773+ #
774+ typo_file = tmp_path / ".abandonned.txt"
775+ hidden_file .rename (typo_file )
776+ run_git (tmp_path , "add" , "." )
777+ assert cs .main ("--git-only" , typo_file ) == 0
778+ assert cs .main ("--git-only" , tmp_path ) == 0
779+ assert cs .main ("--git-only" , "--check-hidden" , typo_file ) == 1
780+ assert cs .main ("--git-only" , "--check-hidden" , tmp_path ) == 1
781+ assert cs .main ("--git-only" , "--check-hidden" , "--check-filenames" , typo_file ) == 2
782+ assert cs .main ("--git-only" , "--check-hidden" , "--check-filenames" , tmp_path ) == 2
783+
784+ # hidden directory
785+ #
786+ # tmp_path
787+ # ├── .abandonned
788+ # │ ├── .abandonned.txt
789+ # │ └── subdir
790+ # │ └── .abandonned.txt
791+ # └── .abandonned.txt
792+ #
793+ assert cs .main ("--git-only" , tmp_path ) == 0
794+ assert cs .main ("--git-only" , "--check-hidden" , tmp_path ) == 1
795+ assert cs .main ("--git-only" , "--check-hidden" , "--check-filenames" , tmp_path ) == 2
796+ hidden = tmp_path / ".abandonned"
797+ hidden .mkdir ()
798+ copyfile (typo_file , hidden / typo_file .name )
799+ subdir = hidden / "subdir"
800+ subdir .mkdir ()
801+ copyfile (typo_file , subdir / typo_file .name )
802+ run_git (tmp_path , "add" , "." )
803+ assert cs .main ("--git-only" , tmp_path ) == 0
804+ assert cs .main ("--git-only" , "--check-hidden" , tmp_path ) == 3
805+ assert cs .main ("--git-only" , "--check-hidden" , "--check-filenames" , tmp_path ) == 8
806+ # check again with a relative path
807+ try :
808+ rel = op .relpath (tmp_path )
809+ except ValueError :
810+ # Windows: path is on mount 'C:', start on mount 'D:'
811+ pass
812+ else :
813+ assert cs .main ("--git-only" , rel ) == 0
814+ assert cs .main ("--git-only" , "--check-hidden" , rel ) == 3
815+ assert cs .main ("--git-only" , "--check-hidden" , "--check-filenames" , rel ) == 8
816+
817+ # hidden subdirectory
818+ #
819+ # tmp_path
820+ # ├── .abandonned
821+ # │ ├── .abandonned.txt
822+ # │ └── subdir
823+ # │ └── .abandonned.txt
824+ # ├── .abandonned.txt
825+ # └── subdir
826+ # └── .abandonned
827+ # └── .abandonned.txt
828+ subdir = tmp_path / "subdir"
829+ subdir .mkdir ()
830+ hidden = subdir / ".abandonned"
831+ hidden .mkdir ()
832+ copyfile (typo_file , hidden / typo_file .name )
833+ run_git (tmp_path , "add" , "." )
834+ assert cs .main ("--git-only" , tmp_path ) == 0
835+ assert cs .main ("--git-only" , "--check-hidden" , tmp_path ) == 4
836+ assert cs .main ("--git-only" , "--check-hidden" , "--check-filenames" , tmp_path ) == 11
837+
838+
665839def test_check_hidden (
666840 tmp_path : Path ,
667841 capsys : pytest .CaptureFixture [str ],
0 commit comments