From c736883046e9f1ccb86c5ceca83be9ea0e36a949 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 2f2ee17a0f01e4..29f073b28842a7 100644 --- a/test/common/README.md +++ b/test/common/README.md @@ -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` [<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 f0d8c72d4a1017..f40914c94928dd 100644 --- a/test/common/index.js +++ b/test/common/index.js @@ -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)) { 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 = [];