github.com/pkumar631/talisman@v0.3.2/detector/ignores_test.go (about) 1 package detector 2 3 import ( 4 "testing" 5 6 "github.com/stretchr/testify/assert" 7 "github.com/thoughtworks/talisman/git_repo" 8 ) 9 10 func TestShouldIgnoreEmptyLinesInTheFile(t *testing.T) { 11 for _, s := range []string{"", " ", " "} { 12 assert.True(t, NewIgnores(s).AcceptsAll(), "Expected '%s' to result in no ignore patterns.", s) 13 } 14 } 15 16 func TestShouldIgnoreLinesThatBeginWithThePound(t *testing.T) { 17 for _, s := range []string{"#", "#monkey", "# this monkey likes bananas "} { 18 assert.True(t, NewIgnores(s).AcceptsAll(), "Expected commented line '%s' to result in no ignore patterns", s) 19 } 20 } 21 22 func TestRawPatterns(t *testing.T) { 23 assertAccepts("foo", "bar", t) 24 assertAccepts("foo", "foobar", t) 25 assertAccepts("foo", "foo/bar", t) 26 assertAccepts("foo", "foo/bar/baz", t) 27 28 assertDenies("foo", "foo", t) 29 assertDenies("foo", "bar/foo", t) 30 assertDenies("foo", "bar/baz/foo", t) 31 } 32 33 func TestSingleStarPatterns(t *testing.T) { 34 assertAccepts("foo*", "bar", t) 35 assertAccepts("foo*", "foo/bar", t) 36 assertAccepts("foo*", "foo/bar/baz", t) 37 38 assertDenies("foo*", "foo", t) 39 assertDenies("foo*", "foobar", t) 40 41 assertAccepts("*.pem", "foo.txt", t) 42 assertAccepts("*.pem", "pem.go", t) 43 assertDenies("*.pem", "secret.pem", t) 44 assertDenies("*.pem", "foo/bar/secret.pem", t) 45 } 46 47 func TestDirectoryPatterns(t *testing.T) { 48 assertAccepts("foo/", "bar", t) 49 assertAccepts("foo/", "foo", t) 50 assertDenies("foo/", "foo/bar", t) 51 assertDenies("foo/", "foo/bar.txt", t) 52 assertDenies("foo/", "foo/bar/baz.txt", t) 53 } 54 55 func assertDenies(pattern, path string, t *testing.T) { 56 assert.True(t, NewIgnores(pattern).Deny(testAddition(path)), "%s is expected to deny a file named %s.", pattern, path) 57 } 58 59 func assertAccepts(pattern, path string, t *testing.T) { 60 assert.True(t, NewIgnores(pattern).Accept(testAddition(path)), "%s is expected to accept a file named %s.", pattern, path) 61 } 62 63 func testAddition(path string) git_repo.Addition { 64 return git_repo.NewAddition(path, make([]byte, 0)) 65 }