-
Notifications
You must be signed in to change notification settings - Fork 30.1k
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: remove common.allowGlobals #25256
Conversation
It loads all modules? I don't see that in that test. How do we make sure (for example) that |
I'm interested that you think this should be removed, as I've kind of felt it's ideally the only thing |
It does not load the internal modules so far (which I thought is not really required due to implicitly loading them when loading the regular modules but I am happy to load them as well by adding the
node/test/parallel/test-global.js Lines 30 to 41 in c992639
Here I made sure to load all regular modules up front. Therefore the child_process is also loaded before verifying the globals right afterwards. node/test/parallel/test-global.js Lines 44 to 63 in c992639
Theoretically, the execution of some code could still add a global somewhere but I feel very safe with this test (and in the very unlikely case that not, we would still have our code reviews). |
What is the advantage in removing it? It doesn't seem to be a maintenance burden and to have a significant overhead when running tests so I think that what we lose is more than what we gain if we remove it. |
I don't share this sentiment, and am -1 to removing this. |
@cjihrig would a lint rule be sufficient on top to detect attaching things to global? @lpinca the only thing I know is that we had to change this code multiple times (and we also made mistakes while working on it) and we have to make sure that our tests work properly with this (which is not that much work since we normally do not manipulate globals in tests either). Are we really running into any danger adding globals to the code base without this check? For me this is just a bunch of code in the common part that does not seem to have a real benefit. If we keep this check, we should remove the part in the I'll keep this open a bit longer but if I am really alone with my view, I'll close it depending on following comments / in a few days. |
How did I miss that? Yeah, that seems fine. Sorry to make you spell it out in such painful detail. I can imagine code in a module that would pollute the global namespace that wouldn't get caught by the one test. exports.foo = () => { global.fhqwhgads = 'come on'; } That won't pollute the global namespace by loading the module, but it will by executing I guess the questions are: Is that scenario above all that likely? And is the cost of checking high? I'd say it's low risk but also low cost to check. |
I just added a custom eslint rule to verify that no global is being accessed at all anywhere under I removed access to it from all files that did not require it and the rule is only ignored in a single file: the bootstrap file where we actually go ahead and add such entries regularly. Now any code that modifies them would also have to deactivate the eslint rule to do so. Opinions? |
Seems OK, but there are things that ESLint can't catch that our current setup would, like polluting global through function setup(obj, key, val) {
obj[key] = val;
}
setup(global, 'foo', 'bar'); As a general guideline, linting is for style and testing is for correctness, so this is properly the purview of tests, not linting. |
@Trott your example would be caught because is using
I don't fully agree: static analysis is very strong and we catch many errors early on which our tests might not always detect. Other languages do the same: they just don't compile in cases where eslint finds correctness issues :D |
4528ebd
to
33928f7
Compare
33928f7
to
437930e
Compare
Even with this PR I updated the custom eslint rule to detect global usage and to prohibit that in @nodejs/testing is this something we want to do or should I close this PR? |
This removes the globals check that was executed in all tests so far. Instead just rely on the explicit globals test to verify that the globals did not leak.
5fdd233
to
7814a35
Compare
This adds a custom eslint rule to make sure the globals are not polluted in any way by adding extra properties to them in any way. Besides that it also removes access to `global` by marking some globals as available to eslint.
7814a35
to
631ed0a
Compare
I am frequently running into issues due to the current test. I often run tests with older Node.js versions to compare some output and that is not possible since tests fail due to added globals in newer Node.js versions. I would really love to make some progress here and get some reviews or at least further opinions.
|
I don't think tests are supposed to work on older versions of Node.js anyway? If this is necessary, I think a better way to solve this is to add a special environment variable that skips the global check, instead of just removing the check unconditionally. |
I do the same and I'm surprised that the globals thing is a noticeable problem. I'm guessing the older versions of Node.js aren't that much older? Running with earlier than 10.x fails because So I usually end up commenting out the I remain opposed to removing the global leak check. I agree with @joyeecheung that an environment variable disabling it (or a CLI flag or even a |
I normally use the latest current and mostly the tests still work in older Node.js versions, since we do not break that much. That might not apply to all code but at least to the things I often work with.
Correct, while I some times also use older versions, I mainly use the latest current to verify that a test change would have the identical behavior in the older version.
I agree. I personally would like common to be used less. Ideally most tests would just work without most common functionality. That way a lot of tests would work significantly better across multiple Node.js versions.
It seems like most people still want to keep it. Thanks for your feedback. |
This removes the globals check that was executed in all tests so far.
Instead just rely on the explicit globals test to verify that the
globals did not leak.
We have dedicated tests for the globals (/test/parallel/test-global.js) which load all modules up front to make sure we definitely get all code which might add globals.
This test is very old and comes from times where we neither had code reviews nor good tests. Now we have both and I don't think we should run this code on each test file anymore.
Checklist
make -j4 test
(UNIX), orvcbuild test
(Windows) passes