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