test: add ancestor package.json checks for tmpdir

Policy tests can fail if a `package.json` exists in any of the parent
directories above the test. The existing checks are done for the
ancestors of the test directory but some tests execute from the tmpdir.

PR-URL: https://github.com/nodejs/node/pull/38285
Refs: https://github.com/nodejs/node/issues/38088
Refs: https://github.com/nodejs/node/issues/35600
Refs: https://github.com/nodejs/node/pull/35633
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
Richard Lau authored and Rich Trott committed 2021-04-23 06:34:53 -07:00
1 parent 4f11b8b8ae
commit f9e07e432b
3 files changed
+8 -5

No files matched your search

+5 -3
View File
@@ -370,10 +370,12 @@ const { spawn } = require('child_process');
spawn(...common.pwdCommand, { stdio: ['pipe'] });
```
### `requireNoPackageJSONAbove()`
### `requireNoPackageJSONAbove([dir])`
Throws an `AssertionError` if a `package.json` file is in any ancestor
directory. Such files may interfere with proper test functionality.
* `dir` [&lt;string>][] default = \_\_dirname
Throws an `AssertionError` if a `package.json` file exists in any ancestor
directory above `dir`. Such files may interfere with proper test functionality.
### `runWithInvalidFD(func)`
+2 -2
View File
@@ -708,8 +708,8 @@ function gcUntil(name, condition) {
});
}
function requireNoPackageJSONAbove() {
let possiblePackage = path.join(__dirname, '..', 'package.json');
function requireNoPackageJSONAbove(dir = __dirname) {
let possiblePackage = path.join(dir, '..', 'package.json');
let lastPackage = null;
while (possiblePackage !== lastPackage) {
if (fs.existsSync(possiblePackage)) {
+1
View File
@@ -76,6 +76,7 @@ function newTestId() {
return nextTestId++;
}
tmpdir.refresh();
common.requireNoPackageJSONAbove(tmpdir.path);
let spawned = 0;
const toSpawn = [];