github.com/sercand/please@v13.4.0+incompatible/src/fs/glob_test.go (about) 1 // Tests for our glob functions. 2 3 package fs 4 5 import ( 6 "testing" 7 8 "github.com/stretchr/testify/assert" 9 ) 10 11 func TestCanGlobFirstFile(t *testing.T) { 12 // If this fails then we probably failed to interpret /**/ properly, 13 // which can resolve to just / - ie. we glob test_data/**/*.txt, 14 // which should include test_data/test.txt 15 if !FileExists("src/core/test_data/test.txt") { 16 t.Errorf("Can't load test_data/test.txt") 17 } 18 } 19 20 func TestCanGlobSecondFile(t *testing.T) { 21 // If this fails then we haven't walked down enough subdirectories 22 // or something. Shouldn't really be hard - it's a sanity check really 23 // since it's similar to the third file but without a package boundary. 24 if !FileExists("src/core/test_data/test_subfolder1/a.txt") { 25 t.Errorf("Can't load test_data/test_subfolder1/a.txt") 26 } 27 } 28 29 func TestCannotGlobThirdFile(t *testing.T) { 30 // This one we should not be able to glob because it's inside its own subpackage. 31 if FileExists("src/core/test_data/test_subfolder2/b.txt") { 32 t.Errorf("Incorrectly loaded test_data/test_subfolder2/b.txt; have globbed it through a package boundary") 33 } 34 } 35 36 func TestCanGlobFileAtRootWithDoubleStar(t *testing.T) { 37 state := NewDefaultBuildState() 38 files, err := glob(state, "src/core/test_data/test_subfolder1", "**/*.txt", false, nil) 39 assert.NoError(t, err) 40 assert.Equal(t, []string{"src/core/test_data/test_subfolder1/a.txt"}, files) 41 } 42 43 func TestIsGlob(t *testing.T) { 44 assert.True(t, IsGlob("a*b")) 45 assert.True(t, IsGlob("ab/*.txt")) 46 assert.True(t, IsGlob("ab/c.tx?")) 47 assert.True(t, IsGlob("ab/[a-z].txt")) 48 assert.False(t, IsGlob("abc.txt")) 49 assert.False(t, IsGlob("ab/c.txt")) 50 } 51 52 func TestGlobPlusPlus(t *testing.T) { 53 state := NewDefaultBuildState() 54 files, err := glob(state, "src/core/test_data/test_subfolder++", "**/*.txt", false, nil) 55 assert.NoError(t, err) 56 assert.Equal(t, []string{"src/core/test_data/test_subfolder++/test.txt"}, files) 57 } 58 59 func TestGlobExcludes(t *testing.T) { 60 state := NewDefaultBuildState() 61 files := Glob(state, "src/core", []string{"test_data/**/*.txt"}, nil, []string{"test.txt"}, false) 62 expected := []string{"test_data/test_subfolder1/a.txt"} 63 assert.Equal(t, expected, files) 64 }