From 5b95f0128467d096e6e7ac9948939ae3f061604d Mon Sep 17 00:00:00 2001 From: Bradley Farias Date: Tue, 13 Oct 2020 14:21:33 -0500 Subject: [PATCH] test: improve error message for policy failures PR-URL: https://github.com/nodejs/node/pull/35633 Fixes: https://github.com/nodejs/node/issues/35600 Reviewed-By: Rich Trott Reviewed-By: James M Snell --- test/common/README.md | 5 +++++ test/common/index.js | 15 +++++++++++++++ test/parallel/test-policy-dependencies.js | 1 + .../parallel/test-policy-dependency-conditions.js | 1 + test/parallel/test-policy-integrity-flag.js | 1 + test/parallel/test-policy-parse-integrity.js | 1 + test/parallel/test-policy-scopes-dependencies.js | 1 + test/parallel/test-policy-scopes-integrity.js | 1 + test/parallel/test-policy-scopes.js | 1 + test/pummel/test-policy-integrity.js | 1 + 10 files changed, 28 insertions(+) diff --git a/test/common/README.md b/test/common/README.md index 31d37d7abd2b4a..5ce7473cb519fc 100644 --- a/test/common/README.md +++ b/test/common/README.md @@ -378,6 +378,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 b75c5441d2e3d5..4ec68cf5857a9f 100644 --- a/test/common/index.js +++ b/test/common/index.js @@ -697,6 +697,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, @@ -736,6 +750,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');