Skip to content

Commit

Permalink
restore and deprecate lookupFiles() in lib/utils
Browse files Browse the repository at this point in the history
lookupFiles() broke (moved) in version v8.1.0, this returns it and issues a soft deprecation warning.
in the browser, this function should not be called, but if it is, an `ERR_MOCHA_UNSUPPORTED` `Error` is thrown
  • Loading branch information
boneskull committed Aug 28, 2020
1 parent 247d961 commit 52f07f0
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 1 deletion.
29 changes: 29 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -645,3 +645,32 @@ exports.cwd = function cwd() {
exports.isBrowser = function isBrowser() {
return Boolean(process.browser);
};

/**
* Lookup file names at the given `path`.
*
* @description
* Filenames are returned in _traversal_ order by the OS/filesystem.
* **Make no assumption that the names will be sorted in any fashion.**
*
* @public
* @alias module:lib/cli.lookupFiles
* @param {string} filepath - Base path to start searching from.
* @param {string[]} [extensions=[]] - File extensions to look for.
* @param {boolean} [recursive=false] - Whether to recurse into subdirectories.
* @return {string[]} An array of paths.
* @throws {Error} if no files match pattern.
* @throws {TypeError} if `filepath` is directory and `extensions` not provided.
* @deprecated Moved to {@link module:lib/cli.lookupFiles}
*/
exports.lookupFiles = (...args) => {
if (exports.isBrowser()) {
throw require('./errors').createUnsupportedError(
'lookupFiles() is only supported in Node.js!'
);
}
exports.deprecate(
'`lookupFiles()` in module `mocha/lib/utils` has moved to module `mocha/lib/cli` and will be removed in the next major revision of Mocha'
);
return require('./cli').lookupFiles(...args);
};
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,8 @@
"./lib/nodejs/worker.js": false,
"./lib/nodejs/buffered-worker-pool.js": false,
"./lib/nodejs/parallel-buffered-runner.js": false,
"./lib/nodejs/reporters/parallel-buffered.js": false
"./lib/nodejs/reporters/parallel-buffered.js": false,
"./lib/cli/index.js": false
},
"prettier": {
"singleQuote": true,
Expand Down
45 changes: 45 additions & 0 deletions test/unit/utils.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -742,4 +742,49 @@ describe('lib/utils', function() {
expect(utils.slug('poppies & fritz'), 'to be', 'poppies-fritz');
});
});

describe('lookupFiles()', function() {
beforeEach(function() {
sinon.stub(utils, 'deprecate');
});

describe('when run in Node.js', function() {
before(function() {
if (process.browser) {
return this.skip();
}
});

beforeEach(function() {
sinon.stub(utils, 'isBrowser').returns(false);
sinon.stub(require('../../lib/cli'), 'lookupFiles').returns([]);
});

it('should print a deprecation message', function() {
utils.lookupFiles();
expect(utils.deprecate, 'was called once');
});

it('should delegate to new location of lookupFiles()', function() {
utils.lookupFiles(['foo']);
expect(
require('../../lib/cli').lookupFiles,
'to have a call satisfying',
[['foo']]
).and('was called once');
});
});

describe('when run in browser', function() {
beforeEach(function() {
sinon.stub(utils, 'isBrowser').returns(true);
});

it('should throw', function() {
expect(() => utils.lookupFiles(['foo']), 'to throw', {
code: 'ERR_MOCHA_UNSUPPORTED'
});
});
});
});
});

0 comments on commit 52f07f0

Please sign in to comment.