From a9ff9c091884d8410dd095b155cd645279ccdd9e Mon Sep 17 00:00:00 2001 From: Richard Lau Date: Sun, 18 Apr 2021 14:56:24 -0400 Subject: [PATCH] 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 Reviewed-By: Colin Ihrig Reviewed-By: James M Snell --- test/common/README.md | 8 +++++--- test/common/index.js | 4 ++-- test/pummel/test-policy-integrity.js | 1 + 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/test/common/README.md b/test/common/README.md index f064ab6e7e14be..a737bca2de5e9f 100644 --- a/test/common/README.md +++ b/test/common/README.md @@ -377,10 +377,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` [<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)` diff --git a/test/common/index.js b/test/common/index.js index 7480368a55ebb3..ae18ee6a75f10c 100644 --- a/test/common/index.js +++ b/test/common/index.js @@ -699,8 +699,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)) { diff --git a/test/pummel/test-policy-integrity.js b/test/pummel/test-policy-integrity.js index 15124aefd46904..c29bb7a7b3f90e 100644 --- a/test/pummel/test-policy-integrity.js +++ b/test/pummel/test-policy-integrity.js @@ -76,6 +76,7 @@ function newTestId() { return nextTestId++; } tmpdir.refresh(); +common.requireNoPackageJSONAbove(tmpdir.path); let spawned = 0; const toSpawn = [];