Skip to content

Commit

Permalink
fixup! fs: introduce readJSON functions
Browse files Browse the repository at this point in the history
  • Loading branch information
targos committed Mar 27, 2021
1 parent 2fc6919 commit c1bf662
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 7 deletions.
8 changes: 5 additions & 3 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -364,19 +364,21 @@ function readFile(path, options, callback) {
function readJSON(path, options, callback) {
callback = maybeCallback(callback || options);
options = getOptions(options, { encoding: 'utf-8', flag: 'r' });

const { reviver } = options;
if (typeof reviver !== 'undefined') {
validateFunction(reviver, 'options.reviver');
}

readFile(path, options, (readFileError, file) => {
if (readFileError) return callback(readFileError);
let json;
try {
callback(null, JSONParse(file, reviver));
json = JSONParse(file, reviver);
} catch (parseError) {
callback(parseError);
return callback(parseError);
}
callback(null, json);
});
}

Expand Down
4 changes: 2 additions & 2 deletions lib/internal/fs/promises.js
Original file line number Diff line number Diff line change
Expand Up @@ -725,8 +725,8 @@ async function readFile(path, options) {
}

async function readJSON(path, options) {
options = getOptions(options, { encoding: 'utf-8', flag: 'r' });
options = getOptions(options, { encoding: 'utf-8', flag: 'r' });

const { reviver } = options;
if (typeof reviver !== 'undefined') {
validateFunction(reviver, 'options.reviver');
Expand Down
5 changes: 3 additions & 2 deletions test/doctool/test-apilinks.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,16 @@ fs.readdirSync(apilinks).forEach((fixture) => {
if (!fixture.endsWith('.js')) return;
const input = path.join(apilinks, fixture);

const expectedContent = fs.readFileSync(`${input}on`, 'utf8');
const outputPath = path.join(tmpdir.path, `${fixture}on`);
execFileSync(
process.execPath,
[script, outputPath, input],
{ encoding: 'utf-8' }
);

const expectedLinks = fs.readJSONSync(`${input}on`);
const actualLinks = fs.readJSONSync(outputPath);
const expectedLinks = JSON.parse(expectedContent);
const actualLinks = JSON.parse(fs.readFileSync(outputPath));

for (const [k, v] of Object.entries(expectedLinks)) {
assert.ok(k in actualLinks, `link not found: ${k}`);
Expand Down

0 comments on commit c1bf662

Please sign in to comment.