Files
Unlucky-Launcher/crates/core/tests/setting/common.rs
T
AskaEth 8a8e5ca0c0
CI / fmt (rustfmt --check) (push) Waiting to run
CI / clippy (ubuntu-latest) (push) Waiting to run
CI / clippy (windows-latest) (push) Waiting to run
CI / test (ubuntu-latest) (push) Waiting to run
CI / test (windows-latest) (push) Waiting to run
CI / build release (ubuntu-latest) (push) Waiting to run
CI / build release (windows-latest) (push) Waiting to run
CI / runtime smoke (ubuntu x64, xvfb) (push) Waiting to run
CI / runtime smoke (macos arm64, probe) (push) Skipped
Guards / core 零 UI 守门 (push) Waiting to run
Guards / presenter 零 UI 守门 (push) Waiting to run
Guards / design 零第三方依赖守门 (push) Waiting to run
Guards / unsafe 落点守门(集合必须恰好一个文件) (push) Waiting to run
Guards / 文件行数 ≤400 守门 (push) Waiting to run
Guards / 依赖许可与来源(cargo-deny) (push) Waiting to run
Unlucky Launcher v0.2.0
首个 iced + Material 3 Expressive 版本的源码快照。
完整历史与日常提交在 dev 分支;main 只放阶段性快照。
2026-11-05 14:22:11 +08:00

82 lines
2.2 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.
//! 测试公用小工具(临时目录 + 文件读写)。
//!
//! 不引 `tempfile`:M11 的依赖面只有 serde/serde_json,测试自己管临时目录。
#![allow(dead_code)]
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicU64, Ordering};
static COUNTER: AtomicU64 = AtomicU64::new(0);
/// 进程内唯一的临时目录,`Drop` 时递归删除。
pub struct TempDir {
path: PathBuf,
}
impl TempDir {
/// 建一个临时目录。
pub fn new(label: &str) -> Self {
let index = COUNTER.fetch_add(1, Ordering::Relaxed);
let path = std::env::temp_dir().join(format!(
"ull-setting-{label}-{}-{index}",
std::process::id()
));
std::fs::remove_dir_all(&path).ok();
std::fs::create_dir_all(&path).expect("创建临时目录");
Self { path }
}
/// 根路径。
pub fn path(&self) -> &Path {
&self.path
}
/// 拼接子路径(不创建)。
pub fn child(&self, name: &str) -> PathBuf {
self.path.join(name)
}
/// 写一个文件(自动建父目录)。
pub fn write(&self, name: &str, contents: &str) -> PathBuf {
let path = self.child(name);
if let Some(parent) = path.parent() {
std::fs::create_dir_all(parent).expect("创建父目录");
}
std::fs::write(&path, contents).expect("写文件");
path
}
/// 读一个文件。
pub fn read(&self, name: &str) -> String {
std::fs::read_to_string(self.child(name)).expect("读文件")
}
/// 子路径是否存在。
pub fn exists(&self, name: &str) -> bool {
self.child(name).exists()
}
}
impl Drop for TempDir {
fn drop(&mut self) {
std::fs::remove_dir_all(&self.path).ok();
}
}
/// 目录里的文件名(排序后返回,便于断言没留下临时文件)。
pub fn dir_entries(dir: &Path) -> Vec<String> {
let mut names: Vec<String> = std::fs::read_dir(dir)
.expect("读目录")
.map(|entry| {
entry
.expect("目录项")
.file_name()
.to_string_lossy()
.into_owned()
})
.collect();
names.sort();
names
}