复审 p2-5items 的五项,全部按「先能红的用例,再改实现」: R1(默认目录可被不同实例隐式共用,产品缺陷):新增 `adopt/run_dir.rs`——登记前读**每个已有实例** 的 `gameDir`(现役设置 + M27 前档案的遗留字段),用「真实路径 + 拼回不存在的尾段 + 大小写归一」 (Windows/macOS)比较;撞上就顺延 `<base>-2/-3…`(上限 32,撞满 `adopt_no_free_run_dir`)并在 `notes` 点名撞的是哪个实例;**非空且无人认领**仍拒绝(那是用户数据,不偷偷换目录)。这样 `Better MC` 与 `Better-MC`(slug 同名)、Windows 上 `Foo` 与 `foo`(同目录)都不会共用一个运行目录。 大小写归一在 macOS 上是**保守**选择:大小写敏感卷上会多一次顺延,换来的是「绝不共用」在任何卷上成立。 R2(配置写面 junction 写穿到外部游戏根,产品缺陷):`run_dir::ensure_config_write_surface()` 在 `plan`(dry-run 也报)与 `adopt`(写盘前)各核一次——`config/`、`config/instances/`、 `profiles.json` 的真实落点必须在 `<.ULL>` 内,且**不得经链接落进游戏根**(错误按真实归属分类: 「逃出 .ULL」vs「落进游戏根」)。`.ULL` 本身在游戏根内是布局事实,附加判据在该布局下零判别力, 主判据仍然拦下一切跑出 `.ULL` 的落点——这不是给链接开后门。新码 `adopt_config_surface_unsafe`。 R3(dangling versions 被当不存在,产品缺陷):`scan` 的 `read_dir` `NotFound` 再三分—— `lstat(versions)` 看见入口(断链符号链接/目录联接/目标被移走)或游戏根解析失败(祖先断链)⇒ `adopt_versions_unreadable`;两者都不是才算「真没有」。`inspect` 也补了同族判据(条目存在但 不是可读目录 ⇒ `directory_unreadable`,不再混成「没装」)。 R4(强制链接测试逻辑反转,假绿):`fixture::link_gate(created, required) -> Run|Skip|Fail` 是纯 真值表,`link_guard_with()` 显式传参(`link_guard()` 只读环境变量);新增 `link_guard_executes_all_three_branches` 用 `catch_unwind` **执行** Fail/Skip 两个分支。 四处调用点统一走 guard;建链接/联接的夹具收进 `fixture.rs`。 R5(快照遗漏目录节点,假绿):新增 `tests/adopt/snapshot.rs`——全节点快照(根/目录/文件/链接 各记类型 + 适用 mtime;目录记 mtime、链接只记目标不跟随、Windows 用重解析点属性认 junction、 读盘失败直接 panic)。**CLI 测试用 `#[path]` 复用同一份**,判据只一处;新增 `fixture_cases` 五条工具自测,`plan`/dry-run 的零写盘判据改用 `snapshot_opt`(连空目录都不许出现)。 验证(本轮持久日志,`target/m221-r1/`,git 忽略、不入库): - `cargo fmt --all --check` 0;`cargo check -p unlucky-core -p unlucky-cli --all-targets` 0; - `cargo test -p unlucky-core --test adopt` **37 passed / 0 failed**(另跑一次 `ULL_ADOPT_TEST_SYMLINK_REQUIRE=1` 同样 37 passed); - `cargo test -p unlucky-cli --test adopt_test` **11 passed / 0 failed**; - `cargo clippy -p unlucky-core -p unlucky-cli --all-targets -- -D warnings` 0; - 四次**实现-only 变异**(缺省不查认领 / 配置写面不核 / versions 的 NotFound 当空 / 快照不记目录) 全部以**断言红**收场(不是编译错误、不是环境错误),每次还原后与基线清单逐文件一致,收尾再跑绿。 界限:只覆盖 core+cli 的定向目标(不等于 workspace 全量);没有启动真实 Minecraft;GUI 未接; 判据是检查那一刻的快照,TOCTOU 与非 junction 重解析点仍列边界。
119 lines
4.9 KiB
Rust
119 lines
4.9 KiB
Rust
//! `plan` / `inspect` 的**只读**判据与拒绝理由:零写盘、缺父/不存在/非法目标的报错形状。
|
||
//!
|
||
//! 与 `register_cases` 的分工:那边验「登记写出了什么」,这边验「还没登记时算出了什么、拒绝得对不对」。
|
||
|
||
use std::path::PathBuf;
|
||
|
||
use unlucky_core::game::adopt::{self, AdoptError, AdoptTarget};
|
||
use unlucky_core::game::install::instance::Isolation;
|
||
|
||
use crate::fixture::{
|
||
assert_unchanged, foreign_minecraft, request, snapshot, snapshot_opt, TempRoot,
|
||
};
|
||
|
||
/// 缺省档位本身就是「数据区独立目录」,而且**不是** `isolated`(判据的单一真相源)。
|
||
///
|
||
/// 抓得住:把 `#[default]` 挪到 `VersionDir`/`Shared`(第一条断言红),或让缺省档映射到
|
||
/// `Isolated`(第二条断言红)——后者正是「导入别人的版本后第一次启动就在他的 `versions/<id>/`
|
||
/// 里长 mods/saves」的那个坑。
|
||
/// 抓不住:CLI 是否真的用 `AdoptTarget::default()`(那条由 cli/adopt_test 的行为断言钉着)。
|
||
#[test]
|
||
fn the_default_target_is_an_own_dir_not_the_global_isolated_mode() {
|
||
let target = AdoptTarget::default();
|
||
assert_eq!(target, AdoptTarget::InstanceDir);
|
||
assert_eq!(target.as_str(), "instance_dir");
|
||
assert_eq!(target.isolation(), Isolation::Custom);
|
||
assert_ne!(target.isolation(), Isolation::Isolated);
|
||
}
|
||
|
||
/// `--isolation custom --game-dir <相对路径>` ⇒ 明确报错(不猜、不悄悄改成绝对路径)。
|
||
///
|
||
/// 抓得住:相对路径被默默接受(`TargetInvalid` 变成成功)。
|
||
/// 抓不住:Windows 上 `C:foo` 这种「带盘符但非绝对」的形状,只看 `is_absolute`。
|
||
#[test]
|
||
fn custom_target_requires_an_absolute_directory() {
|
||
let root = TempRoot::new("custom-rel");
|
||
foreign_minecraft(&root.game_root());
|
||
let error = adopt::adopt(
|
||
&root.game(),
|
||
&root.launcher(),
|
||
&request("1.20.1", AdoptTarget::Custom(PathBuf::from("relative/dir"))),
|
||
)
|
||
.expect_err("相对路径应被拒");
|
||
assert_eq!(error.code(), "adopt_target_invalid");
|
||
assert!(error.to_string().contains("绝对路径"), "{error}");
|
||
}
|
||
|
||
/// `--dry-run` 的 core 形态(`plan`):一个文件都不写、目录也不建。
|
||
///
|
||
/// 抓得住:`plan` 顺手建了运行目录或写了档案(两种断言都会红)。
|
||
/// 抓不住:CLI 的 `--dry-run` 是否真的只调 `plan`(那条在 cli 侧用例里)。
|
||
#[test]
|
||
fn plan_writes_nothing_at_all() {
|
||
let root = TempRoot::new("plan");
|
||
foreign_minecraft(&root.game_root());
|
||
let before = snapshot(root.path());
|
||
|
||
let plan = adopt::plan(
|
||
&root.game(),
|
||
&root.launcher(),
|
||
&request("1.20.1", AdoptTarget::InstanceDir),
|
||
)
|
||
.expect("计划应成功");
|
||
let after = snapshot(root.path());
|
||
assert_unchanged(&before, &after, "plan 之后(含 .ULL)");
|
||
|
||
assert!(plan.candidate.adoptable());
|
||
assert!(!plan.run_dir.exists(), "plan 不该建目录");
|
||
// `.ULL` 连**空目录**都不许出现(存在性用 `snapshot_opt`,与「空树」区分开)。
|
||
assert!(
|
||
snapshot_opt(&root.path().join(".ULL")).is_none(),
|
||
"plan 不该建配置目录(连空的也不行)"
|
||
);
|
||
}
|
||
|
||
/// 不可登记的版本(缺父)⇒ 一条都不登记,问题清单原样带出。
|
||
///
|
||
/// 抓得住:缺父的版本被登记(启动必失败却显示「导入成功」)、问题被折叠成一句空话。
|
||
/// 抓不住:用户在原来的启动器里修好之后重试(那条由重试路径自然覆盖)。
|
||
#[test]
|
||
fn not_adoptable_versions_are_refused_with_their_reasons() {
|
||
let root = TempRoot::new("refuse");
|
||
foreign_minecraft(&root.game_root());
|
||
let error = adopt::adopt(
|
||
&root.game(),
|
||
&root.launcher(),
|
||
&request("1.20.1-orphan", AdoptTarget::InstanceDir),
|
||
)
|
||
.expect_err("缺父版本应被拒");
|
||
assert_eq!(error.code(), "adopt_not_adoptable");
|
||
let codes: Vec<&str> = error
|
||
.problems()
|
||
.iter()
|
||
.map(|problem| problem.code())
|
||
.collect();
|
||
assert_eq!(codes, vec!["missing_parent"]);
|
||
assert!(
|
||
!root.path().join(".ULL/config/profiles.json").exists(),
|
||
"拒绝时不许留档案"
|
||
);
|
||
}
|
||
|
||
/// 不存在的版本 ⇒ `adopt_version_not_found`(不是「帮你在网上下一个」)。
|
||
///
|
||
/// 抓得住:不存在的 id 被当成可登记(档案指向空版本)。
|
||
/// 抓不住:`versions/` 整个不存在的情形(上一条扫描用例覆盖)。
|
||
#[test]
|
||
fn an_unknown_version_is_reported_as_not_found() {
|
||
let root = TempRoot::new("unknown");
|
||
foreign_minecraft(&root.game_root());
|
||
let error = adopt::adopt(
|
||
&root.game(),
|
||
&root.launcher(),
|
||
&request("9.9.9", AdoptTarget::InstanceDir),
|
||
)
|
||
.expect_err("不存在的版本应报错");
|
||
assert!(matches!(error, AdoptError::UnknownVersion { .. }));
|
||
assert_eq!(error.code(), "adopt_version_not_found");
|
||
}
|