github.com/driusan/dgit@v0.0.0-20221118233547-f39f0c15edbb/git/ignore_test.go (about)

     1  package git
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  )
     7  
     8  func TestIgnoreMatchStringer(t *testing.T) {
     9  	zero := IgnoreMatch{PathName: File("myfile.txt")}
    10  	if zero.String() != fmt.Sprintf("::\tmyfile.txt") {
    11  		t.Fail()
    12  	}
    13  
    14  	nonZero := IgnoreMatch{PathName: File(".././abc/xyz/foo.txt"), IgnorePattern: IgnorePattern{Pattern: "/**/*.txt", LineNum: 5, Source: File("xyz/.gitignore")}}
    15  	if nonZero.String() != fmt.Sprintf("xyz/.gitignore:5:/**/*.txt\t.././abc/xyz/foo.txt") {
    16  		t.Fail()
    17  	}
    18  }
    19  
    20  func TestIgnorePatternMatches(t *testing.T) {
    21  	pattern1 := IgnorePattern{Pattern: "*.txt", Scope: File("")}
    22  	if !pattern1.Matches("foo/bar.txt", false) {
    23  		t.Fail()
    24  	}
    25  	if pattern1.Matches("foo/bar.go", false) {
    26  		t.Fail()
    27  	}
    28  	if !pattern1.Matches("bar.txt", false) {
    29  		t.Fail()
    30  	}
    31  	if pattern1.Matches("bar.go", false) {
    32  		t.Fail()
    33  	}
    34  
    35  	// Out of scope, should never match
    36  	pattern2 := IgnorePattern{Pattern: "*.txt", Scope: File("other")}
    37  	if pattern2.Matches("foo/bar.txt", false) {
    38  		t.Fail()
    39  	}
    40  	if pattern2.Matches("foo/bar.go", false) {
    41  		t.Fail()
    42  	}
    43  	if pattern2.Matches("bar.txt", false) {
    44  		t.Fail()
    45  	}
    46  	if pattern2.Matches("bar.go", false) {
    47  		t.Fail()
    48  	}
    49  
    50  	// Another out of scope, should never match
    51  	pattern3 := IgnorePattern{Pattern: "*.txt", Scope: File("other")}
    52  	if pattern3.Matches("other2/bar.txt", false) {
    53  		t.Fail()
    54  	}
    55  	if pattern3.Matches("other2/bar.go", false) {
    56  		t.Fail()
    57  	}
    58  	if pattern3.Matches("bar.txt", false) {
    59  		t.Fail()
    60  	}
    61  	if pattern3.Matches("bar.go", false) {
    62  		t.Fail()
    63  	}
    64  
    65  	// In scope, but with a specific path in the pattern
    66  	pattern4 := IgnorePattern{Pattern: "folder2/test.txt", Scope: File("folder1")}
    67  	if pattern4.Matches("folder1/test.txt", false) {
    68  		t.Fail()
    69  	}
    70  	if !pattern4.Matches("folder1/folder2/test.txt", false) {
    71  		t.Fail()
    72  	}
    73  
    74  	// In scope and a directory specific match
    75  	pattern5 := IgnorePattern{Pattern: "folder2/", Scope: File("")}
    76  	if pattern5.Matches("folder2", false) {
    77  		t.Fail()
    78  	}
    79  	if !pattern5.Matches("folder2", true) {
    80  		t.Fail()
    81  	}
    82  	if !pattern5.Matches("folder2/blarg.txt", false) {
    83  		t.Fail()
    84  	}
    85  	if !pattern5.Matches("folder2/blarg.txt", true) {
    86  		t.Fail()
    87  	}
    88  
    89  	// In scope with a variable number of folders in the pattern
    90  	pattern6 := IgnorePattern{Pattern: "foo/**/bar.txt", Scope: File("")}
    91  	if !pattern6.Matches("foo/bar.txt", false) {
    92  		t.Fail()
    93  	}
    94  	if !pattern6.Matches("foo/folder1/bar.txt", false) {
    95  		t.Fail()
    96  	}
    97  	if !pattern6.Matches("foo/folder1/folder2/bar.txt", false) {
    98  		t.Fail()
    99  	}
   100  
   101  	// Variable number of folders and a regular wildcard
   102  	pattern7 := IgnorePattern{Pattern: "foo/**/*.txt", Scope: File("")}
   103  	if !pattern7.Matches("foo/test.txt", false) {
   104  		t.Fail()
   105  	}
   106  	if !pattern7.Matches("foo/bar/t.txt", false) {
   107  		t.Fail()
   108  	}
   109  }
   110  
   111  func TestPatternSort(t *testing.T) {
   112  	patterns := []IgnorePattern{IgnorePattern{Pattern: "abc", Scope: File("")}, IgnorePattern{Pattern: "xyz", Scope: File("folder1")}}
   113  	sortPatterns(&patterns)
   114  	if patterns[0].Scope != "folder1" {
   115  		t.Fail()
   116  	}
   117  }