Skip to content

Commit

Permalink
test: apply test-fs-access to promises API
Browse files Browse the repository at this point in the history
Add tests for `fs.promises.access()` in all test cases in
`test-fs-access` that are relevant to the Promises-based API.

PR-URL: #20667
Reviewed-By: Daniel Bevenius <[email protected]>
Reviewed-By: Trivikram Kamat <[email protected]>
Reviewed-By: Benjamin Gruenbaum <[email protected]>
  • Loading branch information
Trott authored and addaleax committed May 14, 2018
1 parent a66aad4 commit 886116f
Showing 1 changed file with 52 additions and 25 deletions.
77 changes: 52 additions & 25 deletions test/parallel/test-fs-access.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,37 +62,64 @@ assert.strictEqual(typeof fs.R_OK, 'number');
assert.strictEqual(typeof fs.W_OK, 'number');
assert.strictEqual(typeof fs.X_OK, 'number');

const throwNextTick = (e) => { process.nextTick(() => { throw e; }); };

fs.access(__filename, common.mustCall(assert.ifError));
fs.promises.access(__filename)
.then(common.mustCall())
.catch(throwNextTick);
fs.access(__filename, fs.R_OK, common.mustCall(assert.ifError));
fs.promises.access(__filename, fs.R_OK)
.then(common.mustCall())
.catch(throwNextTick);
fs.access(readOnlyFile, fs.F_OK | fs.R_OK, common.mustCall(assert.ifError));
fs.promises.access(readOnlyFile, fs.F_OK | fs.R_OK)
.then(common.mustCall())
.catch(throwNextTick);

fs.access(doesNotExist, common.mustCall((err) => {
assert.notStrictEqual(err, null, 'error should exist');
assert.strictEqual(err.code, 'ENOENT');
assert.strictEqual(err.path, doesNotExist);
}));

fs.access(readOnlyFile, fs.W_OK, common.mustCall(function(err) {
assert.strictEqual(this, undefined);
if (hasWriteAccessForReadonlyFile) {
assert.ifError(err);
} else {
assert.notStrictEqual(err, null, 'error should exist');
assert.strictEqual(err.path, readOnlyFile);
}
}));
{
const expectedError = (err) => {
assert.notStrictEqual(err, null);
assert.strictEqual(err.code, 'ENOENT');
assert.strictEqual(err.path, doesNotExist);
};
fs.access(doesNotExist, common.mustCall(expectedError));
fs.promises.access(doesNotExist)
.then(common.mustNotCall(), common.mustCall(expectedError))
.catch(throwNextTick);
}

common.expectsError(
() => {
fs.access(100, fs.F_OK, common.mustNotCall());
},
{
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: 'The "path" argument must be one of type string, Buffer, or URL.' +
' Received type number'
{
function expectedError(err) {
assert.strictEqual(this, undefined);
if (hasWriteAccessForReadonlyFile) {
assert.ifError(err);
} else {
assert.notStrictEqual(err, null);
assert.strictEqual(err.path, readOnlyFile);
}
}
);
fs.access(readOnlyFile, fs.W_OK, common.mustCall(expectedError));
fs.promises.access(readOnlyFile, fs.W_OK)
.then(common.mustNotCall(), common.mustCall(expectedError))
.catch(throwNextTick);
}

{
const expectedError = (err) => {
assert.strictEqual(err.code, 'ERR_INVALID_ARG_TYPE');
assert.ok(err instanceof TypeError);
return true;
};
assert.throws(
() => { fs.access(100, fs.F_OK, common.mustNotCall()); },
expectedError
);

fs.promises.access(100, fs.F_OK)
.then(common.mustNotCall(), common.mustCall(expectedError))
.catch(throwNextTick);
}

common.expectsError(
() => {
Expand Down

0 comments on commit 886116f

Please sign in to comment.