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

test: add test for Module._stat #44713

Merged
merged 6 commits into from Oct 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 25 additions & 0 deletions test/parallel/test-module-stat.js
@@ -0,0 +1,25 @@
'use strict';
require('../common');

// This tests Module._stat.

const Module = require('module');
const fs = require('fs');
const tmpdir = require('../common/tmpdir');
const { ok, strictEqual } = require('assert');
const { join } = require('path');

const directory = join(tmpdir.path, 'directory');
const doesNotExist = join(tmpdir.path, 'does-not-exist');
const file = join(tmpdir.path, 'file.js');

tmpdir.refresh();
fs.writeFileSync(file, "module.exports = { a: 'b' }");
fs.mkdirSync(directory);

strictEqual(Module._stat(directory), 1); // Returns 1 for directories.
strictEqual(Module._stat(file), 0); // Returns 0 for files.
ok(Module._stat(doesNotExist) < 0); // Returns a negative integer for any other kind of strings.

// TODO(RaisinTen): Add tests that make sure that Module._stat() does not crash when called
// with a non-string data type. It crashes currently.
88 changes: 88 additions & 0 deletions test/parallel/test-vfs.js
@@ -0,0 +1,88 @@
'use strict';
const common = require('../common');

// This tests the creation of a vfs by monkey-patching fs and Module._stat.
GeoffreyBooth marked this conversation as resolved.
Show resolved Hide resolved

const Module = require('module');
const fs = require('fs');
const tmpdir = require('../common/tmpdir');
const { deepStrictEqual, ok, strictEqual, throws } = require('assert');
const { join } = require('path');

const directory = join(tmpdir.path, 'directory');
const doesNotExist = join(tmpdir.path, 'does-not-exist');
const file = join(tmpdir.path, 'file.js');

tmpdir.refresh();
fs.writeFileSync(file, "module.exports = { a: 'b' }");
fs.mkdirSync(directory);

strictEqual(Module._stat(directory), 1);
ok(Module._stat(doesNotExist) < 0);
strictEqual(Module._stat(file), 0);

const vfsDirectory = join(process.execPath, 'directory');
const vfsDoesNotExist = join(process.execPath, 'does-not-exist');
const vfsFile = join(process.execPath, 'file.js');

ok(Module._stat(vfsDirectory) < 0);
ok(Module._stat(vfsDoesNotExist) < 0);
ok(Module._stat(vfsFile) < 0);

deepStrictEqual(require(file), { a: 'b' });
throws(() => require(vfsFile), { code: 'MODULE_NOT_FOUND' });

common.expectWarning(
'ExperimentalWarning',
'Module._stat is an experimental feature. This feature could change at any time');

process.on('warning', common.mustCall());

const originalStat = Module._stat;
Module._stat = function(filename) {
RaisinTen marked this conversation as resolved.
Show resolved Hide resolved
if (!filename.startsWith(process.execPath)) {
return originalStat(filename);
}

if (filename === process.execPath) {
return 1;
}

switch (filename) {
case vfsDirectory:
return 1;
case vfsDoesNotExist:
return -2;
case vfsFile:
return 0;
}
};

const originalReadFileSync = fs.readFileSync;
// TODO(aduh95): We'd like to have a better way to achieve this without monkey-patching fs.
fs.readFileSync = function readFileSync(pathArgument, options) {
RaisinTen marked this conversation as resolved.
Show resolved Hide resolved
if (!pathArgument.startsWith(process.execPath)) {
return originalReadFileSync.apply(this, arguments);
}
if (pathArgument === vfsFile) {
return "module.exports = { x: 'y' };";
}
throw new Error();
};

fs.realpathSync = function realpathSync(pathArgument, options) {
return pathArgument;
};
Comment on lines +63 to +75
Copy link
Contributor

Choose a reason for hiding this comment

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

Wait, that's not a use case we want to support, mutating fs should not have any effect on Node.js internals, we should fix that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is actually how VFSs are implemented in the ecosystem (pkg, electron, etc.) currently and fixing that would break a lot of packages and I believe the intention behind exposing Module._stat is to allow this? I don't think there is any other use case behind Module._stat or Module._readPackage. cc @arcanis

FWIW, we are also trying to find better ways of doing this without monkey-patching in nodejs/single-executable#37.

Copy link
Contributor

Choose a reason for hiding this comment

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

It shows that we probably also need Module._realPath and Module._readFileSync – or rather, that we need the loader hook API to stabilize. Anyway, I don't know if we want this is our tests, I think we want to break this at some point.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@aduh95 if we start exposing Module._* functions for these, we would have to do so for a lot more functions. These are the ones that Electron overrides - https://github.com/electron/electron/blob/eebf34cc6c4691e2ddca9b5a0a97566aeabd9072/lib/asar/fs-wrapper.ts#L236-L854 (quite a lot!) and there are probably additional ones in yarn's fslib implementation - https://github.com/yarnpkg/berry/tree/76ccb18b3b8cc81e28dbef5f3f867395aa31d5fb/packages/yarnpkg-fslib/sources/patchFs.

Copy link
Contributor

Choose a reason for hiding this comment

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

Fwiw I personally have an expectation that Node.js should abide to its own fs API (which is part of why _stat and _readPackage were so problematic, being the two places not doing so purely for optimization purposes).

It's probably never been discussed formally before though, and perhaps doing so would be a good thing (if only to get this use case formally recognized, supported, and covered by tests).

Copy link
Contributor

Choose a reason for hiding this comment

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

I agree that the lack of consistency is quite bad. IMHO Node.js internals should not be affected by user-land actions, however I could see that we still want to support the use case of alternative fs implementation, which could be supplied by e.g. a CLI flag and would affect the whole process, not just the few files where we forgot to use destructuring.


strictEqual(Module._stat(directory), 1);
ok(Module._stat(doesNotExist) < 0);
strictEqual(Module._stat(file), 0);

strictEqual(Module._stat(vfsDirectory), 1);
ok(Module._stat(vfsDoesNotExist) < 0);
strictEqual(Module._stat(vfsFile), 0);

strictEqual(Module._stat(process.execPath), 1);

deepStrictEqual(require(file), { a: 'b' });
deepStrictEqual(require(vfsFile), { x: 'y' });