Skip to content

Commit

Permalink
test: refactor test-fs-error-messages.js
Browse files Browse the repository at this point in the history
* group tests by error type
* improve error validation for all messages
* use assert.throws instead of try and catch
* use arrow functions
* add missing test for readdir
* add missing test for readFileSync
* remove unnecessary variables

PR-URL: #11096
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Italo A. Casas <me@italoacasas.com>
  • Loading branch information
edsadr authored and jasnell committed Feb 3, 2017
1 parent 42e73ae commit 907ce8d
Showing 1 changed file with 118 additions and 164 deletions.
282 changes: 118 additions & 164 deletions test/parallel/test-fs-error-messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,202 +9,156 @@ const existingFile2 = path.join(common.fixturesDir, 'create-file.js');
const existingDir = path.join(common.fixturesDir, 'empty');
const existingDir2 = path.join(common.fixturesDir, 'keys');

// ASYNC_CALL
// Test all operations failing with ENOENT errors
function testEnoentError(file, endMessage, syscal, err) {
const sufix = (endMessage) ? endMessage : '';

fs.stat(fn, function(err) {
assert(err instanceof Error);
assert.strictEqual(fn, err.path);
assert.ok(0 <= err.message.indexOf(fn));
});
assert.strictEqual(
err.message,
`ENOENT: no such file or directory, ${syscal} '${file}'${sufix}`
);

fs.lstat(fn, function(err) {
assert.ok(0 <= err.message.indexOf(fn));
});
return true;
}

fs.readlink(fn, function(err) {
assert.ok(0 <= err.message.indexOf(fn));
});
// Test all operations failing with EEXIST errors
function testEexistError(source, dest, syscal, err) {
const sufix = (dest) ? ` -> '${dest}'` : '';

fs.link(fn, 'foo', function(err) {
assert.ok(0 <= err.message.indexOf(fn));
});
assert(err instanceof Error);
assert.strictEqual(source, err.path);
assert.strictEqual(
err.message,
`EEXIST: file already exists, ${syscal} '${source}'${sufix}`
);

fs.link(existingFile, existingFile2, function(err) {
assert.ok(0 <= err.message.indexOf(existingFile));
assert.ok(0 <= err.message.indexOf(existingFile2));
});
return true;
}

fs.symlink(existingFile, existingFile2, function(err) {
assert.ok(0 <= err.message.indexOf(existingFile));
assert.ok(0 <= err.message.indexOf(existingFile2));
});
// Test all operations failing with ENOTEMPTY errors
function testEnoemptyError(source, dest, err) {
assert(err instanceof Error);
assert.strictEqual(source, err.path);
assert.strictEqual(
err.message,
`ENOTEMPTY: directory not empty, rename '${source}' ` +
`-> '${dest}'`
);

return true;
}

fs.unlink(fn, function(err) {
assert.ok(0 <= err.message.indexOf(fn));
});
// Test all operations failing with ENOTDIR errors
function testEnotdirError(dir, err) {
assert(err instanceof Error);
assert.strictEqual(dir, err.path);
assert.strictEqual(
err.message,
`ENOTDIR: not a directory, rmdir '${dir}'`
);

fs.rename(fn, 'foo', function(err) {
assert.ok(0 <= err.message.indexOf(fn));
});
return true;
}

fs.rename(existingDir, existingDir2, function(err) {
assert.ok(0 <= err.message.indexOf(existingDir));
assert.ok(0 <= err.message.indexOf(existingDir2));
});
// Generating ENOENTS errors
fs.stat(fn, (err) => testEnoentError(fn, '', 'stat', err));
fs.lstat(fn, (err) => testEnoentError(fn, '', 'lstat', err));
fs.readlink(fn, (err) => testEnoentError(fn, '', 'readlink', err));
fs.link(fn, 'foo', (err) => testEnoentError(fn, ' -> \'foo\'', 'link', err));
fs.unlink(fn, (err) => testEnoentError(fn, '', 'unlink', err));
fs.rmdir(fn, (err) => testEnoentError(fn, '', 'rmdir', err));
fs.chmod(fn, 0o666, (err) => testEnoentError(fn, '', 'chmod', err));
fs.open(fn, 'r', 0o666, (err) => testEnoentError(fn, '', 'open', err));
fs.readFile(fn, (err) => testEnoentError(fn, '', 'open', err));
fs.readdir(fn, (err) => testEnoentError(fn, '', 'scandir', err));

fs.rmdir(fn, function(err) {
assert.ok(0 <= err.message.indexOf(fn));
fs.rename(fn, 'foo', (err) => {
testEnoentError(fn, ' -> \'foo\'', 'rename', err);
});

fs.mkdir(existingFile, 0o666, function(err) {
assert.ok(0 <= err.message.indexOf(existingFile));
});
assert.throws(() => {
fs.statSync(fn);
}, (err) => testEnoentError(fn, '', 'stat', err));

fs.rmdir(existingFile, function(err) {
assert.ok(0 <= err.message.indexOf(existingFile));
});
assert.throws(() => {
fs.lstatSync(fn);
}, (err) => testEnoentError(fn, '', 'lstat', err));

fs.chmod(fn, 0o666, function(err) {
assert.ok(0 <= err.message.indexOf(fn));
});
assert.throws(() => {
fs.readlinkSync(fn);
}, (err) => testEnoentError(fn, '', 'readlink', err));

fs.open(fn, 'r', 0o666, function(err) {
assert.ok(0 <= err.message.indexOf(fn));
});
assert.throws(() => {
fs.linkSync(fn, 'foo');
}, (err) => testEnoentError(fn, ' -> \'foo\'', 'link', err));

fs.readFile(fn, function(err) {
assert.ok(0 <= err.message.indexOf(fn));
});
assert.throws(() => {
fs.unlinkSync(fn);
}, (err) => testEnoentError(fn, '', 'unlink', err));

// Sync
assert.throws(() => {
fs.rmdirSync(fn);
}, (err) => testEnoentError(fn, '', 'rmdir', err));

const errors = [];
let expected = 0;
assert.throws(() => {
fs.chmodSync(fn, 0o666);
}, (err) => testEnoentError(fn, '', 'chmod', err));

try {
++expected;
fs.statSync(fn);
} catch (err) {
errors.push('stat');
assert.ok(0 <= err.message.indexOf(fn));
}
assert.throws(() => {
fs.openSync(fn, 'r');
}, (err) => testEnoentError(fn, '', 'open', err));

try {
++expected;
fs.mkdirSync(existingFile, 0o666);
} catch (err) {
errors.push('mkdir');
assert.ok(0 <= err.message.indexOf(existingFile));
}
assert.throws(() => {
fs.readFileSync(fn);
}, (err) => testEnoentError(fn, '', 'open', err));

try {
++expected;
fs.chmodSync(fn, 0o666);
} catch (err) {
errors.push('chmod');
assert.ok(0 <= err.message.indexOf(fn));
}
assert.throws(() => {
fs.readdirSync(fn);
}, (err) => testEnoentError(fn, '', 'scandir', err));

try {
++expected;
fs.lstatSync(fn);
} catch (err) {
errors.push('lstat');
assert.ok(0 <= err.message.indexOf(fn));
}
assert.throws(() => {
fs.renameSync(fn, 'foo');
}, (err) => testEnoentError(fn, ' -> \'foo\'', 'rename', err));

try {
++expected;
fs.readlinkSync(fn);
} catch (err) {
errors.push('readlink');
assert.ok(0 <= err.message.indexOf(fn));
}
// Generating EEXIST errors
fs.link(existingFile, existingFile2, (err) => {
testEexistError(existingFile, existingFile2, 'link', err);
});

try {
++expected;
fs.linkSync(fn, 'foo');
} catch (err) {
errors.push('link');
assert.ok(0 <= err.message.indexOf(fn));
}
fs.symlink(existingFile, existingFile2, (err) => {
testEexistError(existingFile, existingFile2, 'symlink', err);
});

try {
++expected;
fs.mkdir(existingFile, 0o666, (err) => {
testEexistError(existingFile, null, 'mkdir', err);
});

assert.throws(() => {
fs.linkSync(existingFile, existingFile2);
} catch (err) {
errors.push('link');
assert.ok(0 <= err.message.indexOf(existingFile));
assert.ok(0 <= err.message.indexOf(existingFile2));
}
}, (err) => testEexistError(existingFile, existingFile2, 'link', err));

try {
++expected;
assert.throws(() => {
fs.symlinkSync(existingFile, existingFile2);
} catch (err) {
errors.push('symlink');
assert.ok(0 <= err.message.indexOf(existingFile));
assert.ok(0 <= err.message.indexOf(existingFile2));
}
}, (err) => testEexistError(existingFile, existingFile2, 'symlink', err));

try {
++expected;
fs.unlinkSync(fn);
} catch (err) {
errors.push('unlink');
assert.ok(0 <= err.message.indexOf(fn));
}

try {
++expected;
fs.rmdirSync(fn);
} catch (err) {
errors.push('rmdir');
assert.ok(0 <= err.message.indexOf(fn));
}

try {
++expected;
fs.rmdirSync(existingFile);
} catch (err) {
errors.push('rmdir');
assert.ok(0 <= err.message.indexOf(existingFile));
}

try {
++expected;
fs.openSync(fn, 'r');
} catch (err) {
errors.push('opens');
assert.ok(0 <= err.message.indexOf(fn));
}
assert.throws(() => {
fs.mkdirSync(existingFile, 0o666);
}, (err) => testEexistError(existingFile, null, 'mkdir', err));

try {
++expected;
fs.renameSync(fn, 'foo');
} catch (err) {
errors.push('rename');
assert.ok(0 <= err.message.indexOf(fn));
}
// Generating ENOTEMPTY errors
fs.rename(existingDir, existingDir2, (err) => {
testEnoemptyError(existingDir, existingDir2, err);
});

try {
++expected;
assert.throws(() => {
fs.renameSync(existingDir, existingDir2);
} catch (err) {
errors.push('rename');
assert.ok(0 <= err.message.indexOf(existingDir));
assert.ok(0 <= err.message.indexOf(existingDir2));
}
}, (err) => testEnoemptyError(existingDir, existingDir2, err));

try {
++expected;
fs.readdirSync(fn);
} catch (err) {
errors.push('readdir');
assert.ok(0 <= err.message.indexOf(fn));
}
// Generating ENOTDIR errors
fs.rmdir(existingFile, (err) => testEnotdirError(existingFile, err));

process.on('exit', function() {
assert.strictEqual(expected, errors.length,
'Test fs sync exceptions raised, got ' + errors.length +
' expected ' + expected);
});
assert.throws(() => {
fs.rmdirSync(existingFile);
}, (err) => testEnotdirError(existingFile, err));

0 comments on commit 907ce8d

Please sign in to comment.