正式构建拒绝源码非规范换行并补充回归门禁

This commit is contained in:
xuans committed 2026-09-30 20:59:07 +08:00
1 parent 70f19d09ff
commit 9e89edae28
6 files changed
+116 -1

No files matched your search

+61 -1
View File
@@ -5,11 +5,71 @@ import { tmpdir } from 'node:os';
import { dirname, join, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';
import test from 'node:test';
import { installDependencies, npmEnvironment, validateEnvironment, validateFrontend, validateGoVersion } from './toolchain.mjs';
import { inspectSourceLineEndings, installDependencies, npmEnvironment, validateEnvironment, validateFrontend, validateGoVersion, validateSourceLineEndings } from './toolchain.mjs';
const root = resolve(dirname(fileURLToPath(import.meta.url)), '..');
const npmCommand = process.platform === 'win32' ? 'npm.cmd' : 'npm';
test('源码换行检查接受规范文本、无换行文件及二进制', () => {
const records = [
'i/lf w/lf attr/text=auto eol=lf \t页面 空格.vue',
'i/none w/none attr/text=auto eol=lf \tempty.js',
'i/-text w/-text attr/text=auto eol=lf \timage.png',
'i/lf w/lf attr/text=auto eol=lf \t换行\n文件.js',
].join('\0') + '\0';
assert.deepEqual(validateSourceLineEndings(records), { policy: 'lf', tracked_files: 4 });
});
for (const ending of ['crlf', 'mixed']) {
test(`源码换行检查拒绝 ${ending} 而不是自动修改文件`, () => {
assert.throws(() => validateSourceLineEndings(`i/lf w/${ending} attr/text=auto eol=lf \t页面.vue\0`), /非规范换行.*页面.vue/);
});
}
test('源码换行检查拒绝空、截断、格式错误或未知状态', () => {
for (const output of ['', '\0', 'i/lf w/lf attr/text\tapp.js', 'invalid\0', 'i/lf w/unknown attr/text\tapp.js\0']) {
assert.throws(() => validateSourceLineEndings(output));
}
});
test('实际 Git 干净差异仍可检测工作树换行,且不改写源文件', (t) => {
const directory = mkdtempSync(join(tmpdir(), 'scert-source-endings-'));
t.after(() => rmSync(directory, { recursive: true, force: true }));
const run = (...args) => {
const result = spawnSync('git', args, { cwd: directory, encoding: 'utf8', windowsHide: true });
assert.equal(result.status, 0, `${result.error ?? ''}\n${result.stderr}`);
return result.stdout;
};
run('init', '--quiet');
writeFileSync(join(directory, '.gitattributes'), '* text=auto eol=lf\n');
const source = join(directory, '页面.vue');
writeFileSync(source, '<template>中文页面</template>\n');
writeFileSync(join(directory, 'image.png'), Buffer.from([0, 13, 10, 255]));
run('add', '--', '.gitattributes', '页面.vue', 'image.png');
assert.equal(inspectSourceLineEndings(directory).tracked_files, 3);
const changed = Buffer.from('<template>中文页面</template>\r\n');
writeFileSync(source, changed);
assert.equal(run('diff', '--exit-code'), '');
assert.throws(() => inspectSourceLineEndings(directory), /非规范换行/);
assert.deepEqual(readFileSync(source), changed);
const result = spawnSync(process.execPath, [join(root, 'build', 'toolchain.mjs'),
'--backend', directory, '--frontend', directory, '--install'], {
cwd: directory, encoding: 'utf8', windowsHide: true,
});
assert.equal(result.status, 1);
assert.match(result.stderr, /非规范换行/);
assert.equal(result.stdout, '');
assert.ok(!existsSync(join(directory, 'node_modules')));
assert.ok(!existsSync(join(directory, 'package-lock.json')));
assert.deepEqual(readFileSync(source), changed);
});
test('非 Git 目录不能跳过源码换行检查', (t) => {
const directory = mkdtempSync(join(tmpdir(), 'scert-source-no-git-'));
t.after(() => rmSync(directory, { recursive: true, force: true }));
assert.throws(() => inspectSourceLineEndings(directory), /git 执行失败/);
});
function fixture() {
const frontend = join(root, 'frontend');
const pkg = JSON.parse(readFileSync(join(frontend, 'package.json'), 'utf8'));