From 044d69a0af23d5eaa48346d9b87fb79f8a78e04f Mon Sep 17 00:00:00 2001 From: Marco Ippolito Date: Fri, 21 Feb 2025 18:02:42 +0100 Subject: [PATCH] test: add more coverage to node_config_file PR-URL: https://github.com/nodejs/node/pull/57170 Reviewed-By: James M Snell Reviewed-By: Paolo Insogna --- test/fixtures/rc/broken-node-options.json | 5 ++ test/fixtures/rc/broken.json | 1 + test/fixtures/rc/non-object-node-options.json | 3 ++ test/fixtures/rc/non-object-root.json | 1 + test/parallel/test-config-file.js | 50 +++++++++++++++++++ 5 files changed, 60 insertions(+) create mode 100644 test/fixtures/rc/broken-node-options.json create mode 100644 test/fixtures/rc/broken.json create mode 100644 test/fixtures/rc/non-object-node-options.json create mode 100644 test/fixtures/rc/non-object-root.json diff --git a/test/fixtures/rc/broken-node-options.json b/test/fixtures/rc/broken-node-options.json new file mode 100644 index 00000000000..beea3f7143f --- /dev/null +++ b/test/fixtures/rc/broken-node-options.json @@ -0,0 +1,5 @@ +{ + "nodeOptions": { + "inspect-port + } +} diff --git a/test/fixtures/rc/broken.json b/test/fixtures/rc/broken.json new file mode 100644 index 00000000000..98232c64fce --- /dev/null +++ b/test/fixtures/rc/broken.json @@ -0,0 +1 @@ +{ diff --git a/test/fixtures/rc/non-object-node-options.json b/test/fixtures/rc/non-object-node-options.json new file mode 100644 index 00000000000..5dc596e4673 --- /dev/null +++ b/test/fixtures/rc/non-object-node-options.json @@ -0,0 +1,3 @@ +{ + "nodeOptions": "string" +} diff --git a/test/fixtures/rc/non-object-root.json b/test/fixtures/rc/non-object-root.json new file mode 100644 index 00000000000..fe51488c706 --- /dev/null +++ b/test/fixtures/rc/non-object-root.json @@ -0,0 +1 @@ +[] diff --git a/test/parallel/test-config-file.js b/test/parallel/test-config-file.js index 49960e262ba..a53de3be8ed 100644 --- a/test/parallel/test-config-file.js +++ b/test/parallel/test-config-file.js @@ -254,3 +254,53 @@ test('should not allow users to sneak in a flag', async () => { strictEqual(result.stdout, ''); strictEqual(result.code, 9); }); + +test('non object root', async () => { + const result = await spawnPromisified(process.execPath, [ + '--no-warnings', + '--experimental-config-file', + fixtures.path('rc/non-object-root.json'), + '-p', '"Hello, World!"', + ]); + match(result.stderr, /Root value unexpected not an object for/); + strictEqual(result.stdout, ''); + strictEqual(result.code, 9); +}); + +test('non object node options', async () => { + const result = await spawnPromisified(process.execPath, [ + '--no-warnings', + '--experimental-config-file', + fixtures.path('rc/non-object-node-options.json'), + '-p', '"Hello, World!"', + ]); + match(result.stderr, /"nodeOptions" value unexpected for/); + strictEqual(result.stdout, ''); + strictEqual(result.code, 9); +}); + +test('should throw correct error when a json is broken', async () => { + const result = await spawnPromisified(process.execPath, [ + '--no-warnings', + '--experimental-config-file', + fixtures.path('rc/broken.json'), + '-p', '"Hello, World!"', + ]); + match(result.stderr, /Can't parse/); + match(result.stderr, /broken\.json: invalid content/); + strictEqual(result.stdout, ''); + strictEqual(result.code, 9); +}); + +test('broken value in node_options', async () => { + const result = await spawnPromisified(process.execPath, [ + '--no-warnings', + '--experimental-config-file', + fixtures.path('rc/broken-node-options.json'), + '-p', '"Hello, World!"', + ]); + match(result.stderr, /Can't parse/); + match(result.stderr, /broken-node-options\.json: invalid content/); + strictEqual(result.stdout, ''); + strictEqual(result.code, 9); +});