-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfs_example_test.go
More file actions
385 lines (337 loc) · 11.1 KB
/
fs_example_test.go
File metadata and controls
385 lines (337 loc) · 11.1 KB
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
package core_test
import (
. "dappco.re/go"
)
// ExampleFs_New creates a sandboxed filesystem rooted at a workspace path. File reads,
// writes, walks, and cleanup stay sandbox-aware through Fs.
func ExampleFs_New() {
f := (&Fs{}).New("/srv/workspaces")
Println(f.Root())
// Output: /srv/workspaces
}
// ExampleFs_Read reads content through `Fs.Read` for sandboxed file operations. File
// reads, writes, walks, and cleanup stay sandbox-aware through Fs.
func ExampleFs_Read() {
f := (&Fs{}).New("/")
dir := f.TempDir("core-fs-example")
defer f.DeleteAll(dir)
f.Write(Path(dir, "hello.txt"), "hello")
Println(f.Read(Path(dir, "hello.txt")).Value)
// Output: hello
}
// ExampleFs_Write writes content through `Fs.Write` for sandboxed file operations. File
// reads, writes, walks, and cleanup stay sandbox-aware through Fs.
func ExampleFs_Write() {
f := (&Fs{}).New("/")
dir := f.TempDir("core-fs-example")
defer f.DeleteAll(dir)
r := f.Write(Path(dir, "hello.txt"), "hello")
Println(r.OK)
Println(f.Read(Path(dir, "hello.txt")).Value)
// Output:
// true
// hello
}
// ExampleFs_WriteMode writes content with explicit permissions through `Fs.WriteMode` for
// sandboxed file operations. File reads, writes, walks, and cleanup stay sandbox-aware
// through Fs.
func ExampleFs_WriteMode() {
f := (&Fs{}).New("/")
dir := f.TempDir("core-fs-example")
defer f.DeleteAll(dir)
r := f.WriteMode(Path(dir, "secret.txt"), "secret", 0600)
Println(r.OK)
// Output: true
}
// ExampleFs_TempDir creates a temporary directory through `Fs.TempDir` for sandboxed file
// operations. File reads, writes, walks, and cleanup stay sandbox-aware through Fs.
func ExampleFs_TempDir() {
f := (&Fs{}).New("/")
dir := f.TempDir("core-fs-example")
defer f.DeleteAll(dir)
Println(PathBase(dir) != "")
// Output: true
}
// ExampleDirFS creates a directory-backed filesystem through `DirFS` for sandboxed file
// operations. File reads, writes, walks, and cleanup stay sandbox-aware through Fs.
func ExampleDirFS() {
f := (&Fs{}).New("/")
dir := f.TempDir("core-fs-example")
defer f.DeleteAll(dir)
f.Write(Path(dir, "hello.txt"), "hello")
emb := Mount(DirFS(dir), ".").Value.(*Embed)
Println(emb.ReadString("hello.txt").Value)
// Output: hello
}
// ExampleFs_WriteAtomic writes content atomically through `Fs.WriteAtomic` for sandboxed
// file operations. File reads, writes, walks, and cleanup stay sandbox-aware through Fs.
func ExampleFs_WriteAtomic() {
f := (&Fs{}).New("/")
dir := f.TempDir("example")
defer f.DeleteAll(dir)
path := Path(dir, "status.json")
f.WriteAtomic(path, `{"status":"completed"}`)
r := f.Read(path)
Println(r.Value)
// Output: {"status":"completed"}
}
// ExampleFs_EnsureDir creates missing directories through `Fs.EnsureDir` for sandboxed
// file operations. File reads, writes, walks, and cleanup stay sandbox-aware through Fs.
func ExampleFs_EnsureDir() {
f := (&Fs{}).New("/")
dir := f.TempDir("core-fs-example")
defer f.DeleteAll(dir)
r := f.EnsureDir(Path(dir, "logs"))
Println(r.OK)
Println(f.IsDir(Path(dir, "logs")))
// Output:
// true
// true
}
// ExampleFs_IsDir checks directory state through `Fs.IsDir` for sandboxed file operations.
// File reads, writes, walks, and cleanup stay sandbox-aware through Fs.
func ExampleFs_IsDir() {
f := (&Fs{}).New("/")
dir := f.TempDir("core-fs-example")
defer f.DeleteAll(dir)
Println(f.IsDir(dir))
// Output: true
}
// ExampleFs_IsFile checks file state through `Fs.IsFile` for sandboxed file operations.
// File reads, writes, walks, and cleanup stay sandbox-aware through Fs.
func ExampleFs_IsFile() {
f := (&Fs{}).New("/")
dir := f.TempDir("core-fs-example")
defer f.DeleteAll(dir)
path := Path(dir, "hello.txt")
f.Write(path, "hello")
Println(f.IsFile(path))
// Output: true
}
// ExampleFs_Exists checks whether a sandboxed path exists after writing a file. File
// reads, writes, walks, and cleanup stay sandbox-aware through Fs.
func ExampleFs_Exists() {
f := (&Fs{}).New("/")
dir := f.TempDir("core-fs-example")
defer f.DeleteAll(dir)
path := Path(dir, "hello.txt")
f.Write(path, "hello")
Println(f.Exists(path))
// Output: true
}
// ExampleFs_List lists entries through `Fs.List` for sandboxed file operations. File
// reads, writes, walks, and cleanup stay sandbox-aware through Fs.
func ExampleFs_List() {
f := (&Fs{}).New("/")
dir := f.TempDir("core-fs-example")
defer f.DeleteAll(dir)
f.Write(Path(dir, "hello.txt"), "hello")
Println(f.List(dir).OK)
// Output: true
}
// ExampleFs_Stat reads metadata through `Fs.Stat` for sandboxed file operations. File
// reads, writes, walks, and cleanup stay sandbox-aware through Fs.
func ExampleFs_Stat() {
f := (&Fs{}).New("/")
dir := f.TempDir("core-fs-example")
defer f.DeleteAll(dir)
path := Path(dir, "hello.txt")
f.Write(path, "hello")
Println(f.Stat(path).OK)
// Output: true
}
// ExampleFs_Open opens a sandboxed file for streaming reads. File reads, writes, walks,
// and cleanup stay sandbox-aware through Fs.
func ExampleFs_Open() {
f := (&Fs{}).New("/")
dir := f.TempDir("core-fs-example")
defer f.DeleteAll(dir)
path := Path(dir, "hello.txt")
f.Write(path, "hello")
r := f.Open(path)
Println(r.OK)
CloseStream(r.Value)
// Output: true
}
// ExampleFs_Create creates a resource through `Fs.Create` for sandboxed file operations.
// File reads, writes, walks, and cleanup stay sandbox-aware through Fs.
func ExampleFs_Create() {
f := (&Fs{}).New("/")
dir := f.TempDir("core-fs-example")
defer f.DeleteAll(dir)
path := Path(dir, "hello.txt")
r := f.Create(path)
WriteAll(r.Value, "hello")
Println(f.Read(path).Value)
// Output: hello
}
// ExampleFs_Append appends content through `Fs.Append` for sandboxed file operations. File
// reads, writes, walks, and cleanup stay sandbox-aware through Fs.
func ExampleFs_Append() {
f := (&Fs{}).New("/")
dir := f.TempDir("core-fs-example")
defer f.DeleteAll(dir)
path := Path(dir, "hello.txt")
f.Write(path, "hello")
r := f.Append(path)
WriteAll(r.Value, " world")
Println(f.Read(path).Value)
// Output: hello world
}
// ExampleFs_ReadStream opens a read stream through `Fs.ReadStream` for sandboxed file
// operations. File reads, writes, walks, and cleanup stay sandbox-aware through Fs.
func ExampleFs_ReadStream() {
f := (&Fs{}).New("/")
dir := f.TempDir("core-fs-example")
defer f.DeleteAll(dir)
path := Path(dir, "hello.txt")
f.Write(path, "hello")
r := f.ReadStream(path)
Println(ReadAll(r.Value).Value)
// Output: hello
}
// ExampleFs_WriteStream opens a write stream through `Fs.WriteStream` for sandboxed file
// operations. File reads, writes, walks, and cleanup stay sandbox-aware through Fs.
func ExampleFs_WriteStream() {
f := (&Fs{}).New("/")
dir := f.TempDir("core-fs-example")
defer f.DeleteAll(dir)
path := Path(dir, "hello.txt")
r := f.WriteStream(path)
WriteAll(r.Value, "hello")
Println(f.Read(path).Value)
// Output: hello
}
// ExampleReadAll reads an entire stream through `ReadAll` for sandboxed file operations.
// File reads, writes, walks, and cleanup stay sandbox-aware through Fs.
func ExampleReadAll() {
r := ReadAll(NewReader("hello"))
Println(r.Value)
// Output: hello
}
// ExampleWriteAll writes a complete payload through `WriteAll` for sandboxed file
// operations. File reads, writes, walks, and cleanup stay sandbox-aware through Fs.
func ExampleWriteAll() {
buf := NewBuffer()
r := WriteAll(buf, "hello")
Println(r.OK)
Println(buf.String())
// Output:
// true
// hello
}
// ExampleCloseStream closes a stream through `CloseStream` for sandboxed file operations.
// File reads, writes, walks, and cleanup stay sandbox-aware through Fs.
func ExampleCloseStream() {
CloseStream(NewReader("not a closer"))
Println("ok")
// Output: ok
}
// ExampleFs_Delete deletes a value through `Fs.Delete` for sandboxed file operations. File
// reads, writes, walks, and cleanup stay sandbox-aware through Fs.
func ExampleFs_Delete() {
f := (&Fs{}).New("/")
dir := f.TempDir("core-fs-example")
defer f.DeleteAll(dir)
path := Path(dir, "hello.txt")
f.Write(path, "hello")
Println(f.Delete(path).OK)
Println(f.Exists(path))
// Output:
// true
// false
}
// ExampleFs_DeleteAll deletes a tree through `Fs.DeleteAll` for sandboxed file operations.
// File reads, writes, walks, and cleanup stay sandbox-aware through Fs.
func ExampleFs_DeleteAll() {
f := (&Fs{}).New("/")
dir := f.TempDir("core-fs-example")
f.Write(Path(dir, "nested", "hello.txt"), "hello")
Println(f.DeleteAll(dir).OK)
Println(f.Exists(dir))
// Output:
// true
// false
}
// ExampleFs_Rename renames a path through `Fs.Rename` for sandboxed file operations. File
// reads, writes, walks, and cleanup stay sandbox-aware through Fs.
func ExampleFs_Rename() {
f := (&Fs{}).New("/")
dir := f.TempDir("core-fs-example")
defer f.DeleteAll(dir)
oldPath := Path(dir, "old.txt")
newPath := Path(dir, "new.txt")
f.Write(oldPath, "hello")
Println(f.Rename(oldPath, newPath).OK)
Println(f.Read(newPath).Value)
// Output:
// true
// hello
}
// ExampleFs_NewUnrestricted creates an unrestricted filesystem through
// `Fs.NewUnrestricted` for sandboxed file operations. File reads, writes, walks, and
// cleanup stay sandbox-aware through Fs.
func ExampleFs_NewUnrestricted() {
f := (&Fs{}).New("/")
dir := f.TempDir("example")
defer f.DeleteAll(dir)
// Write outside sandbox using Core's Fs
outside := Path(dir, "outside.txt")
f.Write(outside, "hello")
sandbox := (&Fs{}).New(Path(dir, "sandbox"))
unrestricted := sandbox.NewUnrestricted()
r := unrestricted.Read(outside)
Println(r.Value)
// Output: hello
}
// ExampleFs_Root reports the root value through `Fs.Root` for sandboxed file operations.
// File reads, writes, walks, and cleanup stay sandbox-aware through Fs.
func ExampleFs_Root() {
f := (&Fs{}).New("/srv/workspaces")
Println(f.Root())
// Output: /srv/workspaces
}
// ExampleFsEntry reads a walked filesystem entry through `FsEntry` for sandboxed file
// operations. File reads, writes, walks, and cleanup stay sandbox-aware through Fs.
func ExampleFsEntry() {
entry := FsEntry{Path: "src/main.go", Name: "main.go", IsDir: false}
Println(entry.Path)
Println(entry.Name)
Println(entry.IsDir)
// Output:
// src/main.go
// main.go
// false
}
// ExampleFs_WalkSeq walks a tree through `Fs.WalkSeq` for sandboxed file operations. File
// reads, writes, walks, and cleanup stay sandbox-aware through Fs.
func ExampleFs_WalkSeq() {
f := (&Fs{}).New("/")
dir := f.TempDir("core-fs-example")
defer f.DeleteAll(dir)
f.Write(Path(dir, "main.go"), "package main")
for entry, err := range f.WalkSeq(dir) {
if err == nil && !entry.IsDir {
Println(entry.Name)
}
}
// Output: main.go
}
// ExampleFs_WalkSeqSkip walks a tree through `Fs.WalkSeqSkip` while skipping a branch for
// sandboxed file operations. File reads, writes, walks, and cleanup stay sandbox-aware
// through Fs.
func ExampleFs_WalkSeqSkip() {
f := (&Fs{}).New("/")
dir := f.TempDir("core-fs-example")
defer f.DeleteAll(dir)
f.Write(Path(dir, "src", "main.go"), "package main")
f.Write(Path(dir, "vendor", "dep.go"), "package dep")
var names []string
for entry, err := range f.WalkSeqSkip(dir, "vendor") {
if err == nil && !entry.IsDir {
names = append(names, entry.Name)
}
}
Println(names)
// Output: [main.go]
}