File: fs_mock.go

package info (click to toggle)
golang-github-evanw-esbuild 0.25.11-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 10,236 kB
  • sloc: javascript: 28,602; makefile: 856; sh: 17
file content (325 lines) | stat: -rw-r--r-- 6,928 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
package fs

// This is a mock implementation of the "fs" module for use with tests. It does
// not actually read from the file system. Instead, it reads from a pre-specified
// map of file paths to files.

import (
	"errors"
	"path"
	"strings"
	"syscall"
)

type MockKind uint8

const (
	MockUnix MockKind = iota
	MockWindows
)

type mockFS struct {
	dirs          map[string]DirEntries
	files         map[string]string
	absWorkingDir string
	defaultVolume string
	Kind          MockKind
}

func MockFS(input map[string]string, kind MockKind, absWorkingDir string) FS {
	dirs := make(map[string]DirEntries)
	files := make(map[string]string)

	var defaultVolume string
	if kind == MockWindows {
		_, defaultVolume = win2unix(absWorkingDir)
	}

	for k, v := range input {
		var volume string
		files[k] = v
		if kind == MockWindows {
			k, volume = win2unix(k)
		}
		original := k

		// Build the directory map
		for {
			kDir := path.Dir(k)
			key := kDir
			if kind == MockWindows {
				key = unix2win(key, volume, defaultVolume)
			}
			dir, ok := dirs[key]
			if !ok {
				dir = DirEntries{dir: key, data: make(map[string]*Entry)}
				dirs[key] = dir
			}
			if kDir == k {
				break
			}
			base := path.Base(k)
			if k == original {
				dir.data[strings.ToLower(base)] = &Entry{kind: FileEntry, base: base}
			} else {
				dir.data[strings.ToLower(base)] = &Entry{kind: DirEntry, base: base}
			}
			k = kDir
		}
	}

	return &mockFS{dirs, files, absWorkingDir, defaultVolume, kind}
}

func (fs *mockFS) ReadDirectory(path string) (DirEntries, error, error) {
	if fs.Kind == MockWindows {
		path = strings.ReplaceAll(path, "/", "\\")
	}

	var slash byte = '/'
	if fs.Kind == MockWindows {
		slash = '\\'
	}

	// Trim trailing slashes before lookup
	firstSlash := strings.IndexByte(path, slash)
	for {
		i := strings.LastIndexByte(path, slash)
		if i != len(path)-1 || i <= firstSlash {
			break
		}
		path = path[:i]
	}

	if dir, ok := fs.dirs[path]; ok {
		return dir, nil, nil
	}
	return DirEntries{}, syscall.ENOENT, syscall.ENOENT
}

func (fs *mockFS) ReadFile(path string) (string, error, error) {
	if fs.Kind == MockWindows {
		path = strings.ReplaceAll(path, "/", "\\")
	}
	if contents, ok := fs.files[path]; ok {
		return contents, nil, nil
	}
	return "", syscall.ENOENT, syscall.ENOENT
}

func (fs *mockFS) OpenFile(path string) (OpenedFile, error, error) {
	if fs.Kind == MockWindows {
		path = strings.ReplaceAll(path, "/", "\\")
	}
	if contents, ok := fs.files[path]; ok {
		return &InMemoryOpenedFile{Contents: []byte(contents)}, nil, nil
	}
	return nil, syscall.ENOENT, syscall.ENOENT
}

func (fs *mockFS) ModKey(path string) (ModKey, error) {
	return ModKey{}, errors.New("This is not available during tests")
}

func win2unix(p string) (result string, volume string) {
	if len(p) > 0 && strings.HasPrefix(p[1:], ":\\") {
		if c := p[0]; (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') {
			volume = p[0:1]
			p = p[2:]
		}
	}
	p = strings.ReplaceAll(p, "\\", "/")
	return p, volume
}

func unix2win(p string, volume string, defaultVolume string) string {
	p = strings.ReplaceAll(p, "/", "\\")
	if strings.HasPrefix(p, "\\") {
		if volume == "" {
			volume = defaultVolume
		}
		p = volume + ":" + p
	}
	return p
}

func (fs *mockFS) IsAbs(p string) bool {
	if fs.Kind == MockWindows {
		p, _ = win2unix(p)
	}
	return path.IsAbs(p)
}

func (fs *mockFS) Abs(p string) (string, bool) {
	var volume string
	if fs.Kind == MockWindows {
		p, volume = win2unix(p)
	}

	p = path.Clean(path.Join("/", p))

	if fs.Kind == MockWindows {
		p = unix2win(p, volume, fs.defaultVolume)
	}

	return p, true
}

func (fs *mockFS) Dir(p string) string {
	var volume string
	if fs.Kind == MockWindows {
		p, volume = win2unix(p)
	}

	p = path.Dir(p)

	if fs.Kind == MockWindows {
		p = unix2win(p, volume, fs.defaultVolume)
	}

	return p
}

func (fs *mockFS) Base(p string) string {
	var volume string
	if fs.Kind == MockWindows {
		p, volume = win2unix(p)
	}

	p = path.Base(p)

	if fs.Kind == MockWindows && p == "/" {
		p = volume + ":\\"
	}

	return p
}

func (fs *mockFS) Ext(p string) string {
	if fs.Kind == MockWindows {
		p, _ = win2unix(p)
	}

	return path.Ext(p)
}

func (fs *mockFS) Join(parts ...string) string {
	var volume string
	if fs.Kind == MockWindows {
		converted := make([]string, len(parts))
		for i, part := range parts {
			var v string
			converted[i], v = win2unix(part)
			if i == 0 {
				volume = v
			}
		}
		parts = converted
	}

	p := path.Clean(path.Join(parts...))

	if fs.Kind == MockWindows {
		p = unix2win(p, volume, fs.defaultVolume)
	}

	return p
}

func (fs *mockFS) Cwd() string {
	return fs.absWorkingDir
}

func splitOnSlash(path string) (string, string) {
	if slash := strings.IndexByte(path, '/'); slash != -1 {
		return path[:slash], path[slash+1:]
	}
	return path, ""
}

func (fs *mockFS) Rel(base string, target string) (string, bool) {
	var volume string
	if fs.Kind == MockWindows {
		var v string
		base, volume = win2unix(base)
		target, v = win2unix(target)
		if volume == "" {
			volume = fs.defaultVolume
		}
		if v == "" {
			v = fs.defaultVolume
		}
		if strings.ToUpper(v) != strings.ToUpper(volume) {
			return "", false
		}
	}

	base = path.Clean(base)
	target = path.Clean(target)

	// Go's implementation does these checks
	if base == target {
		return ".", true
	}
	if base == "." {
		base = ""
	}

	// Go's implementation fails when this condition is false. I believe this is
	// because of this part of the contract, from Go's documentation: "An error
	// is returned if targpath can't be made relative to basepath or if knowing
	// the current working directory would be necessary to compute it."
	if (len(base) > 0 && base[0] == '/') != (len(target) > 0 && target[0] == '/') {
		return "", false
	}

	// Find the common parent directory
	for {
		bHead, bTail := splitOnSlash(base)
		tHead, tTail := splitOnSlash(target)
		if bHead != tHead {
			break
		}
		base = bTail
		target = tTail
	}

	// Stop now if base is a subpath of target
	if base == "" {
		if fs.Kind == MockWindows {
			target = unix2win(target, volume, fs.defaultVolume)
		}
		return target, true
	}

	// Traverse up to the common parent
	commonParent := strings.Repeat("../", strings.Count(base, "/")+1)

	// Stop now if target is a subpath of base
	if target == "" {
		target = commonParent[:len(commonParent)-1]
		if fs.Kind == MockWindows {
			target = unix2win(target, volume, fs.defaultVolume)
		}
		return target, true
	}

	// Otherwise, down to the parent
	target = commonParent + target
	if fs.Kind == MockWindows {
		target = unix2win(target, volume, fs.defaultVolume)
	}
	return target, true
}

func (fs *mockFS) EvalSymlinks(path string) (string, bool) {
	return "", false
}

func (fs *mockFS) kind(dir string, base string) (symlink string, kind EntryKind) {
	panic("This should never be called")
}

func (fs *mockFS) WatchData() WatchData {
	panic("This should never be called")
}