Skip to content

Commit

Permalink
test: add ancestor package.json checks for tmpdir
Browse files Browse the repository at this point in the history
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: nodejs#38285
Refs: nodejs#38088
Refs: nodejs#35600
Refs: nodejs#35633
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
richardlau authored and sbaayel committed Feb 17, 2024
1 parent daf4841 commit c736883
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 5 deletions.
8 changes: 5 additions & 3 deletions test/common/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)`

Expand Down
4 changes: 2 additions & 2 deletions test/common/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
1 change: 1 addition & 0 deletions test/pummel/test-policy-integrity.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ function newTestId() {
return nextTestId++;
}
tmpdir.refresh();
common.requireNoPackageJSONAbove(tmpdir.path);

let spawned = 0;
const toSpawn = [];
Expand Down

0 comments on commit c736883

Please sign in to comment.