|
| 1 | +package fsext |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "path/filepath" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/stretchr/testify/require" |
| 9 | +) |
| 10 | + |
| 11 | +func TestDefaultDir(t *testing.T) { |
| 12 | + wd, err := os.Getwd() |
| 13 | + require.NoError(t, err) |
| 14 | + tests := []struct { |
| 15 | + name string |
| 16 | + entrypoint string |
| 17 | + dir string |
| 18 | + expected string |
| 19 | + }{ |
| 20 | + { |
| 21 | + name: "default to current working directory", |
| 22 | + entrypoint: "", |
| 23 | + dir: "", |
| 24 | + expected: wd, |
| 25 | + }, |
| 26 | + { |
| 27 | + name: "resolves relative dir path", |
| 28 | + entrypoint: "", |
| 29 | + dir: "./dir", |
| 30 | + expected: filepath.Join(wd, "dir"), |
| 31 | + }, |
| 32 | + { |
| 33 | + name: "return entrypoint if set", |
| 34 | + entrypoint: filepath.Join(wd, "entrypoint"), |
| 35 | + dir: "", |
| 36 | + expected: "", |
| 37 | + }, |
| 38 | + { |
| 39 | + name: "if entrypoint and dir are set", |
| 40 | + entrypoint: filepath.Join(wd, "entrypoint"), |
| 41 | + dir: filepath.Join(wd, "dir"), |
| 42 | + expected: filepath.Join(wd, "dir"), |
| 43 | + }, |
| 44 | + { |
| 45 | + name: "if entrypoint and dir are set and dir is relative", |
| 46 | + entrypoint: filepath.Join(wd, "entrypoint"), |
| 47 | + dir: "./dir", |
| 48 | + expected: filepath.Join(wd, "dir"), |
| 49 | + }, |
| 50 | + } |
| 51 | + for _, tt := range tests { |
| 52 | + t.Run(tt.name, func(t *testing.T) { |
| 53 | + require.Equal(t, tt.expected, DefaultDir(tt.entrypoint, tt.dir)) |
| 54 | + }) |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +func TestSearch(t *testing.T) { |
| 59 | + wd, err := os.Getwd() |
| 60 | + require.NoError(t, err) |
| 61 | + tests := []struct { |
| 62 | + name string |
| 63 | + entrypoint string |
| 64 | + dir string |
| 65 | + possibleFilenames []string |
| 66 | + expectedEntrypoint string |
| 67 | + expectedDir string |
| 68 | + }{ |
| 69 | + { |
| 70 | + name: "find foo.txt using relative entrypoint", |
| 71 | + entrypoint: "./testdata/foo.txt", |
| 72 | + possibleFilenames: []string{"foo.txt"}, |
| 73 | + expectedEntrypoint: filepath.Join(wd, "testdata", "foo.txt"), |
| 74 | + expectedDir: filepath.Join(wd, "testdata"), |
| 75 | + }, |
| 76 | + { |
| 77 | + name: "find foo.txt using absolute entrypoint", |
| 78 | + entrypoint: filepath.Join(wd, "testdata", "foo.txt"), |
| 79 | + possibleFilenames: []string{"foo.txt"}, |
| 80 | + expectedEntrypoint: filepath.Join(wd, "testdata", "foo.txt"), |
| 81 | + expectedDir: filepath.Join(wd, "testdata"), |
| 82 | + }, |
| 83 | + { |
| 84 | + name: "find foo.txt using relative dir", |
| 85 | + dir: "./testdata", |
| 86 | + possibleFilenames: []string{"foo.txt"}, |
| 87 | + expectedEntrypoint: filepath.Join(wd, "testdata", "foo.txt"), |
| 88 | + expectedDir: filepath.Join(wd, "testdata"), |
| 89 | + }, |
| 90 | + { |
| 91 | + name: "find foo.txt using absolute dir", |
| 92 | + dir: filepath.Join(wd, "testdata"), |
| 93 | + possibleFilenames: []string{"foo.txt"}, |
| 94 | + expectedEntrypoint: filepath.Join(wd, "testdata", "foo.txt"), |
| 95 | + expectedDir: filepath.Join(wd, "testdata"), |
| 96 | + }, |
| 97 | + { |
| 98 | + name: "find foo.txt using relative dir and relative entrypoint", |
| 99 | + entrypoint: "./testdata/foo.txt", |
| 100 | + dir: "./testdata/some/other/dir", |
| 101 | + possibleFilenames: []string{"foo.txt"}, |
| 102 | + expectedEntrypoint: filepath.Join(wd, "testdata", "foo.txt"), |
| 103 | + expectedDir: filepath.Join(wd, "testdata", "some", "other", "dir"), |
| 104 | + }, |
| 105 | + { |
| 106 | + name: "find fs.go using no entrypoint or dir", |
| 107 | + entrypoint: "", |
| 108 | + dir: "", |
| 109 | + possibleFilenames: []string{"fs.go"}, |
| 110 | + expectedEntrypoint: filepath.Join(wd, "fs.go"), |
| 111 | + expectedDir: wd, |
| 112 | + }, |
| 113 | + { |
| 114 | + name: "find ../../Taskfile.yml using no entrypoint or dir by walking", |
| 115 | + entrypoint: "", |
| 116 | + dir: "", |
| 117 | + possibleFilenames: []string{"Taskfile.yml"}, |
| 118 | + expectedEntrypoint: filepath.Join(wd, "..", "..", "Taskfile.yml"), |
| 119 | + expectedDir: filepath.Join(wd, "..", ".."), |
| 120 | + }, |
| 121 | + { |
| 122 | + name: "find foo.txt first if listed first in possible filenames", |
| 123 | + entrypoint: "./testdata", |
| 124 | + possibleFilenames: []string{"foo.txt", "bar.txt"}, |
| 125 | + expectedEntrypoint: filepath.Join(wd, "testdata", "foo.txt"), |
| 126 | + expectedDir: filepath.Join(wd, "testdata"), |
| 127 | + }, |
| 128 | + { |
| 129 | + name: "find bar.txt first if listed first in possible filenames", |
| 130 | + entrypoint: "./testdata", |
| 131 | + possibleFilenames: []string{"bar.txt", "foo.txt"}, |
| 132 | + expectedEntrypoint: filepath.Join(wd, "testdata", "bar.txt"), |
| 133 | + expectedDir: filepath.Join(wd, "testdata"), |
| 134 | + }, |
| 135 | + } |
| 136 | + for _, tt := range tests { |
| 137 | + t.Run(tt.name, func(t *testing.T) { |
| 138 | + entrypoint, dir, err := Search(tt.entrypoint, tt.dir, tt.possibleFilenames) |
| 139 | + require.NoError(t, err) |
| 140 | + require.Equal(t, tt.expectedEntrypoint, entrypoint) |
| 141 | + require.Equal(t, tt.expectedDir, dir) |
| 142 | + }) |
| 143 | + } |
| 144 | +} |
0 commit comments