forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-fs-promises-file-handle-aggregate-errors.js
More file actions
71 lines (60 loc) · 2.23 KB
/
test-fs-promises-file-handle-aggregate-errors.js
File metadata and controls
71 lines (60 loc) · 2.23 KB
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
'use strict';
// Flags: --expose-internals
const common = require('../common');
const tmpdir = require('../common/tmpdir');
const { test } = require('node:test');
const { readFile, writeFile, truncate, lchmod } = require('node:fs/promises');
const { FileHandle } = require('internal/fs/promises');
const assert = require('node:assert');
const originalFd = Object.getOwnPropertyDescriptor(FileHandle.prototype, 'fd');
let count = 0;
async function createFile() {
const filePath = tmpdir.resolve(`aggregate_errors_${++count}.txt`);
await writeFile(filePath, 'content');
return filePath;
}
async function checkAggregateError(op) {
try {
const filePath = await createFile();
Object.defineProperty(FileHandle.prototype, 'fd', {
get: function() {
// Close is set by using a setter,
// so it needs to be set on the instance.
const originalClose = this.close;
this.close = async () => {
// close the file
await originalClose.call(this);
const closeError = new Error('CLOSE_ERROR');
closeError.code = 456;
throw closeError;
};
const opError = new Error('INTERNAL_ERROR');
opError.code = 123;
throw opError;
},
});
// Perform the operation and check the aggregate error
await assert.rejects(op(filePath), (err) => {
assert.strictEqual(err.name, 'AggregateError');
assert.strictEqual(err.code, 123);
assert.strictEqual(err.errors.length, 2);
// Check the individual errors
assert.strictEqual(err.errors[0].message, 'INTERNAL_ERROR');
assert.strictEqual(err.errors[0].code, 123);
assert.strictEqual(err.errors[1].message, 'CLOSE_ERROR');
assert.strictEqual(err.errors[1].code, 456);
return true;
});
} finally {
Object.defineProperty(FileHandle.prototype, 'fd', originalFd);
}
}
test('Test aggregate errors for file operations', async () => {
tmpdir.refresh();
await checkAggregateError((filePath) => truncate(filePath));
await checkAggregateError((filePath) => readFile(filePath));
await checkAggregateError((filePath) => writeFile(filePath, '123'));
if (common.isMacOS) {
await checkAggregateError((filePath) => lchmod(filePath, 0o777));
}
});