From 3e6ec37d247598f6e750202c848958fcdd07de9f Mon Sep 17 00:00:00 2001 From: MrChees8157 Date: Wed, 8 Jul 2026 20:32:07 +0200 Subject: [PATCH 1/2] test: add unit tests for shell-substitution helpers Cover the three parsers exported from scripts/lib/shell-substitution.js (extractCommandSubstitutions, extractSubshellGroups, extractBraceGroups) which previously had no matching test in tests/lib/. Cases assert the documented quoting semantics: substitutions are scanned inside double quotes but ignored inside single quotes, plain (...) and { ...; } groups are distinguished from $(...), and nested groups are extracted recursively. Co-Authored-By: Claude Opus 4.8 --- tests/lib/shell-substitution.test.js | 96 ++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 tests/lib/shell-substitution.test.js diff --git a/tests/lib/shell-substitution.test.js b/tests/lib/shell-substitution.test.js new file mode 100644 index 00000000000..40567cc2771 --- /dev/null +++ b/tests/lib/shell-substitution.test.js @@ -0,0 +1,96 @@ +'use strict'; +const assert = require('assert'); +const { + extractCommandSubstitutions, + extractSubshellGroups, + extractBraceGroups, +} = require('../../scripts/lib/shell-substitution'); + +console.log('=== Testing shell-substitution.js ===\n'); + +let passed = 0; +let failed = 0; + +function test(desc, fn) { + try { + fn(); + console.log(` ✓ ${desc}`); + passed++; + } catch (e) { + console.log(` ✗ ${desc}: ${e.message}`); + failed++; + } +} + +// extractCommandSubstitutions +console.log('extractCommandSubstitutions:'); +test('extracts a plain $(...) body', () => { + assert.deepStrictEqual(extractCommandSubstitutions('echo $(whoami)'), ['whoami']); +}); +test('extracts a backtick body', () => { + assert.deepStrictEqual(extractCommandSubstitutions('echo `id`'), ['id']); +}); +test('extracts nested $(...) recursively', () => { + assert.deepStrictEqual(extractCommandSubstitutions('echo $(echo $(id))'), [ + 'echo $(id)', + 'id', + ]); +}); +test('ignores substitutions inside single quotes', () => { + assert.deepStrictEqual(extractCommandSubstitutions("echo '$(whoami)'"), []); +}); +test('scans substitutions inside double quotes', () => { + assert.deepStrictEqual(extractCommandSubstitutions('echo "$(whoami)"'), ['whoami']); +}); +test('returns [] when there is no substitution', () => { + assert.deepStrictEqual(extractCommandSubstitutions('echo hello'), []); +}); +test('handles empty string', () => { + assert.deepStrictEqual(extractCommandSubstitutions(''), []); +}); +test('handles null/undefined input', () => { + assert.deepStrictEqual(extractCommandSubstitutions(null), []); + assert.deepStrictEqual(extractCommandSubstitutions(undefined), []); +}); + +// extractSubshellGroups +console.log('\nextractSubshellGroups:'); +test('extracts a plain (...) subshell body', () => { + assert.deepStrictEqual(extractSubshellGroups('(npm run dev)'), ['npm run dev']); +}); +test('skips $(...) command substitutions', () => { + assert.deepStrictEqual(extractSubshellGroups('echo $(whoami)'), []); +}); +test('extracts nested (...) recursively', () => { + assert.deepStrictEqual(extractSubshellGroups('(a && (b))'), ['a && (b)', 'b']); +}); +test('ignores parens inside single quotes', () => { + assert.deepStrictEqual(extractSubshellGroups("echo '(not a subshell)'"), []); +}); +test('ignores parens inside double quotes', () => { + assert.deepStrictEqual(extractSubshellGroups('echo "(not a subshell)"'), []); +}); +test('returns [] when there is no subshell', () => { + assert.deepStrictEqual(extractSubshellGroups('echo hello'), []); +}); + +// extractBraceGroups +console.log('\nextractBraceGroups:'); +test('extracts a { ...; } brace group body', () => { + assert.deepStrictEqual(extractBraceGroups('{ npm run dev; }'), [' npm run dev; ']); +}); +test('does not treat {token} (no space) as a group', () => { + assert.deepStrictEqual(extractBraceGroups('{npm run dev}'), []); +}); +test('skips (...) subshell spans', () => { + assert.deepStrictEqual(extractBraceGroups('(npm run dev)'), []); +}); +test('extracts nested brace groups recursively', () => { + assert.deepStrictEqual(extractBraceGroups('{ a; { b; }; }'), [' a; { b; }; ', ' b; ']); +}); +test('returns [] when there is no brace group', () => { + assert.deepStrictEqual(extractBraceGroups('echo hello'), []); +}); + +console.log(`\n=== Results: ${passed} passed, ${failed} failed ===`); +if (failed > 0) process.exit(1); From a459507ddb086fd14060a732d2cb88023ca21e0d Mon Sep 17 00:00:00 2001 From: MrChees8157 Date: Wed, 8 Jul 2026 20:39:55 +0200 Subject: [PATCH 2/2] test: cover null/undefined input for subshell and brace parsers Address Greptile review feedback on #2478: extractSubshellGroups and extractBraceGroups share the same String(input || '') coercion as extractCommandSubstitutions, so assert the same null/undefined guard for them too. Co-Authored-By: Claude Opus 4.8 --- tests/lib/shell-substitution.test.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/lib/shell-substitution.test.js b/tests/lib/shell-substitution.test.js index 40567cc2771..ec529f0e177 100644 --- a/tests/lib/shell-substitution.test.js +++ b/tests/lib/shell-substitution.test.js @@ -73,6 +73,10 @@ test('ignores parens inside double quotes', () => { test('returns [] when there is no subshell', () => { assert.deepStrictEqual(extractSubshellGroups('echo hello'), []); }); +test('handles null/undefined input', () => { + assert.deepStrictEqual(extractSubshellGroups(null), []); + assert.deepStrictEqual(extractSubshellGroups(undefined), []); +}); // extractBraceGroups console.log('\nextractBraceGroups:'); @@ -91,6 +95,10 @@ test('extracts nested brace groups recursively', () => { test('returns [] when there is no brace group', () => { assert.deepStrictEqual(extractBraceGroups('echo hello'), []); }); +test('handles null/undefined input', () => { + assert.deepStrictEqual(extractBraceGroups(null), []); + assert.deepStrictEqual(extractBraceGroups(undefined), []); +}); console.log(`\n=== Results: ${passed} passed, ${failed} failed ===`); if (failed > 0) process.exit(1);