|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const { spawnPromisified } = require('../common'); |
| 4 | +const fixtures = require('../common/fixtures'); |
| 5 | +const { match, strictEqual } = require('node:assert'); |
| 6 | +const { test } = require('node:test'); |
| 7 | + |
| 8 | +test('should handle non existing json', async () => { |
| 9 | + const result = await spawnPromisified(process.execPath, [ |
| 10 | + '--experimental-config-file', |
| 11 | + 'i-do-not-exist.json', |
| 12 | + '-p', '"Hello, World!"', |
| 13 | + ]); |
| 14 | + match(result.stderr, /Cannot read configuration from i-do-not-exist\.json: no such file or directory/); |
| 15 | + match(result.stderr, /i-do-not-exist\.json: not found/); |
| 16 | + strictEqual(result.stdout, ''); |
| 17 | + strictEqual(result.code, 9); |
| 18 | +}); |
| 19 | + |
| 20 | +test('should handle empty json', async () => { |
| 21 | + const result = await spawnPromisified(process.execPath, [ |
| 22 | + '--experimental-config-file', |
| 23 | + fixtures.path('rc/empty.json'), |
| 24 | + '-p', '"Hello, World!"', |
| 25 | + ]); |
| 26 | + match(result.stderr, /Can't parse/); |
| 27 | + match(result.stderr, /empty\.json: invalid content/); |
| 28 | + strictEqual(result.stdout, ''); |
| 29 | + strictEqual(result.code, 9); |
| 30 | +}); |
| 31 | + |
| 32 | +test('should handle empty object json', async () => { |
| 33 | + const result = await spawnPromisified(process.execPath, [ |
| 34 | + '--no-warnings', |
| 35 | + '--experimental-config-file', |
| 36 | + fixtures.path('rc/empty-object.json'), |
| 37 | + '-p', '"Hello, World!"', |
| 38 | + ]); |
| 39 | + strictEqual(result.stderr, ''); |
| 40 | + match(result.stdout, /Hello, World!/); |
| 41 | + strictEqual(result.code, 0); |
| 42 | +}); |
| 43 | + |
| 44 | +test('should parse boolean flag', async () => { |
| 45 | + const result = await spawnPromisified(process.execPath, [ |
| 46 | + '--experimental-config-file', |
| 47 | + fixtures.path('rc/transform-types.json'), |
| 48 | + fixtures.path('typescript/ts/transformation/test-enum.ts'), |
| 49 | + ]); |
| 50 | + match(result.stderr, /--experimental-config-file is an experimental feature and might change at any time/); |
| 51 | + match(result.stdout, /Hello, TypeScript!/); |
| 52 | + strictEqual(result.code, 0); |
| 53 | +}); |
| 54 | + |
| 55 | +test('should not override a flag declared twice', async () => { |
| 56 | + const result = await spawnPromisified(process.execPath, [ |
| 57 | + '--no-warnings', |
| 58 | + '--experimental-config-file', |
| 59 | + fixtures.path('rc/override-property.json'), |
| 60 | + fixtures.path('typescript/ts/transformation/test-enum.ts'), |
| 61 | + ]); |
| 62 | + strictEqual(result.stderr, ''); |
| 63 | + strictEqual(result.stdout, 'Hello, TypeScript!\n'); |
| 64 | + strictEqual(result.code, 0); |
| 65 | +}); |
| 66 | + |
| 67 | +test('should override env-file', async () => { |
| 68 | + const result = await spawnPromisified(process.execPath, [ |
| 69 | + '--no-warnings', |
| 70 | + '--experimental-config-file', |
| 71 | + fixtures.path('rc/transform-types.json'), |
| 72 | + '--env-file', fixtures.path('dotenv/node-options-no-tranform.env'), |
| 73 | + fixtures.path('typescript/ts/transformation/test-enum.ts'), |
| 74 | + ]); |
| 75 | + strictEqual(result.stderr, ''); |
| 76 | + match(result.stdout, /Hello, TypeScript!/); |
| 77 | + strictEqual(result.code, 0); |
| 78 | +}); |
| 79 | + |
| 80 | +test('should not override NODE_OPTIONS', async () => { |
| 81 | + const result = await spawnPromisified(process.execPath, [ |
| 82 | + '--no-warnings', |
| 83 | + '--experimental-config-file', |
| 84 | + fixtures.path('rc/transform-types.json'), |
| 85 | + fixtures.path('typescript/ts/transformation/test-enum.ts'), |
| 86 | + ], { |
| 87 | + env: { |
| 88 | + ...process.env, |
| 89 | + NODE_OPTIONS: '--no-experimental-transform-types', |
| 90 | + }, |
| 91 | + }); |
| 92 | + match(result.stderr, /ERR_UNSUPPORTED_TYPESCRIPT_SYNTAX/); |
| 93 | + strictEqual(result.stdout, ''); |
| 94 | + strictEqual(result.code, 1); |
| 95 | +}); |
| 96 | + |
| 97 | +test('should not ovverride CLI flags', async () => { |
| 98 | + const result = await spawnPromisified(process.execPath, [ |
| 99 | + '--no-warnings', |
| 100 | + '--no-experimental-transform-types', |
| 101 | + '--experimental-config-file', |
| 102 | + fixtures.path('rc/transform-types.json'), |
| 103 | + fixtures.path('typescript/ts/transformation/test-enum.ts'), |
| 104 | + ]); |
| 105 | + match(result.stderr, /ERR_UNSUPPORTED_TYPESCRIPT_SYNTAX/); |
| 106 | + strictEqual(result.stdout, ''); |
| 107 | + strictEqual(result.code, 1); |
| 108 | +}); |
| 109 | + |
| 110 | +test('should parse array flag correctly', async () => { |
| 111 | + const result = await spawnPromisified(process.execPath, [ |
| 112 | + '--no-warnings', |
| 113 | + '--experimental-config-file', |
| 114 | + fixtures.path('rc/import.json'), |
| 115 | + '--eval', 'setTimeout(() => console.log("D"),99)', |
| 116 | + ]); |
| 117 | + strictEqual(result.stderr, ''); |
| 118 | + strictEqual(result.stdout, 'A\nB\nC\nD\n'); |
| 119 | + strictEqual(result.code, 0); |
| 120 | +}); |
| 121 | + |
| 122 | +test('should validate invalid array flag', async () => { |
| 123 | + const result = await spawnPromisified(process.execPath, [ |
| 124 | + '--no-warnings', |
| 125 | + '--experimental-config-file', |
| 126 | + fixtures.path('rc/invalid-import.json'), |
| 127 | + '--eval', 'setTimeout(() => console.log("D"),99)', |
| 128 | + ]); |
| 129 | + match(result.stderr, /invalid-import\.json: invalid content/); |
| 130 | + strictEqual(result.stdout, ''); |
| 131 | + strictEqual(result.code, 9); |
| 132 | +}); |
| 133 | + |
| 134 | +test('should validate array flag as string', async () => { |
| 135 | + const result = await spawnPromisified(process.execPath, [ |
| 136 | + '--no-warnings', |
| 137 | + '--experimental-config-file', |
| 138 | + fixtures.path('rc/import-as-string.json'), |
| 139 | + '--eval', 'setTimeout(() => console.log("B"),99)', |
| 140 | + ]); |
| 141 | + strictEqual(result.stderr, ''); |
| 142 | + strictEqual(result.stdout, 'A\nB\n'); |
| 143 | + strictEqual(result.code, 0); |
| 144 | +}); |
| 145 | + |
| 146 | +test('should throw at unknown flag', async () => { |
| 147 | + const result = await spawnPromisified(process.execPath, [ |
| 148 | + '--no-warnings', |
| 149 | + '--experimental-config-file', |
| 150 | + fixtures.path('rc/unknown-flag.json'), |
| 151 | + '-p', '"Hello, World!"', |
| 152 | + ]); |
| 153 | + match(result.stderr, /Unknown or not allowed option some-unknown-flag/); |
| 154 | + strictEqual(result.stdout, ''); |
| 155 | + strictEqual(result.code, 9); |
| 156 | +}); |
| 157 | + |
| 158 | +test('should throw at flag not available in NODE_OPTIONS', async () => { |
| 159 | + const result = await spawnPromisified(process.execPath, [ |
| 160 | + '--no-warnings', |
| 161 | + '--experimental-config-file', |
| 162 | + fixtures.path('rc/not-node-options-flag.json'), |
| 163 | + '-p', '"Hello, World!"', |
| 164 | + ]); |
| 165 | + match(result.stderr, /Unknown or not allowed option --test/); |
| 166 | + strictEqual(result.stdout, ''); |
| 167 | + strictEqual(result.code, 9); |
| 168 | +}); |
| 169 | + |
| 170 | +test('unsigned flag should be parsed correctly', async () => { |
| 171 | + const result = await spawnPromisified(process.execPath, [ |
| 172 | + '--no-warnings', |
| 173 | + '--experimental-config-file', |
| 174 | + fixtures.path('rc/numeric.json'), |
| 175 | + '-p', 'http.maxHeaderSize', |
| 176 | + ]); |
| 177 | + strictEqual(result.stderr, ''); |
| 178 | + strictEqual(result.stdout, '4294967295\n'); |
| 179 | + strictEqual(result.code, 0); |
| 180 | +}); |
| 181 | + |
| 182 | +test('numeric flag should not allow negative values', async () => { |
| 183 | + const result = await spawnPromisified(process.execPath, [ |
| 184 | + '--no-warnings', |
| 185 | + '--experimental-config-file', |
| 186 | + fixtures.path('rc/negative-numeric.json'), |
| 187 | + '-p', 'http.maxHeaderSize', |
| 188 | + ]); |
| 189 | + match(result.stderr, /Invalid value for --max-http-header-size/); |
| 190 | + match(result.stderr, /negative-numeric\.json: invalid content/); |
| 191 | + strictEqual(result.stdout, ''); |
| 192 | + strictEqual(result.code, 9); |
| 193 | +}); |
| 194 | + |
| 195 | +test('v8 flag should not be allowed in config file', async () => { |
| 196 | + const result = await spawnPromisified(process.execPath, [ |
| 197 | + '--no-warnings', |
| 198 | + '--experimental-config-file', |
| 199 | + fixtures.path('rc/v8-flag.json'), |
| 200 | + '-p', '"Hello, World!"', |
| 201 | + ]); |
| 202 | + match(result.stderr, /V8 flag --abort-on-uncaught-exception is currently not supported/); |
| 203 | + strictEqual(result.stdout, ''); |
| 204 | + strictEqual(result.code, 9); |
| 205 | +}); |
| 206 | + |
| 207 | +test('string flag should be parsed correctly', async () => { |
| 208 | + const result = await spawnPromisified(process.execPath, [ |
| 209 | + '--no-warnings', |
| 210 | + '--test', |
| 211 | + '--experimental-config-file', |
| 212 | + fixtures.path('rc/string.json'), |
| 213 | + fixtures.path('rc/test.js'), |
| 214 | + ]); |
| 215 | + strictEqual(result.stderr, ''); |
| 216 | + strictEqual(result.stdout, '.\n'); |
| 217 | + strictEqual(result.code, 0); |
| 218 | +}); |
| 219 | + |
| 220 | +test('host port flag should be parsed correctly', { skip: !process.features.inspector }, async () => { |
| 221 | + const result = await spawnPromisified(process.execPath, [ |
| 222 | + '--no-warnings', |
| 223 | + '--expose-internals', |
| 224 | + '--experimental-config-file', |
| 225 | + fixtures.path('rc/host-port.json'), |
| 226 | + '-p', 'require("internal/options").getOptionValue("--inspect-port").port', |
| 227 | + ]); |
| 228 | + strictEqual(result.stderr, ''); |
| 229 | + strictEqual(result.stdout, '65535\n'); |
| 230 | + strictEqual(result.code, 0); |
| 231 | +}); |
| 232 | + |
| 233 | +test('no op flag should throw', async () => { |
| 234 | + const result = await spawnPromisified(process.execPath, [ |
| 235 | + '--no-warnings', |
| 236 | + '--experimental-config-file', |
| 237 | + fixtures.path('rc/no-op.json'), |
| 238 | + '-p', '"Hello, World!"', |
| 239 | + ]); |
| 240 | + match(result.stderr, /No-op flag --http-parser is currently not supported/); |
| 241 | + match(result.stderr, /no-op\.json: invalid content/); |
| 242 | + strictEqual(result.stdout, ''); |
| 243 | + strictEqual(result.code, 9); |
| 244 | +}); |
| 245 | + |
| 246 | +test('should not allow users to sneak in a flag', async () => { |
| 247 | + const result = await spawnPromisified(process.execPath, [ |
| 248 | + '--no-warnings', |
| 249 | + '--experimental-config-file', |
| 250 | + fixtures.path('rc/sneaky-flag.json'), |
| 251 | + '-p', '"Hello, World!"', |
| 252 | + ]); |
| 253 | + match(result.stderr, /The number of NODE_OPTIONS doesn't match the number of flags in the config file/); |
| 254 | + strictEqual(result.stdout, ''); |
| 255 | + strictEqual(result.code, 9); |
| 256 | +}); |
0 commit comments