Skip to content

Commit 52f07f0

Browse files
committed
restore and deprecate lookupFiles() in lib/utils
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
1 parent 247d961 commit 52f07f0

File tree

3 files changed

+76
-1
lines changed

3 files changed

+76
-1
lines changed

lib/utils.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -645,3 +645,32 @@ exports.cwd = function cwd() {
645645
exports.isBrowser = function isBrowser() {
646646
return Boolean(process.browser);
647647
};
648+
649+
/**
650+
* Lookup file names at the given `path`.
651+
*
652+
* @description
653+
* Filenames are returned in _traversal_ order by the OS/filesystem.
654+
* **Make no assumption that the names will be sorted in any fashion.**
655+
*
656+
* @public
657+
* @alias module:lib/cli.lookupFiles
658+
* @param {string} filepath - Base path to start searching from.
659+
* @param {string[]} [extensions=[]] - File extensions to look for.
660+
* @param {boolean} [recursive=false] - Whether to recurse into subdirectories.
661+
* @return {string[]} An array of paths.
662+
* @throws {Error} if no files match pattern.
663+
* @throws {TypeError} if `filepath` is directory and `extensions` not provided.
664+
* @deprecated Moved to {@link module:lib/cli.lookupFiles}
665+
*/
666+
exports.lookupFiles = (...args) => {
667+
if (exports.isBrowser()) {
668+
throw require('./errors').createUnsupportedError(
669+
'lookupFiles() is only supported in Node.js!'
670+
);
671+
}
672+
exports.deprecate(
673+
'`lookupFiles()` in module `mocha/lib/utils` has moved to module `mocha/lib/cli` and will be removed in the next major revision of Mocha'
674+
);
675+
return require('./cli').lookupFiles(...args);
676+
};

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,8 @@
173173
"./lib/nodejs/worker.js": false,
174174
"./lib/nodejs/buffered-worker-pool.js": false,
175175
"./lib/nodejs/parallel-buffered-runner.js": false,
176-
"./lib/nodejs/reporters/parallel-buffered.js": false
176+
"./lib/nodejs/reporters/parallel-buffered.js": false,
177+
"./lib/cli/index.js": false
177178
},
178179
"prettier": {
179180
"singleQuote": true,

test/unit/utils.spec.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -742,4 +742,49 @@ describe('lib/utils', function() {
742742
expect(utils.slug('poppies & fritz'), 'to be', 'poppies-fritz');
743743
});
744744
});
745+
746+
describe('lookupFiles()', function() {
747+
beforeEach(function() {
748+
sinon.stub(utils, 'deprecate');
749+
});
750+
751+
describe('when run in Node.js', function() {
752+
before(function() {
753+
if (process.browser) {
754+
return this.skip();
755+
}
756+
});
757+
758+
beforeEach(function() {
759+
sinon.stub(utils, 'isBrowser').returns(false);
760+
sinon.stub(require('../../lib/cli'), 'lookupFiles').returns([]);
761+
});
762+
763+
it('should print a deprecation message', function() {
764+
utils.lookupFiles();
765+
expect(utils.deprecate, 'was called once');
766+
});
767+
768+
it('should delegate to new location of lookupFiles()', function() {
769+
utils.lookupFiles(['foo']);
770+
expect(
771+
require('../../lib/cli').lookupFiles,
772+
'to have a call satisfying',
773+
[['foo']]
774+
).and('was called once');
775+
});
776+
});
777+
778+
describe('when run in browser', function() {
779+
beforeEach(function() {
780+
sinon.stub(utils, 'isBrowser').returns(true);
781+
});
782+
783+
it('should throw', function() {
784+
expect(() => utils.lookupFiles(['foo']), 'to throw', {
785+
code: 'ERR_MOCHA_UNSUPPORTED'
786+
});
787+
});
788+
});
789+
});
745790
});

0 commit comments

Comments
 (0)