Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix extension and spec handling; closes #3808 #3834

Merged
merged 2 commits into from May 5, 2019
Merged

Conversation

boneskull
Copy link
Member

Also fixes some issues in the options unit tests and adds coverage around spec handling.

This needs some manual testing, as I had to monkey with watch a bit.

cc @dedene

@boneskull boneskull added type: bug a defect, confirmed by a maintainer area: node.js command-line-or-Node.js-specific labels Mar 14, 2019
@boneskull boneskull self-assigned this Mar 14, 2019
lib/utils.js Outdated
return true;
}
})
) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only reason this was here was to allow the historical usage of "test.js" via the default spec "test". IMO, we should not propagate this further.

The "Handle directory" code below already handles extensions for "test/*".

@@ -10,7 +10,8 @@ const fs = require('fs');
const yargsParser = require('yargs-parser');
const {types, aliases} = require('./run-option-metadata');
const {ONE_AND_DONE_ARGS} = require('./one-and-dones');
const mocharc = require('../mocharc.json');
// paranoia
const mocharc = Object.freeze(require('../mocharc.json'));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const rcDefaults = Object.freeze(require('../mocharc.json'));

lib/cli/options.js Outdated Show resolved Hide resolved
lib/cli/options.js Outdated Show resolved Hide resolved
@plroebuck
Copy link
Contributor

Rebase

@boneskull boneskull assigned boneskull and unassigned boneskull Mar 14, 2019
@juergba
Copy link
Member

juergba commented Mar 19, 2019

I think there is a standard yargs way to achieve this, using the "default" option. I just did some short testing, nevertheless it seems to work.

// line 109
const result = yargsParser.detailed(args, {
    default: {extension: ["js"]},      // set default values with "mocharc"
    configuration,
    configObjects,
    coerce: coerceOpts,
    narg: nargOpts,
    alias: aliases,
    string: types.string,
    array: types.array,
    number: types.number,
    boolean: types.boolean.concat(nodeArgs.map(pair => pair[0]))
  });
// line 326
args = parse(
    args._,
    args,
    rcConfig || {},
    pkgConfig || {},
    optsConfig || {},
    //mocharc                           don't set default values here
  );

@boneskull
Copy link
Member Author

@juergba Hmm, you're right.

I think instead of this strategy, I'll change it so that we can still keep the default config in our lib/mocharc.json, but use its values as defaults instead of just another config object which is merged in.

@juergba
Copy link
Member

juergba commented Mar 27, 2019

Also in "lib/cli/run-helpers.js" the default extension of "js" is set hardcoded several times.
i.e. watchRun() or runMocha().

@juergba
Copy link
Member

juergba commented Apr 25, 2019

@boneskull is it ok for you, if I work on this one?

@juergba juergba added the semver-patch implementation requires increase of "patch" version number; "bug fixes" label Apr 25, 2019
@coveralls
Copy link

coveralls commented Apr 25, 2019

Coverage Status

Coverage increased (+0.08%) to 91.819% when pulling 2d1dc34 on boneskull/issue/3808 into a4f1a44 on master.

@boneskull
Copy link
Member Author

@juergba yes, go ahead

@juergba juergba added the status: needs review a maintainer should (re-)review this pull request label Apr 26, 2019
@juergba juergba added this to the next milestone Apr 26, 2019
@juergba juergba merged commit aed20bd into master May 5, 2019
@juergba juergba removed the status: needs review a maintainer should (re-)review this pull request label May 5, 2019
@juergba juergba deleted the boneskull/issue/3808 branch May 5, 2019 07:01
@plroebuck
Copy link
Contributor

@juergba second time now you merged without any review (despite prior request to do otherwise) and this time you introduced bug...

@plroebuck
Copy link
Contributor

Corrected code...

"lib/utils.js"

/**
 * 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
 * @memberof Mocha.utils
 * @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 file pathnames.
 * @throws {Error} if no files match pattern.
 * @throws {TypeError} if `filepath` is directory and `extensions` not provided or empty.
 */
exports.lookupFiles = function lookupFiles(filepath, extensions, recursive) {
  var files = [];
  var stat;

  if (extensions === undefined) {
    extensions = [];
  }
  if (recursive === undefined) {
    recursive = false;
  }

  if (!fs.existsSync(filepath)) {
    // Check all extensions, using first match found (if any)
    if (
      !(extensions.some(function(ext) {
        var filepathWithExtname = filepath + '.' + ext;
        if (fs.existsSync(filepathWithExtname)) {
          filepath = filepathWithExtname;
          return true;
        }
      })
    ) {
      // Handle glob
      files = glob.sync(filepath);
      if (!files.length) {
        throw createNoFilesMatchPatternError(
          'Cannot find any files matching pattern ' + exports.dQuote(filepath),
          filepath
        );
      }
      return files;
    }
  }

  // ...no further changes to merged code...
}

@juergba
Copy link
Member

juergba commented May 8, 2019

@plroebuck
Yes, without @boneskull contributions to "needs-review"-PRs have been close to null during the last few weeks. There is no one stepping into his gap, that's worrying.

you introduced bug... can you be more ... specific?

corrected code: this is the solution @boneskull already proposed, so nothing new. It only uses the first match of filepath + '.' + ext and ignores the rest of matches. I wonder why this should be correct.

@plroebuck
Copy link
Contributor

As I commented originally in his PR, the part of the code that added ".js" should probably have been left alone as an anachronism; it allows "test.js" to satisfy Mocha's default, rather than just putting spec files in a "test" directory. But filepath (in file or directory mode) cannot have multiple names, which is why .some() is used. You moved the return (intended solely for glob handling) and bypassed the remaining code in the function. But that code you skipped over provided handling of directories and ensured that only files were returned.

To bust your PR, create a directory named "test.js" and run lookupFiles (with extensions=['js']).
It now brokenly returns a directory.

Please use the corrected code above to fix this.

@plroebuck
Copy link
Contributor

Yes, without @boneskull contributions to "needs-review"-PRs have been close to null during the last few weeks. There is no one stepping into his gap, that's worrying.

True, but almost no PRs here are ever critical; they can sit until needed. Create your PR and add "NeedsReview" tag when ready; make sure and add reviewers (especially ones who might actually do so). Then give it a week. If no one reviewed it by that point, make comment on the PR announcing intent to merge within n days (2 would be nice). If still no review and/or comment after deadline, then merge.

@juergba
Copy link
Member

juergba commented May 9, 2019

As I commented originally in his PR [...]

You are right, a directory named "test.js" will fail. I will open a PR and use your proposition. I don't agree completely, though. With two files (eg. "unit.ts" and "unit.js") a call "unit" plus extension: ['ts', 'js'] should return both files, not just some/the first one.
I will also have to check wether the glob always returns files.

EDIT: the glob returns files and directories, so this function has never really worked correctly. The directories could be skipped with option.nodir: true

see #3905

True, but almost no PRs here [...]

I can live with that procedure.

@plroebuck
Copy link
Contributor

You are right, a directory named "test.js" will fail. I will open a PR and use your proposition. I don't agree completely, though. With two files (eg. "unit.ts" and "unit.js") a call "unit" plus extension: ['ts', 'js'] should return both files, not just some/the first one.

The filepath + '.js' hack was never intended to handle what you're trying to do with it. Its purpose was to allow small packages that only exported a couple functions (typically from "index.js") to use "test.js" to test them.

If a package is exporting functions in more than one language, its specifications should live in the "test" directory -- the simplistic specification scheme above shouldn't apply.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: node.js command-line-or-Node.js-specific semver-patch implementation requires increase of "patch" version number; "bug fixes" type: bug a defect, confirmed by a maintainer
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants