fix: protect existing test sources

This commit is contained in:
bincxz committed 2026-07-31 19:34:21 +08:00
1 parent 33c6570e08
commit e1998cfcd2
2 files changed
+61 -1

No files matched your search

+42 -1
View File
@@ -2303,6 +2303,43 @@ function listProtectedPathHits(filePaths) {
return hits;
}
function isTestSourcePath(filePath) {
const normalized = String(filePath || '').replace(/\\/g, '/');
return (
/(?:^|\/)[^/]+\.(?:test|spec)\.[cm]?[jt]sx?$/.test(normalized) ||
/(?:^|\/)__tests__\//.test(normalized)
);
}
function listModifiedExistingTestPaths({
gitStatusPorcelain = '',
nameStatusText = '',
} = {}) {
const hits = [];
for (const line of String(gitStatusPorcelain || '').split('\n')) {
if (!line.trim()) continue;
const status = line.slice(0, 2);
if (status === '??' || status.includes('A')) continue;
const rest = line.slice(3).trim();
const paths = rest.includes(' -> ')
? rest.split(' -> ').map((value) => unquoteGitPath(value.trim()))
: [unquoteGitPath(rest)];
hits.push(...paths.filter(isTestSourcePath));
}
for (const line of String(nameStatusText || '').split('\n')) {
const trimmed = line.trim();
if (!trimmed) continue;
const parts = trimmed.split(/\t/);
const status = parts[0] || '';
if (/^A\d*$/.test(status)) continue;
const paths = /^R\d*/.test(status) && parts.length >= 3
? [parts[1], parts[2]]
: parts.slice(1, 2);
hits.push(...paths.map(unquoteGitPath).filter(isTestSourcePath));
}
return [...new Set(hits)];
}
function hasProtectedChanges(gitStatusPorcelain) {
return listProtectedPathHits(pathsFromGitStatusPorcelain(gitStatusPorcelain));
}
@@ -2316,11 +2353,15 @@ function hasProtectedChangesInSources({
const fromStatus = pathsFromGitStatusPorcelain(gitStatusPorcelain);
const fromCommits = (changedFiles || []).map(String);
const fromNameStatus = pathsFromGitDiffNameStatus(nameStatusText);
return listProtectedPathHits([
const protectedHits = listProtectedPathHits([
...fromStatus,
...fromCommits,
...fromNameStatus,
]);
return [...new Set([
...protectedHits,
...listModifiedExistingTestPaths({ gitStatusPorcelain, nameStatusText }),
])];
}
function getCodexRoundFromComments(comments = [], options = {}) {
+19
View File
@@ -1734,6 +1734,25 @@ test('hasProtectedChangesInSources checks commit names', () => {
]);
});
test('generated changes may add tests but cannot alter existing test sources', () => {
const committed = auto.hasProtectedChangesInSources({
nameStatusText: [
'M\tcomponents/existing.test.ts',
'A\tcomponents/new-regression.test.ts',
'M\tcomponents/App.tsx',
].join('\n'),
});
assert.deepEqual(committed, ['components/existing.test.ts']);
const workingTree = auto.hasProtectedChangesInSources({
gitStatusPorcelain: [
' M components/other.test.ts',
'?? components/new-regression-2.test.ts',
].join('\n'),
});
assert.deepEqual(workingTree, ['components/other.test.ts']);
});
test('hasProtectedChangesInSources blocks electron-builder configs', () => {
const hits = auto.hasProtectedChangesInSources({
changedFiles: ['electron-builder.config.cjs', 'components/App.tsx', 'nix/release.nix'],