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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
|
import { beforeEach, describe, expect, it } from "bun:test";
import { gc, gcTick } from "./gc";
import {
closeSync,
existsSync,
mkdirSync,
openSync,
readdirSync,
readFile,
readFileSync,
readSync,
writeFileSync,
writeSync,
statSync,
lstatSync,
copyFileSync,
rmSync,
rmdir,
rmdirSync,
createReadStream,
createWriteStream,
promises,
unlinkSync,
mkdtempSync,
} from "node:fs";
import { join } from "node:path";
const Buffer = globalThis.Buffer || Uint8Array;
if (!import.meta.dir) {
import.meta.dir = ".";
}
describe("copyFileSync", () => {
it("should work for files < 128 KB", () => {
const tempdir = `/tmp/fs.test.js/${Date.now()}/1234/hi`;
expect(existsSync(tempdir)).toBe(false);
expect(tempdir.includes(mkdirSync(tempdir, { recursive: true }))).toBe(
true,
);
// that don't exist
copyFileSync(import.meta.path, tempdir + "/copyFileSync.js");
expect(existsSync(tempdir + "/copyFileSync.js")).toBe(true);
expect(readFileSync(tempdir + "/copyFileSync.js", "utf-8")).toBe(
readFileSync(import.meta.path, "utf-8"),
);
// that do exist
copyFileSync(tempdir + "/copyFileSync.js", tempdir + "/copyFileSync.js1");
writeFileSync(tempdir + "/copyFileSync.js1", "hello");
copyFileSync(tempdir + "/copyFileSync.js1", tempdir + "/copyFileSync.js");
expect(readFileSync(tempdir + "/copyFileSync.js", "utf-8")).toBe("hello");
});
it("should work for files > 128 KB ", () => {
const tempdir = `/tmp/fs.test.js/${Date.now()}-1/1234/hi`;
expect(existsSync(tempdir)).toBe(false);
expect(tempdir.includes(mkdirSync(tempdir, { recursive: true }))).toBe(
true,
);
var buffer = new Int32Array(128 * 1024);
for (let i = 0; i < buffer.length; i++) {
buffer[i] = i % 256;
}
const hash = Bun.hash(buffer.buffer);
writeFileSync(tempdir + "/copyFileSync.src.blob", buffer.buffer);
expect(existsSync(tempdir + "/copyFileSync.dest.blob")).toBe(false);
expect(existsSync(tempdir + "/copyFileSync.src.blob")).toBe(true);
copyFileSync(
tempdir + "/copyFileSync.src.blob",
tempdir + "/copyFileSync.dest.blob",
);
expect(Bun.hash(readFileSync(tempdir + "/copyFileSync.dest.blob"))).toBe(
hash,
);
buffer[0] = 255;
writeFileSync(tempdir + "/copyFileSync.src.blob", buffer.buffer);
copyFileSync(
tempdir + "/copyFileSync.src.blob",
tempdir + "/copyFileSync.dest.blob",
);
expect(Bun.hash(readFileSync(tempdir + "/copyFileSync.dest.blob"))).toBe(
Bun.hash(buffer.buffer),
);
});
});
describe("mkdirSync", () => {
it("should create a directory", () => {
const tempdir = `/tmp/fs.test.js/${Date.now()}/1234/hi`;
expect(existsSync(tempdir)).toBe(false);
expect(tempdir.includes(mkdirSync(tempdir, { recursive: true }))).toBe(
true,
);
expect(existsSync(tempdir)).toBe(true);
});
});
it("readdirSync on import.meta.dir", () => {
const dirs = readdirSync(import.meta.dir);
expect(dirs.length > 0).toBe(true);
var match = false;
gc(true);
for (let i = 0; i < dirs.length; i++) {
if (dirs[i] === import.meta.file) {
match = true;
}
}
gc(true);
expect(match).toBe(true);
});
// https://github.com/oven-sh/bun/issues/1887
it("mkdtempSync, readdirSync, rmdirSync and unlinkSync with non-ascii", () => {
const tempdir = mkdtempSync(`/tmp/emoji-fruit-🍇 🍈 🍉 🍊 🍋`);
expect(existsSync(tempdir)).toBe(true);
writeFileSync(tempdir + "/non-ascii-👍.txt", "hello");
const dirs = readdirSync(tempdir);
expect(dirs.length > 0).toBe(true);
var match = false;
gc(true);
for (let i = 0; i < dirs.length; i++) {
if (dirs[i].endsWith("non-ascii-👍.txt")) {
match = true;
break;
}
}
gc(true);
expect(match).toBe(true);
unlinkSync(tempdir + "/non-ascii-👍.txt");
expect(existsSync(tempdir + "/non-ascii-👍.txt")).toBe(false);
rmdirSync(tempdir);
expect(existsSync(tempdir)).toBe(false);
});
it("mkdtempSync() empty name", () => {
const tempdir = mkdtempSync();
expect(existsSync(tempdir)).toBe(true);
writeFileSync(tempdir + "/non-ascii-👍.txt", "hello");
const dirs = readdirSync(tempdir);
expect(dirs.length > 0).toBe(true);
var match = false;
gc(true);
for (let i = 0; i < dirs.length; i++) {
if (dirs[i].endsWith("non-ascii-👍.txt")) {
match = true;
break;
}
}
gc(true);
expect(match).toBe(true);
unlinkSync(tempdir + "/non-ascii-👍.txt");
expect(existsSync(tempdir + "/non-ascii-👍.txt")).toBe(false);
rmdirSync(tempdir);
expect(existsSync(tempdir)).toBe(false);
});
it("readdirSync on import.meta.dir with trailing slash", () => {
const dirs = readdirSync(import.meta.dir + "/");
expect(dirs.length > 0).toBe(true);
// this file should exist in it
var match = false;
for (let i = 0; i < dirs.length; i++) {
if (dirs[i] === import.meta.file) {
match = true;
}
}
expect(match).toBe(true);
});
it("readdirSync works on empty directories", () => {
const path = `/tmp/fs-test-empty-dir-${(
Math.random() * 100000 +
100
).toString(32)}`;
mkdirSync(path, { recursive: true });
expect(readdirSync(path).length).toBe(0);
});
it("readdirSync works on directories with under 32 files", () => {
const path = `/tmp/fs-test-one-dir-${(Math.random() * 100000 + 100).toString(
32,
)}`;
mkdirSync(path, { recursive: true });
writeFileSync(`${path}/a`, "a");
const results = readdirSync(path);
expect(results.length).toBe(1);
expect(results[0]).toBe("a");
});
it("readdirSync throws when given a file path", () => {
try {
readdirSync(import.meta.path);
throw new Error("should not get here");
} catch (exception) {
expect(exception.name).toBe("ENOTDIR");
}
});
it("readdirSync throws when given a path that doesn't exist", () => {
try {
readdirSync(import.meta.path + "/does-not-exist/really");
throw new Error("should not get here");
} catch (exception) {
expect(exception.name).toBe("ENOTDIR");
}
});
it("readdirSync throws when given a file path with trailing slash", () => {
try {
readdirSync(import.meta.path + "/");
throw new Error("should not get here");
} catch (exception) {
expect(exception.name).toBe("ENOTDIR");
}
});
describe("readSync", () => {
const firstFourBytes = new Uint32Array(
new TextEncoder().encode("File").buffer,
)[0];
it("works with a position set to 0", () => {
const fd = openSync(import.meta.dir + "/readFileSync.txt", "r");
const four = new Uint8Array(4);
{
const count = readSync(fd, four, 0, 4, 0);
const u32 = new Uint32Array(four.buffer)[0];
expect(u32).toBe(firstFourBytes);
expect(count).toBe(4);
}
closeSync(fd);
});
it("works without position set", () => {
const fd = openSync(import.meta.dir + "/readFileSync.txt", "r");
const four = new Uint8Array(4);
{
const count = readSync(fd, four);
const u32 = new Uint32Array(four.buffer)[0];
expect(u32).toBe(firstFourBytes);
expect(count).toBe(4);
}
closeSync(fd);
});
});
describe("writeSync", () => {
it("works with a position set to 0", () => {
const fd = openSync(import.meta.dir + "/writeFileSync.txt", "w+");
const four = new Uint8Array(4);
{
const count = writeSync(fd, new TextEncoder().encode("File"), 0, 4, 0);
expect(count).toBe(4);
}
closeSync(fd);
});
it("works without position set", () => {
const fd = openSync(import.meta.dir + "/writeFileSync.txt", "w+");
const four = new Uint8Array(4);
{
const count = writeSync(fd, new TextEncoder().encode("File"));
expect(count).toBe(4);
}
closeSync(fd);
});
});
describe("readFileSync", () => {
it("works", () => {
gc();
const text = readFileSync(import.meta.dir + "/readFileSync.txt", "utf8");
gc();
expect(text).toBe("File read successfully");
gc();
});
it("works with a file url", () => {
gc();
const text = readFileSync(
new URL("file://" + import.meta.dir + "/readFileSync.txt"),
"utf8",
);
gc();
expect(text).toBe("File read successfully");
});
it("works with special files in the filesystem", () => {
{
const text = readFileSync("/dev/null", "utf8");
gc();
expect(text).toBe("");
}
if (process.platform === "linux") {
const text = readFileSync("/proc/filesystems");
gc();
expect(text.length > 0).toBe(true);
}
});
it("returning Buffer works", () => {
const text = readFileSync(import.meta.dir + "/readFileSync.txt");
const encoded = [
70, 105, 108, 101, 32, 114, 101, 97, 100, 32, 115, 117, 99, 99, 101, 115,
115, 102, 117, 108, 108, 121,
];
for (let i = 0; i < encoded.length; i++) {
expect(text[i]).toBe(encoded[i]);
}
});
});
describe("readFile", () => {
it("works", async () => {
gc();
await new Promise((resolve, reject) => {
readFile(import.meta.dir + "/readFileSync.txt", "utf8", (err, text) => {
gc();
expect(text).toBe("File read successfully");
resolve(true);
});
});
});
it("returning Buffer works", async () => {
gc();
await new Promise((resolve, reject) => {
gc();
readFile(import.meta.dir + "/readFileSync.txt", (err, text) => {
const encoded = [
70, 105, 108, 101, 32, 114, 101, 97, 100, 32, 115, 117, 99, 99, 101,
115, 115, 102, 117, 108, 108, 121,
];
gc();
for (let i = 0; i < encoded.length; i++) {
expect(text[i]).toBe(encoded[i]);
}
resolve(true);
});
});
});
});
describe("writeFileSync", () => {
it("works", () => {
const path = `/tmp/${Date.now()}.writeFileSync.txt`;
writeFileSync(path, "File written successfully", "utf8");
expect(readFileSync(path, "utf8")).toBe("File written successfully");
});
it("returning Buffer works", () => {
const buffer = new Buffer([
70, 105, 108, 101, 32, 119, 114, 105, 116, 116, 101, 110, 32, 115, 117,
99, 99, 101, 115, 115, 102, 117, 108, 108, 121,
]);
const path = `/tmp/${Date.now()}.blob.writeFileSync.txt`;
writeFileSync(path, buffer);
const out = readFileSync(path);
for (let i = 0; i < buffer.length; i++) {
expect(buffer[i]).toBe(out[i]);
}
});
it("returning ArrayBuffer works", () => {
const buffer = new Buffer([
70, 105, 108, 101, 32, 119, 114, 105, 116, 116, 101, 110, 32, 115, 117,
99, 99, 101, 115, 115, 102, 117, 108, 108, 121,
]);
const path = `/tmp/${Date.now()}.blob2.writeFileSync.txt`;
writeFileSync(path, buffer);
const out = readFileSync(path);
for (let i = 0; i < buffer.length; i++) {
expect(buffer[i]).toBe(out[i]);
}
});
});
describe("lstat", () => {
it("file metadata is correct", () => {
const fileStats = lstatSync(
new URL("./fs-stream.js", import.meta.url)
.toString()
.slice("file://".length - 1),
);
expect(fileStats.isSymbolicLink()).toBe(false);
expect(fileStats.isFile()).toBe(true);
expect(fileStats.isDirectory()).toBe(false);
});
it("folder metadata is correct", () => {
const fileStats = lstatSync(
new URL("../../test", import.meta.url)
.toString()
.slice("file://".length - 1),
);
expect(fileStats.isSymbolicLink()).toBe(false);
expect(fileStats.isFile()).toBe(false);
expect(fileStats.isDirectory()).toBe(true);
});
it("symlink metadata is correct", () => {
const linkStats = lstatSync(
new URL("./fs-stream.link.js", import.meta.url)
.toString()
.slice("file://".length - 1),
);
expect(linkStats.isSymbolicLink()).toBe(true);
expect(linkStats.isFile()).toBe(false);
expect(linkStats.isDirectory()).toBe(false);
});
});
describe("stat", () => {
it("file metadata is correct", () => {
const fileStats = statSync(
new URL("./fs-stream.js", import.meta.url)
.toString()
.slice("file://".length - 1),
);
expect(fileStats.isSymbolicLink()).toBe(false);
expect(fileStats.isFile()).toBe(true);
expect(fileStats.isDirectory()).toBe(false);
});
it("folder metadata is correct", () => {
const fileStats = statSync(
new URL("../../test", import.meta.url)
.toString()
.slice("file://".length - 1),
);
expect(fileStats.isSymbolicLink()).toBe(false);
expect(fileStats.isFile()).toBe(false);
expect(fileStats.isDirectory()).toBe(true);
expect(typeof fileStats.dev).toBe("number");
expect(typeof fileStats.ino).toBe("number");
expect(typeof fileStats.mode).toBe("number");
expect(typeof fileStats.nlink).toBe("number");
expect(typeof fileStats.uid).toBe("number");
expect(typeof fileStats.gid).toBe("number");
expect(typeof fileStats.rdev).toBe("number");
expect(typeof fileStats.size).toBe("number");
expect(typeof fileStats.blksize).toBe("number");
expect(typeof fileStats.blocks).toBe("number");
expect(typeof fileStats.atimeMs).toBe("number");
expect(typeof fileStats.mtimeMs).toBe("number");
expect(typeof fileStats.ctimeMs).toBe("number");
expect(typeof fileStats.birthtimeMs).toBe("number");
expect(typeof fileStats.atime).toBe("object");
expect(typeof fileStats.mtime).toBe("object");
expect(typeof fileStats.ctime).toBe("object");
expect(typeof fileStats.birthtime).toBe("object");
});
it("stat returns ENOENT", () => {
try {
statSync("/tmp/doesntexist");
throw "statSync should throw";
} catch (e) {
expect(e.code).toBe("ENOENT");
}
});
});
describe("rm", () => {
it("removes a file", () => {
const path = `/tmp/${Date.now()}.rm.txt`;
writeFileSync(path, "File written successfully", "utf8");
expect(existsSync(path)).toBe(true);
rmSync(path);
expect(existsSync(path)).toBe(false);
});
it("removes a dir", () => {
const path = `/tmp/${Date.now()}.rm.dir`;
try {
mkdirSync(path);
} catch (e) {}
expect(existsSync(path)).toBe(true);
rmSync(path);
expect(existsSync(path)).toBe(false);
});
it("removes a dir recursively", () => {
const path = `/tmp/${Date.now()}.rm.dir/foo/bar`;
try {
mkdirSync(path, { recursive: true });
} catch (e) {}
expect(existsSync(path)).toBe(true);
rmSync(join(path, "../../"), { recursive: true });
expect(existsSync(path)).toBe(false);
});
});
describe("rmdir", () => {
it("removes a file", (done) => {
const path = `/tmp/${Date.now()}.rm.txt`;
writeFileSync(path, "File written successfully", "utf8");
expect(existsSync(path)).toBe(true);
rmdir(path, (err) => {
try {
expect(err).toBeDefined();
expect(err.code).toBe("EPERM");
expect(err.message).toBe("Operation not permitted");
expect(existsSync(path)).toBe(true);
} catch (e) {
return done(e);
} finally {
done();
}
});
});
it("removes a dir", (done) => {
const path = `/tmp/${Date.now()}.rm.dir`;
try {
mkdirSync(path);
} catch (e) {}
expect(existsSync(path)).toBe(true);
rmdir(path, (err) => {
if (err) return done(err);
expect(existsSync(path)).toBe(false);
done();
});
});
// TODO support `recursive: true`
it("removes a dir recursively", (done) => {
const path = `/tmp/${Date.now()}.rm.dir/foo/bar`;
try {
mkdirSync(path, { recursive: true });
} catch (e) {}
expect(existsSync(path)).toBe(true);
rmdir(join(path, "../../"), { recursive: true }, (err) => {
try {
expect(existsSync(path)).toBe(false);
done(err);
} catch (e) {
return done(e);
} finally {
done();
}
});
});
});
describe("rmdirSync", () => {
it("removes a file", () => {
const path = `/tmp/${Date.now()}.rm.txt`;
writeFileSync(path, "File written successfully", "utf8");
expect(existsSync(path)).toBe(true);
expect(() => {
rmdirSync(path);
}).toThrow("Operation not permitted");
expect(existsSync(path)).toBe(true);
});
it("removes a dir", () => {
const path = `/tmp/${Date.now()}.rm.dir`;
try {
mkdirSync(path);
} catch (e) {}
expect(existsSync(path)).toBe(true);
rmdirSync(path);
expect(existsSync(path)).toBe(false);
});
// TODO support `recursive: true`
it("removes a dir recursively", () => {
const path = `/tmp/${Date.now()}.rm.dir/foo/bar`;
try {
mkdirSync(path, { recursive: true });
} catch (e) {}
expect(existsSync(path)).toBe(true);
rmdirSync(join(path, "../../"), { recursive: true });
expect(existsSync(path)).toBe(false);
});
});
describe("createReadStream", () => {
it("works (1 chunk)", async () => {
return await new Promise((resolve, reject) => {
var stream = createReadStream(import.meta.dir + "/readFileSync.txt", {});
stream.on("error", (e) => {
reject(e);
});
stream.on("data", (chunk) => {
expect(chunk instanceof Buffer).toBe(true);
expect(chunk.length).toBe("File read successfully".length);
expect(chunk.toString()).toBe("File read successfully");
});
stream.on("close", () => {
resolve(true);
});
});
});
it("works (22 chunk)", async () => {
var stream = createReadStream(import.meta.dir + "/readFileSync.txt", {
highWaterMark: 1,
});
var data = readFileSync(import.meta.dir + "/readFileSync.txt", "utf8");
var i = 0;
return await new Promise((resolve) => {
stream.on("data", (chunk) => {
expect(chunk instanceof Buffer).toBe(true);
expect(chunk.length).toBe(1);
expect(chunk.toString()).toBe(data[i++]);
});
stream.on("end", () => {
resolve(true);
});
});
});
});
describe("createWriteStream", () => {
it("simple write stream finishes", async () => {
const path = `/tmp/fs.test.js/${Date.now()}.createWriteStream.txt`;
const stream = createWriteStream(path);
stream.write("Test file written successfully");
stream.end();
return await new Promise((resolve, reject) => {
stream.on("error", (e) => {
reject(e);
});
stream.on("finish", () => {
expect(readFileSync(path, "utf8")).toBe(
"Test file written successfully",
);
resolve(true);
});
});
});
it("writing null throws ERR_STREAM_NULL_VALUES", async () => {
const path = `/tmp/fs.test.js/${Date.now()}.createWriteStreamNulls.txt`;
const stream = createWriteStream(path);
try {
stream.write(null);
expect(() => {}).toThrow(Error);
} catch (exception) {
expect(exception.code).toBe("ERR_STREAM_NULL_VALUES");
}
});
it("writing null throws ERR_STREAM_NULL_VALUES (objectMode: true)", async () => {
const path = `/tmp/fs.test.js/${Date.now()}.createWriteStreamNulls.txt`;
const stream = createWriteStream(path, {
objectMode: true,
});
try {
stream.write(null);
expect(() => {}).toThrow(Error);
} catch (exception) {
expect(exception.code).toBe("ERR_STREAM_NULL_VALUES");
}
});
it("writing false throws ERR_INVALID_ARG_TYPE", async () => {
const path = `/tmp/fs.test.js/${Date.now()}.createWriteStreamFalse.txt`;
const stream = createWriteStream(path);
try {
stream.write(false);
expect(() => {}).toThrow(Error);
} catch (exception) {
expect(exception.code).toBe("ERR_INVALID_ARG_TYPE");
}
});
it("writing false throws ERR_INVALID_ARG_TYPE (objectMode: true)", async () => {
const path = `/tmp/fs.test.js/${Date.now()}.createWriteStreamFalse.txt`;
const stream = createWriteStream(path, {
objectMode: true,
});
try {
stream.write(false);
expect(() => {}).toThrow(Error);
} catch (exception) {
expect(exception.code).toBe("ERR_INVALID_ARG_TYPE");
}
});
});
describe("fs/promises", () => {
const { exists, mkdir, readFile, rmdir, stat, writeFile } = promises;
it("should not segfault on exception", async () => {
try {
await stat("foo/bar");
} catch (e) {}
});
it("readFile", async () => {
const data = await readFile(import.meta.dir + "/readFileSync.txt", "utf8");
expect(data).toBe("File read successfully");
});
it("writeFile", async () => {
const path = `/tmp/fs.test.js/${Date.now()}.writeFile.txt`;
await writeFile(path, "File written successfully");
expect(readFileSync(path, "utf8")).toBe("File written successfully");
});
it("readdir()", async () => {
const files = await promises.readdir(import.meta.dir);
expect(files.length).toBeGreaterThan(0);
});
it("readdir() no args doesnt segfault", async () => {
const fizz = [
[],
[Symbol("ok")],
[Symbol("ok"), Symbol("ok")],
[Symbol("ok"), Symbol("ok"), Symbol("ok")],
[Infinity, -NaN, -Infinity],
"\0\0\0\0",
"\r\n",
];
for (const args of fizz) {
try {
// check it doens't segfault when called with invalid arguments
await promises.readdir(...args);
} catch (e) {
// check that producing the error doesn't cause any crashes
Bun.inspect(e);
}
}
});
describe("rmdir", () => {
it("removes a file", async () => {
const path = `/tmp/${Date.now()}.rm.txt`;
await writeFile(path, "File written successfully", "utf8");
expect(await exists(path)).toBe(true);
try {
await rmdir(path);
expect(() => {}).toThrow();
} catch (err) {
expect(err.code).toBe("ENOTDIR");
// expect(err.message).toBe("Operation not permitted");
expect(await exists(path)).toBe(true);
}
});
it("removes a dir", async () => {
const path = `/tmp/${Date.now()}.rm.dir`;
try {
await mkdir(path);
} catch (e) {}
expect(await exists(path)).toBe(true);
await rmdir(path);
expect(await exists(path)).toBe(false);
});
// TODO support `recursive: true`
// it("removes a dir recursively", async () => {
// const path = `/tmp/${Date.now()}.rm.dir/foo/bar`;
// try {
// await mkdir(path, { recursive: true });
// } catch (e) {}
// expect(await exists(path)).toBe(true);
// await rmdir(join(path, "../../"), { recursive: true });
// expect(await exists(path)).toBe(false);
// });
});
});
|