Files
ECC/tests/lib/gateguard-code-lexer.test.js
T
montjeffrey 9faa6b76d2 test(gateguard): pin searches that never saw the target and Unicode line terminators
Each of these is credited on main, reproduced against upstream 2d515e4f:

- rg -t py / -T js / --type-not js, fd -e py, and the Grep tool's
  type: "py" credit an Edit of tokenizer.js. Type and extension values
  are skipped as plain value flags, so they never filter.
- Glob **/tokenizer.py, rg tokenizer src/*.py, and
  git grep tokenizer -- '*.py' credit tokenizer.js. The Glob pattern is
  never a filter, and a glob operand scopes as its literal directory.
- Glob **/tokenizer.js, find -name tokenizer.js, rg -g tokenizer.js and
  Grep glob: "tokenizer.js" credit tokenizer.js, although each search
  could only find the target itself.
- An Edit of "// note<U+2028>foo(1);" to "// note<U+2028>foo(2);" passes
  as comment-only, although JavaScript ends the comment at U+2028 and
  foo(2) runs.

Scenario 21-bypass-filter-forms adds ten must-deny steps and four
controls. Four hook controls that asserted exact-name lookups still
credit (find -name widget.py, Grep glob widget.py, Get-ChildItem
-Filter widget.py) now assert the lookup is denied; each keeps its
original purpose, that the exclusion parser leaves a positive include
alone, through a wildcard include (widget.*).
2026-10-10 23:38:47 -04:00

110 lines
4.0 KiB
JavaScript

'use strict';
/**
* Tests for scripts/lib/gateguard-code-lexer.js.
*
* Run with: node tests/lib/gateguard-code-lexer.test.js
*/
const assert = require('assert');
const path = require('path');
const { isTrivialEdit, shCodeLines, psCodeLines, batchCodeLines, normalizeShellLine } = require(
path.join(__dirname, '..', '..', 'scripts', 'lib', 'gateguard-code-lexer.js')
);
console.log('=== Testing gateguard-code-lexer.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++;
}
}
const JS = { lineComment: '//', blockComment: true, quotes: '"\'', indentSensitive: false, flavor: 'js', forbiddenCode: '`/', jsx: true };
const PY = { lineComment: '#', blockComment: false, quotes: '"\'', indentSensitive: true, tripleQuotes: true, fStrings: true };
test('a comment-only change to C-family code is trivial', () => {
assert.strictEqual(isTrivialEdit('a(); // x', 'a(); // y', JS), true);
});
test('a Unicode line terminator makes an edit non-trivial', () => {
// JavaScript ends a line comment at U+2028 and U+2029, and C# also at U+0085.
for (const sep of ['\u2028', '\u2029', '\u0085']) {
const code = sep.codePointAt(0).toString(16);
assert.strictEqual(isTrivialEdit(`// note${sep}foo(1);`, `// note${sep}foo(2);`, JS), false, `U+${code} ends the comment`);
assert.strictEqual(isTrivialEdit(`a(); // x${sep}y`, `a(); // z${sep}y`, JS), false, `U+${code} anywhere`);
assert.strictEqual(isTrivialEdit(`x = 1 # a${sep}b`, `x = 1 # c${sep}b`, PY), false, `U+${code} in any language`);
}
});
test('comment markers inside a string are code', () => {
assert.strictEqual(isTrivialEdit('const s = "a // b";', 'const s = "a // c";', JS), false);
assert.strictEqual(isTrivialEdit("const s = 'x /* y */ z';", "const s = 'x /* q */ z';", JS), false);
});
test('an escaped quote does not end a string', () => {
assert.strictEqual(isTrivialEdit('const s = "a\\" // b";', 'const s = "a\\" // c";', JS), false);
});
test('a long block comment with stars and slashes stays a comment', () => {
const before = 'a();\n/* one * two / three\n * four ** five\n */\nb();';
const after = 'a();\n/* one * two / 3\n * 4 ** five\n */\nb();';
assert.strictEqual(isTrivialEdit(before, after, JS), true);
});
test('a line comment ending in CRLF keeps the next line as code', () => {
assert.strictEqual(isTrivialEdit('a(); // x\r\nb();', 'a(); // y\r\nc();', JS), false);
assert.strictEqual(isTrivialEdit('a(); // x\r\nb();', 'a(); // y\r\nb();', JS), true);
});
test('a code change is not trivial', () => {
assert.strictEqual(isTrivialEdit('a();', 'b();', JS), false);
});
test('a directive comment counts as code', () => {
assert.strictEqual(isTrivialEdit('// eslint-disable-next-line', '// note', JS), false);
});
test('an indentation change is not trivial in an indentation-sensitive language', () => {
assert.strictEqual(isTrivialEdit(' x = 1', 'x = 1', PY), false);
});
test('a trailing comment change in Python is trivial', () => {
assert.strictEqual(isTrivialEdit('x = 1 # a', 'x = 1 # b', PY), true);
});
test('shell code lines drop comments', () => {
assert.deepStrictEqual(shCodeLines('echo hi # c\nls'), ['echo hi ', 'ls']);
});
test('shell heredocs are not lexed', () => {
assert.strictEqual(shCodeLines('cat <<EOF\nx\nEOF'), null);
});
test('PowerShell code lines drop line and block comments', () => {
assert.deepStrictEqual(psCodeLines('Get-X # c\n<# b #>\nY'), ['Get-X ', ' ', 'Y']);
});
test('batch code lines drop REM lines', () => {
assert.deepStrictEqual(batchCodeLines('@echo off\nREM x\nset a=1'), ['@echo off', '', 'set a=1']);
});
test('a batch line continuation is not lexed', () => {
assert.strictEqual(batchCodeLines('echo ^'), null);
});
test('shell line normalization collapses blanks but keeps escaped ones', () => {
assert.strictEqual(normalizeShellLine('a b\\ c', '\\'), 'a b\\ c');
});
console.log(`\nResults: Passed: ${passed}, Failed: ${failed}`);
process.exit(failed > 0 ? 1 : 0);