From aa1f9055696c752ee28719f40815dc06dc5b7c06 Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Thu, 28 Dec 2017 23:59:53 +0800 Subject: [PATCH] test: verify errors thrown from fs stat APIs --- test/parallel/test-fs-error-messages.js | 127 ++++++++++++++++-------- 1 file changed, 85 insertions(+), 42 deletions(-) diff --git a/test/parallel/test-fs-error-messages.js b/test/parallel/test-fs-error-messages.js index f4d203e7d5e79f..39270002b08b5c 100644 --- a/test/parallel/test-fs-error-messages.js +++ b/test/parallel/test-fs-error-messages.js @@ -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'); @@ -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 @@ -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 { @@ -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 { @@ -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);