github.com/jxskiss/gopkg/v2@v2.14.9-0.20240514120614-899f3e7952b4/easy/filepath_test.go (about) 1 package easy 2 3 import ( 4 "os" 5 "path/filepath" 6 "testing" 7 8 "github.com/stretchr/testify/assert" 9 ) 10 11 func makeGlobTestDir(t *testing.T) { 12 t.Helper() 13 path := "./testdata/a/b/c.d/e.f" 14 err := os.MkdirAll(path, 0755) 15 if err != nil { 16 t.Fatalf("os.MkdirAll: %v", err) 17 } 18 } 19 20 func removeGlobTestDir() { 21 _ = os.RemoveAll("./testdata/a") 22 } 23 24 func TestGlob(t *testing.T) { 25 makeGlobTestDir(t) 26 defer removeGlobTestDir() 27 28 // test pass-through to vanilla path/filepath 29 { 30 matches, err := Glob("./*/*/*/*.d") 31 if err != nil { 32 t.Fatalf("Glob: %s", err) 33 } 34 if len(matches) != 1 { 35 t.Fatalf("got %d matches, expected 1", len(matches)) 36 } 37 expected := filepath.Clean("testdata/a/b/c.d") 38 if matches[0] != expected { 39 t.Fatalf("matched [%s], expected [%s]", matches[0], expected) 40 } 41 } 42 43 // test a single double-star 44 { 45 matches, err := Glob("./**/*.f") 46 if err != nil { 47 t.Fatalf("Glob: %s", err) 48 } 49 if len(matches) != 1 { 50 t.Fatalf("got %d matches, expected 1", len(matches)) 51 } 52 expected := filepath.Clean("testdata/a/b/c.d/e.f") 53 if matches[0] != expected { 54 t.Fatalf("matched [%s], expected [%s]", matches[0], expected) 55 } 56 } 57 58 // test a single double-star 59 { 60 matches, err := Glob("./testdata/**/*.*") 61 if err != nil { 62 t.Fatalf("Glob: %s", err) 63 } 64 if len(matches) != 2 { 65 t.Fatalf("got %d matches, expected 2", len(matches)) 66 } 67 expected := []string{ 68 filepath.Clean("testdata/a/b/c.d"), 69 filepath.Clean("testdata/a/b/c.d/e.f"), 70 } 71 for i, match := range matches { 72 if match != expected[i] { 73 t.Fatalf("matched [%s], expected [%s]", match, expected[i]) 74 } 75 } 76 } 77 78 // test two double-stars 79 { 80 matches, err := Glob("./**/b/**/*.f") 81 if err != nil { 82 t.Fatalf("Glob: %s", err) 83 } 84 if len(matches) != 1 { 85 t.Fatalf("got %d matches, expected 1", len(matches)) 86 } 87 expected := filepath.Clean("testdata/a/b/c.d/e.f") 88 if matches[0] != expected { 89 t.Fatalf("matched [%s], expected [%s]", matches[0], expected) 90 } 91 } 92 } 93 94 func Test_getDirectoryPermFromFilePerm(t *testing.T) { 95 testData := []struct { 96 FilePerm os.FileMode 97 Want os.FileMode 98 }{ 99 {0o020, 0o730}, 100 {0o040, 0o750}, 101 {0o060, 0o770}, 102 {0o002, 0o703}, 103 {0o004, 0o705}, 104 {0o006, 0o707}, 105 106 {0o644, 0o755}, 107 {0o666, 0o777}, 108 } 109 for _, tCase := range testData { 110 got := getDirectoryPermFromFilePerm(tCase.FilePerm) 111 assert.Equal(t, tCase.Want, got) 112 } 113 }