Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 96 additions & 0 deletions tests/lib/shell-substitution.test.js
Original file line number Diff line number Diff line change
@@ -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'), []);
});
Comment thread
greptile-apps[bot] marked this conversation as resolved.
Resolved
Comment on lines +56 to +75

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

πŸ“ Maintainability & Code Quality | πŸ”΅ Trivial | ⚑ Quick win

Add a test for backtick span skipping in extractSubshellGroups.

The implementation explicitly skips backtick spans (lines ~189-196 in scripts/lib/shell-substitution.js), but no test exercises this path. A command like echo \id`should return[]` since the parens inside backticks (if any) should be ignored. Adding this test would cover an important branch of the skip logic.

πŸ§ͺ Suggested additional test
 test('returns [] when there is no subshell', () => {
   assert.deepStrictEqual(extractSubshellGroups('echo hello'), []);
 });
+test('skips backtick command substitutions', () => {
+  assert.deepStrictEqual(extractSubshellGroups('echo `id`'), []);
+});
πŸ“ Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
// 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'), []);
});
// 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'), []);
});
test('skips backtick command substitutions', () => {
assert.deepStrictEqual(extractSubshellGroups('echo `id`'), []);
});
πŸ€– Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@tests/lib/shell-substitution.test.js` around lines 56 - 75, Add a focused
test in extractSubshellGroups coverage for backtick span skipping: verify that a
command containing backticks (for example via the existing extractSubshellGroups
test block) returns an empty array and does not treat any parenthesized content
inside the backtick span as a subshell. Use the existing extractSubshellGroups
test suite in shell-substitution.test.js to exercise the backtick-ignoring
branch in the implementation.


// 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'), []);
});
Comment thread
greptile-apps[bot] marked this conversation as resolved.
Resolved
Comment on lines +77 to +93

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

πŸ“ Maintainability & Code Quality | πŸ”΅ Trivial | ⚑ Quick win

Add tests for $(...) and backtick span skipping inside brace groups.

The extractBraceGroups implementation has explicit skip logic for $(...) spans (lines ~393-418) and backtick spans (lines ~420+) inside brace group bodies, but neither path is tested. These are important branches β€” a } inside a command substitution or backtick body must not close the enclosing brace group. Consider adding:

πŸ§ͺ Suggested additional tests
 test('returns [] when there is no brace group', () => {
   assert.deepStrictEqual(extractBraceGroups('echo hello'), []);
 });
+test('skips $(...) spans inside brace groups', () => {
+  assert.deepStrictEqual(extractBraceGroups('{ echo $(whoami); }'), [' echo $(whoami); ']);
+});
+test('skips backtick spans inside brace groups', () => {
+  assert.deepStrictEqual(extractBraceGroups('{ echo `id`; }'), [' echo `id`; ']);
+});
πŸ“ Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
// 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'), []);
});
// 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'), []);
});
test('skips $(...) spans inside brace groups', () => {
assert.deepStrictEqual(extractBraceGroups('{ echo $(whoami); }'), [' echo $(whoami); ']);
});
test('skips backtick spans inside brace groups', () => {
assert.deepStrictEqual(extractBraceGroups('{ echo `id`; }'), [' echo `id`; ']);
});
πŸ€– Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@tests/lib/shell-substitution.test.js` around lines 77 - 93, Add coverage in
extractBraceGroups tests for the skip logic that handles $(...) and backtick
spans inside brace group bodies. Update the test block in
shell-substitution.test.js to include cases where a closing brace appears inside
a command substitution or backtick expression and confirm extractBraceGroups
still returns the enclosing brace group unchanged. Use the existing
extractBraceGroups test suite and its helper expectations to locate the right
area and verify both nested and non-nested behavior.


console.log(`\n=== Results: ${passed} passed, ${failed} failed ===`);
if (failed > 0) process.exit(1);