Files
Unlucky-Launcher/crates/cli/tests/adopt_test/cli_surface.rs
T
AskaEth 9f5c001387 feat(game): 从其他启动器只读登记已有版本(ull instances scan|adopt)+ 登记可一并定隔离档(M221)
盘上已经铺好的版本(多半是 PCL/HMCL 装的)今天在 ULL 里看不见,用户只能重装一遍。这个
commit 把它们登记成实例,**只读外部、只写自己的配置**:不搬文件、不补清单、不改任何外部
文件的 mtime(versions/ libraries/ assets/ 一个字节都不写)。

core(`game::adopt`,新模块):
- `scan` / `inspect`:只读扫描 + 逐项检查,坏 JSON(带行列号)/ 缺 id / id 与目录名不一致 /
  缺父 / 成环 / 读不出来 / 越界 / 同名冲突一律**结构化**报错(稳定 `code`),不吞成空列表;
  `versions/` 不存在才是空结果,读不了就是错误;
- 越界判据用 canonicalize 后的前缀比较,同时覆盖**符号链接、Windows 目录联接(junction)与
  祖先链接**(junction 不需要管理员权限,是最常见的「把别人版本目录挂进来」);越界条目在
  边界检查就停手,一个字节都不去读;
- `plan`(零写盘,喂 dry-run 与后续 GUI 预检)/ `adopt`(plan + 只写自己的配置):
  **缺省运行目录落 ULL 数据区 `<data>/instances/<实例目录名>`(custom 档),绝不沿用全局
  `isolated`**——那条缺省的运行目录正是 `versions/<versionId>/`,会在他人的版本目录里长出
  mods/saves/config;`shared` / `isolated` 必须显式选,且 `target_notes()` 给出后果话术;
- 缺省运行目录已存在且非空 ⇒ 拒绝(不覆盖、不猜「合并进已有实例」);数据区里已有的祖先
  若解析到数据根之外(符号链接/联接)⇒ 拒绝,不沿它写盘;
- 登记与隔离档一次写清:`install/instance` 拆出 `register` 子模块,新增
  `register_with(InstanceSettings)`;写盘顺序改成「先实例设置、后 profiles」(中断只留孤立
  设置文件,不会留下按全局档启动的实例),profiles 写失败会撤掉刚写的设置。`register`
  签名与行为不变(`register` = `register_with(inherit)`;同版本同名的幂等复用照旧)。

CLI:
- `ull instances scan`(只读列出候选与原因)、`ull instances adopt <version-id> [--name N]
  [--isolation shared|isolated|custom] [--game-dir DIR] [--dry-run]`;`--game-dir` 单独给即
  custom,相对路径在解析期就报用法错误;
- 人读/`--json`/help 三条出口齐备(`error.detail.problems` 逐条可分流),已有命令未改。

测试(真磁盘夹具,`cargo test -p unlucky-core --test adopt` 22 passed /
`cargo test -p unlucky-cli --test adopt_test` 10 passed):
- 一份「别的启动器铺好的」`.minecraft`(versions/libraries/assets/mods/saves/config +
  launcher_profiles.json),扫描与登记前后做「路径集合 + 字节数 + 内容哈希 + mtime」全等断言;
- 正常 / 缺清单 / 坏 JSON / 缺父 / 成环 / id 不匹配 / 非法 id / 符号链接越界 / 根内符号链接
  放行 / **junction 越界** / 数据区 junction 写盘边界 / 缺省非 isolated / shared 与 isolated
  的话术 / 重名 / 重复 / 非空目录 / 空目录复用 / dry-run 零写盘;
- CLI 侧验 `launch --dry-run` 的 `--gameDir` 指向选定运行目录(argv 已脱敏,比尾段;
  `data.game_dir` 全等);
- 变异核对(只改实现、跑完即回滚):缺省档改 `VersionDir` ⇒ 红;边界判据摘掉 ⇒ 符号链接与
  junction 两条红;写盘边界摘掉 ⇒ 数据区 junction 那条红;缺父当链终点 ⇒ 两条红。

界限:没有启动真实 Minecraft;GUI(presenter/app-iced)接线另派;D20 单游戏根模型未改
(不扫第二个 `.minecraft`)。
2026-11-09 22:21:37 +08:00

103 lines
3.7 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//! 命令面:help 文案、用法错误、错误码——不碰数据或者只碰「拒绝」那条路。
//!
//! 从 `tests/adopt_test.rs` 拆出来只为守 400 行硬线;搬动是纯搬,断言未变。
use crate::support::{parse_json, run_cli, TempDir};
use crate::fixture::foreign_data_root;
/// 运行目录选项的用法错误:一律退出码 1 + `code=usage`(不是内部错误)。
///
/// 抓得住:`--isolation custom` 少 `--game-dir` 被默默当成缺省、`shared` 同时给 `--game-dir`
/// 被静默忽略、未知词被当缺省。
/// 抓不住:提示文案的措辞(只断言 code 与关键字)。
#[test]
fn isolation_option_misuse_is_a_usage_error() {
let dir = TempDir::new("adopt-usage");
foreign_data_root(dir.path());
let cases: Vec<Vec<&str>> = vec![
vec!["--isolation", "custom"],
vec!["--isolation", "shared", "--game-dir", "C:/tmp/x"],
vec!["--isolation", "nope"],
vec!["--game-dir", "relative/dir"],
];
for extra in cases {
let mut args = vec!["instances", "adopt", "1.20.1", "--json", "--data-dir"];
let dir_arg = dir.arg();
args.push(&dir_arg);
args.extend(extra.iter().copied());
let (code, stdout, stderr) = run_cli(&args);
assert_eq!(code, 1, "args={args:?}\nstdout={stdout}\nstderr={stderr}");
let value = parse_json(&stdout);
assert_eq!(
value["error"]["code"], "usage",
"args={args:?} 应是用法错误:{stdout}"
);
}
}
/// 不存在的版本 ⇒ `adopt_version_not_found`(不联网、不偷偷装一个)。
///
/// 抓得住:不存在的 id 被登记成功。
/// 抓不住:`versions/` 整个不存在时 `scan` 的空结果(core 侧用例覆盖)。
#[test]
fn unknown_version_reports_not_found() {
let dir = TempDir::new("adopt-unknown");
foreign_data_root(dir.path());
let (code, stdout, _) = run_cli(&[
"instances",
"adopt",
"9.9.9",
"--json",
"--data-dir",
&dir.arg(),
]);
assert_eq!(code, 1, "{stdout}");
assert_eq!(
parse_json(&stdout)["error"]["code"],
"adopt_version_not_found"
);
}
/// help 里必须有这两条命令(新命令不留「有实现没文档」的状态),且旧命令仍在。
///
/// 抓得住:help 漏列 `instances scan` / `instances adopt`(子串断言),或把旧的
/// `instances launch-args` / `pack import` 弄丢。
/// 抓不住:帮助文案与真实行为是否一致(那要靠上面的行为用例 + 人读)。
#[test]
fn help_lists_the_new_subcommands_next_to_the_old_ones() {
let general = unlucky_cli::cli::help::general();
for needle in [
"instances scan",
"instances adopt",
"instances list",
"install <version>",
] {
assert!(general.contains(needle), "总帮助缺 `{needle}`:\n{general}");
}
let topic = unlucky_cli::cli::help::topic("instances").expect("help instances 存在");
for needle in [
"instances scan",
"instances adopt",
"缺省不沿用全局 isolated",
"一个字节都不写",
] {
assert!(topic.contains(needle), "help instances 缺 `{needle}`");
}
// 旧的 `per_version` 字面量只在 core 的 `Isolation::parse` 里被兼容,CLI 词表不接受它。
let bogus = std::env::temp_dir().join("ull-adopt-help-nonexistent");
let bogus = bogus.display().to_string();
let args = [
"instances",
"adopt",
"1.20.1",
"--isolation",
"per_version",
"--json",
"--data-dir",
bogus.as_str(),
];
let (code, stdout, _) = run_cli(&args);
assert_eq!(code, 1, "`per_version` 不是 CLI 词表:{stdout}");
}