mocha icon indicating copy to clipboard operation
mocha copied to clipboard

🚀 Feature: Add option to follow symlinks found by glob patterns

Open mbeerta-factset opened this issue 6 years ago • 8 comments

Prerequisites

  • [x] Checked that your issue hasn't already been filed by cross-referencing issues with the faq label
  • [x] Checked next-gen ES issues and syntax problems by using the same environment and/or transpiler configuration without Mocha to ensure it isn't just a feature that actually isn't supported in the environment in question or a bug in your code.
  • [x] 'Smoke tested' the code to be tested by running it outside the real test suite to get a better sense of whether the problem is in the code under test, your usage of Mocha, or Mocha itself
  • [x] Ensured that there is no discrepancy between the locally and globally installed versions of Mocha. You can find them with: node node_modules/.bin/mocha --version(Local) and mocha --version(Global). We recommend avoiding the use of globally installed Mocha.

Description

This is related to #1223 where util.lookupFiles does not follow symlinks and this still happens if provided with a glob pattern. The solution would be to add a {follow: true} argument to the glob.sync call.

Steps to Reproduce

Create a symlink to a directory containing test files, call mocha with a glob to that directory.

Expected behavior: Tests in symlinked folders will be executed

Actual behavior: Tests are ignored

Reproduces how often: every time

Versions

  • The output of mocha --version and node node_modules/.bin/mocha --version: latest
  • The output of node --version:
  • The version and architecture of your operating system: X64 Linux
  • Your shell (bash, zsh, PowerShell, cmd, etc.): bash
  • Your browser and version (if running browser tests): -
  • Any other third party Mocha related modules (with versions): -
  • The code transpiler being used: -

Additional Information

I'm happy to create a pull request, however i'm not familiar with the implications such a change might have.

mbeerta-factset avatar Nov 28 '18 11:11 mbeerta-factset

I'm not sure if we specify a minimum Windows OS version, so someone else will need to interpret its impact. Unsure whether this was done intentionally to "return the same results" on all platforms.

See NTFS symlinks.

plroebuck avatar Nov 28 '18 23:11 plroebuck

fast-glob is support followSymlinkedDirectories

so i think can replace glob => fast-glob in dependencies

bluelovers avatar Nov 29 '18 21:11 bluelovers

Maybe this should be added as an additional flag rather than the default behavior? I added the "future" tag since I believe it might be a good idea to land yargs first (https://github.com/mochajs/mocha/pull/3556) (feel free to disagree)

Bamieh avatar Dec 03 '18 08:12 Bamieh

Agree it would probably not be a great idea to make this the default behavior.

@mbeerta-factset Are there any workarounds at present time?

boneskull avatar Dec 04 '18 22:12 boneskull

unfortunately not, but i had the same thoughts of making this switchable. Might be reusing the --preserve-symlinks option a viable approach (although it is also passed to the node runtime)?

mbeerta-factset avatar Dec 05 '18 04:12 mbeerta-factset

Hi, I would love to do this issue if it isn't fixed yet.

shubham-rocko avatar Jan 21 '20 10:01 shubham-rocko

I'm posting here my current workaround (via monkeypatching) for this:

mocha-monkeypatched.js:

// monkeypatching libs
var Module = require('module');
var originalRequire = Module.prototype.require;

let globMonkeyPatched = false;

Module.prototype.require = function () {
    const original = originalRequire.apply(this, arguments);

    if (!globMonkeyPatched && "glob" === arguments[0]) {
        // BUG #1: cf. https://github.com/mochajs/mocha/issues/3584
        // mocha doesn't detect test files in linked dirs
        var originalSync = original.sync;
        original.sync = function () {
            const options = arguments[1];
            options.follow = true;
            return originalSync.apply(this, arguments);
        }
        globMonkeyPatched = true;
    }

    return original;
};

// BUG #2: cf. https://github.com/paulmillr/chokidar/issues/987
// mocha cannot watch multiple file extensions; hence this workaround
process.argv.push("-w", "--watch-files", ".");

// INIT_CWD only exists when launched w/ npm run ...; if needed to run as stand alone, a solution around process.argv[1] can be built
require(process.env.INIT_CWD + "/node_modules/mocha/bin/_mocha");

package.json:

    "test": "set NODE_OPTIONS=--preserve-symlinks && node mocha-monkeypatched"

NOTE: I also discovered another issue regarding linked files, but related to watching.

cristian-spiescu avatar Mar 22 '20 11:03 cristian-spiescu

👍 that making this the default behavior is too scary of a change, but that as a new option it would be good.

JoshuaKGoldberg avatar Feb 06 '24 21:02 JoshuaKGoldberg