refactored: test style cleanup + mmc archive extract test
Picks up the style audit's findings without touching production
behaviour (except for two em-dash characters in non-test code that
violated the no-em-dash rule).
- cli/import.rs, instance/import/mmc.rs: replace the two em-dashes
that leaked into source (one in a user-facing format string, one
in a code comment) with hyphen / colon.
- instance/manager.rs: rewrite test_manager to return TempDir instead
of a hand-rolled tempdir path with manual cleanup. Drop the
trailing std::fs::remove_dir_all from every test. Rename test_*
functions to describe the scenario per CONTRIBUTING (delete_missing_
instance_returns_not_found, save_then_load_all_round_trips_config,
load_one_missing_returns_not_found, etc.).
- migrate.rs: replace unique_tmp + atomic counter with
tempfile::tempdir() across all seven tests; ~10 lines of plumbing
gone, automatic cleanup via Drop.
- auth/accounts.rs: drop the dummy_account wrapper that just forwarded
to create_offline_account; inline the real call at the eight use
sites.
- instance/models.rs: rename test_instance_config_roundtrip to
instance_config_roundtrips_through_json.
- instance/content/mods.rs: drop the "what" comments above straight
forward asserts; keep the one "why" comment that explains an
empty-name + present-logo coverage gap. Trim the obvious "helper to
create a jar (zip) file" comment above make_jar.
- tests/smoke.rs: //! to // for the file header; project convention is
// for files (//! is for crate-level rustdoc, which doesn't render
for integration test crates).
Adds one new test:
- instance/import/mmc.rs: extract_mmc_archive_copies_minecraft_subtree
builds an in-memory zip with a Pack/ prefix, a Pack/mmc-pack.json
sibling, and Pack/.minecraft/{options.txt, mods/test-mod.jar},
then asserts only the .minecraft/ subtree lands in the destination
with relative paths preserved. Closes the mmc extract coverage gap
the audit flagged.
This commit is contained in:
1 parent
623acd9e2c
commit
1f1a0ebb3d
8 files changed
+173
-185
No files matched your search
+83
-121
@@ -314,110 +314,16 @@ fn validate_name(name: &str) -> Result<(), InstanceError> {
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::instance::models::ModLoader;
|
||||
use std::path::PathBuf;
|
||||
use tempfile::TempDir;
|
||||
|
||||
fn test_manager() -> (InstanceManager, PathBuf) {
|
||||
let tmp = std::env::temp_dir().join(format!("rmcl_test_{}", uuid_like()));
|
||||
let meta = std::env::temp_dir().join(format!("rmcl_meta_test_{}", uuid_like()));
|
||||
std::fs::create_dir_all(&tmp).ok();
|
||||
std::fs::create_dir_all(&meta).ok();
|
||||
(InstanceManager::new(tmp.clone(), meta), tmp)
|
||||
}
|
||||
|
||||
fn uuid_like() -> String {
|
||||
format!(
|
||||
"{:x}",
|
||||
std::time::SystemTime::now()
|
||||
.duration_since(std::time::UNIX_EPOCH)
|
||||
.map(|d| d.as_nanos())
|
||||
.unwrap_or(0)
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_validate_name_valid() {
|
||||
assert!(validate_name("my-instance").is_ok());
|
||||
assert!(validate_name("test_world").is_ok());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_validate_name_invalid() {
|
||||
assert!(validate_name("").is_err());
|
||||
assert!(validate_name("path/traversal").is_err());
|
||||
assert!(validate_name(".hidden").is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_delete_nonexistent() {
|
||||
let (manager, tmp) = test_manager();
|
||||
let result = manager.delete("ghost-instance");
|
||||
assert!(matches!(result, Err(InstanceError::NotFound(_))));
|
||||
std::fs::remove_dir_all(&tmp).ok();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_save_and_load_all() {
|
||||
let (manager, tmp) = test_manager();
|
||||
let instance_dir = tmp.join("test-save");
|
||||
std::fs::create_dir_all(&instance_dir).ok();
|
||||
|
||||
let config = InstanceConfig {
|
||||
name: "test-save".to_string(),
|
||||
game_version: "1.20.1".to_string(),
|
||||
loader: ModLoader::Vanilla,
|
||||
loader_version: None,
|
||||
created: chrono::Utc::now(),
|
||||
last_played: None,
|
||||
java_path: None,
|
||||
memory_max: None,
|
||||
memory_min: None,
|
||||
jvm_args: vec![],
|
||||
resolution: None,
|
||||
};
|
||||
|
||||
manager.save(&config).expect("save failed");
|
||||
|
||||
let all = manager.load_all();
|
||||
assert_eq!(all.len(), 1);
|
||||
assert_eq!(all[0].name, "test-save");
|
||||
assert_eq!(all[0].game_version, "1.20.1");
|
||||
|
||||
std::fs::remove_dir_all(&tmp).ok();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_load_all_accepts_numeric_memory() {
|
||||
let (manager, tmp) = test_manager();
|
||||
let instance_dir = tmp.join("test-memory");
|
||||
std::fs::create_dir_all(&instance_dir).ok();
|
||||
std::fs::write(
|
||||
instance_dir.join("instance.json"),
|
||||
r#"{
|
||||
"name": "test-memory",
|
||||
"game_version": "1.7.10",
|
||||
"loader": "forge",
|
||||
"loader_version": "10.13.4.1614",
|
||||
"created": "2026-04-20T18:04:25.567993893Z",
|
||||
"memory_max": 8,
|
||||
"memory_min": 512
|
||||
}"#,
|
||||
)
|
||||
.expect("write config");
|
||||
|
||||
let all = manager.load_all();
|
||||
assert_eq!(all.len(), 1);
|
||||
assert_eq!(all[0].memory_max.as_deref(), Some("8G"));
|
||||
assert_eq!(all[0].memory_min.as_deref(), Some("512M"));
|
||||
|
||||
std::fs::remove_dir_all(&tmp).ok();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_load_one_not_found() {
|
||||
let (manager, tmp) = test_manager();
|
||||
let result = manager.load_one("ghost-instance");
|
||||
assert!(matches!(result, Err(InstanceError::NotFound(_))));
|
||||
std::fs::remove_dir_all(&tmp).ok();
|
||||
// tmp owns the temp directory; its Drop impl cleans up everything when
|
||||
// the test ends. the returned InstanceManager points at tmp.path() so
|
||||
// tests can join("name") off of either to refer to the same locations.
|
||||
fn test_manager() -> (InstanceManager, TempDir) {
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
let meta = tmp.path().join("meta");
|
||||
std::fs::create_dir_all(&meta).unwrap();
|
||||
(InstanceManager::new(tmp.path().to_path_buf(), meta), tmp)
|
||||
}
|
||||
|
||||
fn dummy_config(name: &str) -> InstanceConfig {
|
||||
@@ -436,81 +342,137 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn validate_name_accepts_safe_names() {
|
||||
assert!(validate_name("my-instance").is_ok());
|
||||
assert!(validate_name("test_world").is_ok());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn validate_name_rejects_empty_traversal_and_hidden() {
|
||||
assert!(validate_name("").is_err());
|
||||
assert!(validate_name("path/traversal").is_err());
|
||||
assert!(validate_name(".hidden").is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn delete_missing_instance_returns_not_found() {
|
||||
let (manager, _tmp) = test_manager();
|
||||
let result = manager.delete("ghost-instance");
|
||||
assert!(matches!(result, Err(InstanceError::NotFound(_))));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn save_then_load_all_round_trips_config() {
|
||||
let (manager, tmp) = test_manager();
|
||||
std::fs::create_dir_all(tmp.path().join("test-save")).unwrap();
|
||||
manager.save(&dummy_config("test-save")).expect("save");
|
||||
|
||||
let all = manager.load_all();
|
||||
assert_eq!(all.len(), 1);
|
||||
assert_eq!(all[0].name, "test-save");
|
||||
assert_eq!(all[0].game_version, "1.20.1");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn load_all_accepts_numeric_memory() {
|
||||
let (manager, tmp) = test_manager();
|
||||
let instance_dir = tmp.path().join("test-memory");
|
||||
std::fs::create_dir_all(&instance_dir).unwrap();
|
||||
std::fs::write(
|
||||
instance_dir.join("instance.json"),
|
||||
r#"{
|
||||
"name": "test-memory",
|
||||
"game_version": "1.7.10",
|
||||
"loader": "forge",
|
||||
"loader_version": "10.13.4.1614",
|
||||
"created": "2026-04-20T18:04:25.567993893Z",
|
||||
"memory_max": 8,
|
||||
"memory_min": 512
|
||||
}"#,
|
||||
)
|
||||
.expect("write config");
|
||||
|
||||
let all = manager.load_all();
|
||||
assert_eq!(all.len(), 1);
|
||||
assert_eq!(all[0].memory_max.as_deref(), Some("8G"));
|
||||
assert_eq!(all[0].memory_min.as_deref(), Some("512M"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn load_one_missing_returns_not_found() {
|
||||
let (manager, _tmp) = test_manager();
|
||||
let result = manager.load_one("ghost-instance");
|
||||
assert!(matches!(result, Err(InstanceError::NotFound(_))));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rename_moves_dir_and_updates_config_name() {
|
||||
let (manager, tmp) = test_manager();
|
||||
let old_dir = tmp.join("old-name");
|
||||
std::fs::create_dir_all(&old_dir).ok();
|
||||
let old_dir = tmp.path().join("old-name");
|
||||
std::fs::create_dir_all(&old_dir).unwrap();
|
||||
manager.save(&dummy_config("old-name")).expect("save");
|
||||
|
||||
manager.rename("old-name", "new-name").expect("rename");
|
||||
|
||||
assert!(!old_dir.exists(), "old dir should be gone");
|
||||
let new_dir = tmp.join("new-name");
|
||||
let new_dir = tmp.path().join("new-name");
|
||||
assert!(new_dir.exists(), "new dir should exist");
|
||||
let reloaded = manager.load_one("new-name").expect("load_one new-name");
|
||||
assert_eq!(reloaded.name, "new-name");
|
||||
std::fs::remove_dir_all(&tmp).ok();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rename_to_same_name_is_noop() {
|
||||
let (manager, tmp) = test_manager();
|
||||
let dir = tmp.join("same");
|
||||
std::fs::create_dir_all(&dir).ok();
|
||||
let dir = tmp.path().join("same");
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
manager.save(&dummy_config("same")).expect("save");
|
||||
manager.rename("same", "same").expect("noop rename");
|
||||
assert!(dir.exists());
|
||||
std::fs::remove_dir_all(&tmp).ok();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rename_empty_target_rejects() {
|
||||
let (manager, tmp) = test_manager();
|
||||
std::fs::create_dir_all(tmp.join("orig")).ok();
|
||||
std::fs::create_dir_all(tmp.path().join("orig")).unwrap();
|
||||
manager.save(&dummy_config("orig")).expect("save");
|
||||
let err = manager.rename("orig", " ").unwrap_err();
|
||||
assert!(matches!(err, InstanceError::InvalidName(_)));
|
||||
std::fs::remove_dir_all(&tmp).ok();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rename_missing_source_errors() {
|
||||
let (manager, tmp) = test_manager();
|
||||
let (manager, _tmp) = test_manager();
|
||||
let err = manager.rename("ghost", "anything").unwrap_err();
|
||||
assert!(matches!(err, InstanceError::NotFound(_)));
|
||||
std::fs::remove_dir_all(&tmp).ok();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rename_target_exists_errors() {
|
||||
let (manager, tmp) = test_manager();
|
||||
std::fs::create_dir_all(tmp.join("source")).ok();
|
||||
std::fs::create_dir_all(tmp.join("collision")).ok();
|
||||
std::fs::create_dir_all(tmp.path().join("source")).unwrap();
|
||||
std::fs::create_dir_all(tmp.path().join("collision")).unwrap();
|
||||
manager.save(&dummy_config("source")).expect("save src");
|
||||
manager.save(&dummy_config("collision")).expect("save dst");
|
||||
let err = manager.rename("source", "collision").unwrap_err();
|
||||
assert!(matches!(err, InstanceError::AlreadyExists(_)));
|
||||
std::fs::remove_dir_all(&tmp).ok();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn touch_last_played_updates_field() {
|
||||
let (manager, tmp) = test_manager();
|
||||
std::fs::create_dir_all(tmp.join("ticker")).ok();
|
||||
std::fs::create_dir_all(tmp.path().join("ticker")).unwrap();
|
||||
manager.save(&dummy_config("ticker")).expect("save");
|
||||
// sanity: last_played starts as None
|
||||
assert!(manager.load_one("ticker").unwrap().last_played.is_none());
|
||||
|
||||
manager.touch_last_played("ticker").expect("touch");
|
||||
let reloaded = manager.load_one("ticker").unwrap();
|
||||
let stamp = reloaded.last_played.expect("last_played should be Some now");
|
||||
// and it must be recent (within 5 seconds of now)
|
||||
let age = chrono::Utc::now() - stamp;
|
||||
assert!(
|
||||
age.num_seconds().abs() < 5,
|
||||
"last_played should be roughly now, got age {age:?}"
|
||||
);
|
||||
std::fs::remove_dir_all(&tmp).ok();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user