Skip to content

Commit 5b95f01

Browse files
bmeckaduh95
authored andcommitted
test: improve error message for policy failures
PR-URL: #35633 Fixes: #35600 Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent caa5a05 commit 5b95f01

10 files changed

+28
-0
lines changed

test/common/README.md

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

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

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

test/common/index.js

+15
Original file line numberDiff line numberDiff line change
@@ -697,6 +697,20 @@ function gcUntil(name, condition) {
697697
});
698698
}
699699

700+
function requireNoPackageJSONAbove() {
701+
let possiblePackage = path.join(__dirname, '..', 'package.json');
702+
let lastPackage = null;
703+
while (possiblePackage !== lastPackage) {
704+
if (fs.existsSync(possiblePackage)) {
705+
assert.fail(
706+
'This test shouldn\'t load properties from a package.json above ' +
707+
`its file location. Found package.json at ${possiblePackage}.`);
708+
}
709+
lastPackage = possiblePackage;
710+
possiblePackage = path.join(possiblePackage, '..', '..', 'package.json');
711+
}
712+
}
713+
700714
const common = {
701715
allowGlobals,
702716
buildType,
@@ -736,6 +750,7 @@ const common = {
736750
platformTimeout,
737751
printSkipMessage,
738752
pwdCommand,
753+
requireNoPackageJSONAbove,
739754
runWithInvalidFD,
740755
skip,
741756
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)