2017-01-03 13:16:48 -08:00
|
|
|
// Copyright Joyent, Inc. and other Node contributors.
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
// copy of this software and associated documentation files (the
|
|
|
|
// "Software"), to deal in the Software without restriction, including
|
|
|
|
// without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
// distribute, sublicense, and/or sell copies of the Software, and to permit
|
|
|
|
// persons to whom the Software is furnished to do so, subject to the
|
|
|
|
// following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included
|
|
|
|
// in all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
|
|
|
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
|
|
|
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
|
|
|
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
|
|
|
// USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
|
2016-07-22 16:38:36 -07:00
|
|
|
'use strict';
|
2016-12-20 19:27:56 -05:00
|
|
|
const common = require('../common');
|
2017-12-25 05:00:24 +08:00
|
|
|
const fs = require('fs');
|
2017-12-07 12:44:42 -05:00
|
|
|
|
|
|
|
// Test that fs.readFile fails correctly on a non-existent file.
|
|
|
|
|
2018-10-08 11:04:40 +02:00
|
|
|
// `fs.readFile('/')` does not fail on AIX and FreeBSD because you can open
|
|
|
|
// and read the directory there.
|
|
|
|
if (common.isAIX || common.isFreeBSD)
|
2016-07-22 16:38:36 -07:00
|
|
|
common.skip('platform not supported.');
|
2017-07-01 02:29:09 +03:00
|
|
|
|
|
|
|
const assert = require('assert');
|
|
|
|
const exec = require('child_process').exec;
|
2017-07-17 15:33:46 -07:00
|
|
|
const fixtures = require('../common/fixtures');
|
2016-07-22 16:38:36 -07:00
|
|
|
|
|
|
|
function test(env, cb) {
|
2017-07-17 15:33:46 -07:00
|
|
|
const filename = fixtures.path('test-fs-readfile-error.js');
|
2024-09-29 22:44:52 +02:00
|
|
|
exec(...common.escapePOSIXShell`"${process.execPath}" "${filename}"`, (err, stdout, stderr) => {
|
2016-07-22 16:38:36 -07:00
|
|
|
assert(err);
|
2016-12-20 19:27:56 -05:00
|
|
|
assert.strictEqual(stdout, '');
|
|
|
|
assert.notStrictEqual(stderr, '');
|
2017-04-28 04:06:42 +03:00
|
|
|
cb(String(stderr));
|
2019-03-18 13:35:46 +01:00
|
|
|
});
|
2016-07-22 16:38:36 -07:00
|
|
|
}
|
|
|
|
|
2016-12-20 19:27:56 -05:00
|
|
|
test({ NODE_DEBUG: '' }, common.mustCall((data) => {
|
2021-08-29 10:14:22 +02:00
|
|
|
assert.match(data, /EISDIR/);
|
|
|
|
assert.match(data, /test-fs-readfile-error/);
|
2016-07-22 16:38:36 -07:00
|
|
|
}));
|
|
|
|
|
2016-12-20 19:27:56 -05:00
|
|
|
test({ NODE_DEBUG: 'fs' }, common.mustCall((data) => {
|
2021-08-29 10:14:22 +02:00
|
|
|
assert.match(data, /EISDIR/);
|
|
|
|
assert.match(data, /test-fs-readfile-error/);
|
2016-07-22 16:38:36 -07:00
|
|
|
}));
|
2017-12-25 05:00:24 +08:00
|
|
|
|
2019-12-25 18:02:16 +01:00
|
|
|
assert.throws(
|
2018-02-09 00:54:31 +01:00
|
|
|
() => { fs.readFile(() => {}, common.mustNotCall()); },
|
2017-12-25 05:00:24 +08:00
|
|
|
{
|
|
|
|
code: 'ERR_INVALID_ARG_TYPE',
|
2019-09-23 08:17:25 +02:00
|
|
|
message: 'The "path" argument must be of type string or an instance of ' +
|
2023-09-30 23:43:40 +02:00
|
|
|
'Buffer or URL. Received function ',
|
2019-12-25 18:02:16 +01:00
|
|
|
name: 'TypeError'
|
2017-12-25 05:00:24 +08:00
|
|
|
}
|
|
|
|
);
|