1'use strict'; 2 3const common = require('../common'); 4if (!common.hasCrypto) 5 common.skip('missing crypto'); 6common.requireNoPackageJSONAbove(); 7 8const fixtures = require('../common/fixtures'); 9 10const assert = require('assert'); 11const { spawnSync } = require('child_process'); 12 13const dep = fixtures.path('policy', 'parent.js'); 14{ 15 const depPolicy = fixtures.path( 16 'policy', 17 'dependencies', 18 'dependencies-redirect-policy.json'); 19 const { status, stderr, stdout } = spawnSync( 20 process.execPath, 21 [ 22 '--experimental-policy', depPolicy, dep, 23 ] 24 ); 25 console.log('%s\n%s', stderr, stdout); 26 assert.strictEqual(status, 0); 27} 28{ 29 const depPolicy = fixtures.path( 30 'policy', 31 'dependencies', 32 'dependencies-redirect-builtin-policy.json'); 33 const { status } = spawnSync( 34 process.execPath, 35 [ 36 '--experimental-policy', depPolicy, dep, 37 ] 38 ); 39 assert.strictEqual(status, 0); 40} 41{ 42 const depPolicy = fixtures.path( 43 'policy', 44 'dependencies', 45 'dependencies-redirect-unknown-builtin-policy.json'); 46 const { status } = spawnSync( 47 process.execPath, 48 [ 49 '--experimental-policy', depPolicy, dep, 50 ] 51 ); 52 assert.strictEqual(status, 1); 53} 54{ 55 const depPolicy = fixtures.path( 56 'policy', 57 'dependencies', 58 'dependencies-wildcard-policy.json'); 59 const { status, stderr, stdout } = spawnSync( 60 process.execPath, 61 [ 62 '--experimental-policy', depPolicy, dep, 63 ] 64 ); 65 console.log('%s\n%s', stderr, stdout); 66 assert.strictEqual(status, 0); 67} 68{ 69 const depPolicy = fixtures.path( 70 'policy', 71 'dependencies', 72 'dependencies-empty-policy.json'); 73 const { status } = spawnSync( 74 process.execPath, 75 [ 76 '--experimental-policy', depPolicy, dep, 77 ] 78 ); 79 assert.strictEqual(status, 1); 80} 81{ 82 const depPolicy = fixtures.path( 83 'policy', 84 'dependencies', 85 'dependencies-missing-policy-default-true.json'); 86 const { status } = spawnSync( 87 process.execPath, 88 [ 89 '--experimental-policy', depPolicy, dep, 90 ] 91 ); 92 assert.strictEqual(status, 0); 93} 94{ 95 const depPolicy = fixtures.path( 96 'policy', 97 'dependencies', 98 'dependencies-missing-policy.json'); 99 const { status } = spawnSync( 100 process.execPath, 101 [ 102 '--experimental-policy', depPolicy, dep, 103 ] 104 ); 105 assert.strictEqual(status, 1); 106} 107{ 108 // Regression test for https://github.com/nodejs/node/issues/37812 109 const depPolicy = fixtures.path( 110 'policy', 111 'dependencies', 112 'dependencies-missing-export-policy.json'); 113 const { status, stderr } = spawnSync( 114 process.execPath, 115 [ 116 '--experimental-policy', 117 depPolicy, 118 fixtures.path('policy', 'bad-main.mjs'), 119 ] 120 ); 121 assert.strictEqual(status, 1); 122 assert.match( 123 `${stderr}`, 124 /SyntaxError: Named export 'doesNotExist' not found\./, 125 new Error('Should give the real SyntaxError and position')); 126} 127{ 128 const depPolicy = fixtures.path( 129 'policy', 130 'dependencies', 131 'dependencies-scopes-relative-specifier.json'); 132 const { status } = spawnSync( 133 process.execPath, 134 [ 135 '--experimental-policy', 136 depPolicy, 137 fixtures.path('policy', 'canonicalize.mjs'), 138 ] 139 ); 140 assert.strictEqual( 141 status, 142 0, 143 new Error( 144 'policies should canonicalize specifiers by default prior to matching') 145 ); 146} 147