Skip to content

Commit

Permalink
test: verify errors thrown from fs stat APIs
Browse files Browse the repository at this point in the history
PR-URL: #17914
Reviewed-By: Matteo Collina <[email protected]>
Reviewed-By: James M Snell <[email protected]>
  • Loading branch information
joyeecheung committed Jan 16, 2018
1 parent 8c00a80 commit 5eccbb0
Showing 1 changed file with 85 additions and 42 deletions.
127 changes: 85 additions & 42 deletions test/parallel/test-fs-error-messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
// USE OR OTHER DEALINGS IN THE SOFTWARE.

'use strict';
require('../common');
const common = require('../common');
const fixtures = require('../common/fixtures');
const assert = require('assert');
const fs = require('fs');
Expand All @@ -29,72 +29,95 @@ const existingFile = fixtures.path('exit.js');
const existingFile2 = fixtures.path('create-file.js');
const existingDir = fixtures.path('empty');
const existingDir2 = fixtures.path('keys');
const uv = process.binding('uv');

// ASYNC_CALL

fs.stat(fn, function(err) {
fs.stat(fn, common.mustCall((err) => {
assert.strictEqual(fn, err.path);
assert.ok(err.message.includes(fn));
});

fs.lstat(fn, function(err) {
assert.ok(err.message.includes(fn));
});
assert.strictEqual(
err.message,
`ENOENT: no such file or directory, stat '${fn}'`);
assert.strictEqual(err.errno, uv.UV_ENOENT);
assert.strictEqual(err.code, 'ENOENT');
assert.strictEqual(err.syscall, 'stat');
}));

fs.lstat(fn, common.mustCall((err) => {
assert.strictEqual(fn, err.path);
assert.strictEqual(
err.message,
`ENOENT: no such file or directory, lstat '${fn}'`);
assert.strictEqual(err.errno, uv.UV_ENOENT);
assert.strictEqual(err.code, 'ENOENT');
assert.strictEqual(err.syscall, 'lstat');
}));

{
const fd = fs.openSync(existingFile, 'r');
fs.closeSync(fd);
fs.fstat(fd, common.mustCall((err) => {
assert.strictEqual(err.message, 'EBADF: bad file descriptor, fstat');
assert.strictEqual(err.errno, uv.UV_EBADF);
assert.strictEqual(err.code, 'EBADF');
assert.strictEqual(err.syscall, 'fstat');
}));
}

fs.readlink(fn, function(err) {
fs.readlink(fn, common.mustCall((err) => {
assert.ok(err.message.includes(fn));
});
}));

fs.link(fn, 'foo', function(err) {
fs.link(fn, 'foo', common.mustCall((err) => {
assert.ok(err.message.includes(fn));
});
}));

fs.link(existingFile, existingFile2, function(err) {
fs.link(existingFile, existingFile2, common.mustCall((err) => {
assert.ok(err.message.includes(existingFile));
assert.ok(err.message.includes(existingFile2));
});
}));

fs.symlink(existingFile, existingFile2, function(err) {
fs.symlink(existingFile, existingFile2, common.mustCall((err) => {
assert.ok(err.message.includes(existingFile));
assert.ok(err.message.includes(existingFile2));
});
}));

fs.unlink(fn, function(err) {
fs.unlink(fn, common.mustCall((err) => {
assert.ok(err.message.includes(fn));
});
}));

fs.rename(fn, 'foo', function(err) {
fs.rename(fn, 'foo', common.mustCall((err) => {
assert.ok(err.message.includes(fn));
});
}));

fs.rename(existingDir, existingDir2, function(err) {
fs.rename(existingDir, existingDir2, common.mustCall((err) => {
assert.ok(err.message.includes(existingDir));
assert.ok(err.message.includes(existingDir2));
});
}));

fs.rmdir(fn, function(err) {
fs.rmdir(fn, common.mustCall((err) => {
assert.ok(err.message.includes(fn));
});
}));

fs.mkdir(existingFile, 0o666, function(err) {
fs.mkdir(existingFile, 0o666, common.mustCall((err) => {
assert.ok(err.message.includes(existingFile));
});
}));

fs.rmdir(existingFile, function(err) {
fs.rmdir(existingFile, common.mustCall((err) => {
assert.ok(err.message.includes(existingFile));
});
}));

fs.chmod(fn, 0o666, function(err) {
fs.chmod(fn, 0o666, common.mustCall((err) => {
assert.ok(err.message.includes(fn));
});
}));

fs.open(fn, 'r', 0o666, function(err) {
fs.open(fn, 'r', 0o666, common.mustCall((err) => {
assert.ok(err.message.includes(fn));
});
}));

fs.readFile(fn, function(err) {
fs.readFile(fn, common.mustCall((err) => {
assert.ok(err.message.includes(fn));
});
}));

// Sync

Expand All @@ -106,7 +129,13 @@ try {
fs.statSync(fn);
} catch (err) {
errors.push('stat');
assert.ok(err.message.includes(fn));
assert.strictEqual(fn, err.path);
assert.strictEqual(
err.message,
`ENOENT: no such file or directory, stat '${fn}'`);
assert.strictEqual(err.errno, uv.UV_ENOENT);
assert.strictEqual(err.code, 'ENOENT');
assert.strictEqual(err.syscall, 'stat');
}

try {
Expand All @@ -130,7 +159,26 @@ try {
fs.lstatSync(fn);
} catch (err) {
errors.push('lstat');
assert.ok(err.message.includes(fn));
assert.strictEqual(fn, err.path);
assert.strictEqual(
err.message,
`ENOENT: no such file or directory, lstat '${fn}'`);
assert.strictEqual(err.errno, uv.UV_ENOENT);
assert.strictEqual(err.code, 'ENOENT');
assert.strictEqual(err.syscall, 'lstat');
}

try {
++expected;
const fd = fs.openSync(existingFile, 'r');
fs.closeSync(fd);
fs.fstatSync(fd);
} catch (err) {
errors.push('fstat');
assert.strictEqual(err.message, 'EBADF: bad file descriptor, fstat');
assert.strictEqual(err.errno, uv.UV_EBADF);
assert.strictEqual(err.code, 'EBADF');
assert.strictEqual(err.syscall, 'fstat');
}

try {
Expand Down Expand Up @@ -224,9 +272,4 @@ try {
assert.ok(err.message.includes(fn));
}

process.on('exit', function() {
assert.strictEqual(
expected, errors.length,
`Test fs sync exceptions raised, got ${errors.length} expected ${expected}`
);
});
assert.strictEqual(expected, errors.length);

0 comments on commit 5eccbb0

Please sign in to comment.