Skip to content

Commit c05f85e

Browse files
bmeckBethGriggs
authored andcommitted
test: improve error message for policy failures
PR-URL: #35633 Fixes: #35600 Reviewed-By: Rich Trott <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 62741b5 commit c05f85e

10 files changed

+28
-0
lines changed

test/common/README.md

+5
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,11 @@ const { spawn } = require('child_process');
377377
spawn(...common.pwdCommand, { stdio: ['pipe'] });
378378
```
379379

380+
### `requireNoPackageJSONAbove()`
381+
382+
Throws an `AssertionError` if a `package.json` file is in any ancestor
383+
directory. Such files may interfere with proper test functionality.
384+
380385
### `runWithInvalidFD(func)`
381386

382387
* `func` [&lt;Function>][]

test/common/index.js

+15
Original file line numberDiff line numberDiff line change
@@ -699,6 +699,20 @@ function gcUntil(name, condition) {
699699
});
700700
}
701701

702+
function requireNoPackageJSONAbove() {
703+
let possiblePackage = path.join(__dirname, '..', 'package.json');
704+
let lastPackage = null;
705+
while (possiblePackage !== lastPackage) {
706+
if (fs.existsSync(possiblePackage)) {
707+
assert.fail(
708+
'This test shouldn\'t load properties from a package.json above ' +
709+
`its file location. Found package.json at ${possiblePackage}.`);
710+
}
711+
lastPackage = possiblePackage;
712+
possiblePackage = path.join(possiblePackage, '..', '..', 'package.json');
713+
}
714+
}
715+
702716
const common = {
703717
allowGlobals,
704718
buildType,
@@ -737,6 +751,7 @@ const common = {
737751
platformTimeout,
738752
printSkipMessage,
739753
pwdCommand,
754+
requireNoPackageJSONAbove,
740755
runWithInvalidFD,
741756
skip,
742757
skipIf32Bits,

test/parallel/test-policy-dependencies.js

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
const common = require('../common');
44
if (!common.hasCrypto)
55
common.skip('missing crypto');
6+
common.requireNoPackageJSONAbove();
67

78
const fixtures = require('../common/fixtures');
89

test/parallel/test-policy-dependency-conditions.js

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
const common = require('../common');
55

66
if (!common.hasCrypto) common.skip('missing crypto');
7+
common.requireNoPackageJSONAbove();
78

89
const Manifest = require('internal/policy/manifest').Manifest;
910

test/parallel/test-policy-integrity-flag.js

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
const common = require('../common');
44
if (!common.hasCrypto)
55
common.skip('missing crypto');
6+
common.requireNoPackageJSONAbove();
67

78
const fixtures = require('../common/fixtures');
89

test/parallel/test-policy-parse-integrity.js

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const common = require('../common');
44
if (!common.hasCrypto) common.skip('missing crypto');
5+
common.requireNoPackageJSONAbove();
56

67
const tmpdir = require('../common/tmpdir');
78
const assert = require('assert');

test/parallel/test-policy-scopes-dependencies.js

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
const common = require('../common');
55

66
if (!common.hasCrypto) common.skip('missing crypto');
7+
common.requireNoPackageJSONAbove();
78

89
const Manifest = require('internal/policy/manifest').Manifest;
910
const assert = require('assert');

test/parallel/test-policy-scopes-integrity.js

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
const common = require('../common');
55

66
if (!common.hasCrypto) common.skip('missing crypto');
7+
common.requireNoPackageJSONAbove();
78

89
const Manifest = require('internal/policy/manifest').Manifest;
910
const assert = require('assert');

test/parallel/test-policy-scopes.js

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
const common = require('../common');
44
if (!common.hasCrypto)
55
common.skip('missing crypto');
6+
common.requireNoPackageJSONAbove();
67

78
const fixtures = require('../common/fixtures');
89

test/pummel/test-policy-integrity.js

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const common = require('../common');
44
if (!common.hasCrypto) common.skip('missing crypto');
5+
common.requireNoPackageJSONAbove();
56

67
const { debuglog } = require('util');
78
const debug = debuglog('test');

0 commit comments

Comments
 (0)