Skip to content

Commit 335fe73

Browse files
committed
Cleaned up FS tests
1 parent 53d58db commit 335fe73

13 files changed

+147
-235
lines changed

tests/fs/append.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@ import { fs } from '../common.js';
55
const content = 'Sample content',
66
original = 'ABCD';
77

8-
suite('appendFile', () => {
9-
test('create an empty file and add content', async () => {
8+
suite('Appends', () => {
9+
test('Create an empty file and add content', async () => {
1010
const filename = 'append.txt';
1111
await fs.promises.appendFile(filename, content);
1212
const data = await fs.promises.readFile(filename, 'utf8');
1313
assert.equal(data, content);
1414
});
1515

16-
test('append data to a non-empty file', async () => {
16+
test('Append data to a non-empty file', async () => {
1717
const filename = 'append2.txt';
1818

1919
await fs.promises.writeFile(filename, original);
@@ -22,7 +22,7 @@ suite('appendFile', () => {
2222
assert.equal(data, original + content);
2323
});
2424

25-
test('append a buffer to the file', async () => {
25+
test('Append a buffer to the file', async () => {
2626
const filename = 'append3.txt';
2727

2828
await fs.promises.writeFile(filename, original);

tests/fs/directory.test.ts

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -70,28 +70,28 @@ suite('Directories', () => {
7070
assert.rejects(fs.promises.readdir('a.js'), { code: 'ENOTDIR' });
7171
});
7272

73-
test('readdirSync on non-existant directory', () => {
73+
test('readdirSync on non-existent directory', () => {
7474
assert.throws(() => fs.readdirSync('/does/not/exist'), { code: 'ENOENT' });
7575
});
7676

77-
test('readdir on non-existant directory', () => {
77+
test('readdir on non-existent directory', () => {
7878
assert.rejects(fs.promises.readdir('/does/not/exist'), { code: 'ENOENT' });
7979
});
8080

81-
test('rm recursively asynchronously', async () => {
82-
await fs.promises.mkdir('/rmDirRecusrively');
83-
await fs.promises.mkdir('/rmDirRecusrively/rmDirNested');
84-
await fs.promises.writeFile('/rmDirRecusrively/rmDirNested/test.txt', 'hello world!');
81+
test('rm recursively', async () => {
82+
await fs.promises.mkdir('/rmDirRecursively');
83+
await fs.promises.mkdir('/rmDirRecursively/rmDirNested');
84+
await fs.promises.writeFile('/rmDirRecursively/rmDirNested/test.txt', 'hello world!');
8585

86-
await fs.promises.rm('/rmDirRecusrively', { recursive: true });
86+
await fs.promises.rm('/rmDirRecursively', { recursive: true });
8787
});
8888

89-
test('rm recursively synchronously', () => {
90-
fs.mkdirSync('/rmDirRecusrively');
91-
fs.mkdirSync('/rmDirRecusrively/rmDirNested');
92-
fs.writeFileSync('/rmDirRecusrively/rmDirNested/test.txt', 'hello world!');
89+
test('rmSync recursively', () => {
90+
fs.mkdirSync('/rmDirRecursively');
91+
fs.mkdirSync('/rmDirRecursively/rmDirNested');
92+
fs.writeFileSync('/rmDirRecursively/rmDirNested/test.txt', 'hello world!');
9393

94-
fs.rmSync('/rmDirRecusrively', { recursive: true });
94+
fs.rmSync('/rmDirRecursively', { recursive: true });
9595
});
9696

9797
test('readdir returns files and directories', async () => {
@@ -143,17 +143,17 @@ suite('Directories', () => {
143143

144144
test('readdir returns Dirent recursively', async () => {
145145
const entries = await fs.promises.readdir(testDir, { recursive: true, withFileTypes: true });
146-
assert(entries.find(entry => entry.path === 'file1.txt'));
147-
assert(entries.find(entry => entry.path === 'subdir1/file4.txt'));
148-
assert(entries.find(entry => entry.path === 'subdir2/file5.txt'));
146+
const paths = entries.map(entry => entry.path).sort();
147+
assert.equal(paths[0], 'file1.txt');
148+
assert.equal(paths[4], 'subdir1/file4.txt');
149+
assert.equal(paths[8], 'subdir2/file5.txt');
149150
});
150151

151-
// New test for readdirSync with recursive: true
152152
test('readdirSync returns files recursively', () => {
153-
const entries = fs.readdirSync(testDir, { recursive: true });
154-
assert(entries.includes('file1.txt'));
155-
assert(entries.includes('subdir1/file4.txt'));
156-
assert(entries.includes('subdir2/file5.txt'));
153+
const entries = fs.readdirSync(testDir, { recursive: true }).sort();
154+
assert.equal(entries[0], 'file1.txt');
155+
assert.equal(entries[4], 'subdir1/file4.txt');
156+
assert.equal(entries[8], 'subdir2/file5.txt');
157157
});
158158

159159
test('Cyrillic file names', () => {

tests/fs/errors.test.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,20 @@
11
import assert from 'node:assert/strict';
22
import test, { suite } from 'node:test';
33
import { fs } from '../common.js';
4-
import type { ErrnoError } from '../../dist/index.js';
54

65
const existingFile = '/exit.js';
76

87
suite('Error messages', () => {
98
const path = '/non-existent';
109

11-
fs.promises.stat(path).catch((error: ErrnoError) => {
12-
assert.equal(error.bufferSize(), 4 + JSON.stringify(error.toJSON()).length);
13-
});
14-
1510
const missing = { path, code: 'ENOENT' };
16-
const existing = { path: existingFile };
11+
const existing = { path: existingFile, code: 'EEXIST' };
12+
const notDir = { path: existingFile, code: 'ENOTDIR' };
1713

1814
test('stat', () => assert.rejects(() => fs.promises.stat(path), missing));
1915
test('mkdir', () => assert.rejects(() => fs.promises.mkdir(existingFile, 0o666), existing));
2016
test('rmdir', () => assert.rejects(() => fs.promises.rmdir(path), missing));
21-
test('rmdir', () => assert.rejects(() => fs.promises.rmdir(existingFile), existing));
17+
test('rmdir', () => assert.rejects(() => fs.promises.rmdir(existingFile), notDir));
2218
test('rename', () => assert.rejects(() => fs.promises.rename(path, 'foo'), missing));
2319
test('open', () => assert.rejects(() => fs.promises.open(path, 'r'), missing));
2420
test('readdir', () => assert.rejects(() => fs.promises.readdir(path), missing));
@@ -30,7 +26,7 @@ suite('Error messages', () => {
3026
test('statSync', () => assert.throws(() => fs.statSync(path), missing));
3127
test('mkdirSync', () => assert.throws(() => fs.mkdirSync(existingFile, 0o666), existing));
3228
test('rmdirSync', () => assert.throws(() => fs.rmdirSync(path), missing));
33-
test('rmdirSync', () => assert.throws(() => fs.rmdirSync(existingFile), existing));
29+
test('rmdirSync', () => assert.throws(() => fs.rmdirSync(existingFile), notDir));
3430
test('renameSync', () => assert.throws(() => fs.renameSync(path, 'foo'), missing));
3531
test('openSync', () => assert.throws(() => fs.openSync(path, 'r'), missing));
3632
test('readdirSync', () => assert.throws(() => fs.readdirSync(path), missing));

tests/fs/open.test.ts

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,13 @@
11
import assert from 'node:assert/strict';
22
import { suite, test } from 'node:test';
3-
import { ErrnoError } from '../../dist/index.js';
43
import { fs } from '../common.js';
54

6-
suite('fs file opening', () => {
5+
suite('Opening files', () => {
76
const filename = 'a.js';
87

9-
test('throw ENOENT when opening non-existent file (sync)', () => {
10-
let caughtException = false;
11-
try {
12-
fs.openSync('/path/to/file/that/does/not/exist', 'r');
13-
} catch (error: any) {
14-
assert(error instanceof ErrnoError);
15-
assert.equal(error?.code, 'ENOENT');
16-
caughtException = true;
17-
}
18-
assert(caughtException);
19-
});
20-
21-
test('throw ENOENT when opening non-existent file (async)', async () => {
22-
try {
23-
await fs.promises.open('/path/to/file/that/does/not/exist', 'r');
24-
} catch (error: any) {
25-
assert(error instanceof ErrnoError);
26-
assert.equal(error?.code, 'ENOENT');
27-
}
8+
test('throw ENOENT when opening non-existent file', () => {
9+
assert.throws(() => fs.openSync('/path/to/file/that/does/not/exist', 'r'), { code: 'ENOENT' });
10+
assert.rejects(fs.promises.open('/path/to/file/that/does/not/exist', 'r'), { code: 'ENOENT' });
2811
});
2912

3013
test('open file with mode "r"', async () => {

tests/fs/permissions.test.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,12 @@ suite('Permissions', () => {
5252
assert(error instanceof ErrnoError);
5353
assert.equal(error.code, 'EACCES');
5454
});
55-
if (!stats) {
56-
return;
57-
}
55+
if (!stats) return;
5856
assert(stats.hasAccess(X_OK));
5957

6058
function checkError(access: number) {
6159
return function (error: ErrnoError) {
6260
assert(error instanceof ErrnoError);
63-
assert(error);
6461
assert(!stats!.hasAccess(access));
6562
};
6663
}
@@ -80,12 +77,10 @@ suite('Permissions', () => {
8077
await fs.promises.unlink(testFile).catch(checkError(W_OK));
8178
} else {
8279
const handle = await fs.promises.open(path, 'a').catch(checkError(W_OK));
83-
if (!handle) {
84-
return;
85-
}
80+
if (!handle) return;
8681
await handle.close();
8782
}
88-
assert(stats.hasAccess(R_OK));
83+
assert(stats.hasAccess(W_OK));
8984
}
9085

9186
const copy = { ...defaultContext.credentials };

tests/fs/read.test.ts

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,33 +22,26 @@ suite('read', () => {
2222
assert.equal(bytesRead, expected.length);
2323
assert.equal(buffer.toString(), expected);
2424
});
25-
});
2625

27-
suite('read binary', () => {
2826
test('Read a file and check its binary bytes (asynchronous)', async () => {
2927
const buff = await fs.promises.readFile('elipses.txt');
28+
const buff2 = fs.readFileSync('elipses.txt');
29+
assert.equal(buff.toString(), buff2.toString());
3030
assert.equal((buff[1] << 8) | buff[0], 32994);
3131
});
3232

33-
test('Read a file and check its binary bytes (synchronous)', () => {
34-
const buff = fs.readFileSync('elipses.txt');
35-
assert.equal((buff[1] << 8) | buff[0], 32994);
36-
});
37-
});
38-
39-
suite('read buffer', () => {
4033
const bufferAsync = Buffer.alloc(expected.length);
4134
const bufferSync = Buffer.alloc(expected.length);
4235

43-
test('read file asynchronously', async () => {
36+
test('read file from handle asynchronously', async () => {
4437
const handle = await fs.promises.open(filepath, 'r');
4538
const { bytesRead } = await handle.read(bufferAsync, 0, expected.length, 0);
4639

4740
assert.equal(bytesRead, expected.length);
4841
assert.equal(bufferAsync.toString(), expected);
4942
});
5043

51-
test('read file synchronously', () => {
44+
test('read file from handle synchronously', () => {
5245
const fd = fs.openSync(filepath, 'r');
5346
const bytesRead = fs.readSync(fd, bufferSync, 0, expected.length, 0);
5447

tests/fs/readFile.test.ts

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,15 @@ import { fs } from '../common.js';
44

55
suite('Reading', () => {
66
test('Cannot read a file with an invalid encoding', () => {
7-
assert.throws(() => fs.readFileSync('a.js', 'wrongencoding' as BufferEncoding));
7+
assert.throws(() => fs.readFileSync('a.js', 'wrong-encoding' as BufferEncoding));
88
});
99

1010
test('Reading past the end of a file should not be an error', async () => {
1111
const handle = await fs.promises.open('a.js', 'r');
1212
const { bytesRead } = await handle.read(new Uint8Array(10), 0, 10, 10000);
1313
assert.equal(bytesRead, 0);
1414
});
15-
});
1615

17-
suite('Read and Unlink', () => {
1816
const dir = 'test-readfile-unlink';
1917
const file = 'test-readfile-unlink/test.bin';
2018
const data = new Uint8Array(512).fill(42);
@@ -34,33 +32,19 @@ suite('Read and Unlink', () => {
3432
await fs.promises.unlink(file);
3533
await fs.promises.rmdir(dir);
3634
});
37-
});
3835

39-
suite('Read File', () => {
40-
const fn = 'empty.txt';
41-
42-
test('read file asynchronously', async () => {
43-
const data: Uint8Array = await fs.promises.readFile(fn);
44-
assert(data != undefined);
45-
});
36+
const fileName = 'empty.txt';
4637

47-
test('read file with utf-8 encoding asynchronously', async () => {
48-
const data: string = await fs.promises.readFile(fn, 'utf8');
49-
assert.equal(data, '');
38+
test('read file', async () => {
39+
assert.equal((await fs.promises.readFile(fileName)).toString(), '');
40+
assert.equal(fs.readFileSync(fileName).toString(), '');
5041
});
5142

52-
test('read file synchronously', () => {
53-
const data: Uint8Array = fs.readFileSync(fn);
54-
assert(data != undefined);
43+
test('read file with utf-8 encoding', async () => {
44+
assert.equal(await fs.promises.readFile(fileName, 'utf8'), '');
45+
assert.equal(fs.readFileSync(fileName, 'utf8'), '');
5546
});
5647

57-
test('read file with utf-8 encoding synchronously', () => {
58-
const data: string = fs.readFileSync(fn, 'utf8');
59-
assert.equal(data, '');
60-
});
61-
});
62-
63-
suite('fs file reading', () => {
6448
test('read file synchronously and verify the content', () => {
6549
const content = fs.readFileSync('elipses.txt', 'utf8');
6650

tests/fs/rename.test.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import assert from 'node:assert/strict';
22
import { suite, test } from 'node:test';
3-
import { ErrnoError } from '../../dist/index.js';
43
import { fs } from '../common.js';
54

65
suite('Rename', () => {
@@ -77,10 +76,8 @@ suite('Rename', () => {
7776
await fs.promises.mkdir(dir);
7877
await fs.promises.writeFile(file, 'file contents go here');
7978

80-
await fs.promises.rename(file, dir).catch((error: ErrnoError) => {
81-
assert(error instanceof ErrnoError);
82-
assert.match(error.code, /EISDIR|EPERM/);
83-
});
79+
assert.rejects(fs.promises.rename(file, dir), { code: /EISDIR|EPERM/ });
80+
assert.throws(() => fs.renameSync(file, dir), { code: /EISDIR|EPERM/ });
8481
});
8582

8683
test('rename directory inside itself', async () => {
@@ -89,9 +86,7 @@ suite('Rename', () => {
8986

9087
await fs.promises.mkdir(renDir1);
9188

92-
await fs.promises.rename(renDir1, renDir2).catch((error: ErrnoError) => {
93-
assert(error instanceof ErrnoError);
94-
assert.equal(error.code, 'EBUSY');
95-
});
89+
assert.rejects(fs.promises.rename(renDir1, renDir2), { code: 'EBUSY' });
90+
assert.throws(() => fs.renameSync(renDir1, renDir2), { code: 'EBUSY' });
9691
});
9792
});

tests/fs/times.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export function unixTimestamps(stats: StatsLike<number>): Record<'atime' | 'mtim
1919
};
2020
}
2121

22-
suite('times', () => {
22+
suite('Times', () => {
2323
async function runTest(atime: Date | number, mtime: Date | number): Promise<void> {
2424
const times = {
2525
atime: typeof atime == 'number' ? Math.floor(atime) : atime.getTime(),

0 commit comments

Comments
 (0)