-
-
Notifications
You must be signed in to change notification settings - Fork 124
Expand file tree
/
Copy pathfs.test.gr
More file actions
292 lines (260 loc) 路 9.01 KB
/
Copy pathfs.test.gr
File metadata and controls
292 lines (260 loc) 路 9.01 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
module FsTest
from "fs" include Fs
from "path" include Path
from "bytes" include Bytes
from "result" include Result
from "list" include List
let path = Path.fromString
let testPath = str => path("test/test-data/" ++ str)
// readFile
assert Fs.Binary.readFile(testPath("foo.txt")) == Ok(b"foo, bar, & baz")
assert Fs.Utf8.readFile(testPath("foo.txt")) == Ok("foo, bar, & baz")
assert Fs.Binary.readFile(path("/test/test-data/foo.txt"))
== Ok(b"foo, bar, & baz")
assert Fs.Binary.readFile(path("./test/test-data/foo.txt"))
== Ok(b"foo, bar, & baz")
assert Fs.Binary.readFile(path("/test/test-data/../test-data/foo.txt"))
== Ok(b"foo, bar, & baz")
assert Fs.Binary.readFile(
baseDirPath=Some(testPath("test-dir")),
path("file1.txt")
)
== Ok(b"File 1")
assert Fs.Binary.readFile(path("blahblah")) == Err(Fs.NoSuchFileOrDirectory)
// writeFile
assert Fs.Binary.readFile(testPath("baz.txt")) == Err(Fs.NoSuchFileOrDirectory)
assert Fs.Binary.writeFile(testPath("baz.txt"), b"some stuff") == Ok(void)
assert Fs.Binary.readFile(testPath("baz.txt")) == Ok(b"some stuff")
assert Fs.Binary.writeFile(sync=false, testPath("baz.txt"), b"some other stuff")
== Ok(void)
assert Fs.Binary.readFile(sync=false, testPath("baz.txt"))
== Ok(b"some other stuff")
assert Fs.Utf8.writeFile(testPath("baz.txt"), "some other stuff 馃尵") == Ok(void)
assert Fs.Utf8.readFile(testPath("baz.txt")) == Ok("some other stuff 馃尵")
assert Fs.Binary.writeFile(
testPath("baz.txt"),
b"\nmore stuff",
writeMode=Fs.Append
)
== Ok(void)
assert Fs.Utf8.readFile(testPath("baz.txt"))
== Ok("some other stuff 馃尵\nmore stuff")
assert Fs.Utf8.writeFile(
testPath("baz.txt"),
"\neven more stuff 馃槼",
writeMode=Fs.Append,
sync=false
)
== Ok(void)
assert Fs.Utf8.readFile(testPath("baz.txt"))
== Ok("some other stuff 馃尵\nmore stuff\neven more stuff 馃槼")
assert Fs.Utf8.readFile(testPath("foobar.txt")) == Err(Fs.NoSuchFileOrDirectory)
assert Fs.Utf8.writeFile(
testPath("foobar.txt"),
"new content",
writeMode=Fs.Append
)
== Ok(void)
assert Fs.Utf8.readFile(testPath("foobar.txt")) == Ok("new content")
assert Fs.Binary.writeFile(
baseDirPath=Some(testPath("test-dir")),
path("in-directory.txt"),
b"some stuff in a directory"
)
== Ok(void)
assert Fs.Binary.readFile(testPath("test-dir/in-directory.txt"))
== Ok(b"some stuff in a directory")
// stats
let stats = Result.unwrap(Fs.stats(testPath("foo.txt")))
assert stats.fileType == Fs.File
assert stats.size == 15
assert Result.unwrap(Fs.stats(testPath("test-dir"))).fileType == Fs.Directory
assert Result.unwrap(
Fs.stats(baseDirPath=Some(testPath("test-dir")), path("link"))
).fileType
== Fs.File
assert Result.unwrap(
Fs.stats(
baseDirPath=Some(testPath("test-dir")),
path("link"),
followSymlink=false
)
).fileType
== Fs.SymbolicLink
assert Fs.stats(testPath("blahblah")) == Err(Fs.NoSuchFileOrDirectory)
// exists
assert Fs.exists(testPath("foo.txt"))
assert !Fs.exists(testPath("blahblah"))
assert !Fs.exists(path(".."))
// readDir
use Fs.{ type DirectoryEntry }
assert Result.map(
x => List.sort(compare=(x, y) => compare(x.name, y.name), x),
Fs.readDir(testPath("test-dir"))
)
== Ok(
[
{ name: "dir", fileType: Fs.Directory },
{ name: "file1.txt", fileType: Fs.File },
{ name: "file2.txt", fileType: Fs.File },
{ name: "file3.txt", fileType: Fs.File },
{ name: "in-directory.txt", fileType: Fs.File },
{ name: "link", fileType: Fs.SymbolicLink },
],
)
assert Fs.readDir(baseDirPath=Some(testPath("test-dir")), path("dir"))
== Ok([{ name: "nested.txt", fileType: Fs.File }])
assert Fs.readDir(path("test/nonexisting")) == Err(Fs.NoSuchFileOrDirectory)
// createDir
assert !Fs.exists(testPath("dir2"))
assert Fs.createDir(testPath("dir2")) == Ok(void)
assert Fs.readDir(testPath("dir2")) == Ok([])
assert Fs.createDir(baseDirPath=Some(testPath("dir2")), path("inner"))
== Ok(void)
assert Fs.readDir(testPath("dir2"))
== Ok([{ name: "inner", fileType: Fs.Directory }])
assert Fs.createDir(testPath("dir2")) == Err(Fs.FileExists)
// readLink
assert Fs.readLink(testPath("test-dir/link")) == Ok(path("file1.txt"))
assert Fs.readLink(baseDirPath=Some(testPath("test-dir")), path("link"))
== Ok(path("file1.txt"))
assert Fs.readLink(testPath("blahblah")) == Err(Fs.NoSuchFileOrDirectory)
assert Fs.readLink(testPath("foo.txt")) == Err(Fs.InvalidArgument)
// createSymlink
assert !Fs.exists(testPath("symlink"))
assert Fs.createSymlink(path("foo.txt"), testPath("symlink")) == Ok(void)
assert Fs.readLink(testPath("symlink")) == Ok(path("foo.txt"))
assert Result.unwrap(Fs.stats(testPath("symlink"), followSymlink=false)).fileType
== Fs.SymbolicLink
assert Result.unwrap(Fs.stats(testPath("symlink"), followSymlink=true)).fileType
== Fs.File
assert Fs.createSymlink(path("bar.txt"), testPath("symlink"))
== Err(Fs.FileExists)
assert Fs.createSymlink(
path("bar.txt"),
targetBaseDirPath=Some(testPath("test-dir")),
path("symlink")
)
== Ok(void)
assert Fs.readLink(testPath("test-dir/symlink")) == Ok(path("bar.txt"))
// copy
assert Fs.copy(testPath("foo.txt"), testPath("foocopy.txt")) == Ok(void)
assert Fs.Utf8.readFile(testPath("foocopy.txt")) == Ok("foo, bar, & baz")
assert Fs.copy(
testPath("test-dir"),
testPath("copied-dir"),
copyMode=Fs.CopyRecursive
)
== Ok(void)
assert Fs.readDir(testPath("copied-dir")) == Fs.readDir(testPath("test-dir"))
assert Fs.Utf8.writeFile(testPath("test-dir/newfile.txt"), "New contents")
== Ok(void)
assert Fs.copy(
sourceBaseDirPath=Some(testPath("test-dir")),
path("file1.txt"),
targetBaseDirPath=Some(testPath("test-dir")),
path("newfile.txt")
)
== Ok(void)
assert Fs.Utf8.readFile(testPath("test-dir/newfile.txt")) == Ok("File 1")
assert Fs.copy(
testPath("test-dir"),
testPath("copied-dir"),
copyMode=Fs.CopyRecursive
)
== Err(Fs.FileExists)
assert Fs.copy(
testPath("test-dir/link"),
testPath("linkcopy"),
followSymlink=false
)
== Ok(void)
assert Fs.readLink(testPath("linkcopy")) == Ok(path("file1.txt"))
assert Fs.copy(
testPath("test-dir/link"),
testPath("contentscopy.txt"),
followSymlink=true
)
== Ok(void)
assert Result.unwrap(
Fs.stats(testPath("contentscopy.txt"), followSymlink=false)
).fileType
== Fs.File
assert Fs.createSymlink(path("test-dir"), testPath("linktodir")) == Ok(void)
assert Fs.copy(
testPath("linktodir"),
testPath("copied-link-to-dir"),
copyMode=Fs.CopyRecursive,
followSymlink=true
)
== Ok(void)
assert Result.unwrap(
Fs.stats(testPath("copied-link-to-dir"), followSymlink=false)
).fileType
== Fs.Directory
assert Fs.copy(
testPath("linktodir"),
testPath("copied-link"),
copyMode=Fs.CopyRecursive,
followSymlink=false
)
== Ok(void)
assert Result.unwrap(Fs.stats(testPath("copied-link"), followSymlink=false)).fileType
== Fs.SymbolicLink
// rename
assert Fs.rename(testPath("foobar.txt"), testPath("boofar.txt")) == Ok(void)
assert !Fs.exists(testPath("foobar.txt"))
assert Fs.Utf8.readFile(testPath("boofar.txt")) == Ok("new content")
assert Fs.rename(testPath("dir2"), testPath("newdir")) == Ok(void)
assert !Fs.exists(testPath("dir2"))
assert Fs.readDir(testPath("newdir"))
== Ok([{ name: "inner", fileType: Fs.Directory }])
assert Fs.rename(
sourceBaseDirPath=Some(testPath("test-dir")),
path("in-directory.txt"),
targetBaseDirPath=Some(testPath("test-dir")),
path("renamed.txt")
)
== Ok(void)
assert !Fs.exists(testPath("test-dir/in-directory.txt"))
assert Fs.Utf8.readFile(testPath("test-dir/renamed.txt"))
== Ok("some stuff in a directory")
assert Fs.rename(
sourceBaseDirPath=Some(testPath("test-dir")),
path("renamed.txt"),
testPath("boofar.txt")
)
== Ok(void)
assert Fs.Utf8.readFile(testPath("boofar.txt"))
== Ok("some stuff in a directory")
// remove
assert Fs.remove(testPath("baz.txt")) == Ok(void)
assert Fs.remove(testPath("newdir")) == Err(Fs.OperationNotPermitted)
assert Fs.createDir(testPath("newdir/innerdir")) == Ok(void)
assert Fs.remove(testPath("newdir"), removeMode=Fs.RemoveEmptyDirectory)
== Err(Fs.DirectoryNotEmpty)
assert Fs.remove(
baseDirPath=Some(testPath("newdir")),
path("innerdir"),
removeMode=Fs.RemoveEmptyDirectory
)
== Ok(void)
assert Fs.createDir(testPath("newdir/innerdir")) == Ok(void)
assert Fs.Utf8.writeFile(testPath("newdir/innerdir/file.txt"), "content")
== Ok(void)
assert Fs.remove(testPath("newdir"), removeMode=Fs.RemoveRecursive) == Ok(void)
assert !Fs.exists(testPath("newdir"))
// clean up created files through tests
assert Fs.remove(testPath("boofar.txt")) == Ok(void)
assert Fs.remove(testPath("symlink")) == Ok(void)
assert Fs.remove(testPath("linkcopy")) == Ok(void)
assert Fs.remove(testPath("foocopy.txt")) == Ok(void)
assert Fs.remove(testPath("contentscopy.txt")) == Ok(void)
assert Fs.remove(testPath("copied-link")) == Ok(void)
assert Fs.remove(testPath("linktodir")) == Ok(void)
assert Fs.remove(testPath("test-dir/symlink")) == Ok(void)
assert Fs.remove(testPath("test-dir/newfile.txt")) == Ok(void)
assert Fs.remove(testPath("copied-dir"), removeMode=Fs.RemoveRecursive)
== Ok(void)
assert Fs.remove(testPath("copied-link-to-dir"), removeMode=Fs.RemoveRecursive)
== Ok(void)