@@ -499,6 +499,7 @@ def test_exclude_file(
499
499
bad_name .write_bytes (
500
500
(combinations + "5 abandonned 5\n 6 abandonned 6" ).encode ("utf-8" )
501
501
)
502
+
502
503
assert cs .main (bad_name ) == 18
503
504
fname = tmp_path / "tmp.txt"
504
505
fname .write_bytes (
@@ -519,6 +520,77 @@ def test_exclude_file(
519
520
assert cs .main ("-x" , f"{ fname_dummy1 } ,{ fname } ,{ fname_dummy2 } " , bad_name ) == 1
520
521
521
522
523
+ def run_git (path : Path , * args : Union [Path , str ]) -> None :
524
+ subprocess .run ( # noqa: S603
525
+ ["git" , "-C" , path , * list (args )], # noqa: S607
526
+ capture_output = False ,
527
+ check = True ,
528
+ text = True ,
529
+ )
530
+
531
+
532
+ def test_git_only_exclude_file (
533
+ tmp_path : Path , capsys : pytest .CaptureFixture [str ], monkeypatch : pytest .MonkeyPatch
534
+ ) -> None :
535
+ monkeypatch .chdir (tmp_path )
536
+ """Test exclude file functionality."""
537
+ bad_name = tmp_path / "bad.txt"
538
+ # check all possible combinations of lines to ignore and ignores
539
+ combinations = "" .join (
540
+ f"{ n } abandonned { n } \n "
541
+ f"{ n } abandonned { n } \r \n "
542
+ f"{ n } abandonned { n } \n "
543
+ f"{ n } abandonned { n } \r \n "
544
+ for n in range (1 , 5 )
545
+ )
546
+ bad_name .write_bytes (
547
+ (combinations + "5 abandonned 5\n 6 abandonned 6" ).encode ("utf-8" )
548
+ )
549
+
550
+ run_git (tmp_path , "init" )
551
+ run_git (tmp_path , "add" , bad_name )
552
+
553
+ assert cs .main (bad_name ) == 18
554
+ fname = tmp_path / "tmp.txt"
555
+ fname .write_bytes (
556
+ b"1 abandonned 1\n "
557
+ b"2 abandonned 2\r \n "
558
+ b"3 abandonned 3 \n "
559
+ b"4 abandonned 4 \r \n "
560
+ b"6 abandonned 6\n "
561
+ )
562
+
563
+ # Not adding fname to git to exclude it
564
+
565
+ # Should have 23 total errors (bad_name + fname)
566
+ assert cs .main (tmp_path ) == 23
567
+
568
+ # Before adding to git, should not report on fname, only 18 error in bad.txt
569
+ assert cs .main ("--git-only" , tmp_path ) == 18
570
+ run_git (tmp_path , "add" , fname )
571
+ assert cs .main (tmp_path ) == 23
572
+ # After adding to git, should report on fname
573
+ assert cs .main ("--git-only" , tmp_path ) == 23
574
+ # After adding to git, should not report on excluded file
575
+ assert cs .main ("--git-only" , "-x" , fname , tmp_path ) == 1
576
+ # comma-separated list of files
577
+ fname_dummy1 = tmp_path / "dummy1.txt"
578
+ fname_dummy1 .touch ()
579
+ fname_dummy2 = tmp_path / "dummy2.txt"
580
+ fname_dummy2 .touch ()
581
+ run_git (tmp_path , "add" , fname_dummy1 , fname_dummy2 )
582
+ assert (
583
+ cs .main (
584
+ "--git-only" , "-x" , fname_dummy1 , "-x" , fname , "-x" , fname_dummy2 , bad_name
585
+ )
586
+ == 1
587
+ )
588
+ assert (
589
+ cs .main ("--git-only" , "-x" , f"{ fname_dummy1 } ,{ fname } ,{ fname_dummy2 } " , bad_name )
590
+ == 1
591
+ )
592
+
593
+
522
594
def test_encoding (
523
595
tmp_path : Path ,
524
596
capsys : pytest .CaptureFixture [str ],
@@ -636,6 +708,108 @@ def test_check_filename_irregular_file(
636
708
assert cs .main ("-f" , tmp_path ) == 1
637
709
638
710
711
+ def test_check_hidden_git (
712
+ tmp_path : Path ,
713
+ capsys : pytest .CaptureFixture [str ],
714
+ monkeypatch : pytest .MonkeyPatch ,
715
+ ) -> None :
716
+ """Test ignoring of hidden files."""
717
+ monkeypatch .chdir (tmp_path )
718
+ run_git (tmp_path , "init" )
719
+ # visible file
720
+ #
721
+ # tmp_path
722
+ # └── test.txt
723
+ #
724
+ fname = tmp_path / "test.txt"
725
+ fname .write_text ("erorr\n " )
726
+ run_git (tmp_path , "add" , "." )
727
+ assert cs .main ("--git-only" , fname ) == 1
728
+ assert cs .main ("--git-only" , tmp_path ) == 1
729
+
730
+ # hidden file
731
+ #
732
+ # tmp_path
733
+ # └── .test.txt
734
+ #
735
+ hidden_file = tmp_path / ".test.txt"
736
+ fname .rename (hidden_file )
737
+ run_git (tmp_path , "add" , "." )
738
+ assert cs .main ("--git-only" , hidden_file ) == 0
739
+ assert cs .main ("--git-only" , tmp_path ) == 0
740
+ assert cs .main ("--git-only" , "--check-hidden" , hidden_file ) == 1
741
+ assert cs .main ("--git-only" , "--check-hidden" , tmp_path ) == 1
742
+
743
+ # hidden file with typo in name
744
+ #
745
+ # tmp_path
746
+ # └── .abandonned.txt
747
+ #
748
+ typo_file = tmp_path / ".abandonned.txt"
749
+ hidden_file .rename (typo_file )
750
+ run_git (tmp_path , "add" , "." )
751
+ assert cs .main ("--git-only" , typo_file ) == 0
752
+ assert cs .main ("--git-only" , tmp_path ) == 0
753
+ assert cs .main ("--git-only" , "--check-hidden" , typo_file ) == 1
754
+ assert cs .main ("--git-only" , "--check-hidden" , tmp_path ) == 1
755
+ assert cs .main ("--git-only" , "--check-hidden" , "--check-filenames" , typo_file ) == 2
756
+ assert cs .main ("--git-only" , "--check-hidden" , "--check-filenames" , tmp_path ) == 2
757
+
758
+ # hidden directory
759
+ #
760
+ # tmp_path
761
+ # ├── .abandonned
762
+ # │ ├── .abandonned.txt
763
+ # │ └── subdir
764
+ # │ └── .abandonned.txt
765
+ # └── .abandonned.txt
766
+ #
767
+ assert cs .main ("--git-only" , tmp_path ) == 0
768
+ assert cs .main ("--git-only" , "--check-hidden" , tmp_path ) == 1
769
+ assert cs .main ("--git-only" , "--check-hidden" , "--check-filenames" , tmp_path ) == 2
770
+ hidden = tmp_path / ".abandonned"
771
+ hidden .mkdir ()
772
+ copyfile (typo_file , hidden / typo_file .name )
773
+ subdir = hidden / "subdir"
774
+ subdir .mkdir ()
775
+ copyfile (typo_file , subdir / typo_file .name )
776
+ run_git (tmp_path , "add" , "." )
777
+ assert cs .main ("--git-only" , tmp_path ) == 0
778
+ assert cs .main ("--git-only" , "--check-hidden" , tmp_path ) == 3
779
+ assert cs .main ("--git-only" , "--check-hidden" , "--check-filenames" , tmp_path ) == 8
780
+ # check again with a relative path
781
+ try :
782
+ rel = op .relpath (tmp_path )
783
+ except ValueError :
784
+ # Windows: path is on mount 'C:', start on mount 'D:'
785
+ pass
786
+ else :
787
+ assert cs .main ("--git-only" , rel ) == 0
788
+ assert cs .main ("--git-only" , "--check-hidden" , rel ) == 3
789
+ assert cs .main ("--git-only" , "--check-hidden" , "--check-filenames" , rel ) == 8
790
+
791
+ # hidden subdirectory
792
+ #
793
+ # tmp_path
794
+ # ├── .abandonned
795
+ # │ ├── .abandonned.txt
796
+ # │ └── subdir
797
+ # │ └── .abandonned.txt
798
+ # ├── .abandonned.txt
799
+ # └── subdir
800
+ # └── .abandonned
801
+ # └── .abandonned.txt
802
+ subdir = tmp_path / "subdir"
803
+ subdir .mkdir ()
804
+ hidden = subdir / ".abandonned"
805
+ hidden .mkdir ()
806
+ copyfile (typo_file , hidden / typo_file .name )
807
+ run_git (tmp_path , "add" , "." )
808
+ assert cs .main ("--git-only" , tmp_path ) == 0
809
+ assert cs .main ("--git-only" , "--check-hidden" , tmp_path ) == 4
810
+ assert cs .main ("--git-only" , "--check-hidden" , "--check-filenames" , tmp_path ) == 11
811
+
812
+
639
813
def test_check_hidden (
640
814
tmp_path : Path ,
641
815
capsys : pytest .CaptureFixture [str ],
0 commit comments