From 52f07f01894504615cd412c91cfad8e9b8a6d76e Mon Sep 17 00:00:00 2001 From: Christopher Hiller Date: Wed, 26 Aug 2020 15:26:52 -0700 Subject: [PATCH] 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 --- lib/utils.js | 29 ++++++++++++++++++++++++++ package.json | 3 ++- test/unit/utils.spec.js | 45 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 76 insertions(+), 1 deletion(-) diff --git a/lib/utils.js b/lib/utils.js index 64dfe9b964..e6eed672ac 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -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); +}; diff --git a/package.json b/package.json index e409e4a6cd..625a4140a5 100644 --- a/package.json +++ b/package.json @@ -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, diff --git a/test/unit/utils.spec.js b/test/unit/utils.spec.js index 9fa6cc74e4..3fa408d064 100644 --- a/test/unit/utils.spec.js +++ b/test/unit/utils.spec.js @@ -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' + }); + }); + }); + }); });