diff --git a/test/common/README.md b/test/common/README.md index 539bbd9cd8f825..07bc3e180df3f2 100644 --- a/test/common/README.md +++ b/test/common/README.md @@ -377,6 +377,11 @@ const { spawn } = require('child_process'); spawn(...common.pwdCommand, { stdio: ['pipe'] }); ``` +### `requireNoPackageJSONAbove()` + +Throws an `AssertionError` if a `package.json` file is in any ancestor +directory. Such files may interfere with proper test functionality. + ### `runWithInvalidFD(func)` * `func` [<Function>][] diff --git a/test/common/index.js b/test/common/index.js index 1351a18d4118eb..95ffa28a2cc3d4 100644 --- a/test/common/index.js +++ b/test/common/index.js @@ -699,6 +699,20 @@ function gcUntil(name, condition) { }); } +function requireNoPackageJSONAbove() { + let possiblePackage = path.join(__dirname, '..', 'package.json'); + let lastPackage = null; + while (possiblePackage !== lastPackage) { + if (fs.existsSync(possiblePackage)) { + assert.fail( + 'This test shouldn\'t load properties from a package.json above ' + + `its file location. Found package.json at ${possiblePackage}.`); + } + lastPackage = possiblePackage; + possiblePackage = path.join(possiblePackage, '..', '..', 'package.json'); + } +} + const common = { allowGlobals, buildType, @@ -737,6 +751,7 @@ const common = { platformTimeout, printSkipMessage, pwdCommand, + requireNoPackageJSONAbove, runWithInvalidFD, skip, skipIf32Bits, diff --git a/test/parallel/test-policy-dependencies.js b/test/parallel/test-policy-dependencies.js index e7b2289714dff4..4486e0f8aa08c0 100644 --- a/test/parallel/test-policy-dependencies.js +++ b/test/parallel/test-policy-dependencies.js @@ -3,6 +3,7 @@ const common = require('../common'); if (!common.hasCrypto) common.skip('missing crypto'); +common.requireNoPackageJSONAbove(); const fixtures = require('../common/fixtures'); diff --git a/test/parallel/test-policy-dependency-conditions.js b/test/parallel/test-policy-dependency-conditions.js index c9db95929daeec..dec17aa22984b0 100644 --- a/test/parallel/test-policy-dependency-conditions.js +++ b/test/parallel/test-policy-dependency-conditions.js @@ -4,6 +4,7 @@ const common = require('../common'); if (!common.hasCrypto) common.skip('missing crypto'); +common.requireNoPackageJSONAbove(); const Manifest = require('internal/policy/manifest').Manifest; diff --git a/test/parallel/test-policy-integrity-flag.js b/test/parallel/test-policy-integrity-flag.js index d97ef86cbe9d0f..ddcd02236d27c0 100644 --- a/test/parallel/test-policy-integrity-flag.js +++ b/test/parallel/test-policy-integrity-flag.js @@ -3,6 +3,7 @@ const common = require('../common'); if (!common.hasCrypto) common.skip('missing crypto'); +common.requireNoPackageJSONAbove(); const fixtures = require('../common/fixtures'); diff --git a/test/parallel/test-policy-parse-integrity.js b/test/parallel/test-policy-parse-integrity.js index d042ccee0a49af..3a31d0d57f25db 100644 --- a/test/parallel/test-policy-parse-integrity.js +++ b/test/parallel/test-policy-parse-integrity.js @@ -2,6 +2,7 @@ const common = require('../common'); if (!common.hasCrypto) common.skip('missing crypto'); +common.requireNoPackageJSONAbove(); const tmpdir = require('../common/tmpdir'); const assert = require('assert'); diff --git a/test/parallel/test-policy-scopes-dependencies.js b/test/parallel/test-policy-scopes-dependencies.js index 202e6459a6543e..a5a9302ac69629 100644 --- a/test/parallel/test-policy-scopes-dependencies.js +++ b/test/parallel/test-policy-scopes-dependencies.js @@ -4,6 +4,7 @@ const common = require('../common'); if (!common.hasCrypto) common.skip('missing crypto'); +common.requireNoPackageJSONAbove(); const Manifest = require('internal/policy/manifest').Manifest; const assert = require('assert'); diff --git a/test/parallel/test-policy-scopes-integrity.js b/test/parallel/test-policy-scopes-integrity.js index b61fc3199cd4e0..8c91cea142b903 100644 --- a/test/parallel/test-policy-scopes-integrity.js +++ b/test/parallel/test-policy-scopes-integrity.js @@ -4,6 +4,7 @@ const common = require('../common'); if (!common.hasCrypto) common.skip('missing crypto'); +common.requireNoPackageJSONAbove(); const Manifest = require('internal/policy/manifest').Manifest; const assert = require('assert'); diff --git a/test/parallel/test-policy-scopes.js b/test/parallel/test-policy-scopes.js index b1ba160bdde3f1..129287c73c7659 100644 --- a/test/parallel/test-policy-scopes.js +++ b/test/parallel/test-policy-scopes.js @@ -3,6 +3,7 @@ const common = require('../common'); if (!common.hasCrypto) common.skip('missing crypto'); +common.requireNoPackageJSONAbove(); const fixtures = require('../common/fixtures'); diff --git a/test/pummel/test-policy-integrity.js b/test/pummel/test-policy-integrity.js index 57625268fae4d0..15124aefd46904 100644 --- a/test/pummel/test-policy-integrity.js +++ b/test/pummel/test-policy-integrity.js @@ -2,6 +2,7 @@ const common = require('../common'); if (!common.hasCrypto) common.skip('missing crypto'); +common.requireNoPackageJSONAbove(); const { debuglog } = require('util'); const debug = debuglog('test');