Files
Unlucky-Launcher/crates/core/tests/game/helpers.rs
T
AskaEth bc41dfcf7d
CI / fmt (rustfmt --check) (push) Canceled after 0s
CI / clippy (ubuntu-latest) (push) Canceled after 0s
CI / clippy (windows-latest) (push) Canceled after 0s
CI / test (ubuntu-latest) (push) Canceled after 0s
CI / test (windows-latest) (push) Canceled after 0s
CI / build release (ubuntu-latest) (push) Canceled after 0s
CI / build release (windows-latest) (push) Canceled after 0s
Guards / core 零 UI 守门 (push) Canceled after 0s
Guards / 文件行数 ≤400 守门 (push) Canceled after 0s
Guards / 依赖许可与来源(cargo-deny) (push) Canceled after 0s
Unlucky Launcher v0.0.1-alpha
Rust 原生单文件 Minecraft 启动器:CLI 完整可用(install/launch/accounts/java/diagnose/agent),
MCP server 接入外部 Agent 工具,GUI 开发中。
2026-10-31 21:54:10 +08:00

67 lines
2.0 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.
//! 测试公用件:fixture 解析、内存父版本表、临时目录守卫。
use std::collections::BTreeMap;
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicU64, Ordering};
use std::time::{SystemTime, UNIX_EPOCH};
use unlucky_core::game::manifest::VersionJson;
/// 解析 fixture JSON(fixture 是测试常量,解析失败即 panic)。
pub fn parse(json: &str) -> VersionJson {
VersionJson::from_json_str(json).expect("fixture 必须可解析")
}
/// 用 `(id, json)` 对建内存父版本表,喂给 `inherit::resolve`。
pub fn source(pairs: &[(&str, &str)]) -> BTreeMap<String, VersionJson> {
pairs
.iter()
.map(|(id, json)| ((*id).to_owned(), parse(json)))
.collect()
}
/// 唯一临时目录守卫:`drop` 时递归删除。
pub struct TempDir {
path: PathBuf,
}
impl TempDir {
/// 建一个唯一临时目录。
pub fn new(tag: &str) -> Self {
static COUNTER: AtomicU64 = AtomicU64::new(0);
let nanos = SystemTime::now()
.duration_since(UNIX_EPOCH)
.map_or(0, |d| d.as_nanos());
let seq = COUNTER.fetch_add(1, Ordering::Relaxed);
let path = std::env::temp_dir().join(format!(
"ull-game-{tag}-{}-{nanos}-{seq}",
std::process::id()
));
std::fs::create_dir_all(&path).expect("临时目录应可创建");
Self { path }
}
/// 目录路径。
pub fn path(&self) -> &Path {
&self.path
}
}
impl Drop for TempDir {
fn drop(&mut self) {
let _ = std::fs::remove_dir_all(&self.path);
}
}
/// 建一个游戏根布局(纯路径计算,不建目录)。
pub fn layout(root: &Path) -> unlucky_core::game::GameLayout {
unlucky_core::game::GameLayout::new(root)
}
/// 造 `dir/<name>` 并写一个文件进去(模拟玩家内容:`mods`/`saves`/`config` 的条目)。
pub fn seed(dir: &Path, name: &str) {
let target = dir.join(name);
std::fs::create_dir_all(&target).expect("建目录");
std::fs::write(target.join("entry.txt"), b"x").expect("写文件");
}