github.com/evanw/esbuild@v0.21.4/internal/fs/fs_mock_test.go (about) 1 package fs 2 3 import ( 4 "fmt" 5 "testing" 6 ) 7 8 func TestMockFSBasicUnix(t *testing.T) { 9 fs := MockFS(map[string]string{ 10 "/README.md": "// README.md", 11 "/package.json": "// package.json", 12 "/src/index.js": "// src/index.js", 13 "/src/util.js": "// src/util.js", 14 }, MockUnix, "/") 15 16 // Test a missing file 17 _, err, _ := fs.ReadFile("/missing.txt") 18 if err == nil { 19 t.Fatal("Unexpectedly found /missing.txt") 20 } 21 22 // Test an existing file 23 readme, err, _ := fs.ReadFile("/README.md") 24 if err != nil { 25 t.Fatal("Expected to find /README.md") 26 } 27 if readme != "// README.md" { 28 t.Fatalf("Incorrect contents for /README.md: %q", readme) 29 } 30 31 // Test an existing nested file 32 index, err, _ := fs.ReadFile("/src/index.js") 33 if err != nil { 34 t.Fatal("Expected to find /src/index.js") 35 } 36 if index != "// src/index.js" { 37 t.Fatalf("Incorrect contents for /src/index.js: %q", index) 38 } 39 40 // Test a missing directory 41 _, err, _ = fs.ReadDirectory("/missing") 42 if err == nil { 43 t.Fatal("Unexpectedly found /missing") 44 } 45 46 // Test a nested directory 47 src, err, _ := fs.ReadDirectory("/src") 48 if err != nil { 49 t.Fatal("Expected to find /src") 50 } 51 indexEntry, _ := src.Get("index.js") 52 utilEntry, _ := src.Get("util.js") 53 if len(src.data) != 2 || 54 indexEntry == nil || indexEntry.Kind(fs) != FileEntry || 55 utilEntry == nil || utilEntry.Kind(fs) != FileEntry { 56 t.Fatalf("Incorrect contents for /src: %v", src) 57 } 58 59 // Test the top-level directory 60 slash, err, _ := fs.ReadDirectory("/") 61 if err != nil { 62 t.Fatal("Expected to find /") 63 } 64 srcEntry, _ := slash.Get("src") 65 readmeEntry, _ := slash.Get("README.md") 66 packageEntry, _ := slash.Get("package.json") 67 if len(slash.data) != 3 || 68 srcEntry == nil || srcEntry.Kind(fs) != DirEntry || 69 readmeEntry == nil || readmeEntry.Kind(fs) != FileEntry || 70 packageEntry == nil || packageEntry.Kind(fs) != FileEntry { 71 t.Fatalf("Incorrect contents for /: %v", slash) 72 } 73 } 74 75 func TestMockFSBasicWindows(t *testing.T) { 76 fs := MockFS(map[string]string{ 77 "/README.md": "// README.md", 78 "/package.json": "// package.json", 79 "/src/index.js": "// src/index.js", 80 "/src/util.js": "// src/util.js", 81 }, MockWindows, "C:\\") 82 83 // Test a missing file 84 _, err, _ := fs.ReadFile("C:\\missing.txt") 85 if err == nil { 86 t.Fatal("Unexpectedly found C:\\missing.txt") 87 } 88 89 // Test an existing file 90 readme, err, _ := fs.ReadFile("C:\\README.md") 91 if err != nil { 92 t.Fatal("Expected to find C:\\README.md") 93 } 94 if readme != "// README.md" { 95 t.Fatalf("Incorrect contents for C:\\README.md: %q", readme) 96 } 97 98 // Test an existing nested file 99 index, err, _ := fs.ReadFile("C:\\src\\index.js") 100 if err != nil { 101 t.Fatal("Expected to find C:\\src\\index.js") 102 } 103 if index != "// src/index.js" { 104 t.Fatalf("Incorrect contents for C:\\src\\index.js: %q", index) 105 } 106 107 // Test a missing directory 108 _, err, _ = fs.ReadDirectory("C:\\missing") 109 if err == nil { 110 t.Fatal("Unexpectedly found C:\\missing") 111 } 112 113 // Test a nested directory 114 src, err, _ := fs.ReadDirectory("C:\\src") 115 if err != nil { 116 t.Fatal("Expected to find C:\\src") 117 } 118 indexEntry, _ := src.Get("index.js") 119 utilEntry, _ := src.Get("util.js") 120 if len(src.data) != 2 || 121 indexEntry == nil || indexEntry.Kind(fs) != FileEntry || 122 utilEntry == nil || utilEntry.Kind(fs) != FileEntry { 123 t.Fatalf("Incorrect contents for C:\\src: %v", src) 124 } 125 126 // Test the top-level directory 127 slash, err, _ := fs.ReadDirectory("C:\\") 128 if err != nil { 129 t.Fatal("Expected to find C:\\") 130 } 131 srcEntry, _ := slash.Get("src") 132 readmeEntry, _ := slash.Get("README.md") 133 packageEntry, _ := slash.Get("package.json") 134 if len(slash.data) != 3 || 135 srcEntry == nil || srcEntry.Kind(fs) != DirEntry || 136 readmeEntry == nil || readmeEntry.Kind(fs) != FileEntry || 137 packageEntry == nil || packageEntry.Kind(fs) != FileEntry { 138 t.Fatalf("Incorrect contents for C:\\: %v", slash) 139 } 140 } 141 142 func TestMockFSRelUnix(t *testing.T) { 143 fs := MockFS(map[string]string{}, MockUnix, "/") 144 145 expect := func(a string, b string, c string) { 146 t.Helper() 147 t.Run(fmt.Sprintf("Rel(%q, %q) == %q", a, b, c), func(t *testing.T) { 148 t.Helper() 149 rel, ok := fs.Rel(a, b) 150 if !ok { 151 t.Fatalf("!ok") 152 } 153 if rel != c { 154 t.Fatalf("Expected %q, got %q", c, rel) 155 } 156 }) 157 } 158 159 expect("/a/b", "/a/b", ".") 160 expect("/a/b", "/a/b/c", "c") 161 expect("/a/b", "/a/b/c/d", "c/d") 162 expect("/a/b/c", "/a/b", "..") 163 expect("/a/b/c/d", "/a/b", "../..") 164 expect("/a/b/c", "/a/b/x", "../x") 165 expect("/a/b/c/d", "/a/b/x", "../../x") 166 expect("/a/b/c", "/a/b/x/y", "../x/y") 167 expect("/a/b/c/d", "/a/b/x/y", "../../x/y") 168 169 expect("a/b", "a/c", "../c") 170 expect("./a/b", "./a/c", "../c") 171 expect(".", "./a/b", "a/b") 172 expect(".", ".//a/b", "a/b") 173 expect(".", "././a/b", "a/b") 174 expect(".", "././/a/b", "a/b") 175 } 176 177 func TestMockFSRelWindows(t *testing.T) { 178 fs := MockFS(map[string]string{}, MockWindows, "C:\\") 179 180 expect := func(a string, b string, c string) { 181 t.Helper() 182 t.Run(fmt.Sprintf("Rel(%q, %q) == %q", a, b, c), func(t *testing.T) { 183 t.Helper() 184 rel, ok := fs.Rel(a, b) 185 if !ok { 186 t.Fatalf("!ok") 187 } 188 if rel != c { 189 t.Fatalf("Expected %q, got %q", c, rel) 190 } 191 }) 192 } 193 194 expect("C:\\a\\b", "C:\\a\\b", ".") 195 expect("C:\\a\\b", "C:\\a\\b\\c", "c") 196 expect("C:\\a\\b", "C:\\a\\b\\c\\d", "c\\d") 197 expect("C:\\a\\b\\c", "C:\\a\\b", "..") 198 expect("C:\\a\\b\\c\\d", "C:\\a\\b", "..\\..") 199 expect("C:\\a\\b\\c", "C:\\a\\b\\x", "..\\x") 200 expect("C:\\a\\b\\c\\d", "C:\\a\\b\\x", "..\\..\\x") 201 expect("C:\\a\\b\\c", "C:\\a\\b\\x\\y", "..\\x\\y") 202 expect("C:\\a\\b\\c\\d", "C:\\a\\b\\x\\y", "..\\..\\x\\y") 203 204 expect("a\\b", "a\\c", "..\\c") 205 expect(".\\a\\b", ".\\a\\c", "..\\c") 206 expect(".", ".\\a\\b", "a\\b") 207 expect(".", ".\\\\a\\b", "a\\b") 208 expect(".", ".\\.\\a\\b", "a\\b") 209 expect(".", ".\\.\\\\a\\b", "a\\b") 210 }