Skip to content

Commit bef0a6c

Browse files
committed
readfilecontext: more tests for coverage + fix AbortError prototype
1 parent 73b015e commit bef0a6c

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

lib/error.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ function AbortError() {
5050
this.name = 'AbortError';
5151
Error.captureStackTrace(this, AbortError);
5252
}
53+
AbortError.prototype = new Error();
5354

5455
/**
5556
* FSError constructor.

test/lib/readfilecontext.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
'use strict';
22

33
const helper = require('../helper');
4+
const fs = require('fs');
5+
const mock = require('../../lib/index');
46
const {
57
patchReadFileContext,
68
getReadFileContextPrototype
79
} = require('../../lib/readfilecontext');
810

911
const assert = helper.assert;
12+
const inVersion = helper.inVersion;
1013

1114
describe('getReadFileContextPrototype', function() {
1215
it('provides access to the internal ReadFileContext', function() {
@@ -105,3 +108,41 @@ describe('patchReadFileContext', function() {
105108
});
106109
});
107110
});
111+
112+
describe('fs.readFile() with ReadFileContext', function() {
113+
// fs.readFile() is already tested elsewhere, here we just make sure we have
114+
// coverage of the mocked ReadFileContext implementation.
115+
116+
beforeEach(function() {
117+
mock({
118+
'path/to/file.txt': 'file content',
119+
1: 'fd content'
120+
});
121+
});
122+
afterEach(mock.restore);
123+
124+
inVersion('>=15.0.0').it('allows file reads to be aborted', function(done) {
125+
const controller = new AbortController();
126+
const {signal} = controller;
127+
128+
fs.readFile('path/to/file.txt', {signal}, function(err) {
129+
assert.instanceOf(err, Error);
130+
assert.equal(err.name, 'AbortError');
131+
assert.equal(err.code, 'ABORT_ERR');
132+
done();
133+
});
134+
135+
// By aborting after the call it will be handled by the context rather than readFile()
136+
controller.abort();
137+
});
138+
139+
it('allows file reads with a numeric descriptor', function(done) {
140+
// This isn't actually supported by mock-fs, but let's make sure the call goes through
141+
// It also covers the case of reading an empty file and reading with encoding
142+
fs.readFile(1, 'utf-8', function(err, data) {
143+
assert.isNull(err);
144+
assert.equal(data, '');
145+
done();
146+
});
147+
});
148+
});

0 commit comments

Comments
 (0)