Skip to content

Commit

Permalink
readfilecontext: more tests for coverage + fix AbortError prototype
Browse files Browse the repository at this point in the history
  • Loading branch information
Rugvip committed Sep 17, 2021
1 parent 73b015e commit bef0a6c
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/error.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ function AbortError() {
this.name = 'AbortError';
Error.captureStackTrace(this, AbortError);
}
AbortError.prototype = new Error();

/**
* FSError constructor.
Expand Down
41 changes: 41 additions & 0 deletions test/lib/readfilecontext.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
'use strict';

const helper = require('../helper');
const fs = require('fs');
const mock = require('../../lib/index');
const {
patchReadFileContext,
getReadFileContextPrototype
} = require('../../lib/readfilecontext');

const assert = helper.assert;
const inVersion = helper.inVersion;

describe('getReadFileContextPrototype', function() {
it('provides access to the internal ReadFileContext', function() {
Expand Down Expand Up @@ -105,3 +108,41 @@ describe('patchReadFileContext', function() {
});
});
});

describe('fs.readFile() with ReadFileContext', function() {
// fs.readFile() is already tested elsewhere, here we just make sure we have
// coverage of the mocked ReadFileContext implementation.

beforeEach(function() {
mock({
'path/to/file.txt': 'file content',
1: 'fd content'
});
});
afterEach(mock.restore);

inVersion('>=15.0.0').it('allows file reads to be aborted', function(done) {
const controller = new AbortController();
const {signal} = controller;

fs.readFile('path/to/file.txt', {signal}, function(err) {
assert.instanceOf(err, Error);
assert.equal(err.name, 'AbortError');
assert.equal(err.code, 'ABORT_ERR');
done();
});

// By aborting after the call it will be handled by the context rather than readFile()
controller.abort();
});

it('allows file reads with a numeric descriptor', function(done) {
// This isn't actually supported by mock-fs, but let's make sure the call goes through
// It also covers the case of reading an empty file and reading with encoding
fs.readFile(1, 'utf-8', function(err, data) {
assert.isNull(err);
assert.equal(data, '');
done();
});
});
});

0 comments on commit bef0a6c

Please # to comment.