cloudflare/pint
Publicmirrored from https://github.com/cloudflare/pintAvailable
internal/discovery/glob_test.go
114lines · modecode
| 1 | package discovery_test |
| 2 | |
| 3 | import ( |
| 4 | "io/ioutil" |
| 5 | "os" |
| 6 | "path/filepath" |
| 7 | "strconv" |
| 8 | "testing" |
| 9 | |
| 10 | "github.com/cloudflare/pint/internal/discovery" |
| 11 | |
| 12 | "github.com/google/go-cmp/cmp" |
| 13 | ) |
| 14 | |
| 15 | func TestGlobFileFinder(t *testing.T) { |
| 16 | type testCaseT struct { |
| 17 | files []string |
| 18 | pattern string |
| 19 | output []discovery.File |
| 20 | shouldError bool |
| 21 | } |
| 22 | |
| 23 | testCases := []testCaseT{ |
| 24 | { |
| 25 | files: []string{}, |
| 26 | pattern: "*", |
| 27 | output: nil, |
| 28 | shouldError: false, |
| 29 | }, |
| 30 | { |
| 31 | files: []string{}, |
| 32 | pattern: "xxx", |
| 33 | output: nil, |
| 34 | shouldError: false, |
| 35 | }, |
| 36 | { |
| 37 | files: []string{"1.txt"}, |
| 38 | pattern: "*", |
| 39 | output: []discovery.File{{Path: "1.txt"}}, |
| 40 | shouldError: false, |
| 41 | }, |
| 42 | { |
| 43 | files: []string{"1.txt", "2.txt"}, |
| 44 | pattern: "*.txt", |
| 45 | output: []discovery.File{{Path: "1.txt"}, {Path: "2.txt"}}, |
| 46 | shouldError: false, |
| 47 | }, |
| 48 | { |
| 49 | files: []string{"1.txt", "2/2.txt"}, |
| 50 | pattern: "*", |
| 51 | output: []discovery.File{{Path: "1.txt"}, {Path: "2/2.txt"}}, |
| 52 | shouldError: false, |
| 53 | }, |
| 54 | { |
| 55 | files: []string{"1.txt", "2/2.txt", "2/2.foo", "3/3.txt"}, |
| 56 | pattern: "2/*.txt", |
| 57 | output: []discovery.File{{Path: "2/2.txt"}}, |
| 58 | shouldError: false, |
| 59 | }, |
| 60 | } |
| 61 | |
| 62 | for i, tc := range testCases { |
| 63 | t.Run(strconv.Itoa(i), func(t *testing.T) { |
| 64 | tmpDir, err := ioutil.TempDir("", "TestGlobFileFinder") |
| 65 | if err != nil { |
| 66 | t.Errorf("ioutil.TempDir() returned an error: %v", err) |
| 67 | return |
| 68 | } |
| 69 | defer func() { |
| 70 | err := os.RemoveAll(tmpDir) |
| 71 | if err != nil { |
| 72 | panic(err) |
| 73 | } |
| 74 | }() |
| 75 | |
| 76 | for _, path := range tc.files { |
| 77 | fullpath := filepath.Join(tmpDir, path) |
| 78 | if dir := filepath.Dir(fullpath); dir != tmpDir { |
| 79 | if _, err := os.Stat(dir); os.IsNotExist(err) { |
| 80 | if err := os.Mkdir(dir, 0755); err != nil { |
| 81 | t.Errorf("os.Mkdir(%s) returned an error: %v", dir, err) |
| 82 | return |
| 83 | } |
| 84 | |
| 85 | } |
| 86 | } |
| 87 | if _, err := os.Create(fullpath); err != nil { |
| 88 | t.Errorf("os.Create(%s) returned an error: %v", path, err) |
| 89 | return |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | err = os.Chdir(tmpDir) |
| 94 | if err != nil { |
| 95 | t.Errorf("os.Chdir(%s) returned an error: %v", tmpDir, err) |
| 96 | } |
| 97 | |
| 98 | gd := discovery.NewGlobFileFinder() |
| 99 | output, err := gd.Find(tc.pattern) |
| 100 | hadError := err != nil |
| 101 | if hadError != tc.shouldError { |
| 102 | t.Errorf("GlobFileFinder.Discover() returned err=%v, expected=%v", err, tc.shouldError) |
| 103 | } |
| 104 | |
| 105 | if hadError { |
| 106 | return |
| 107 | } |
| 108 | |
| 109 | if diff := cmp.Diff(tc.output, output.Results()); diff != "" { |
| 110 | t.Errorf("GlobFileFinder.Discover() returned wrong output (-want +got):\n%s", diff) |
| 111 | } |
| 112 | }) |
| 113 | } |
| 114 | } |
| 115 | |