37 lines
1.5 KiB
JavaScript
37 lines
1.5 KiB
JavaScript
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/,
|
|
);
|
|
});
|