fix(GOV-009): 完成脚手架审查整改
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
import { execFileSync } from 'node:child_process';
|
||||
import path from 'node:path';
|
||||
import process from 'node:process';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
|
||||
/**
|
||||
* 生成命令可以覆盖旧文件,但覆盖不等于契约已同步提交;这里仅检查指定生成物,
|
||||
* 避免用户其他工作区改动干扰契约门禁。
|
||||
*/
|
||||
export function assertGeneratedContractsUnchanged(repositoryRoot, contractFiles) {
|
||||
try {
|
||||
execFileSync('git', ['diff', '--exit-code', '--', ...contractFiles], {
|
||||
cwd: repositoryRoot,
|
||||
encoding: 'utf8',
|
||||
stdio: ['ignore', 'ignore', 'pipe'],
|
||||
});
|
||||
} catch {
|
||||
throw new Error(`生成契约与 Git 提交版本不一致:${contractFiles.join('、')}`);
|
||||
}
|
||||
}
|
||||
|
||||
const currentFile = fileURLToPath(import.meta.url);
|
||||
if (process.argv[1] && path.resolve(process.argv[1]) === path.resolve(currentFile)) {
|
||||
assertGeneratedContractsUnchanged(process.cwd(), [
|
||||
'server/openapi/openapi.json',
|
||||
'web/src/api/generated/schema.ts',
|
||||
]);
|
||||
process.stdout.write('生成契约与 Git 提交版本一致\n');
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
import assert from 'node:assert/strict';
|
||||
import { execFileSync } from 'node:child_process';
|
||||
import { mkdtempSync, writeFileSync } from 'node:fs';
|
||||
import { tmpdir } from 'node:os';
|
||||
import path from 'node:path';
|
||||
import test from 'node:test';
|
||||
|
||||
import { assertGeneratedContractsUnchanged } from './verify-generated-contracts.mjs';
|
||||
|
||||
function createGitFixture() {
|
||||
const repositoryRoot = mkdtempSync(path.join(tmpdir(), 'a-share-contracts-'));
|
||||
execFileSync('git', ['init', '-q'], { cwd: repositoryRoot });
|
||||
execFileSync('git', ['config', 'user.name', 'Contract Test'], { cwd: repositoryRoot });
|
||||
execFileSync('git', ['config', 'user.email', 'contract-test@example.invalid'], {
|
||||
cwd: repositoryRoot,
|
||||
});
|
||||
writeFileSync(path.join(repositoryRoot, 'contract.json'), '{"version":1}\n');
|
||||
execFileSync('git', ['add', 'contract.json'], { cwd: repositoryRoot });
|
||||
execFileSync('git', ['commit', '-qm', '测试基线'], { cwd: repositoryRoot });
|
||||
return repositoryRoot;
|
||||
}
|
||||
|
||||
test('生成契约与提交版本一致时通过', () => {
|
||||
const repositoryRoot = createGitFixture();
|
||||
assert.doesNotThrow(() => assertGeneratedContractsUnchanged(repositoryRoot, ['contract.json']));
|
||||
});
|
||||
|
||||
test('生成契约发生漂移时以明确错误阻断', () => {
|
||||
const repositoryRoot = createGitFixture();
|
||||
writeFileSync(path.join(repositoryRoot, 'contract.json'), '{"version":2}\n');
|
||||
|
||||
assert.throws(
|
||||
() => assertGeneratedContractsUnchanged(repositoryRoot, ['contract.json']),
|
||||
/生成契约与 Git 提交版本不一致:contract\.json/,
|
||||
);
|
||||
});
|
||||
Reference in New Issue
Block a user