mirror of
https://github.com/YuJunZhiXue/dsh-purge.git
synced 2026-11-28 10:00:22 +08:00
fix: cordis.patch.yml 为 [] 时替换而非追加,修复宿主启动解析失败(#191)
宿主生成的 profile 层 cordis.patch.yml 可能整体只有一个空 flow 序列 [],
那本身就是一个完整 YAML 文档。旧实现无条件在文件尾追加 `- insert:`,
结果是第二个文档,宿主启动解析报
"end of the stream or a document separator is expected" 直接崩掉
(macOS DSH Desktop 0.2.1-rc.4)。
- 新增 classifyPatchBody() 判断数据部分形态:empty / empty-flow /
block-seq / flow-seq / unknown,只看最后一个文档的根节点
- 空 flow 序列([] 或 [ ],可带 --- 前缀)→ 把整行替换成 insert 块,
绝不追加成第二个文档
- 空文件 / 只有注释和空行 → 写 insert 块,注释保留
- 正常块序列 → 沿用旧的追加行为,前导空行规则不变
- 非空 flow 序列(宿主/用户内容)以及认不准的形态 → 不动,
返回 { ok: false, wrote: false },宁可留着也别写坏
- postinstall 脚本区分「跳过」和「已有」,跳过时打印说明
- profileHasPurgeBodyInsert() 检测逻辑未动
- 新增 9 条用例(形态分类 / 空文件 / 只有注释 / 注释+[] / ---+[] /
块序列追加 / 非空 flow 不写 / 已有本体行零写入 / 替换后幂等),
版本 1.1.83
Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
1 parent
97e4e6094d
commit
6533b08004
5 files changed
+222
-2
No files matched your search
@@ -10,6 +10,9 @@ import {
|
||||
purgePnpmBuildSpecs,
|
||||
rewritePnpmAllowBuilds,
|
||||
ensurePnpmAllowBuild,
|
||||
ensureProfileBodyInsert,
|
||||
classifyPatchBody,
|
||||
INSERT_BLOCK,
|
||||
} from "../lib/profile-insert.js";
|
||||
|
||||
const MASTER_URL = "https://codeload.github.com/YuJunZhiXue/dsh-purge/tar.gz/refs/heads/master";
|
||||
@@ -23,6 +26,26 @@ function withTempProfile(lockText) {
|
||||
return dir;
|
||||
}
|
||||
|
||||
function withTempPatch(content) {
|
||||
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "dsh-purge-patch-"));
|
||||
if (content !== undefined) {
|
||||
fs.writeFileSync(path.join(dir, "cordis.patch.yml"), content, "utf8");
|
||||
}
|
||||
return dir;
|
||||
}
|
||||
|
||||
/** 去掉注释和空行后,统计顶层 `- insert:` 组数(判定是不是第二个文档)。 */
|
||||
function topLevelInsertGroups(text) {
|
||||
const lines = String(text || "").split(/\r?\n/);
|
||||
let count = 0;
|
||||
for (const line of lines) {
|
||||
const trimmed = line.trim();
|
||||
if (!trimmed || trimmed.startsWith("#")) continue;
|
||||
if (/^-\s*insert:\s*$/.test(line)) count += 1;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
test("URL 键解析不被冒号截断", () => {
|
||||
const parsed = parseAllowBuildLine(` dsh-purge@${MASTER_URL}: true`);
|
||||
assert.equal(parsed.key, `dsh-purge@${MASTER_URL}`);
|
||||
@@ -233,3 +256,105 @@ test("幂等:第二次 ensure 不再写盘(自愈后收敛)", () => {
|
||||
assert.equal(fs.readFileSync(fp, "utf8"), healed);
|
||||
});
|
||||
|
||||
// ---- #191:cordis.patch.yml 是 `[]` 时追加会导致宿主启动解析失败 ----
|
||||
|
||||
test("patch 形态分类:空 / 空 flow / 块序列 / 非空 flow / 未知", () => {
|
||||
assert.equal(classifyPatchBody("").kind, "empty");
|
||||
assert.equal(classifyPatchBody("# 只有注释\n\n").kind, "empty");
|
||||
assert.equal(classifyPatchBody("[]").kind, "empty-flow");
|
||||
assert.equal(classifyPatchBody("# c\n[]\n").kind, "empty-flow");
|
||||
assert.equal(classifyPatchBody("---\n[]\n").kind, "empty-flow");
|
||||
assert.equal(classifyPatchBody("- id: foo\n name: foo\n").kind, "block-seq");
|
||||
assert.equal(classifyPatchBody("[a, b]\n").kind, "flow-seq");
|
||||
assert.equal(classifyPatchBody("foo: bar\n").kind, "unknown");
|
||||
});
|
||||
|
||||
test("空文件:直接写 insert 块", () => {
|
||||
const dir = withTempPatch(undefined);
|
||||
const fp = path.join(dir, "cordis.patch.yml");
|
||||
const result = ensureProfileBodyInsert(dir);
|
||||
assert.equal(result.ok, true);
|
||||
assert.equal(result.wrote, true);
|
||||
assert.equal(fs.readFileSync(fp, "utf8"), INSERT_BLOCK);
|
||||
});
|
||||
|
||||
test("只有注释和空行:写 insert 块,注释保留", () => {
|
||||
const dir = withTempPatch("# overrides, disables, and insertions\n\n");
|
||||
const fp = path.join(dir, "cordis.patch.yml");
|
||||
const result = ensureProfileBodyInsert(dir);
|
||||
assert.equal(result.ok, true);
|
||||
assert.equal(result.wrote, true);
|
||||
const text = fs.readFileSync(fp, "utf8");
|
||||
assert.equal(text.includes("# overrides, disables, and insertions"), true);
|
||||
assert.equal(text.includes("- insert:"), true);
|
||||
assert.equal(topLevelInsertGroups(text), 1);
|
||||
});
|
||||
|
||||
test("注释 + `[]`:`[]` 被替换成 insert 块,注释保留,无第二个文档", () => {
|
||||
const dir = withTempPatch("# overrides, disables, and insertions\n[]\n");
|
||||
const fp = path.join(dir, "cordis.patch.yml");
|
||||
const result = ensureProfileBodyInsert(dir);
|
||||
assert.equal(result.ok, true);
|
||||
assert.equal(result.wrote, true);
|
||||
const text = fs.readFileSync(fp, "utf8");
|
||||
assert.equal(text.includes("[]"), false);
|
||||
assert.equal(text.includes("# overrides, disables, and insertions"), true);
|
||||
assert.equal(text.includes("- insert:"), true);
|
||||
// 只有一个顶层 `- insert:` 组 → 没有变成第二个文档
|
||||
assert.equal(topLevelInsertGroups(text), 1);
|
||||
});
|
||||
|
||||
test("`---` 前缀 + `[]`:同样替换,无第二个文档", () => {
|
||||
const dir = withTempPatch("---\n[]\n");
|
||||
const fp = path.join(dir, "cordis.patch.yml");
|
||||
const result = ensureProfileBodyInsert(dir);
|
||||
assert.equal(result.ok, true);
|
||||
assert.equal(result.wrote, true);
|
||||
const text = fs.readFileSync(fp, "utf8");
|
||||
assert.equal(text.includes("[]"), false);
|
||||
assert.equal(topLevelInsertGroups(text), 1);
|
||||
});
|
||||
|
||||
test("正常块序列:沿用旧的追加行为", () => {
|
||||
const original = "- id: foo\n name: foo\n";
|
||||
const dir = withTempPatch(original);
|
||||
const fp = path.join(dir, "cordis.patch.yml");
|
||||
const result = ensureProfileBodyInsert(dir);
|
||||
assert.equal(result.ok, true);
|
||||
assert.equal(result.wrote, true);
|
||||
const text = fs.readFileSync(fp, "utf8");
|
||||
assert.equal(text.startsWith(original), true);
|
||||
assert.equal(text.includes("- insert:"), true);
|
||||
assert.equal(topLevelInsertGroups(text), 1);
|
||||
});
|
||||
|
||||
test("非空 flow 序列:宿主内容不动,返回 ok:false", () => {
|
||||
const original = "[a, b]\n";
|
||||
const dir = withTempPatch(original);
|
||||
const fp = path.join(dir, "cordis.patch.yml");
|
||||
const result = ensureProfileBodyInsert(dir);
|
||||
assert.equal(result.ok, false);
|
||||
assert.equal(result.wrote, false);
|
||||
assert.equal(fs.readFileSync(fp, "utf8"), original);
|
||||
});
|
||||
|
||||
test("已有 dsh-purge 本体行:零写入", () => {
|
||||
const original = "- insert:\n - id: dsh-purge\n name: dsh-purge\n";
|
||||
const dir = withTempPatch(original);
|
||||
const fp = path.join(dir, "cordis.patch.yml");
|
||||
const result = ensureProfileBodyInsert(dir);
|
||||
assert.equal(result.ok, true);
|
||||
assert.equal(result.wrote, false);
|
||||
assert.equal(fs.readFileSync(fp, "utf8"), original);
|
||||
});
|
||||
|
||||
test("`[]` 替换后幂等:再 ensure 一次零写入", () => {
|
||||
const dir = withTempPatch("# c\n[]\n");
|
||||
const fp = path.join(dir, "cordis.patch.yml");
|
||||
const first = ensureProfileBodyInsert(dir);
|
||||
assert.equal(first.wrote, true);
|
||||
const healed = fs.readFileSync(fp, "utf8");
|
||||
const second = ensureProfileBodyInsert(dir);
|
||||
assert.equal(second.wrote, false);
|
||||
assert.equal(fs.readFileSync(fp, "utf8"), healed);
|
||||
});
|
||||
Reference in new issue
Block a user