Skip to content

Commit 5eccbb0

Browse files
committed
test: verify errors thrown from fs stat APIs
PR-URL: #17914 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 8c00a80 commit 5eccbb0

File tree

1 file changed

+85
-42
lines changed

1 file changed

+85
-42
lines changed

test/parallel/test-fs-error-messages.js

Lines changed: 85 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
// USE OR OTHER DEALINGS IN THE SOFTWARE.
2121

2222
'use strict';
23-
require('../common');
23+
const common = require('../common');
2424
const fixtures = require('../common/fixtures');
2525
const assert = require('assert');
2626
const fs = require('fs');
@@ -29,72 +29,95 @@ const existingFile = fixtures.path('exit.js');
2929
const existingFile2 = fixtures.path('create-file.js');
3030
const existingDir = fixtures.path('empty');
3131
const existingDir2 = fixtures.path('keys');
32+
const uv = process.binding('uv');
3233

3334
// ASYNC_CALL
3435

35-
fs.stat(fn, function(err) {
36+
fs.stat(fn, common.mustCall((err) => {
3637
assert.strictEqual(fn, err.path);
37-
assert.ok(err.message.includes(fn));
38-
});
39-
40-
fs.lstat(fn, function(err) {
41-
assert.ok(err.message.includes(fn));
42-
});
38+
assert.strictEqual(
39+
err.message,
40+
`ENOENT: no such file or directory, stat '${fn}'`);
41+
assert.strictEqual(err.errno, uv.UV_ENOENT);
42+
assert.strictEqual(err.code, 'ENOENT');
43+
assert.strictEqual(err.syscall, 'stat');
44+
}));
45+
46+
fs.lstat(fn, common.mustCall((err) => {
47+
assert.strictEqual(fn, err.path);
48+
assert.strictEqual(
49+
err.message,
50+
`ENOENT: no such file or directory, lstat '${fn}'`);
51+
assert.strictEqual(err.errno, uv.UV_ENOENT);
52+
assert.strictEqual(err.code, 'ENOENT');
53+
assert.strictEqual(err.syscall, 'lstat');
54+
}));
55+
56+
{
57+
const fd = fs.openSync(existingFile, 'r');
58+
fs.closeSync(fd);
59+
fs.fstat(fd, common.mustCall((err) => {
60+
assert.strictEqual(err.message, 'EBADF: bad file descriptor, fstat');
61+
assert.strictEqual(err.errno, uv.UV_EBADF);
62+
assert.strictEqual(err.code, 'EBADF');
63+
assert.strictEqual(err.syscall, 'fstat');
64+
}));
65+
}
4366

44-
fs.readlink(fn, function(err) {
67+
fs.readlink(fn, common.mustCall((err) => {
4568
assert.ok(err.message.includes(fn));
46-
});
69+
}));
4770

48-
fs.link(fn, 'foo', function(err) {
71+
fs.link(fn, 'foo', common.mustCall((err) => {
4972
assert.ok(err.message.includes(fn));
50-
});
73+
}));
5174

52-
fs.link(existingFile, existingFile2, function(err) {
75+
fs.link(existingFile, existingFile2, common.mustCall((err) => {
5376
assert.ok(err.message.includes(existingFile));
5477
assert.ok(err.message.includes(existingFile2));
55-
});
78+
}));
5679

57-
fs.symlink(existingFile, existingFile2, function(err) {
80+
fs.symlink(existingFile, existingFile2, common.mustCall((err) => {
5881
assert.ok(err.message.includes(existingFile));
5982
assert.ok(err.message.includes(existingFile2));
60-
});
83+
}));
6184

62-
fs.unlink(fn, function(err) {
85+
fs.unlink(fn, common.mustCall((err) => {
6386
assert.ok(err.message.includes(fn));
64-
});
87+
}));
6588

66-
fs.rename(fn, 'foo', function(err) {
89+
fs.rename(fn, 'foo', common.mustCall((err) => {
6790
assert.ok(err.message.includes(fn));
68-
});
91+
}));
6992

70-
fs.rename(existingDir, existingDir2, function(err) {
93+
fs.rename(existingDir, existingDir2, common.mustCall((err) => {
7194
assert.ok(err.message.includes(existingDir));
7295
assert.ok(err.message.includes(existingDir2));
73-
});
96+
}));
7497

75-
fs.rmdir(fn, function(err) {
98+
fs.rmdir(fn, common.mustCall((err) => {
7699
assert.ok(err.message.includes(fn));
77-
});
100+
}));
78101

79-
fs.mkdir(existingFile, 0o666, function(err) {
102+
fs.mkdir(existingFile, 0o666, common.mustCall((err) => {
80103
assert.ok(err.message.includes(existingFile));
81-
});
104+
}));
82105

83-
fs.rmdir(existingFile, function(err) {
106+
fs.rmdir(existingFile, common.mustCall((err) => {
84107
assert.ok(err.message.includes(existingFile));
85-
});
108+
}));
86109

87-
fs.chmod(fn, 0o666, function(err) {
110+
fs.chmod(fn, 0o666, common.mustCall((err) => {
88111
assert.ok(err.message.includes(fn));
89-
});
112+
}));
90113

91-
fs.open(fn, 'r', 0o666, function(err) {
114+
fs.open(fn, 'r', 0o666, common.mustCall((err) => {
92115
assert.ok(err.message.includes(fn));
93-
});
116+
}));
94117

95-
fs.readFile(fn, function(err) {
118+
fs.readFile(fn, common.mustCall((err) => {
96119
assert.ok(err.message.includes(fn));
97-
});
120+
}));
98121

99122
// Sync
100123

@@ -106,7 +129,13 @@ try {
106129
fs.statSync(fn);
107130
} catch (err) {
108131
errors.push('stat');
109-
assert.ok(err.message.includes(fn));
132+
assert.strictEqual(fn, err.path);
133+
assert.strictEqual(
134+
err.message,
135+
`ENOENT: no such file or directory, stat '${fn}'`);
136+
assert.strictEqual(err.errno, uv.UV_ENOENT);
137+
assert.strictEqual(err.code, 'ENOENT');
138+
assert.strictEqual(err.syscall, 'stat');
110139
}
111140

112141
try {
@@ -130,7 +159,26 @@ try {
130159
fs.lstatSync(fn);
131160
} catch (err) {
132161
errors.push('lstat');
133-
assert.ok(err.message.includes(fn));
162+
assert.strictEqual(fn, err.path);
163+
assert.strictEqual(
164+
err.message,
165+
`ENOENT: no such file or directory, lstat '${fn}'`);
166+
assert.strictEqual(err.errno, uv.UV_ENOENT);
167+
assert.strictEqual(err.code, 'ENOENT');
168+
assert.strictEqual(err.syscall, 'lstat');
169+
}
170+
171+
try {
172+
++expected;
173+
const fd = fs.openSync(existingFile, 'r');
174+
fs.closeSync(fd);
175+
fs.fstatSync(fd);
176+
} catch (err) {
177+
errors.push('fstat');
178+
assert.strictEqual(err.message, 'EBADF: bad file descriptor, fstat');
179+
assert.strictEqual(err.errno, uv.UV_EBADF);
180+
assert.strictEqual(err.code, 'EBADF');
181+
assert.strictEqual(err.syscall, 'fstat');
134182
}
135183

136184
try {
@@ -224,9 +272,4 @@ try {
224272
assert.ok(err.message.includes(fn));
225273
}
226274

227-
process.on('exit', function() {
228-
assert.strictEqual(
229-
expected, errors.length,
230-
`Test fs sync exceptions raised, got ${errors.length} expected ${expected}`
231-
);
232-
});
275+
assert.strictEqual(expected, errors.length);

0 commit comments

Comments
 (0)