fix(deploy): 中文路径被 git 转义 → 按需重建误判为"两端全建"
首次真机验证按需重建时踩到:`git diff --name-only` 默认 core.quotePath=true, docs/部署说明.md 会输出成 "docs/\351\203\250...",匹配不到 docs/* 规则, 一路掉进"拿不准就两端都重建"的兜底分支——只改文档却重建了 backend + web。 修复: - 新增 classify-changes.sh --git-diff <from> <to> [repo],内部用 `git -c core.quotePath=false diff --name-only`,部署脚本改走它; - 分类时顺带剥掉可能残留的首尾引号; - 补回归用例:临时 git 仓库里提交中文文件名,断言改动列表保留中文原文、 且最终分类为空(不加 --git-diff 时该用例会失败,已实测复现)。
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import assert from 'node:assert/strict'
|
||||
import { execFileSync } from 'node:child_process'
|
||||
import fs from 'node:fs'
|
||||
import os from 'node:os'
|
||||
import path from 'node:path'
|
||||
import test from 'node:test'
|
||||
import { fileURLToPath } from 'node:url'
|
||||
@@ -21,6 +22,21 @@ function classify(files: string[]): string {
|
||||
}).trim()
|
||||
}
|
||||
|
||||
function withTempGitRepo(fn: (repo: string, git: (...args: string[]) => string) => void): void {
|
||||
const repo = fs.mkdtempSync(path.join(os.tmpdir(), 'deploy-classify-'))
|
||||
const git = (...args: string[]) =>
|
||||
execFileSync('git', ['-C', repo, '-c', 'user.email=t@t', '-c', 'user.name=t', ...args], {
|
||||
encoding: 'utf8',
|
||||
})
|
||||
try {
|
||||
git('init', '-q')
|
||||
git('commit', '-q', '--allow-empty', '-m', 'base')
|
||||
fn(repo, git)
|
||||
} finally {
|
||||
fs.rmSync(repo, { recursive: true, force: true })
|
||||
}
|
||||
}
|
||||
|
||||
test(
|
||||
'改动分类:只改后端源码只重建 backend,只改前端源码只重建 web',
|
||||
{ skip: !hasDeployScripts },
|
||||
@@ -69,11 +85,32 @@ test('部署脚本:按改动重建,不再无脑两端全量构建', { skip:
|
||||
assert.ok(!source.includes('log "构建 Web 镜像"'), '旧的写死 Web 构建仍在')
|
||||
})
|
||||
|
||||
test('改动分类:中文文件名不会被 git 转义成"漏判 → 两端全建"', { skip: !hasDeployScripts }, () => {
|
||||
// 真实踩过的坑:docs/部署说明.md 被 git 输出成 "docs/\351\203\250...",
|
||||
// 匹配不到 docs/* 规则,直接掉进兜底分支把 backend + web 都重建了。
|
||||
withTempGitRepo((repo, git) => {
|
||||
fs.mkdirSync(path.join(repo, 'docs'), { recursive: true })
|
||||
fs.writeFileSync(path.join(repo, 'docs', '部署说明.md'), 'v1')
|
||||
git('add', '-A')
|
||||
git('commit', '-q', '-m', 'docs: 中文文件名')
|
||||
|
||||
const changed = execFileSync('bash', [CLASSIFIER, '--git-diff', 'HEAD~1', 'HEAD', repo], {
|
||||
encoding: 'utf8',
|
||||
})
|
||||
assert.ok(
|
||||
changed.includes('docs/部署说明.md'),
|
||||
`git 改动列表应保留中文原文,实际输出:${changed.trim()}`,
|
||||
)
|
||||
assert.equal(classify(changed.split('\n').filter(Boolean)), '')
|
||||
})
|
||||
})
|
||||
|
||||
test('部署脚本:基础镜像只补缺、部署状态在健康检查之后才落盘', { skip: !hasDeployScripts }, () => {
|
||||
const source = fs.readFileSync(DEPLOY_SCRIPT, 'utf8')
|
||||
assert.ok(source.includes('--policy missing'), 'compose pull 应使用 --policy missing')
|
||||
assert.ok(source.includes('write_deploy_state'), '缺少部署状态落盘')
|
||||
assert.ok(source.includes('print_timing_summary'), '缺少阶段耗时输出')
|
||||
assert.ok(source.includes('--git-diff'), '改动列表应通过 classify-changes.sh --git-diff 获取')
|
||||
|
||||
const healthIndex = source.indexOf('wait_for_health\n')
|
||||
const stateIndex = source.indexOf('write_deploy_state\n')
|
||||
|
||||
Reference in New Issue
Block a user