From b89d172fbcd999b9a2ffec43de205c65ddc24c9c Mon Sep 17 00:00:00 2001 From: Evaline Ju Date: Mon, 9 Nov 2020 22:20:37 -0500 Subject: [PATCH 1/4] feat: Add new code for unparsable files --- lib/cli/config.js | 3 ++- lib/cli/options.js | 3 ++- lib/errors.js | 23 ++++++++++++++++++++++- 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/lib/cli/config.js b/lib/cli/config.js index 011b065510..008b2a3b32 100644 --- a/lib/cli/config.js +++ b/lib/cli/config.js @@ -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'); /** @@ -81,7 +82,7 @@ exports.loadConfig = filepath => { config = parsers.json(filepath); } } catch (err) { - throw new Error(`failed to parse config "${filepath}": ${err}`); + throw createUnparsableFileError(err, filepath); } return config; }; diff --git a/lib/cli/options.js b/lib/cli/options.js index 95428c4f96..4eedab8228 100644 --- a/lib/cli/options.js +++ b/lib/cli/options.js @@ -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 @@ -190,7 +191,7 @@ const loadPkgRc = (args = {}) => { } } catch (err) { if (args.package) { - throw new Error(`Unable to read/parse ${filepath}: ${err}`); + throw createUnparsableFileError(err, filepath); } debug('failed to read default package.json at %s; ignoring', filepath); } diff --git a/lib/errors.js b/lib/errors.js index 4289676edc..b8fb28b257 100644 --- a/lib/errors.js +++ b/lib/errors.js @@ -163,7 +163,12 @@ var constants = { * @constant * @default */ - TIMEOUT: 'ERR_MOCHA_TIMEOUT' + TIMEOUT: 'ERR_MOCHA_TIMEOUT', + + /** + * Input file is not able to be parsed + */ + UNPARSABLE_FILE: 'ERR_MOCHA_UNPARSABLE_FILE' }; /** @@ -495,6 +500,21 @@ function createTimeoutError(msg, timeout, file) { return err; } +/** +* Creates an error object to be thrown when file is unparsable +* +* @public +* @param {string} message - Error message to be displayed. +* @param {string} filename - File name +* @param {string} [reason] - Why the filename is unparsable. +*/ +function createUnparsableFileError(message, filename, reason) { + var err = new Error(message); + err.code = constants.UNPARSABLE_FILE; + err.reason = typeof reason !== 'undefined' ? reason : `${filename} is unparsable`; + return err; + } + /** * Returns `true` if an error came out of Mocha. * _Can suffer from false negatives, but not false positives._ @@ -525,6 +545,7 @@ module.exports = { createMultipleDoneError, createNoFilesMatchPatternError, createTimeoutError, + createUnparsableFileError, createUnsupportedError, deprecate, isMochaError, From 6e0c5220f224f1902a9c5da9c4555fe00879a0a7 Mon Sep 17 00:00:00 2001 From: Evaline Ju Date: Mon, 9 Nov 2020 22:21:44 -0500 Subject: [PATCH 2/4] test: Update tests with new unparsable file code --- test/node-unit/cli/config.spec.js | 8 ++++++-- test/node-unit/cli/options.spec.js | 4 ++-- test/unit/errors.spec.js | 14 ++++++++++++++ 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/test/node-unit/cli/config.spec.js b/test/node-unit/cli/config.spec.js index 6f63d508f1..05c8fa82b7 100644 --- a/test/node-unit/cli/config.spec.js +++ b/test/node-unit/cli/config.spec.js @@ -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', + 'goo.yaml is unparsable' + ); }); }); }); diff --git a/test/node-unit/cli/options.spec.js b/test/node-unit/cli/options.spec.js index fa376fe286..94d9790495 100644 --- a/test/node-unit/cli/options.spec.js +++ b/test/node-unit/cli/options.spec.js @@ -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(); @@ -148,7 +148,7 @@ describe('options', function() { loadOptions('--package /something/wherever --require butts'); }, 'to throw', - /unable to read\/parse/i + 'bad file message' ); }); }); diff --git a/test/unit/errors.spec.js b/test/unit/errors.spec.js index 9c57e55bc2..c478b4f323 100644 --- a/test/unit/errors.spec.js +++ b/test/unit/errors.spec.js @@ -69,6 +69,20 @@ 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', + reason: 'badFilePath is unparsable' + } + ); + }); + }); + describe('deprecate()', function() { var emitWarning; From 624ab537dc0aab8ea6231288180c3bfaaa2ad4fe Mon Sep 17 00:00:00 2001 From: Evaline Ju Date: Wed, 18 Nov 2020 20:23:03 -0800 Subject: [PATCH 3/4] fix: remove reason from unparsable file error and add to messages --- lib/cli/config.js | 5 ++++- lib/cli/options.js | 5 ++++- lib/errors.js | 25 ++++++++++++------------- test/node-unit/cli/config.spec.js | 2 +- test/node-unit/cli/options.spec.js | 2 +- test/unit/errors.spec.js | 3 +-- 6 files changed, 23 insertions(+), 19 deletions(-) diff --git a/lib/cli/config.js b/lib/cli/config.js index 008b2a3b32..b7510d7deb 100644 --- a/lib/cli/config.js +++ b/lib/cli/config.js @@ -82,7 +82,10 @@ exports.loadConfig = filepath => { config = parsers.json(filepath); } } catch (err) { - throw createUnparsableFileError(err, filepath); + throw createUnparsableFileError( + `Unable to read/parse ${filepath}: ${err}`, + filepath + ); } return config; }; diff --git a/lib/cli/options.js b/lib/cli/options.js index 4eedab8228..8fa9470e6f 100644 --- a/lib/cli/options.js +++ b/lib/cli/options.js @@ -191,7 +191,10 @@ const loadPkgRc = (args = {}) => { } } catch (err) { if (args.package) { - throw createUnparsableFileError(err, filepath); + throw createUnparsableFileError( + `Unable to read/parse ${filepath}: ${err}`, + filepath + ); } debug('failed to read default package.json at %s; ignoring', filepath); } diff --git a/lib/errors.js b/lib/errors.js index b8fb28b257..b74536ff86 100644 --- a/lib/errors.js +++ b/lib/errors.js @@ -501,19 +501,18 @@ function createTimeoutError(msg, timeout, file) { } /** -* Creates an error object to be thrown when file is unparsable -* -* @public -* @param {string} message - Error message to be displayed. -* @param {string} filename - File name -* @param {string} [reason] - Why the filename is unparsable. -*/ -function createUnparsableFileError(message, filename, reason) { - var err = new Error(message); - err.code = constants.UNPARSABLE_FILE; - err.reason = typeof reason !== 'undefined' ? reason : `${filename} is unparsable`; - 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. diff --git a/test/node-unit/cli/config.spec.js b/test/node-unit/cli/config.spec.js index 05c8fa82b7..270e3ec95a 100644 --- a/test/node-unit/cli/config.spec.js +++ b/test/node-unit/cli/config.spec.js @@ -116,7 +116,7 @@ describe('cli/config', function() { expect( () => loadConfig('goo.yaml'), 'to throw', - 'goo.yaml is unparsable' + 'Unable to read/parse goo.yaml: goo.yaml is unparsable' ); }); }); diff --git a/test/node-unit/cli/options.spec.js b/test/node-unit/cli/options.spec.js index 94d9790495..0455be0aa5 100644 --- a/test/node-unit/cli/options.spec.js +++ b/test/node-unit/cli/options.spec.js @@ -148,7 +148,7 @@ describe('options', function() { loadOptions('--package /something/wherever --require butts'); }, 'to throw', - 'bad file message' + 'Unable to read/parse /something/wherever: bad file message' ); }); }); diff --git a/test/unit/errors.spec.js b/test/unit/errors.spec.js index c478b4f323..d610b04acd 100644 --- a/test/unit/errors.spec.js +++ b/test/unit/errors.spec.js @@ -76,8 +76,7 @@ describe('Errors', function() { 'to satisfy', { message: message, - code: 'ERR_MOCHA_UNPARSABLE_FILE', - reason: 'badFilePath is unparsable' + code: 'ERR_MOCHA_UNPARSABLE_FILE' } ); }); From 3c81ed880f290f73515fe860d69f17a395846f57 Mon Sep 17 00:00:00 2001 From: Evaline Ju Date: Thu, 1 Apr 2021 22:20:20 -0400 Subject: [PATCH 4/4] chore: Fix rebase --- lib/errors.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/errors.js b/lib/errors.js index b74536ff86..681198ae27 100644 --- a/lib/errors.js +++ b/lib/errors.js @@ -167,6 +167,8 @@ var constants = { /** * Input file is not able to be parsed + * @constant + * @default */ UNPARSABLE_FILE: 'ERR_MOCHA_UNPARSABLE_FILE' };