|
| 1 | +// node --test specs for the shared bash-quote-mask helper. |
| 2 | +// |
| 3 | +// Run from this dir: |
| 4 | +// node --test test/*.test.mts |
| 5 | + |
| 6 | +import test from 'node:test' |
| 7 | +import assert from 'node:assert/strict' |
| 8 | + |
| 9 | +import { |
| 10 | + buildQuoteMask, |
| 11 | + containsOutsideQuotes, |
| 12 | + matchOutsideQuotes, |
| 13 | + stripHeredocBodies, |
| 14 | +} from '../bash-quote-mask.mts' |
| 15 | + |
| 16 | +test('buildQuoteMask: empty string', () => { |
| 17 | + assert.deepEqual(buildQuoteMask(''), []) |
| 18 | +}) |
| 19 | + |
| 20 | +test("buildQuoteMask: plain text is all false", () => { |
| 21 | + const mask = buildQuoteMask('git status --short') |
| 22 | + assert.ok(mask.every(b => b === false)) |
| 23 | +}) |
| 24 | + |
| 25 | +test('buildQuoteMask: single-quoted region is true', () => { |
| 26 | + const s = "echo 'hi'" |
| 27 | + const mask = buildQuoteMask(s) |
| 28 | + // 'echo ' → 5 false |
| 29 | + for (let i = 0; i < 5; i += 1) { |
| 30 | + assert.strictEqual(mask[i], false) |
| 31 | + } |
| 32 | + // "'hi'" → 4 true (open quote, h, i, close quote) |
| 33 | + for (let i = 5; i < 9; i += 1) { |
| 34 | + assert.strictEqual(mask[i], true) |
| 35 | + } |
| 36 | +}) |
| 37 | + |
| 38 | +test('buildQuoteMask: double-quoted region is true', () => { |
| 39 | + const s = 'echo "hi"' |
| 40 | + const mask = buildQuoteMask(s) |
| 41 | + assert.strictEqual(mask[5], true) // " |
| 42 | + assert.strictEqual(mask[6], true) // h |
| 43 | + assert.strictEqual(mask[7], true) // i |
| 44 | + assert.strictEqual(mask[8], true) // " |
| 45 | +}) |
| 46 | + |
| 47 | +test('buildQuoteMask: escaped double quote inside double quotes', () => { |
| 48 | + const s = 'echo "a\\"b"' |
| 49 | + const mask = buildQuoteMask(s) |
| 50 | + // 'a' is at index 6, the \\ at 7-8 should be marked, then "b" at 9, " at 10. |
| 51 | + assert.strictEqual(mask[5], true) // opening " |
| 52 | + assert.strictEqual(mask[6], true) // a |
| 53 | + assert.strictEqual(mask[7], true) // backslash (escape) |
| 54 | + assert.strictEqual(mask[8], true) // escaped " |
| 55 | + assert.strictEqual(mask[9], true) // b |
| 56 | + assert.strictEqual(mask[10], true) // closing " |
| 57 | +}) |
| 58 | + |
| 59 | +test('buildQuoteMask: single quotes do not honor backslash', () => { |
| 60 | + // POSIX single quotes: backslash is literal. The runtime string is |
| 61 | + // `echo 'a\b'` (10 chars): e c h o ␠ ' a \ b ' |
| 62 | + const s = "echo 'a\\b'" |
| 63 | + const mask = buildQuoteMask(s) |
| 64 | + assert.strictEqual(s.length, 10) |
| 65 | + // Opening quote through closing quote (indices 5..9) are all masked. |
| 66 | + for (let i = 5; i < 10; i += 1) { |
| 67 | + assert.strictEqual(mask[i], true, `index ${i} should be masked`) |
| 68 | + } |
| 69 | +}) |
| 70 | + |
| 71 | +test('buildQuoteMask: single quotes nested inside double quotes are text', () => { |
| 72 | + // Inside a double-quoted string, ' is just a literal apostrophe. |
| 73 | + const s = 'echo "it\'s ok"' |
| 74 | + const mask = buildQuoteMask(s) |
| 75 | + // Every char from index 5 to end is inside the double-quoted region. |
| 76 | + for (let i = 5; i < s.length; i += 1) { |
| 77 | + assert.strictEqual(mask[i], true) |
| 78 | + } |
| 79 | +}) |
| 80 | + |
| 81 | +test('stripHeredocBodies: replaces body with spaces, preserves length', () => { |
| 82 | + const s = "cat <<EOF\nhello\nworld\nEOF\nrest" |
| 83 | + const stripped = stripHeredocBodies(s) |
| 84 | + assert.strictEqual(stripped.length, s.length) |
| 85 | + // The word `hello` should be blanked out. |
| 86 | + assert.ok(!stripped.includes('hello')) |
| 87 | + // The opening `<<EOF` and closing `EOF` remain. |
| 88 | + assert.ok(stripped.includes('<<EOF')) |
| 89 | + // `rest` after the heredoc is untouched. |
| 90 | + assert.ok(stripped.endsWith('rest')) |
| 91 | +}) |
| 92 | + |
| 93 | +test('stripHeredocBodies: handles quoted delimiter', () => { |
| 94 | + const s = "cat <<'EOF'\nbody\nEOF" |
| 95 | + const stripped = stripHeredocBodies(s) |
| 96 | + assert.ok(!stripped.includes('body')) |
| 97 | +}) |
| 98 | + |
| 99 | +test('stripHeredocBodies: handles tab-stripped form', () => { |
| 100 | + const s = "cat <<-EOF\n\tbody\n\tEOF" |
| 101 | + const stripped = stripHeredocBodies(s) |
| 102 | + assert.ok(!stripped.includes('body')) |
| 103 | +}) |
| 104 | + |
| 105 | +test('containsOutsideQuotes: matches free text', () => { |
| 106 | + assert.ok(containsOutsideQuotes('node --bad-flag foo', /--bad-flag/)) |
| 107 | +}) |
| 108 | + |
| 109 | +test('containsOutsideQuotes: does not match inside single quotes', () => { |
| 110 | + assert.ok( |
| 111 | + !containsOutsideQuotes( |
| 112 | + "echo 'reminder: --bad-flag is gone'", |
| 113 | + /--bad-flag/, |
| 114 | + ), |
| 115 | + ) |
| 116 | +}) |
| 117 | + |
| 118 | +test('containsOutsideQuotes: does not match inside double quotes', () => { |
| 119 | + assert.ok( |
| 120 | + !containsOutsideQuotes( |
| 121 | + 'echo "reminder: --bad-flag is gone"', |
| 122 | + /--bad-flag/, |
| 123 | + ), |
| 124 | + ) |
| 125 | +}) |
| 126 | + |
| 127 | +test('containsOutsideQuotes: does not match inside heredoc body', () => { |
| 128 | + assert.ok( |
| 129 | + !containsOutsideQuotes( |
| 130 | + "git commit -m \"$(cat <<'EOF'\nmention --bad-flag here\nEOF\n)\"", |
| 131 | + /--bad-flag/, |
| 132 | + ), |
| 133 | + ) |
| 134 | +}) |
| 135 | + |
| 136 | +test('containsOutsideQuotes: matches when both quoted + unquoted occurrences exist', () => { |
| 137 | + assert.ok( |
| 138 | + containsOutsideQuotes( |
| 139 | + "echo 'tip: --bad-flag' && node --bad-flag foo", |
| 140 | + /--bad-flag/, |
| 141 | + ), |
| 142 | + ) |
| 143 | +}) |
| 144 | + |
| 145 | +test('matchOutsideQuotes: returns the unquoted match', () => { |
| 146 | + const m = matchOutsideQuotes( |
| 147 | + "echo 'noise --x' && node --x foo", |
| 148 | + /--x/, |
| 149 | + ) |
| 150 | + assert.ok(m) |
| 151 | + // The unquoted occurrence sits at the end, well past the quoted one. |
| 152 | + assert.ok(m!.index > 20) |
| 153 | +}) |
| 154 | + |
| 155 | +test('matchOutsideQuotes: handles non-global regex by cloning', () => { |
| 156 | + const m = matchOutsideQuotes('node --x foo', /--x/) |
| 157 | + assert.ok(m) |
| 158 | + assert.strictEqual(m![0], '--x') |
| 159 | +}) |
0 commit comments