Skip to content

Commit

Permalink
Change CLI file parsing errors to use an error code (#4502)
Browse files Browse the repository at this point in the history
  • Loading branch information
evaline-ju committed May 5, 2021
1 parent d35eb9d commit 0079ae7
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 7 deletions.
6 changes: 5 additions & 1 deletion lib/cli/config.js
Expand Up @@ -11,6 +11,7 @@ const fs = require('fs');
const path = require('path');
const debug = require('debug')('mocha:cli:config');
const findUp = require('find-up');
const {createUnparsableFileError} = require('../errors');
const utils = require('../utils');

/**
Expand Down Expand Up @@ -81,7 +82,10 @@ exports.loadConfig = filepath => {
config = parsers.json(filepath);
}
} catch (err) {
throw new Error(`failed to parse config "${filepath}": ${err}`);
throw createUnparsableFileError(
`Unable to read/parse ${filepath}: ${err}`,
filepath
);
}
return config;
};
Expand Down
6 changes: 5 additions & 1 deletion lib/cli/options.js
Expand Up @@ -18,6 +18,7 @@ const {loadConfig, findConfig} = require('./config');
const findUp = require('find-up');
const debug = require('debug')('mocha:cli:options');
const {isNodeFlag} = require('./node-flags');
const {createUnparsableFileError} = require('../errors');

/**
* The `yargs-parser` namespace
Expand Down Expand Up @@ -190,7 +191,10 @@ const loadPkgRc = (args = {}) => {
}
} catch (err) {
if (args.package) {
throw new Error(`Unable to read/parse ${filepath}: ${err}`);
throw createUnparsableFileError(
`Unable to read/parse ${filepath}: ${err}`,
filepath
);
}
debug('failed to read default package.json at %s; ignoring', filepath);
}
Expand Down
24 changes: 23 additions & 1 deletion lib/errors.js
Expand Up @@ -163,7 +163,14 @@ var constants = {
* @constant
* @default
*/
TIMEOUT: 'ERR_MOCHA_TIMEOUT'
TIMEOUT: 'ERR_MOCHA_TIMEOUT',

/**
* Input file is not able to be parsed
* @constant
* @default
*/
UNPARSABLE_FILE: 'ERR_MOCHA_UNPARSABLE_FILE'
};

/**
Expand Down Expand Up @@ -495,6 +502,20 @@ function createTimeoutError(msg, timeout, file) {
return err;
}

/**
* Creates an error object to be thrown when file is unparsable
* @public
* @static
* @param {string} message - Error message to be displayed.
* @param {string} filename - File name
* @returns {Error} Error with code {@link constants.UNPARSABLE_FILE}
*/
function createUnparsableFileError(message, filename) {
var err = new Error(message);
err.code = constants.UNPARSABLE_FILE;
return err;
}

/**
* Returns `true` if an error came out of Mocha.
* _Can suffer from false negatives, but not false positives._
Expand Down Expand Up @@ -525,6 +546,7 @@ module.exports = {
createMultipleDoneError,
createNoFilesMatchPatternError,
createTimeoutError,
createUnparsableFileError,
createUnsupportedError,
deprecate,
isMochaError,
Expand Down
8 changes: 6 additions & 2 deletions test/node-unit/cli/config.spec.js
Expand Up @@ -109,11 +109,15 @@ describe('cli/config', function() {

describe('when config file parsing fails', function() {
beforeEach(function() {
sinon.stub(parsers, 'yaml').throws();
sinon.stub(parsers, 'yaml').throws('goo.yaml is unparsable');
});

it('should throw', function() {
expect(() => loadConfig('goo.yaml'), 'to throw', /failed to parse/);
expect(
() => loadConfig('goo.yaml'),
'to throw',
'Unable to read/parse goo.yaml: goo.yaml is unparsable'
);
});
});
});
Expand Down
4 changes: 2 additions & 2 deletions test/node-unit/cli/options.spec.js
Expand Up @@ -130,7 +130,7 @@ describe('options', function() {
beforeEach(function() {
readFileSync = sinon.stub();
// package.json
readFileSync.onFirstCall().throws('yikes');
readFileSync.onFirstCall().throws('bad file message');
findConfig = sinon.stub().returns('/some/.mocharc.json');
loadConfig = sinon.stub().returns({});
findupSync = sinon.stub();
Expand All @@ -148,7 +148,7 @@ describe('options', function() {
loadOptions('--package /something/wherever --require butts');
},
'to throw',
/unable to read\/parse/i
'Unable to read/parse /something/wherever: bad file message'
);
});
});
Expand Down
13 changes: 13 additions & 0 deletions test/unit/errors.spec.js
Expand Up @@ -69,6 +69,19 @@ describe('Errors', function() {
});
});

describe('createUnparsableFileError()', function() {
it('should include expected code in thrown unparsable file errors', function() {
expect(
errors.createUnparsableFileError(message, 'badFilePath'),
'to satisfy',
{
message: message,
code: 'ERR_MOCHA_UNPARSABLE_FILE'
}
);
});
});

describe('deprecate()', function() {
var emitWarning;

Expand Down

0 comments on commit 0079ae7

Please sign in to comment.