mirror of
https://github.com/affaan-m/ECC.git
synced 2026-11-24 00:35:31 +00:00
fix(ci): reject duplicate root keys in hook documents
This commit is contained in:
1 parent
244210bbd1
commit
0268e2ef40
2 files changed
+33
-2
No files matched your search
@@ -293,6 +293,7 @@ function checkHookDuplicates(source, eventMap) {
|
||||
const events = new Map();
|
||||
const ids = new Map();
|
||||
const rootProperties = [];
|
||||
let eventMapLine = null;
|
||||
|
||||
function checkEvent(key, line) {
|
||||
if (events.has(key)) {
|
||||
@@ -306,7 +307,12 @@ function checkHookDuplicates(source, eventMap) {
|
||||
const parent = getPath();
|
||||
if (parent.length === 1 && parent[0] === eventMap) {
|
||||
checkEvent(key, line);
|
||||
} else if (parent.length === 0 && eventMap === 'hooks') {
|
||||
} else if (parent.length === 0 && key === eventMap) {
|
||||
if (eventMapLine !== null) {
|
||||
throw new Error(`line ${line + 1}: duplicate root key '${key}' (first on line ${eventMapLine})`);
|
||||
}
|
||||
eventMapLine = line + 1;
|
||||
} else if (parent.length === 0 && eventMap === 'hooks' && eventMapLine === null) {
|
||||
rootProperties.push({ key, line });
|
||||
}
|
||||
},
|
||||
@@ -326,7 +332,7 @@ function checkHookDuplicates(source, eventMap) {
|
||||
|
||||
// A hooks wrapper can appear after other root properties. Only an unwrapped
|
||||
// manifest uses those root properties as its event map.
|
||||
if (!rootProperties.some(({ key }) => key === 'hooks')) {
|
||||
if (eventMapLine === null) {
|
||||
for (const { key, line } of rootProperties) checkEvent(key, line);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3038,6 +3038,31 @@ function runTests() {
|
||||
cleanupTestDir(testDir);
|
||||
}
|
||||
})) passed++; else failed++;
|
||||
|
||||
if (test(`rejects a repeated root ${eventMap} key in ${file}`, () => {
|
||||
const testDir = createTestDir();
|
||||
try {
|
||||
const hooksFile = path.join(testDir, 'hooks.json');
|
||||
writeJson(hooksFile, { hooks: {} });
|
||||
const firstEntry = eventMap === 'hooks'
|
||||
? { id: 'first', hooks: [{ type: 'command', command: 'echo first' }] }
|
||||
: { id: 'first', fingerprint: '0123456789ab' };
|
||||
fs.writeFileSync(path.join(testDir, file), `{"${eventMap}":
|
||||
${JSON.stringify({ Stop: [firstEntry] })},
|
||||
"${eventMap}": {}
|
||||
}`);
|
||||
const result = runValidatorWithDirs('validate-hooks', {
|
||||
HOOKS_FILE: hooksFile,
|
||||
HOOKS_SCHEMA_PATH: path.join(repoRoot, 'schemas', 'hooks.schema.json'),
|
||||
METADATA_SCHEMA_PATH: path.join(repoRoot, 'schemas', 'hooks-metadata.schema.json'),
|
||||
});
|
||||
assert.strictEqual(result.code, 1, result.stdout);
|
||||
const diagnostic = `${file}: line 3: duplicate root key '${eventMap}' (first on line 1)`;
|
||||
assert.ok(result.stderr.includes(diagnostic), result.stderr);
|
||||
} finally {
|
||||
cleanupTestDir(testDir);
|
||||
}
|
||||
})) passed++; else failed++;
|
||||
}
|
||||
|
||||
if (test('reports duplicate root events in a schema-free unwrapped hook map', () => {
|
||||
|
||||
Reference in new issue
Block a user