@gjsify/fs 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (80) hide show
  1. package/README.md +6 -8
  2. package/lib/cjs/callback.js +131 -0
  3. package/lib/cjs/dirent.js +127 -0
  4. package/lib/cjs/encoding.js +53 -0
  5. package/lib/cjs/file-handle.js +473 -0
  6. package/lib/cjs/fs-watcher.js +79 -0
  7. package/lib/cjs/index.js +95 -0
  8. package/lib/cjs/promises.js +189 -0
  9. package/lib/cjs/read-stream.js +107 -0
  10. package/lib/cjs/stats.js +74 -0
  11. package/lib/cjs/sync.js +155 -0
  12. package/lib/cjs/types/encoding-option.js +15 -0
  13. package/lib/cjs/types/file-read-options.js +15 -0
  14. package/lib/cjs/types/file-read-result.js +15 -0
  15. package/lib/cjs/types/flag-and-open-mode.js +15 -0
  16. package/lib/cjs/types/index.js +22 -0
  17. package/lib/cjs/types/open-flags.js +15 -0
  18. package/lib/cjs/types/read-options.js +15 -0
  19. package/lib/cjs/utils.js +37 -0
  20. package/lib/cjs/write-stream.js +135 -0
  21. package/lib/esm/callback.js +112 -0
  22. package/lib/esm/dirent.js +98 -0
  23. package/lib/esm/encoding.js +34 -0
  24. package/lib/esm/file-handle.js +444 -0
  25. package/lib/esm/fs-watcher.js +50 -0
  26. package/lib/esm/index.js +95 -0
  27. package/lib/esm/promises.js +160 -0
  28. package/lib/esm/read-stream.js +78 -0
  29. package/lib/esm/stats.js +45 -0
  30. package/lib/esm/sync.js +126 -0
  31. package/lib/esm/types/encoding-option.js +0 -0
  32. package/lib/esm/types/file-read-options.js +0 -0
  33. package/lib/esm/types/file-read-result.js +0 -0
  34. package/lib/esm/types/flag-and-open-mode.js +0 -0
  35. package/lib/esm/types/index.js +6 -0
  36. package/lib/esm/types/open-flags.js +0 -0
  37. package/lib/esm/types/read-options.js +0 -0
  38. package/lib/esm/utils.js +18 -0
  39. package/lib/esm/write-stream.js +116 -0
  40. package/package.json +52 -17
  41. package/src/callback.spec.ts +42 -0
  42. package/src/callback.ts +362 -0
  43. package/src/dirent.ts +117 -0
  44. package/src/encoding.ts +40 -0
  45. package/src/file-handle.spec.ts +34 -0
  46. package/src/file-handle.ts +606 -0
  47. package/{fs-watcher.js → src/fs-watcher.ts} +10 -9
  48. package/src/index.ts +95 -0
  49. package/src/promises.spec.ts +172 -0
  50. package/src/promises.ts +349 -0
  51. package/src/read-stream.ts +98 -0
  52. package/src/stat.spec.ts +79 -0
  53. package/src/stats.ts +106 -0
  54. package/src/symlink.spec.ts +38 -0
  55. package/src/sync.spec.ts +205 -0
  56. package/src/sync.ts +239 -0
  57. package/src/test.mts +11 -0
  58. package/src/types/encoding-option.ts +3 -0
  59. package/src/types/file-read-options.ts +15 -0
  60. package/src/types/file-read-result.ts +4 -0
  61. package/src/types/flag-and-open-mode.ts +6 -0
  62. package/src/types/index.ts +6 -0
  63. package/src/types/open-flags.ts +14 -0
  64. package/src/types/read-options.ts +9 -0
  65. package/src/utils.ts +19 -0
  66. package/src/write-stream.ts +143 -0
  67. package/test/file.txt +1 -0
  68. package/test/watch.js +1 -0
  69. package/test.gjs.js +35359 -0
  70. package/test.gjs.js.map +7 -0
  71. package/test.gjs.mjs +40570 -0
  72. package/test.gjs.mjs.meta.json +1 -0
  73. package/test.node.js +1479 -0
  74. package/test.node.js.map +7 -0
  75. package/test.node.mjs +710 -0
  76. package/tsconfig.json +18 -0
  77. package/tsconfig.types.json +8 -0
  78. package/index.js +0 -114
  79. package/test/index.js +0 -90
  80. package/test/resources/file.txt +0 -1
package/test.node.mjs ADDED
@@ -0,0 +1,710 @@
1
+ // ../../../node_modules/@girs/gjs/gjs.js
2
+ var imports = globalThis.imports || {};
3
+
4
+ // ../../gjs/unit/lib/esm/index.js
5
+ import nodeAssert from "assert";
6
+ var mainloop = globalThis?.imports?.mainloop;
7
+ var countTestsOverall = 0;
8
+ var countTestsFailed = 0;
9
+ var countTestsIgnored = 0;
10
+ var runtime = "";
11
+ var RED = "\x1B[31m";
12
+ var GREEN = "\x1B[32m";
13
+ var BLUE = "\x1B[34m";
14
+ var GRAY = "\x1B[90m";
15
+ var RESET = "\x1B[39m";
16
+ var print = globalThis.print || console.log;
17
+ var MatcherFactory = class {
18
+ constructor(actualValue, positive, negated) {
19
+ this.actualValue = actualValue;
20
+ this.positive = positive;
21
+ if (negated) {
22
+ this.not = negated;
23
+ } else {
24
+ this.not = new MatcherFactory(actualValue, !positive, this);
25
+ }
26
+ }
27
+ not;
28
+ triggerResult(success, msg) {
29
+ if (success && !this.positive || !success && this.positive) {
30
+ ++countTestsFailed;
31
+ throw new Error(msg);
32
+ }
33
+ }
34
+ to(callback) {
35
+ this.triggerResult(
36
+ callback(this.actualValue),
37
+ ` Expected callback to validate`
38
+ );
39
+ }
40
+ toBe(expectedValue) {
41
+ this.triggerResult(
42
+ this.actualValue === expectedValue,
43
+ ` Expected values to match using ===
44
+ Expected: ${expectedValue} (${typeof expectedValue})
45
+ Actual: ${this.actualValue} (${typeof this.actualValue})`
46
+ );
47
+ }
48
+ toEqual(expectedValue) {
49
+ this.triggerResult(
50
+ this.actualValue == expectedValue,
51
+ ` Expected values to match using ==
52
+ Expected: ${expectedValue} (${typeof expectedValue})
53
+ Actual: ${this.actualValue} (${typeof this.actualValue})`
54
+ );
55
+ }
56
+ toEqualArray(expectedValue) {
57
+ let success = Array.isArray(this.actualValue) && Array.isArray(expectedValue) && this.actualValue.length === expectedValue.length;
58
+ for (let i = 0; i < this.actualValue.length; i++) {
59
+ const actualVal = this.actualValue[i];
60
+ const expectedVal = expectedValue[i];
61
+ success = actualVal == expectedVal;
62
+ if (!success)
63
+ break;
64
+ }
65
+ this.triggerResult(
66
+ success,
67
+ ` Expected array items to match using ==
68
+ Expected: ${expectedValue} (${typeof expectedValue})
69
+ Actual: ${this.actualValue} (${typeof this.actualValue})`
70
+ );
71
+ }
72
+ toMatch(expectedValue) {
73
+ if (typeof this.actualValue.match !== "function") {
74
+ throw new Error(`You can not use toMatch on type ${typeof this.actualValue}`);
75
+ }
76
+ this.triggerResult(
77
+ !!this.actualValue.match(expectedValue),
78
+ " Expected values to match using regular expression\n Expression: " + expectedValue + "\n Actual: " + this.actualValue
79
+ );
80
+ }
81
+ toBeDefined() {
82
+ this.triggerResult(
83
+ typeof this.actualValue !== "undefined",
84
+ ` Expected value to be defined`
85
+ );
86
+ }
87
+ toBeUndefined() {
88
+ this.triggerResult(
89
+ typeof this.actualValue === "undefined",
90
+ ` Expected value to be undefined`
91
+ );
92
+ }
93
+ toBeNull() {
94
+ this.triggerResult(
95
+ this.actualValue === null,
96
+ ` Expected value to be null`
97
+ );
98
+ }
99
+ toBeTruthy() {
100
+ this.triggerResult(
101
+ this.actualValue,
102
+ ` Expected value to be truthy`
103
+ );
104
+ }
105
+ toBeFalsy() {
106
+ this.triggerResult(
107
+ !this.actualValue,
108
+ ` Expected value to be falsy`
109
+ );
110
+ }
111
+ toContain(needle) {
112
+ this.triggerResult(
113
+ this.actualValue instanceof Array && this.actualValue.indexOf(needle) !== -1,
114
+ ` Expected ` + this.actualValue + ` to contain ` + needle
115
+ );
116
+ }
117
+ toBeLessThan(greaterValue) {
118
+ this.triggerResult(
119
+ this.actualValue < greaterValue,
120
+ ` Expected ` + this.actualValue + ` to be less than ` + greaterValue
121
+ );
122
+ }
123
+ toBeGreaterThan(smallerValue) {
124
+ this.triggerResult(
125
+ this.actualValue > smallerValue,
126
+ ` Expected ` + this.actualValue + ` to be greater than ` + smallerValue
127
+ );
128
+ }
129
+ toBeCloseTo(expectedValue, precision) {
130
+ const shiftHelper = Math.pow(10, precision);
131
+ this.triggerResult(
132
+ Math.round(this.actualValue * shiftHelper) / shiftHelper === Math.round(expectedValue * shiftHelper) / shiftHelper,
133
+ ` Expected ` + this.actualValue + ` with precision ` + precision + ` to be close to ` + expectedValue
134
+ );
135
+ }
136
+ toThrow(ErrorType) {
137
+ let errorMessage = "";
138
+ let didThrow = false;
139
+ let typeMatch = true;
140
+ try {
141
+ this.actualValue();
142
+ didThrow = false;
143
+ } catch (e) {
144
+ errorMessage = e.message || "";
145
+ didThrow = true;
146
+ if (ErrorType) {
147
+ typeMatch = e instanceof ErrorType;
148
+ }
149
+ }
150
+ const functionName = this.actualValue.name || typeof this.actualValue === "function" ? "[anonymous function]" : this.actualValue.toString();
151
+ this.triggerResult(
152
+ didThrow,
153
+ ` Expected ${functionName} to ${this.positive ? "throw" : "not throw"} an exception ${!this.positive && errorMessage ? `, but an error with the message "${errorMessage}" was thrown` : ""}`
154
+ );
155
+ if (ErrorType) {
156
+ this.triggerResult(
157
+ typeMatch,
158
+ ` Expected Error type '${ErrorType.name}', but the error is not an instance of it`
159
+ );
160
+ }
161
+ }
162
+ };
163
+ var describe = async function(moduleName, callback) {
164
+ print("\n" + moduleName);
165
+ await callback();
166
+ beforeEachCb = null;
167
+ afterEachCb = null;
168
+ };
169
+ var beforeEachCb;
170
+ var afterEachCb;
171
+ var it = async function(expectation, callback) {
172
+ try {
173
+ if (typeof beforeEachCb === "function") {
174
+ await beforeEachCb();
175
+ }
176
+ await callback();
177
+ if (typeof afterEachCb === "function") {
178
+ await afterEachCb();
179
+ }
180
+ print(` ${GREEN}\u2714${RESET} ${GRAY}${expectation}${RESET}`);
181
+ } catch (e) {
182
+ print(` ${RED}\u274C${RESET} ${GRAY}${expectation}${RESET}`);
183
+ print(`${RED}${e.message}${RESET}`);
184
+ if (e.stack)
185
+ print(e.stack);
186
+ }
187
+ };
188
+ var expect = function(actualValue) {
189
+ ++countTestsOverall;
190
+ const expecter = new MatcherFactory(actualValue, true);
191
+ return expecter;
192
+ };
193
+ var assert = function(success, message) {
194
+ ++countTestsOverall;
195
+ if (!success) {
196
+ ++countTestsFailed;
197
+ }
198
+ nodeAssert(success, message);
199
+ };
200
+ assert.strictEqual = function(actual, expected, message) {
201
+ ++countTestsOverall;
202
+ try {
203
+ nodeAssert.strictEqual(actual, expected, message);
204
+ } catch (error) {
205
+ ++countTestsFailed;
206
+ throw error;
207
+ }
208
+ };
209
+ assert.throws = function(promiseFn, ...args) {
210
+ ++countTestsOverall;
211
+ let error;
212
+ try {
213
+ promiseFn();
214
+ } catch (e) {
215
+ error = e;
216
+ }
217
+ if (!error)
218
+ ++countTestsFailed;
219
+ nodeAssert.throws(() => {
220
+ if (error)
221
+ throw error;
222
+ }, args[0], args[1]);
223
+ };
224
+ assert.deepStrictEqual = function(actual, expected, message) {
225
+ ++countTestsOverall;
226
+ try {
227
+ nodeAssert.deepStrictEqual(actual, expected, message);
228
+ } catch (error) {
229
+ ++countTestsFailed;
230
+ throw error;
231
+ }
232
+ };
233
+ var runTests = async function(namespaces) {
234
+ for (const subNamespace in namespaces) {
235
+ const namespace = namespaces[subNamespace];
236
+ if (typeof namespace === "function") {
237
+ await namespace();
238
+ } else if (typeof namespace === "object") {
239
+ await runTests(namespace);
240
+ }
241
+ }
242
+ };
243
+ var printResult = () => {
244
+ if (countTestsIgnored) {
245
+ print(`
246
+ ${BLUE}\u2714 ${countTestsIgnored} ignored test${countTestsIgnored > 1 ? "s" : ""}${RESET}`);
247
+ }
248
+ if (countTestsFailed) {
249
+ print(`
250
+ ${RED}\u274C ${countTestsFailed} of ${countTestsOverall} tests failed${RESET}`);
251
+ } else {
252
+ print(`
253
+ ${GREEN}\u2714 ${countTestsOverall} completed${RESET}`);
254
+ }
255
+ };
256
+ var getRuntime = async () => {
257
+ if (runtime && runtime !== "Unknown") {
258
+ return runtime;
259
+ }
260
+ if (globalThis.Deno?.version?.deno) {
261
+ return "Deno " + globalThis.Deno?.version?.deno;
262
+ } else {
263
+ let process = globalThis.process;
264
+ if (!process) {
265
+ try {
266
+ process = await import("process");
267
+ } catch (error) {
268
+ console.error(error);
269
+ console.warn(error.message);
270
+ runtime = "Unknown";
271
+ }
272
+ }
273
+ if (process?.versions?.gjs) {
274
+ runtime = "Gjs " + process.versions.gjs;
275
+ } else if (process?.versions?.node) {
276
+ runtime = "Node.js " + process.versions.node;
277
+ }
278
+ }
279
+ return runtime || "Unknown";
280
+ };
281
+ var printRuntime = async () => {
282
+ const runtime2 = await getRuntime();
283
+ print(`
284
+ Running on ${runtime2}`);
285
+ };
286
+ var run = async (namespaces) => {
287
+ printRuntime().then(async () => {
288
+ return runTests(namespaces).then(() => {
289
+ printResult();
290
+ print();
291
+ mainloop?.quit();
292
+ });
293
+ });
294
+ mainloop?.run();
295
+ };
296
+
297
+ // src/callback.spec.ts
298
+ import { open, write, close, rm } from "fs";
299
+ import { Buffer } from "buffer";
300
+ var callback_spec_default = async () => {
301
+ await describe("fs.open", async () => {
302
+ await it(`should open a file for writing`, () => {
303
+ const path = "./test/open.txt";
304
+ open(path, "w+", 438, (err, fd) => {
305
+ expect(err).toBeNull();
306
+ console.log("fs.open: file open");
307
+ let buffWrite = Buffer.from("Hello World", "utf8"), buffStart = 0, buffLength = buffWrite.length, filePos = 0;
308
+ write(fd, buffWrite, buffStart, buffLength, filePos, (err2, written, buffer) => {
309
+ expect(err2).toBeNull();
310
+ console.log("fs.open: file written");
311
+ expect(written).toBe(buffWrite.length);
312
+ expect(buffer).toBe(buffWrite);
313
+ close(fd, (err3) => {
314
+ expect(err3).toBeNull();
315
+ console.log("fs.open: file closed");
316
+ rm(path, (err4) => {
317
+ expect(err4).toBeNull();
318
+ console.log("fs.open: file removed");
319
+ });
320
+ });
321
+ });
322
+ });
323
+ });
324
+ });
325
+ };
326
+
327
+ // src/file-handle.spec.ts
328
+ import { promises } from "fs";
329
+ import { Buffer as Buffer2 } from "buffer";
330
+ var file_handle_spec_default = async () => {
331
+ await describe("FileHandle", async () => {
332
+ await it(`should open a file for writing`, async () => {
333
+ const path = "./test/openP.txt";
334
+ const fileHandle = await promises.open(path, "w+", 438);
335
+ console.log("FileHandle: file open");
336
+ let buffWrite = Buffer2.from("Hello World", "utf8"), buffStart = 0, buffLength = buffWrite.length, filePos = 0;
337
+ const res = await fileHandle.write(buffWrite, buffStart, buffLength, filePos);
338
+ console.log("FileHandle: file written");
339
+ expect(res.bytesWritten).toBe(buffWrite.length);
340
+ expect(res.buffer).toBe(buffWrite);
341
+ await fileHandle.close();
342
+ console.log("FileHandle: file closed");
343
+ await promises.rm(path);
344
+ console.log("FileHandle: file removed");
345
+ });
346
+ });
347
+ };
348
+
349
+ // src/promises.spec.ts
350
+ import { promises as promises2, existsSync } from "fs";
351
+ import { mkdir, readdir, mkdtemp, writeFile, rm as rm2, rmdir } from "fs/promises";
352
+ import { join } from "path";
353
+ import { Buffer as Buffer3 } from "buffer";
354
+ var promises_spec_default = async () => {
355
+ await describe("fs/promises", async () => {
356
+ await it('import over "fs/should" should be work', async () => {
357
+ expect(typeof mkdir).toBe("function");
358
+ });
359
+ });
360
+ await describe("fs.promises.readdir", async () => {
361
+ await it("should return no files for an empty directory", async () => {
362
+ const dir = await mkdtemp("fs-test-");
363
+ const files = await readdir(dir);
364
+ expect(files.length).toBe(0);
365
+ await rmdir(dir);
366
+ });
367
+ await it("should return the files for non-empty directory", async () => {
368
+ const dir = await mkdtemp("fs-test-");
369
+ const txt1 = join(dir, "test1.txt");
370
+ const txt2 = join(dir, "test2.txt");
371
+ const dir1 = join(dir, "empty-dir");
372
+ await writeFile(txt1, "");
373
+ await writeFile(txt2, "");
374
+ await mkdir(dir1);
375
+ const files = await readdir(dir);
376
+ expect(files.length).toEqual(3);
377
+ await rm2(txt1);
378
+ await rm2(txt2);
379
+ await rmdir(dir1);
380
+ await rmdir(dir);
381
+ });
382
+ await it('should return the file with the name "file.txt"', async () => {
383
+ const dir = await mkdtemp("fs-test-");
384
+ const expectedFileName = "file.txt";
385
+ const file = join(dir, expectedFileName);
386
+ await writeFile(file, "");
387
+ const files = await readdir(dir);
388
+ expect(files[0]).toEqual(expectedFileName);
389
+ await rm2(file);
390
+ await rmdir(dir);
391
+ });
392
+ await it('should return with file types if option "withFileTypes" is `true`', async () => {
393
+ const dir = await mkdtemp("fs-test-");
394
+ const expectedFile = "file.txt";
395
+ const expectedDir = "subdir";
396
+ const file = join(dir, expectedFile);
397
+ const subdir = join(dir, expectedDir);
398
+ await writeFile(file, "");
399
+ await mkdir(subdir);
400
+ const files = await readdir(dir, { withFileTypes: true });
401
+ expect(files.length).toBe(2);
402
+ const fileWithTypes = files.find((f) => f.name === expectedFile);
403
+ const dirWithTypes = files.find((f) => f.name === expectedDir);
404
+ expect(fileWithTypes.isFile()).toBeTruthy();
405
+ expect(fileWithTypes.isDirectory()).toBeFalsy();
406
+ expect(dirWithTypes.isFile()).toBeFalsy();
407
+ expect(dirWithTypes.isDirectory()).toBeTruthy();
408
+ await rm2(file);
409
+ await rmdir(subdir);
410
+ await rmdir(dir);
411
+ });
412
+ });
413
+ await describe("fs.promises.readFile", async () => {
414
+ await it("should be a function", async () => {
415
+ expect(typeof promises2.readFile).toBe("function");
416
+ });
417
+ await it("should be a promise", async () => {
418
+ expect(promises2.readFile("./test/file.txt", "utf-8") instanceof Promise).toBeTruthy();
419
+ });
420
+ await it("should return a Buffer if no encoding was specified", async () => {
421
+ const bufferData = await promises2.readFile("package.json");
422
+ expect(bufferData instanceof Buffer3).toBeTruthy();
423
+ });
424
+ await it("should return a string when encoding is utf-8", async () => {
425
+ const utf8Data = await promises2.readFile("./test/file.txt", "utf-8");
426
+ expect(typeof utf8Data === "string").toBeTruthy();
427
+ });
428
+ await it('should return a string with "Hello World"', async () => {
429
+ const utf8Data = await promises2.readFile("./test/file.txt", "utf-8");
430
+ expect(utf8Data).toBe("Hello World");
431
+ });
432
+ });
433
+ await describe("fs.promises.mkdtemp", async () => {
434
+ await it("should be a function", async () => {
435
+ expect(typeof promises2.mkdtemp).toBe("function");
436
+ });
437
+ await it("should create a new directory", async () => {
438
+ const directory = await promises2.mkdtemp("fs-test-");
439
+ expect(existsSync(directory)).toBeTruthy();
440
+ await promises2.rmdir(directory);
441
+ });
442
+ });
443
+ await describe("fs.promises.rm", async () => {
444
+ await it("should be a function", async () => {
445
+ expect(typeof promises2.rm).toBe("function");
446
+ });
447
+ await it("should remove an text file", async () => {
448
+ const dir = await promises2.mkdtemp("fs-test-");
449
+ const txt1 = join(dir, "file1.txt");
450
+ await promises2.writeFile(txt1, "");
451
+ expect(existsSync(txt1)).toBeTruthy();
452
+ await promises2.rm(txt1);
453
+ expect(existsSync(txt1)).toBeFalsy();
454
+ await promises2.rmdir(dir);
455
+ });
456
+ await it("should not remove an non-empty folder if recursive option is false and should remove an non-empty folder if recursive option is true", async () => {
457
+ const dir = await promises2.mkdtemp("fs-test-");
458
+ await promises2.writeFile(join(dir, "file1.txt"), "");
459
+ await promises2.writeFile(join(dir, "file2.txt"), "");
460
+ await promises2.mkdir(join(dir, "some_dir"));
461
+ await promises2.mkdir(join(dir, "some_dir", "file.txt"));
462
+ expect(existsSync(dir)).toBeTruthy();
463
+ try {
464
+ await promises2.rm(dir, { recursive: false });
465
+ } catch (error) {
466
+ expect(error).toBeDefined();
467
+ }
468
+ expect(existsSync(dir)).toBeTruthy();
469
+ await promises2.rm(dir, { recursive: true });
470
+ expect(existsSync(dir)).toBeFalsy();
471
+ });
472
+ });
473
+ };
474
+
475
+ // src/sync.spec.ts
476
+ import { join as join2, dirname } from "path";
477
+ import { fileURLToPath } from "url";
478
+ import { existsSync as existsSync2, readdirSync, readFileSync, mkdirSync, rmdirSync, writeFileSync, unlinkSync, watch, mkdtempSync, rmSync, realpathSync, symlinkSync } from "fs";
479
+ import { Buffer as Buffer4 } from "buffer";
480
+ var __filename = fileURLToPath(import.meta.url);
481
+ var __dirname = dirname(__filename);
482
+ var sync_spec_default = async () => {
483
+ await describe("fs.existsSync", async () => {
484
+ const existingFiles = ["tsconfig.json", "package.json", "README.md"];
485
+ const nonExistingFiles = ["asdasd", "/asdasd", ""];
486
+ await it("should return true for existing files", () => {
487
+ for (const file of existingFiles) {
488
+ const result = existsSync2(file);
489
+ expect(result).toBe(true);
490
+ }
491
+ });
492
+ await it("should return false for non existing files", () => {
493
+ for (const file of nonExistingFiles) {
494
+ const result = existsSync2(file);
495
+ expect(result).toBe(false);
496
+ }
497
+ });
498
+ });
499
+ await describe("fs.readdirSync", async () => {
500
+ await it("should return no files for an empty directory", () => {
501
+ const dir = mkdtempSync("fs-test-");
502
+ const files = readdirSync(dir);
503
+ expect(files.length).toBe(0);
504
+ rmdirSync(dir);
505
+ });
506
+ await it("should return the files for non-empty directory", () => {
507
+ const dir = mkdtempSync("fs-test-");
508
+ const txt1 = join2(dir, "test1.txt");
509
+ const txt2 = join2(dir, "test2.txt");
510
+ const dir1 = join2(dir, "empty-dir");
511
+ writeFileSync(txt1, "");
512
+ writeFileSync(txt2, "");
513
+ mkdirSync(dir1);
514
+ const files = readdirSync(dir);
515
+ expect(files.length).toEqual(3);
516
+ rmSync(txt1);
517
+ rmSync(txt2);
518
+ rmdirSync(dir1);
519
+ rmdirSync(dir);
520
+ });
521
+ await it('should return the file with the name "file.txt"', () => {
522
+ const dir = mkdtempSync("fs-test-");
523
+ const expectedFileName = "file.txt";
524
+ const file = join2(dir, expectedFileName);
525
+ writeFileSync(file, "");
526
+ const files = readdirSync(dir);
527
+ expect(files[0]).toEqual(expectedFileName);
528
+ rmSync(file);
529
+ rmdirSync(dir);
530
+ });
531
+ await it('should return with file types if option "withFileTypes" is `true`', () => {
532
+ const dir = mkdtempSync("fs-test-");
533
+ const expectedFile = "file.txt";
534
+ const expectedDir = "subdir";
535
+ const file = join2(dir, expectedFile);
536
+ const subdir = join2(dir, expectedDir);
537
+ writeFileSync(file, "");
538
+ mkdirSync(subdir);
539
+ const files = readdirSync(dir, { withFileTypes: true });
540
+ expect(files.length).toBe(2);
541
+ const fileWithTypes = files.find((f) => f.name === expectedFile);
542
+ const dirWithTypes = files.find((f) => f.name === expectedDir);
543
+ expect(fileWithTypes.isFile()).toBeTruthy();
544
+ expect(fileWithTypes.isDirectory()).toBeFalsy();
545
+ expect(dirWithTypes.isFile()).toBeFalsy();
546
+ expect(dirWithTypes.isDirectory()).toBeTruthy();
547
+ rmSync(file);
548
+ rmdirSync(subdir);
549
+ rmdirSync(dir);
550
+ });
551
+ });
552
+ await describe("fs.readFileSync", async () => {
553
+ await it("should return a Buffer if no encoding was specified", () => {
554
+ const bufferData = readFileSync("package.json");
555
+ expect(bufferData instanceof Buffer4).toBeTruthy();
556
+ });
557
+ await it("should return a string when encoding is utf-8", () => {
558
+ const utf8Data = readFileSync("./test/file.txt", "utf-8");
559
+ expect(typeof utf8Data === "string").toBeTruthy();
560
+ });
561
+ await it('should return a string with "Hello World"', () => {
562
+ const utf8Data = readFileSync("./test/file.txt", "utf-8");
563
+ expect(utf8Data).toBe("Hello World");
564
+ });
565
+ });
566
+ await describe("fs.mkdirSync", async () => {
567
+ const dir = "./foobar";
568
+ await it(`should create the directory "${dir}" without error`, () => {
569
+ mkdirSync(dir);
570
+ });
571
+ await it(`${dir} should exists`, () => {
572
+ expect(existsSync2(dir)).toBeTruthy();
573
+ });
574
+ });
575
+ await describe("fs.rmdirSync", async () => {
576
+ const dir = "./foobar";
577
+ await it(`should be remove the directory "${dir}" without error`, () => {
578
+ rmdirSync(dir);
579
+ });
580
+ await it(`"${dir}" should not exists (anymore)`, () => {
581
+ expect(existsSync2(dir)).toBeFalsy();
582
+ });
583
+ });
584
+ await describe("fs.writeFileSync", async () => {
585
+ const watchMe = join2(__dirname, "test/watch.js");
586
+ await it(`should be executed without error`, () => {
587
+ writeFileSync(watchMe, "// test");
588
+ });
589
+ await it(`fs.watch should watch ${watchMe} for changes`, () => {
590
+ const watcher = watch(watchMe, { persistent: true }, console.log);
591
+ watcher.on("change", console.log).on("rename", console.log);
592
+ setTimeout(() => {
593
+ watcher.close();
594
+ }, 1e3);
595
+ setTimeout(() => {
596
+ writeFileSync(watchMe, "// test");
597
+ setTimeout(() => {
598
+ unlinkSync(watchMe);
599
+ }, 100);
600
+ }, 100);
601
+ });
602
+ });
603
+ await describe("fs.mkdtempSync", async () => {
604
+ await it("should be a function", () => {
605
+ expect(typeof mkdtempSync).toBe("function");
606
+ });
607
+ await it("should create a new directory", () => {
608
+ const directory = mkdtempSync("fs-test-");
609
+ expect(existsSync2(directory)).toBeTruthy();
610
+ rmdirSync(directory);
611
+ });
612
+ });
613
+ await describe("fs.realpathSync", async () => {
614
+ await it("should be a function", () => {
615
+ expect(typeof realpathSync).toBe("function");
616
+ });
617
+ await it("should return the real and absolute path", () => {
618
+ const tsConfig = "./tsconfig.json";
619
+ const tsConfigSymlink = "./symlink_tsconfig.json";
620
+ if (!existsSync2(tsConfigSymlink)) {
621
+ symlinkSync(tsConfig, tsConfigSymlink);
622
+ const realPath = realpathSync(tsConfig);
623
+ const realSymLinkPath = realpathSync(tsConfigSymlink);
624
+ expect(realSymLinkPath).toBe(realPath);
625
+ }
626
+ unlinkSync(tsConfigSymlink);
627
+ });
628
+ });
629
+ };
630
+
631
+ // src/symlink.spec.ts
632
+ import { symlink as symlinkCb } from "fs";
633
+ var symlink_spec_default = async () => {
634
+ await describe("fs.symlink", async () => {
635
+ await it("ASYNC: no callback function results in Error", () => {
636
+ expect(() => {
637
+ symlinkCb("some/path", "some/other/path", "dir");
638
+ }).toThrow(Error);
639
+ });
640
+ });
641
+ };
642
+
643
+ // src/stat.spec.ts
644
+ import { statSync } from "fs";
645
+ import { stat } from "fs/promises";
646
+ var stat_spec_default = async () => {
647
+ await describe("fs.statSync", async () => {
648
+ await it("Should return the file stat", async () => {
649
+ const s = statSync("tsconfig.json");
650
+ expect(s.atime instanceof Date).toBeTruthy();
651
+ expect(s.atimeMs).toBeGreaterThan(0);
652
+ expect(s.birthtime instanceof Date).toBeTruthy();
653
+ expect(s.birthtimeMs).toBeGreaterThan(0);
654
+ expect(s.blksize).toBeGreaterThan(0);
655
+ expect(s.blocks).toBeGreaterThan(0);
656
+ expect(s.ctime instanceof Date).toBeTruthy();
657
+ expect(s.ctimeMs).toBeGreaterThan(0);
658
+ expect(s.dev).toBeGreaterThan(0);
659
+ expect(s.gid).toBeGreaterThan(0);
660
+ expect(s.ino).toBeGreaterThan(0);
661
+ expect(s.mode).toBeGreaterThan(0);
662
+ expect(s.mtime instanceof Date).toBeTruthy();
663
+ expect(s.mtimeMs).toBeGreaterThan(0);
664
+ expect(s.nlink).toBeGreaterThan(0);
665
+ expect(s.rdev).toBeGreaterThan(-1);
666
+ expect(s.size).toBeGreaterThan(0);
667
+ expect(s.uid).toBeGreaterThan(0);
668
+ expect(s.isBlockDevice()).toBeFalsy();
669
+ expect(s.isCharacterDevice()).toBeFalsy();
670
+ expect(s.isDirectory()).toBeFalsy();
671
+ expect(s.isFIFO()).toBeFalsy();
672
+ expect(s.isFile()).toBeTruthy();
673
+ expect(s.isSocket()).toBeFalsy();
674
+ expect(s.isSymbolicLink()).toBeFalsy();
675
+ });
676
+ });
677
+ await describe("fs.stat (promise)", async () => {
678
+ await it("Should return the file stat", async () => {
679
+ const s = await stat("tsconfig.json");
680
+ expect(s.atime instanceof Date).toBeTruthy();
681
+ expect(s.atimeMs).toBeGreaterThan(0);
682
+ expect(s.birthtime instanceof Date).toBeTruthy();
683
+ expect(s.birthtimeMs).toBeGreaterThan(0);
684
+ expect(s.blksize).toBeGreaterThan(0);
685
+ expect(s.blocks).toBeGreaterThan(0);
686
+ expect(s.ctime instanceof Date).toBeTruthy();
687
+ expect(s.ctimeMs).toBeGreaterThan(0);
688
+ expect(s.dev).toBeGreaterThan(0);
689
+ expect(s.gid).toBeGreaterThan(0);
690
+ expect(s.ino).toBeGreaterThan(0);
691
+ expect(s.mode).toBeGreaterThan(0);
692
+ expect(s.mtime instanceof Date).toBeTruthy();
693
+ expect(s.mtimeMs).toBeGreaterThan(0);
694
+ expect(s.nlink).toBeGreaterThan(0);
695
+ expect(s.rdev).toBeGreaterThan(-1);
696
+ expect(s.size).toBeGreaterThan(0);
697
+ expect(s.uid).toBeGreaterThan(0);
698
+ expect(s.isBlockDevice()).toBeFalsy();
699
+ expect(s.isCharacterDevice()).toBeFalsy();
700
+ expect(s.isDirectory()).toBeFalsy();
701
+ expect(s.isFIFO()).toBeFalsy();
702
+ expect(s.isFile()).toBeTruthy();
703
+ expect(s.isSocket()).toBeFalsy();
704
+ expect(s.isSymbolicLink()).toBeFalsy();
705
+ });
706
+ });
707
+ };
708
+
709
+ // src/test.mts
710
+ run({ testSuiteCallback: callback_spec_default, testSuiteFileHandle: file_handle_spec_default, testSuitePromise: promises_spec_default, testSuiteSync: sync_spec_default, testSuiteSymlink: symlink_spec_default, testSuiteStat: stat_spec_default });