1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
|
/*---
includes: [compareArray.js, compatFs.js]
flags: [async]
---*/
var fname = `${test_dir}/fs_promises_02`;
let stages = [];
var testSync = () => new Promise((resolve, reject) => {
try {
fs.writeFileSync(fname, fname);
fs.accessSync(fname);
fs.accessSync(fname, fs.constants.R_OK | fs.constants.W_OK);
try {
fs.accessSync(fname + '___');
reject(new Error('fs.accessSync error 1'));
} catch (e) {
if (e.syscall != 'access' || e.code != 'ENOENT') {
reject(new Error('fs.accessSync error 2'));
}
}
stages.push('testSync');
resolve();
} catch (e) {
reject(e);
}
});
var testCallback = () => new Promise((resolve, reject) => {
fs.writeFileSync(fname, fname);
fs.access(fname, (err) => {
if (err) {
reject(new Error('fs.access error 1'));
}
fs.access(fname, fs.constants.R_OK | fs.constants.W_OK, (err) => {
if (err) {
reject(err);
}
fs.access(fname + '___', (err) => {
if (!err
|| err.syscall != 'access'
|| err.code != 'ENOENT')
{
reject(new Error('fs.access error 2'));
}
stages.push('testCallback');
resolve();
});
});
});
});
let testFsp = () => Promise.resolve()
.then(() => {
fs.writeFileSync(fname, fname);
return fsp.access(fname)
.then(() => fsp.access(fname, fs.constants.R_OK | fs.constants.W_OK))
.then(() => fsp.access(fname + '___'));
})
.then(() => {
$DONOTEVALUATE();
})
.catch(e => {
stages.push('testPromise');
assert.sameValue(e.syscall, 'access', 'testPromise');
assert.sameValue(e.path, fname + '___', 'testPromise');
assert.sameValue(e.code, 'ENOENT', 'testPromise');
})
let p = Promise.resolve()
.then(testSync)
.then(testCallback)
.then(testFsp)
.then(() => assert.compareArray(stages, ["testSync", "testCallback", "testPromise"]))
p.then($DONE, $DONE);
|