@zenfs/devel 0.0.0-stage → 0.1.0

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 (73) hide show
  1. package/LICENSE.md +157 -0
  2. package/README.md +46 -2
  3. package/dist/bin/make-index.d.ts +2 -0
  4. package/dist/bin/make-index.js +110 -0
  5. package/dist/bin/zci.d.ts +2 -0
  6. package/dist/bin/zci.js +51 -0
  7. package/dist/bin/zenfs-test.d.ts +2 -0
  8. package/dist/bin/zenfs-test.js +243 -0
  9. package/dist/ci.d.ts +15 -0
  10. package/dist/ci.js +74 -0
  11. package/dist/tests/common.d.ts +28 -0
  12. package/dist/tests/common.js +37 -0
  13. package/dist/tests/fs/append.test.d.ts +1 -0
  14. package/dist/tests/fs/append.test.js +27 -0
  15. package/dist/tests/fs/concurrency.test.d.ts +1 -0
  16. package/dist/tests/fs/concurrency.test.js +76 -0
  17. package/dist/tests/fs/dir.test.d.ts +1 -0
  18. package/dist/tests/fs/dir.test.js +83 -0
  19. package/dist/tests/fs/directory.test.d.ts +1 -0
  20. package/dist/tests/fs/directory.test.js +151 -0
  21. package/dist/tests/fs/errors.test.d.ts +1 -0
  22. package/dist/tests/fs/errors.test.js +141 -0
  23. package/dist/tests/fs/exists.test.d.ts +1 -0
  24. package/dist/tests/fs/exists.test.js +19 -0
  25. package/dist/tests/fs/links.test.d.ts +1 -0
  26. package/dist/tests/fs/links.test.js +248 -0
  27. package/dist/tests/fs/open.test.d.ts +1 -0
  28. package/dist/tests/fs/open.test.js +95 -0
  29. package/dist/tests/fs/permissions.test.d.ts +1 -0
  30. package/dist/tests/fs/permissions.test.js +346 -0
  31. package/dist/tests/fs/read.test.d.ts +1 -0
  32. package/dist/tests/fs/read.test.js +149 -0
  33. package/dist/tests/fs/readFile.test.d.ts +1 -0
  34. package/dist/tests/fs/readFile.test.js +110 -0
  35. package/dist/tests/fs/rename.test.d.ts +1 -0
  36. package/dist/tests/fs/rename.test.js +70 -0
  37. package/dist/tests/fs/scaling.test.d.ts +1 -0
  38. package/dist/tests/fs/scaling.test.js +27 -0
  39. package/dist/tests/fs/stat.test.d.ts +1 -0
  40. package/dist/tests/fs/stat.test.js +93 -0
  41. package/dist/tests/fs/streams.test.d.ts +1 -0
  42. package/dist/tests/fs/streams.test.js +161 -0
  43. package/dist/tests/fs/tempdir.test.d.ts +1 -0
  44. package/dist/tests/fs/tempdir.test.js +118 -0
  45. package/dist/tests/fs/times.test.d.ts +7 -0
  46. package/dist/tests/fs/times.test.js +134 -0
  47. package/dist/tests/fs/truncate.test.d.ts +1 -0
  48. package/dist/tests/fs/truncate.test.js +62 -0
  49. package/dist/tests/fs/watch.test.d.ts +1 -0
  50. package/dist/tests/fs/watch.test.js +320 -0
  51. package/dist/tests/fs/write.test.d.ts +1 -0
  52. package/dist/tests/fs/write.test.js +83 -0
  53. package/dist/tests/fs/xattr.test.d.ts +1 -0
  54. package/dist/tests/fs/xattr.test.js +137 -0
  55. package/dist/tests/logs.d.ts +1 -0
  56. package/dist/tests/logs.js +21 -0
  57. package/dist/tests/setup/memory.d.ts +1 -0
  58. package/dist/tests/setup/memory.js +3 -0
  59. package/dist/tests/setup.d.ts +9 -0
  60. package/dist/tests/setup.js +40 -0
  61. package/eslint.shared.js +60 -0
  62. package/package.json +91 -5
  63. package/tests/data/49chars.txt +1 -0
  64. package/tests/data/a.js +46 -0
  65. package/tests/data/a1.js +46 -0
  66. package/tests/data/elipses.txt +1 -0
  67. package/tests/data/empty.txt +0 -0
  68. package/tests/data/exit.js +22 -0
  69. package/tests/data/image.jpg +0 -0
  70. package/tests/data/utf8.txt +1 -0
  71. package/tests/data/x.txt +1 -0
  72. package/tests/data-license.md +19 -0
  73. package/tests/readme.md +5 -0
package/dist/ci.js ADDED
@@ -0,0 +1,74 @@
1
+ // SPDX-License-Identifier: LGPL-3.0-or-later
2
+ import { Octokit } from '@octokit/action';
3
+ import { mkdirSync } from 'node:fs';
4
+ import { join } from 'node:path/posix';
5
+ import { _throw } from 'utilium';
6
+ import { JSONFileMap } from 'utilium/fs';
7
+ const octokit = new Octokit();
8
+ const [owner, repo] = process.env.GITHUB_REPOSITORY?.split('/') || _throw('GITHUB_REPOSITORY is not set');
9
+ const head_sha = process.env.GITHUB_SHA || _throw('GITHUB_SHA is not set');
10
+ mkdirSync(join(import.meta.dirname, '../tmp'), { recursive: true });
11
+ const checks = new JSONFileMap(join(import.meta.dirname, '../tmp/checks.json'), { overwrite_invalid: true });
12
+ /** Maps test names and shortcuts to full check names */
13
+ export const checkNames = {
14
+ // Basic ones
15
+ format: 'Formatting',
16
+ lint: 'Linting',
17
+ build: 'Build',
18
+ license: 'SDPX License Check',
19
+ // Tests
20
+ 'Common tests': 'Unit tests: common',
21
+ memory: 'Unit tests: InMemory',
22
+ context: 'Unit tests: contexts',
23
+ index: 'Unit tests: Index',
24
+ port: 'Unit tests: Port',
25
+ fetch: 'Unit tests: Fetch',
26
+ cow: 'Unit tests: Copy-On-Write',
27
+ 'single-buffer': 'Unit tests: SingleBuffer',
28
+ };
29
+ /**
30
+ * Create a new GitHub check run
31
+ */
32
+ export async function createCheck(id, name) {
33
+ const response = await octokit.request('POST /repos/{owner}/{repo}/check-runs', {
34
+ owner,
35
+ repo,
36
+ name,
37
+ head_sha,
38
+ status: 'queued',
39
+ started_at: new Date().toISOString(),
40
+ });
41
+ checks.set(id, { id: Number(response.data.id), completed: false });
42
+ }
43
+ /**
44
+ * Move an existing check run from "queued" to "in_progress".
45
+ */
46
+ export async function startCheck(id) {
47
+ const check = checks.get(id);
48
+ await octokit.request('PATCH /repos/{owner}/{repo}/check-runs/{check_run_id}', {
49
+ owner,
50
+ repo,
51
+ check_run_id: check.id,
52
+ status: 'in_progress',
53
+ started_at: new Date().toISOString(),
54
+ });
55
+ }
56
+ /**
57
+ * Complete a check run
58
+ */
59
+ export async function completeCheck(id, conclusion, title = '', summary = '') {
60
+ const check = checks.get(id);
61
+ if (check.completed)
62
+ return;
63
+ await octokit.request('PATCH /repos/{owner}/{repo}/check-runs/{check_run_id}', {
64
+ owner,
65
+ repo,
66
+ check_run_id: check.id,
67
+ status: 'completed',
68
+ completed_at: new Date().toISOString(),
69
+ conclusion,
70
+ output: { title, summary },
71
+ });
72
+ check.completed = true;
73
+ checks.set(id, check);
74
+ }
@@ -0,0 +1,28 @@
1
+ import { fs as defaultFS } from '@zenfs/core';
2
+ export declare const fs: typeof defaultFS;
3
+ /**
4
+ * A feature or function a backend may not support.
5
+ * Tests that need one are skipped when the backend under test does not have it.
6
+ */
7
+ export type TestFlag = 'sync' | 'async' | 'write' | 'appends' | 'directories' | 'links' | 'symlinks' | 'rename' | 'truncate' | 'streams' | 'watch' | 'permissions' | 'xattr' | 'times' | 'tempdir' | 'lchmod' | 'promises.exists' | 'root';
8
+ /**
9
+ * Whether a backend supports a flag.
10
+ *
11
+ * `false` is for things a backend can not support, while `'skip'` is for temporarily disabling something.
12
+ * `'todo'` is for things that should work but don't yet.
13
+ */
14
+ export type TestFlagState = boolean | 'skip' | 'todo';
15
+ /** Options for `suite` and `test`. Note this is a subset of both, since they do not share a type. */
16
+ export interface TestConfig {
17
+ skip?: string;
18
+ todo?: string;
19
+ }
20
+ /**
21
+ * Configures a `suite` or `test` to run only if the backend supports all of the given flags.
22
+ *
23
+ * @example
24
+ * ```ts
25
+ * test('lchmod', config('lchmod', 'async'), async () => { ... });
26
+ * ```
27
+ */
28
+ export declare function config(...required: TestFlag[]): TestConfig;
@@ -0,0 +1,37 @@
1
+ // SPDX-License-Identifier: LGPL-3.0-or-later
2
+ import { fs as defaultFS } from '@zenfs/core';
3
+ import { join, resolve } from 'node:path';
4
+ import { styleText } from 'node:util';
5
+ import { setupLogs } from './logs.js';
6
+ setupLogs();
7
+ const setupPath = resolve(process.env.SETUP || join(import.meta.dirname, 'setup/memory.js'));
8
+ process.on('unhandledRejection', (reason) => {
9
+ console.error('Unhandled rejection:', styleText('red', reason.stack || reason.message));
10
+ });
11
+ const setup = await import(setupPath).catch(error => {
12
+ console.log('Failed to import test setup:');
13
+ throw error;
14
+ });
15
+ // Satisfies is used to make sure that ZenFS is fully type compatible with Node.js
16
+ export const fs = (setup.fs || defaultFS);
17
+ /** Flags are supported unless the setup says otherwise */
18
+ const flags = (setup.flags ?? {});
19
+ /**
20
+ * Configures a `suite` or `test` to run only if the backend supports all of the given flags.
21
+ *
22
+ * @example
23
+ * ```ts
24
+ * test('lchmod', config('lchmod', 'async'), async () => { ... });
25
+ * ```
26
+ */
27
+ export function config(...required) {
28
+ for (const flag of required) {
29
+ const state = flags[flag] ?? true;
30
+ if (state === true)
31
+ continue;
32
+ if (state == 'todo')
33
+ return { todo: `${flag} is not implemented yet` };
34
+ return { skip: state == 'skip' ? `${flag} is temporarily skipped` : `${flag} is not supported` };
35
+ }
36
+ return {};
37
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,27 @@
1
+ // SPDX-License-Identifier: LGPL-3.0-or-later
2
+ import assert from 'node:assert/strict';
3
+ import { suite, test } from 'node:test';
4
+ import { fs, config } from '../common.js';
5
+ const content = 'Sample content', original = 'ABCD';
6
+ suite('Appends', config('appends'), () => {
7
+ test('Create an empty file and add content', config('async'), async () => {
8
+ const filename = 'append.txt';
9
+ await fs.promises.appendFile(filename, content);
10
+ const data = await fs.promises.readFile(filename, 'utf8');
11
+ assert.equal(data, content);
12
+ });
13
+ test('Append data to a non-empty file', config('async'), async () => {
14
+ const filename = 'append2.txt';
15
+ await fs.promises.writeFile(filename, original);
16
+ await fs.promises.appendFile(filename, content);
17
+ const data = await fs.promises.readFile(filename, 'utf8');
18
+ assert.equal(data, original + content);
19
+ });
20
+ test('Append a buffer to the file', config('async'), async () => {
21
+ const filename = 'append3.txt';
22
+ await fs.promises.writeFile(filename, original);
23
+ await fs.promises.appendFile(filename, content);
24
+ const data = await fs.promises.readFile(filename, 'utf8');
25
+ assert.equal(data, original + content);
26
+ });
27
+ });
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,76 @@
1
+ // SPDX-License-Identifier: LGPL-3.0-or-later
2
+ import assert from 'node:assert/strict';
3
+ import { suite, test } from 'node:test';
4
+ import { config, fs } from '../common.js';
5
+ suite('Concurrency', config('write', 'async'), () => {
6
+ test('parallel writeFile in the same directory does not lose entries #256', async () => {
7
+ await fs.promises.mkdir('/issue-256');
8
+ await Promise.all([fs.promises.writeFile('/issue-256/a1.txt', 'a1'), fs.promises.writeFile('/issue-256/a2.txt', 'a2')]);
9
+ const files = await fs.promises.readdir('/issue-256');
10
+ assert.deepEqual(files.sort(), ['a1.txt', 'a2.txt']);
11
+ });
12
+ test('concurrent file creation does not lose directory entries #298', async () => {
13
+ await fs.promises.mkdir('/issue-298');
14
+ const names = Array.from({ length: 32 }, (_, i) => `file-${i}.txt`);
15
+ await Promise.all(names.map(name => fs.promises.writeFile(`/issue-298/${name}`, name)));
16
+ const files = await fs.promises.readdir('/issue-298');
17
+ assert.deepEqual(files.sort(), names.sort());
18
+ });
19
+ test('open handles for the same file share metadata #287', async () => {
20
+ await fs.promises.writeFile('/issue-287.txt', 'Hello World');
21
+ const a = await fs.promises.open('/issue-287.txt', 'r+');
22
+ const b = await fs.promises.open('/issue-287.txt', 'r+');
23
+ await a.truncate(5); // contents are now "Hello"
24
+ const { size } = await b.stat();
25
+ assert.equal(size, 5);
26
+ await a.close();
27
+ await b.close();
28
+ });
29
+ test('writes are visible to other open handles before syncing', async () => {
30
+ const a = await fs.promises.open('/shared-write.txt', 'w+');
31
+ const b = await fs.promises.open('/shared-write.txt', 'r');
32
+ await a.write('some shared data');
33
+ const { buffer } = await b.read(new Uint8Array(16), 0, 16, 0);
34
+ assert.equal(new TextDecoder().decode(buffer), 'some shared data');
35
+ await a.close();
36
+ await b.close();
37
+ });
38
+ test('reads concurrent with an overwrite of the same file do not fail #303', async () => {
39
+ await fs.promises.writeFile('/issue-303.txt', 'original contents');
40
+ /* Every handle for a file shares one vnode, so closing any of these flushes whatever is dirty.
41
+ Syncing took a shared lock, letting several of these run at once and corrupt each other. */
42
+ for (let round = 0; round < 20; round++) {
43
+ const contents = `rewritten contents for round ${round}`;
44
+ const results = await Promise.allSettled([
45
+ ...Array.from({ length: 4 }, () => fs.promises.readFile('/issue-303.txt', 'utf8')),
46
+ fs.promises.writeFile('/issue-303.txt', contents),
47
+ ...Array.from({ length: 4 }, () => fs.promises.readFile('/issue-303.txt', 'utf8')),
48
+ ]);
49
+ const rejected = results.filter(r => r.status === 'rejected');
50
+ assert.deepEqual(rejected.map(r => r.reason.code ?? r.reason.message), []);
51
+ assert.equal(await fs.promises.readFile('/issue-303.txt', 'utf8'), contents);
52
+ }
53
+ });
54
+ test('closing a handle does not fail because another handle has unsynced writes #303', async () => {
55
+ await fs.promises.writeFile('/issue-303-close.txt', 'original');
56
+ const reader = await fs.promises.open('/issue-303-close.txt', 'r');
57
+ const writer = await fs.promises.open('/issue-303-close.txt', 'r+');
58
+ // The reader wrote nothing, so its close must not report the writer's dirty data as EBUSY
59
+ const [closed] = await Promise.allSettled([reader.close(), writer.write(new TextEncoder().encode('DIRTY'), 0, 5, 0)]);
60
+ assert.equal(closed.status, 'fulfilled');
61
+ await writer.close();
62
+ assert.equal(await fs.promises.readFile('/issue-303-close.txt', 'utf8'), 'DIRTYnal');
63
+ });
64
+ test('concurrent recursive mkdir of overlapping trees does not fail #308', async () => {
65
+ const paths = ['/308/a', '/308/a', '/308/a/b', '/308/a/b', '/308/c', '/308/c', '/308-2', '/308-2'];
66
+ const results = await Promise.allSettled(paths.map(path => fs.promises.mkdir(path, { recursive: true })));
67
+ const rejected = results.filter(r => r.status == 'rejected');
68
+ assert.deepEqual(rejected.map(r => r.reason.code ?? r.reason.message), []);
69
+ for (const path of paths)
70
+ assert((await fs.promises.stat(path)).isDirectory());
71
+ });
72
+ test('parallel recursive mkdir of the same path creates it once #308', async () => {
73
+ const results = await Promise.all(Array.from({ length: 8 }, () => fs.promises.mkdir('/308-same/nested', { recursive: true })));
74
+ assert.deepEqual(results.filter(result => result !== undefined), ['/308-same']);
75
+ });
76
+ });
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,83 @@
1
+ // SPDX-License-Identifier: LGPL-3.0-or-later
2
+ import { sync } from '@zenfs/core';
3
+ import assert, { rejects } from 'node:assert/strict';
4
+ import { suite, test } from 'node:test';
5
+ import { config, fs } from '../common.js';
6
+ const testFile = 'test-file.txt';
7
+ fs.writeFileSync(testFile, 'Sample content');
8
+ fs.mkdirSync('test-directory');
9
+ fs.symlinkSync(testFile, 'test-symlink');
10
+ const testDirPath = 'test-dir';
11
+ const testFiles = ['file1.txt', 'file2.txt'];
12
+ fs.mkdirSync(testDirPath);
13
+ for (const file of testFiles) {
14
+ fs.writeFileSync(`${testDirPath}/${file}`, 'Sample content');
15
+ }
16
+ await sync();
17
+ suite('Dir', config('directories'), () => {
18
+ test('read()', config('async'), async () => {
19
+ const dir = fs.opendirSync(testDirPath);
20
+ const dirent1 = await dir.read();
21
+ assert(dirent1 instanceof fs.Dirent);
22
+ assert(testFiles.includes(dirent1?.name));
23
+ const dirent2 = await dir.read();
24
+ assert(dirent2 instanceof fs.Dirent);
25
+ assert(testFiles.includes(dirent2?.name));
26
+ const dirent3 = await dir.read();
27
+ assert.equal(dirent3, null);
28
+ await dir.close();
29
+ });
30
+ test('readSync()', config('sync'), () => {
31
+ const dir = fs.opendirSync(testDirPath);
32
+ const dirent1 = dir.readSync();
33
+ assert(dirent1 instanceof fs.Dirent);
34
+ assert(testFiles.includes(dirent1?.name));
35
+ const dirent2 = dir.readSync();
36
+ assert(dirent2 instanceof fs.Dirent);
37
+ assert(testFiles.includes(dirent2?.name));
38
+ const dirent3 = dir.readSync();
39
+ assert.equal(dirent3, null);
40
+ dir.closeSync();
41
+ });
42
+ test('close()', config('async'), async () => {
43
+ const dir = fs.opendirSync(testDirPath);
44
+ await dir.close();
45
+ rejects(dir.read());
46
+ });
47
+ test('closeSync()', config('sync'), () => {
48
+ const dir = fs.opendirSync(testDirPath);
49
+ dir.closeSync();
50
+ assert.throws(() => dir.readSync());
51
+ });
52
+ test('asynchronous iteration', config('async'), async () => {
53
+ const dir = fs.opendirSync(testDirPath);
54
+ const dirents = [];
55
+ for await (const dirent of dir) {
56
+ dirents.push(dirent);
57
+ }
58
+ assert.equal(dirents.length, 2);
59
+ assert(dirents[0] instanceof fs.Dirent);
60
+ assert(testFiles.includes(dirents[0].name));
61
+ assert(testFiles.includes(dirents[1].name));
62
+ });
63
+ test('read after directory is closed', config('async'), async () => {
64
+ const dir = fs.opendirSync(testDirPath);
65
+ await dir.close();
66
+ await assert.rejects(dir.read());
67
+ });
68
+ test('readSync after directory is closed', config('sync'), () => {
69
+ const dir = fs.opendirSync(testDirPath);
70
+ dir.closeSync();
71
+ assert.throws(() => dir.readSync());
72
+ });
73
+ test('close multiple times', config('async'), async () => {
74
+ const dir = fs.opendirSync(testDirPath);
75
+ await dir.close();
76
+ await assert.rejects(dir.close(), { code: 'ERR_DIR_CLOSED' });
77
+ });
78
+ test('closeSync multiple times', config('sync'), () => {
79
+ const dir = fs.opendirSync(testDirPath);
80
+ dir.closeSync();
81
+ assert.throws(() => dir.closeSync(), { code: 'ERR_DIR_CLOSED' });
82
+ });
83
+ });
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,151 @@
1
+ // SPDX-License-Identifier: LGPL-3.0-or-later
2
+ import assert from 'node:assert/strict';
3
+ import { join } from 'node:path/posix';
4
+ import { suite, test } from 'node:test';
5
+ import { config, fs } from '../common.js';
6
+ const testDir = 'test-dir';
7
+ const testFiles = ['file1.txt', 'file2.txt', 'file3.txt'];
8
+ const testDirectories = ['subdir1', 'subdir2'];
9
+ await fs.promises.mkdir(testDir);
10
+ for (const file of testFiles) {
11
+ await fs.promises.writeFile(`${testDir}/${file}`, 'Sample content');
12
+ }
13
+ for (const dir of testDirectories) {
14
+ await fs.promises.mkdir(`${testDir}/${dir}`);
15
+ for (const file of ['file4.txt', 'file5.txt']) {
16
+ await fs.promises.writeFile(`${testDir}/${dir}/${file}`, 'Sample content');
17
+ }
18
+ }
19
+ suite('Directories', config('directories'), () => {
20
+ test('mkdir', config('async'), async () => {
21
+ await fs.promises.mkdir('/one', 0o755);
22
+ assert(await fs.promises.stat('/one', { throwIfNoEntry: false }));
23
+ await assert.rejects(fs.promises.mkdir('/one', 0o755), { code: 'EEXIST' });
24
+ });
25
+ test('mkdirSync', config('sync'), () => fs.mkdirSync('/two', 0o000));
26
+ test('mkdir, nested', config('async'), async () => {
27
+ await assert.rejects(fs.promises.mkdir('/nested/dir'), { code: 'ENOENT', path: '/nested/dir' });
28
+ assert(!(await fs.promises.stat('/nested/dir', { throwIfNoEntry: false })));
29
+ });
30
+ test('mkdir, recursive', config('async'), async () => {
31
+ assert.equal(await fs.promises.mkdir('/recursiveP/A/B', { recursive: true, mode: 0o755 }), '/recursiveP');
32
+ assert.equal(await fs.promises.mkdir('/recursiveP/A/B/C/D', { recursive: true, mode: 0o777 }), '/recursiveP/A/B/C');
33
+ assert.equal(await fs.promises.mkdir('/recursiveP/A/B/C/D', { recursive: true, mode: 0o700 }), undefined);
34
+ assert.equal((await fs.promises.stat('/recursiveP')).mode, fs.constants.S_IFDIR | 0o755);
35
+ assert.equal((await fs.promises.stat('/recursiveP/A')).mode, fs.constants.S_IFDIR | 0o755);
36
+ assert.equal((await fs.promises.stat('/recursiveP/A/B')).mode, fs.constants.S_IFDIR | 0o755);
37
+ assert.equal((await fs.promises.stat('/recursiveP/A/B/C')).mode, fs.constants.S_IFDIR | 0o777);
38
+ assert.equal((await fs.promises.stat('/recursiveP/A/B/C/D')).mode, fs.constants.S_IFDIR | 0o777);
39
+ });
40
+ test('mkdir, recursive with a file in the way', config('async'), async () => {
41
+ await fs.promises.writeFile('/recursiveF', 'Sample content');
42
+ await assert.rejects(fs.promises.mkdir('/recursiveF', { recursive: true }), { code: 'EEXIST', path: '/recursiveF' });
43
+ await assert.rejects(fs.promises.mkdir('/recursiveF/A/B', { recursive: true }), { code: 'ENOTDIR', path: '/recursiveF/A/B' });
44
+ });
45
+ test('mkdirSync, recursive', config('sync'), () => {
46
+ assert.equal(fs.mkdirSync('/recursiveS/A/B', { recursive: true, mode: 0o755 }), '/recursiveS');
47
+ assert.equal(fs.mkdirSync('/recursiveS/A/B/C/D', { recursive: true, mode: 0o777 }), '/recursiveS/A/B/C');
48
+ assert.equal(fs.mkdirSync('/recursiveS/A/B/C/D', { recursive: true, mode: 0o700 }), undefined);
49
+ assert.equal(fs.statSync('/recursiveS').mode, fs.constants.S_IFDIR | 0o755);
50
+ assert.equal(fs.statSync('/recursiveS/A').mode, fs.constants.S_IFDIR | 0o755);
51
+ assert.equal(fs.statSync('/recursiveS/A/B').mode, fs.constants.S_IFDIR | 0o755);
52
+ assert.equal(fs.statSync('/recursiveS/A/B/C').mode, fs.constants.S_IFDIR | 0o777);
53
+ assert.equal(fs.statSync('/recursiveS/A/B/C/D').mode, fs.constants.S_IFDIR | 0o777);
54
+ });
55
+ test('mkdirSync, recursive with a file in the way', config('sync'), () => {
56
+ fs.writeFileSync('/recursiveSF', 'Sample content');
57
+ assert.throws(() => fs.mkdirSync('/recursiveSF', { recursive: true }), { code: 'EEXIST', path: '/recursiveSF' });
58
+ assert.throws(() => fs.mkdirSync('/recursiveSF/A/B', { recursive: true }), { code: 'ENOTDIR', path: '/recursiveSF/A/B' });
59
+ });
60
+ test('rmdir (non-empty)', config('async'), async () => {
61
+ await fs.promises.mkdir('/rmdirTest');
62
+ await fs.promises.mkdir('/rmdirTest/rmdirTest2');
63
+ await assert.rejects(fs.promises.rmdir('/rmdirTest'), { code: 'ENOTEMPTY' });
64
+ });
65
+ test('readdirSync on file', config('sync'), () => {
66
+ assert.throws(() => fs.readdirSync('a.js'), { code: 'ENOTDIR' });
67
+ });
68
+ test('readdir on file', config('async'), async () => {
69
+ await assert.rejects(fs.promises.readdir('a.js'), { code: 'ENOTDIR' });
70
+ });
71
+ test('readdirSync on non-existent directory', config('sync'), () => {
72
+ assert.throws(() => fs.readdirSync('/does/not/exist'), { code: 'ENOENT' });
73
+ });
74
+ test('readdir on non-existent directory', config('async'), async () => {
75
+ await assert.rejects(fs.promises.readdir('/does/not/exist'), { code: 'ENOENT' });
76
+ });
77
+ test('rm recursively', config('async'), async () => {
78
+ await fs.promises.mkdir('/rmDirRecursively');
79
+ await fs.promises.mkdir('/rmDirRecursively/rmDirNested');
80
+ await fs.promises.writeFile('/rmDirRecursively/rmDirNested/test.txt', 'hello world!');
81
+ await fs.promises.rm('/rmDirRecursively', { recursive: true });
82
+ });
83
+ test('rmSync recursively', config('sync'), () => {
84
+ fs.mkdirSync('/rmDirRecursively');
85
+ fs.mkdirSync('/rmDirRecursively/rmDirNested');
86
+ fs.writeFileSync('/rmDirRecursively/rmDirNested/test.txt', 'hello world!');
87
+ fs.rmSync('/rmDirRecursively', { recursive: true });
88
+ });
89
+ test('readdir returns files and directories', config('async'), async () => {
90
+ const dirents = await fs.promises.readdir(testDir, { withFileTypes: true });
91
+ const files = dirents.filter(dirent => dirent.isFile()).map(dirent => dirent.name);
92
+ const dirs = dirents.filter(dirent => dirent.isDirectory()).map(dirent => dirent.name);
93
+ assert(testFiles.every(file => files.includes(file)));
94
+ assert(testDirectories.every(dir => dirs.includes(dir)));
95
+ });
96
+ test('readdirSync returns files and directories', config('sync'), () => {
97
+ const dirents = fs.readdirSync(testDir, { withFileTypes: true });
98
+ const files = dirents.filter(dirent => dirent.isFile()).map(dirent => dirent.name);
99
+ const dirs = dirents.filter(dirent => dirent.isDirectory()).map(dirent => dirent.name);
100
+ assert(testFiles.every(file => files.includes(file)));
101
+ assert(testDirectories.every(dir => dirs.includes(dir)));
102
+ });
103
+ test('readdir returns Dirent objects', config('async'), async () => {
104
+ const dirents = await fs.promises.readdir(testDir, { withFileTypes: true });
105
+ assert(dirents[0] instanceof fs.Dirent);
106
+ });
107
+ test('readdirSync returns Dirent objects', config('sync'), () => {
108
+ const dirents = fs.readdirSync(testDir, { withFileTypes: true });
109
+ assert(dirents[0] instanceof fs.Dirent);
110
+ });
111
+ test('readdir works without withFileTypes option', config('async'), async () => {
112
+ const files = await fs.promises.readdir(testDir);
113
+ assert(testFiles.every(entry => files.includes(entry)));
114
+ assert(testDirectories.every(entry => files.includes(entry)));
115
+ });
116
+ test('readdirSync works without withFileTypes option', config('sync'), () => {
117
+ const files = fs.readdirSync(testDir);
118
+ assert(testFiles.every(entry => files.includes(entry)));
119
+ assert(testDirectories.every(entry => files.includes(entry)));
120
+ });
121
+ test('readdir returns files recursively', config('async'), async () => {
122
+ const entries = await fs.promises.readdir(testDir, { recursive: true });
123
+ assert(entries.includes('file1.txt'));
124
+ assert(entries.includes('subdir1/file4.txt'));
125
+ assert(entries.includes('subdir2/file5.txt'));
126
+ });
127
+ test('readdir returns Dirent recursively', config('async'), async () => {
128
+ const entries = await fs.promises.readdir(testDir, { recursive: true, withFileTypes: true });
129
+ entries.sort((a, b) => join(a.parentPath, a.name).localeCompare(join(b.parentPath, b.name)));
130
+ const values = entries.map(entry => [entry.parentPath, entry.name]);
131
+ // `parentPath` is resolved against the directory that was read
132
+ assert.deepEqual(values[0], ['/' + testDir, 'file1.txt']);
133
+ assert.deepEqual(values[4], ['/' + testDir + '/subdir1', 'file4.txt']);
134
+ assert.deepEqual(values[8], ['/' + testDir + '/subdir2', 'file5.txt']);
135
+ });
136
+ test('readdirSync returns files recursively', config('sync'), () => {
137
+ const entries = fs.readdirSync(testDir, { recursive: true }).sort();
138
+ assert.equal(entries[0], 'file1.txt');
139
+ assert.equal(entries[4], 'subdir1/file4.txt');
140
+ assert.equal(entries[8], 'subdir2/file5.txt');
141
+ });
142
+ test('Cyrillic file names', config('sync'), () => {
143
+ fs.writeFileSync('/мой-файл.txt', 'HELLO!', 'utf-8');
144
+ assert(fs.readdirSync('/').includes('мой-файл.txt'));
145
+ });
146
+ test('re-creating a deleted path still shows up in readdir #305', config('sync'), () => {
147
+ fs.unlinkSync('/elipses.txt');
148
+ fs.writeFileSync('/elipses.txt', '...');
149
+ assert(fs.readdirSync('/').includes('elipses.txt'));
150
+ });
151
+ });
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,141 @@
1
+ // SPDX-License-Identifier: LGPL-3.0-or-later
2
+ import assert from 'node:assert/strict';
3
+ import test, { suite } from 'node:test';
4
+ import { config, fs } from '../common.js';
5
+ const existingFile = '/exit.js';
6
+ const path = '/non-existent';
7
+ const cases = [
8
+ {
9
+ name: 'stat',
10
+ code: 'ENOENT',
11
+ syscall: 'stat',
12
+ path,
13
+ sync: () => fs.statSync(path),
14
+ async: () => fs.promises.stat(path),
15
+ },
16
+ {
17
+ name: 'lstat',
18
+ code: 'ENOENT',
19
+ syscall: 'lstat',
20
+ path,
21
+ sync: () => fs.lstatSync(path),
22
+ async: () => fs.promises.lstat(path),
23
+ },
24
+ {
25
+ name: 'mkdir',
26
+ code: 'EEXIST',
27
+ syscall: 'mkdir',
28
+ path: existingFile,
29
+ sync: () => fs.mkdirSync(existingFile, 0o666),
30
+ async: () => fs.promises.mkdir(existingFile, 0o666),
31
+ },
32
+ {
33
+ name: 'rmdir (missing)',
34
+ code: 'ENOENT',
35
+ syscall: 'rmdir',
36
+ path,
37
+ sync: () => fs.rmdirSync(path),
38
+ async: () => fs.promises.rmdir(path),
39
+ },
40
+ {
41
+ name: 'rmdir (not a directory)',
42
+ code: 'ENOTDIR',
43
+ syscall: 'rmdir',
44
+ path: existingFile,
45
+ sync: () => fs.rmdirSync(existingFile),
46
+ async: () => fs.promises.rmdir(existingFile),
47
+ },
48
+ {
49
+ name: 'rename',
50
+ code: 'ENOENT',
51
+ syscall: 'rename',
52
+ path,
53
+ dest: '/foo',
54
+ sync: () => fs.renameSync(path, 'foo'),
55
+ async: () => fs.promises.rename(path, 'foo'),
56
+ },
57
+ {
58
+ name: 'open',
59
+ code: 'ENOENT',
60
+ syscall: 'open',
61
+ path,
62
+ sync: () => fs.openSync(path, 'r'),
63
+ async: () => fs.promises.open(path, 'r'),
64
+ },
65
+ {
66
+ // Note Node attributes `readdir` failures to `scandir`
67
+ name: 'readdir',
68
+ code: 'ENOENT',
69
+ syscall: 'scandir',
70
+ path,
71
+ sync: () => fs.readdirSync(path),
72
+ async: () => fs.promises.readdir(path),
73
+ },
74
+ {
75
+ name: 'unlink',
76
+ code: 'ENOENT',
77
+ syscall: 'unlink',
78
+ path,
79
+ sync: () => fs.unlinkSync(path),
80
+ async: () => fs.promises.unlink(path),
81
+ },
82
+ {
83
+ // Note `path` is the existing file and `dest` is the new link
84
+ name: 'link',
85
+ code: 'ENOENT',
86
+ syscall: 'link',
87
+ path,
88
+ dest: '/foo',
89
+ sync: () => fs.linkSync(path, 'foo'),
90
+ async: () => fs.promises.link(path, 'foo'),
91
+ },
92
+ {
93
+ name: 'chmod',
94
+ code: 'ENOENT',
95
+ syscall: 'chmod',
96
+ path,
97
+ sync: () => fs.chmodSync(path, 0o666),
98
+ async: () => fs.promises.chmod(path, 0o666),
99
+ },
100
+ {
101
+ name: 'readlink',
102
+ code: 'ENOENT',
103
+ syscall: 'readlink',
104
+ path,
105
+ sync: () => fs.readlinkSync(path),
106
+ async: () => fs.promises.readlink(path),
107
+ },
108
+ ];
109
+ /**
110
+ * Checks an error against the expected code, syscall, path, and message.
111
+ *
112
+ * The message is only matched at the start, since the description text for an errno
113
+ * is not standardized and implementations may add more information at the end.
114
+ */
115
+ function check(error, c) {
116
+ const e = error;
117
+ assert.equal(e.code, c.code);
118
+ assert.equal(e.syscall, c.syscall);
119
+ assert.equal(e.path, c.path);
120
+ if (c.dest)
121
+ assert.equal(e.dest, c.dest);
122
+ // e.g. `ENOENT: no such file or directory, stat '/non-existent'`
123
+ const expected = new RegExp(`^${c.code}: .+, ${c.syscall} '${c.path}'` + (c.dest ? ` -> '${c.dest}'` : ''));
124
+ assert.match(e.message, expected);
125
+ }
126
+ suite('Error messages', () => {
127
+ for (const c of cases) {
128
+ test(c.name, config('async'), async () => {
129
+ await assert.rejects(c.async, (error) => {
130
+ check(error, c);
131
+ return true;
132
+ });
133
+ });
134
+ test(c.name + 'Sync', config('sync'), () => {
135
+ assert.throws(c.sync, (error) => {
136
+ check(error, c);
137
+ return true;
138
+ });
139
+ });
140
+ }
141
+ });
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,19 @@
1
+ // SPDX-License-Identifier: LGPL-3.0-or-later
2
+ import assert from 'node:assert/strict';
3
+ import { suite, test } from 'node:test';
4
+ import { config, fs } from '../common.js';
5
+ suite('exists', () => {
6
+ const f = 'x.txt';
7
+ test('return true for an existing file', config('async', 'promises.exists'), async () => {
8
+ const exists = await fs.promises.exists(f);
9
+ assert(exists);
10
+ });
11
+ test('return false for a non-existent file', config('async', 'promises.exists'), async () => {
12
+ const exists = await fs.promises.exists(f + '-NO');
13
+ assert(!exists);
14
+ });
15
+ test('have sync methods that behave the same', config('sync'), () => {
16
+ assert(fs.existsSync(f));
17
+ assert(!fs.existsSync(f + '-NO'));
18
+ });
19
+ });