diff --git a/src/auth/accounts.rs b/src/auth/accounts.rs index 0d6123f..0c5addf 100644 --- a/src/auth/accounts.rs +++ b/src/auth/accounts.rs @@ -199,10 +199,6 @@ mod tests { } } - fn dummy_account(name: &str) -> Account { - create_offline_account(name) - } - fn microsoft_account(name: &str) -> Account { Account { uuid: format!("00000000-0000-0000-0000-{:012}", name.len()), @@ -219,7 +215,7 @@ mod tests { fn store_add_first_becomes_active() { let tmp = tempfile::tempdir().unwrap(); let mut store = make_store(tmp.path()); - store.add(dummy_account("Alice")); + store.add(create_offline_account("Alice")); assert_eq!(store.accounts.len(), 1); assert!(store.accounts[0].active); } @@ -228,8 +224,8 @@ mod tests { fn store_add_second_stays_inactive() { let tmp = tempfile::tempdir().unwrap(); let mut store = make_store(tmp.path()); - store.add(dummy_account("Alice")); - store.add(dummy_account("Bob")); + store.add(create_offline_account("Alice")); + store.add(create_offline_account("Bob")); assert_eq!(store.accounts.len(), 2); assert!(store.accounts[0].active); assert!(!store.accounts[1].active); @@ -239,8 +235,8 @@ mod tests { fn store_add_duplicate_uuid_replaces() { let tmp = tempfile::tempdir().unwrap(); let mut store = make_store(tmp.path()); - store.add(dummy_account("Alice")); - let mut dup = dummy_account("Alice"); + store.add(create_offline_account("Alice")); + let mut dup = create_offline_account("Alice"); dup.username = "AliceRenamed".to_owned(); dup.uuid = store.accounts[0].uuid.clone(); store.add(dup); @@ -259,7 +255,7 @@ mod tests { fn store_has_microsoft_account_when_one_exists() { let tmp = tempfile::tempdir().unwrap(); let mut store = make_store(tmp.path()); - store.add(dummy_account("Offline")); + store.add(create_offline_account("Offline")); assert!(!store.has_microsoft_account()); store.add(microsoft_account("Owner")); @@ -270,8 +266,8 @@ mod tests { fn store_active_account_returns_active() { let tmp = tempfile::tempdir().unwrap(); let mut store = make_store(tmp.path()); - store.add(dummy_account("Alice")); - store.add(dummy_account("Bob")); + store.add(create_offline_account("Alice")); + store.add(create_offline_account("Bob")); let active = store.active_account().unwrap(); assert_eq!(active.username, "Alice"); } @@ -280,8 +276,8 @@ mod tests { fn store_set_active_changes_active() { let tmp = tempfile::tempdir().unwrap(); let mut store = make_store(tmp.path()); - store.add(dummy_account("Alice")); - store.add(dummy_account("Bob")); + store.add(create_offline_account("Alice")); + store.add(create_offline_account("Bob")); store.set_active(1); assert!(!store.accounts[0].active); assert!(store.accounts[1].active); @@ -291,8 +287,8 @@ mod tests { fn store_remove_activates_first_remaining() { let tmp = tempfile::tempdir().unwrap(); let mut store = make_store(tmp.path()); - store.add(dummy_account("Alice")); - store.add(dummy_account("Bob")); + store.add(create_offline_account("Alice")); + store.add(create_offline_account("Bob")); store.remove(0); assert_eq!(store.accounts.len(), 1); assert_eq!(store.accounts[0].username, "Bob"); @@ -303,7 +299,7 @@ mod tests { fn store_remove_out_of_bounds_noop() { let tmp = tempfile::tempdir().unwrap(); let mut store = make_store(tmp.path()); - store.add(dummy_account("Alice")); + store.add(create_offline_account("Alice")); store.remove(5); assert_eq!(store.accounts.len(), 1); } @@ -312,8 +308,8 @@ mod tests { fn store_save_and_reload() { let tmp = tempfile::tempdir().unwrap(); let mut store = make_store(tmp.path()); - store.add(dummy_account("Alice")); - store.add(dummy_account("Bob")); + store.add(create_offline_account("Alice")); + store.add(create_offline_account("Bob")); store.save(); let reloaded = AccountStore { diff --git a/src/cli/import.rs b/src/cli/import.rs index 3a62625..b08a1df 100644 --- a/src/cli/import.rs +++ b/src/cli/import.rs @@ -95,7 +95,7 @@ pub async fn handle_import(matches: &ArgMatches) -> CliResult { } println!( - "Importing '{}' — {} {} ({} mods, {} overrides)", + "Importing '{}' - {} {} ({} mods, {} overrides)", summary.name, summary.game_version, summary.loader, diff --git a/src/instance/content/mods.rs b/src/instance/content/mods.rs index 1b03cdd..5e98909 100644 --- a/src/instance/content/mods.rs +++ b/src/instance/content/mods.rs @@ -567,7 +567,6 @@ mod tests { use std::io::Write as _; - // helper to create a jar (zip) file with the given entries fn make_jar(dir: &Path, name: &str, entries: &[(&str, &[u8])]) { let path = dir.join(name); let file = std::fs::File::create(&path).unwrap(); @@ -776,9 +775,9 @@ description = "A neoforge mod" ); let mods = scan_mods(tmp.path(), "inst"); assert_eq!(mods.len(), 1); - // metadata name is empty, so scan_one_mod falls back to the file stem + // file-stem fallback when metadata name is empty - covered here + // because no other test exercises an empty-name + present-logo combo. assert_eq!(mods[0].name, "lib-only"); - // the icon path from logoFile must resolve to the in-jar entry assert_eq!(mods[0].icon_bytes.as_deref(), Some(png_bytes.as_slice())); } @@ -786,14 +785,11 @@ description = "A neoforge mod" fn scan_mods_fallback_icon_paths() { let tmp = tempfile::tempdir().unwrap(); let dir = setup_mods_dir(tmp.path(), "inst"); - // jar with no metadata but has logo.png let png_bytes = b"\x89PNG fake"; make_jar(&dir, "no-meta.jar", &[("logo.png", png_bytes)]); let mods = scan_mods(tmp.path(), "inst"); assert_eq!(mods.len(), 1); - // should use filename as name since no metadata assert_eq!(mods[0].name, "no-meta"); - // icon_bytes should contain the logo.png content assert_eq!(mods[0].icon_bytes.as_deref(), Some(png_bytes.as_slice())); } diff --git a/src/instance/import/mmc.rs b/src/instance/import/mmc.rs index 9967e3b..d08557b 100644 --- a/src/instance/import/mmc.rs +++ b/src/instance/import/mmc.rs @@ -181,7 +181,7 @@ fn instance_name_from_cfg(path: &Path) -> Option { None } -// finds the prefix for the archive — empty for flat zips, "DirName/" for nested +// finds the prefix for the archive: empty for flat zips, "DirName/" for nested fn find_archive_prefix(archive: &zip::ZipArchive) -> String { for name in archive.file_names() { if name.ends_with("mmc-pack.json") { @@ -269,4 +269,52 @@ mod tests { let pack: MmcPack = serde_json::from_str(json).unwrap(); assert!(pack.loader().0.is_none()); } + + // builds an in-memory mmc-style pack zip and verifies that + // extract_mmc_archive copies only the .minecraft/ subtree into the + // destination, preserving relative paths and skipping siblings. + #[test] + fn extract_mmc_archive_copies_minecraft_subtree() { + use std::io::Write; + + let tmp = tempfile::tempdir().unwrap(); + let archive_path = tmp.path().join("pack.zip"); + let dest = tmp.path().join("instance/.minecraft"); + std::fs::create_dir_all(&dest).unwrap(); + + // Pack/ is the prefix; only .minecraft/ entries should land in dest. + // mmc-style pack: a root dir "Pack/" wrapping the .minecraft tree + // plus a sibling mmc-pack.json that should NOT be extracted. + { + let file = std::fs::File::create(&archive_path).unwrap(); + let mut zip = zip::ZipWriter::new(file); + let opts: zip::write::SimpleFileOptions = Default::default(); + + zip.start_file("Pack/mmc-pack.json", opts).unwrap(); + zip.write_all(b"{}").unwrap(); + + zip.start_file("Pack/.minecraft/options.txt", opts).unwrap(); + zip.write_all(b"lang:en_us").unwrap(); + + zip.start_file("Pack/.minecraft/mods/test-mod.jar", opts) + .unwrap(); + zip.write_all(b"jar-bytes").unwrap(); + + zip.finish().unwrap(); + } + + extract_mmc_archive(&archive_path, &dest).expect("extract"); + + // .minecraft/ entries must have been copied with their relative paths + let options = std::fs::read(dest.join("options.txt")).expect("options.txt"); + assert_eq!(options, b"lang:en_us"); + let modjar = std::fs::read(dest.join("mods/test-mod.jar")).expect("mods/test-mod.jar"); + assert_eq!(modjar, b"jar-bytes"); + + // and the sibling outside .minecraft/ must not have been copied + assert!( + !dest.join("mmc-pack.json").exists(), + "mmc-pack.json should not land in the instance dir" + ); + } } diff --git a/src/instance/manager.rs b/src/instance/manager.rs index 0433c48..845d193 100644 --- a/src/instance/manager.rs +++ b/src/instance/manager.rs @@ -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(); } } diff --git a/src/instance/models.rs b/src/instance/models.rs index 9d82fa3..7f59a75 100644 --- a/src/instance/models.rs +++ b/src/instance/models.rs @@ -124,7 +124,7 @@ mod tests { use super::*; #[test] - fn test_instance_config_roundtrip() { + fn instance_config_roundtrips_through_json() { let config = InstanceConfig { name: "test".to_string(), game_version: "1.20.1".to_string(), diff --git a/src/migrate.rs b/src/migrate.rs index af4e297..8b23810 100644 --- a/src/migrate.rs +++ b/src/migrate.rs @@ -171,27 +171,12 @@ fn sanitize(name: &str) -> String { #[cfg(test)] mod tests { use super::*; - use std::path::PathBuf; - use std::sync::atomic::{AtomicU64, Ordering}; - - static COUNTER: AtomicU64 = AtomicU64::new(0); - - fn unique_tmp(label: &str) -> PathBuf { - let n = COUNTER.fetch_add(1, Ordering::SeqCst); - let nanos = std::time::SystemTime::now() - .duration_since(std::time::UNIX_EPOCH) - .map(|d| d.as_nanos()) - .unwrap_or(0); - let p = std::env::temp_dir().join(format!("rmcl_migrate_test_{label}_{nanos}_{n}")); - std::fs::create_dir_all(&p).unwrap(); - p - } #[test] fn rename_top_level_moves_when_only_old_exists() { - let root = unique_tmp("only_old"); - let old = root.join("mcl"); - let new = root.join("rmcl"); + let tmp = tempfile::tempdir().unwrap(); + let old = tmp.path().join("mcl"); + let new = tmp.path().join("rmcl"); fs::create_dir_all(old.join("sub")).unwrap(); fs::write(old.join("sub").join("f.txt"), b"hi").unwrap(); @@ -204,9 +189,9 @@ mod tests { #[test] fn rename_top_level_skips_when_only_new_exists() { - let root = unique_tmp("only_new"); - let old = root.join("mcl"); - let new = root.join("rmcl"); + let tmp = tempfile::tempdir().unwrap(); + let old = tmp.path().join("mcl"); + let new = tmp.path().join("rmcl"); fs::create_dir_all(&new).unwrap(); fs::write(new.join("marker.txt"), b"keep").unwrap(); @@ -218,9 +203,9 @@ mod tests { #[test] fn rename_top_level_skips_when_both_exist() { - let root = unique_tmp("both"); - let old = root.join("mcl"); - let new = root.join("rmcl"); + let tmp = tempfile::tempdir().unwrap(); + let old = tmp.path().join("mcl"); + let new = tmp.path().join("rmcl"); fs::create_dir_all(&old).unwrap(); fs::create_dir_all(&new).unwrap(); fs::write(old.join("a"), b"old").unwrap(); @@ -236,9 +221,9 @@ mod tests { #[test] fn rename_top_level_noop_when_neither_exists() { - let root = unique_tmp("neither"); - let old = root.join("mcl"); - let new = root.join("rmcl"); + let tmp = tempfile::tempdir().unwrap(); + let old = tmp.path().join("mcl"); + let new = tmp.path().join("rmcl"); rename_top_level(&old, &new); @@ -248,7 +233,8 @@ mod tests { #[test] fn cleanup_instance_leftovers_removes_shim_and_log4j() { - let instances = unique_tmp("inst").join("instances"); + let tmp = tempfile::tempdir().unwrap(); + let instances = tmp.path().join("instances"); let mc = instances.join("Test").join(".minecraft"); fs::create_dir_all(&mc).unwrap(); fs::write(mc.join(".mcl-shim.jar"), b"jar").unwrap(); @@ -265,8 +251,8 @@ mod tests { #[test] #[cfg(target_os = "linux")] fn rewrite_linux_desktop_entries_renames_and_rewrites_exec() { - let root = unique_tmp("linux_desk"); - let data = root.clone(); + let tmp = tempfile::tempdir().unwrap(); + let data = tmp.path(); let instances = data.join("rmcl").join("instances"); let apps = data.join("applications"); fs::create_dir_all(instances.join("MyPack")).unwrap(); @@ -278,7 +264,7 @@ mod tests { ) .unwrap(); - rewrite_linux_desktop_entries(&data, &instances); + rewrite_linux_desktop_entries(data, &instances); let new_entry = apps.join("rmcl-MyPack.desktop"); assert!(!old_entry.exists(), "old .desktop should be removed"); @@ -289,9 +275,9 @@ mod tests { #[test] fn copy_dir_recursive_copies_nested_tree() { - let root = unique_tmp("copy"); - let src = root.join("a"); - let dst = root.join("b"); + let tmp = tempfile::tempdir().unwrap(); + let src = tmp.path().join("a"); + let dst = tmp.path().join("b"); fs::create_dir_all(src.join("nested")).unwrap(); fs::write(src.join("top.txt"), b"top").unwrap(); fs::write(src.join("nested").join("inner.txt"), b"inner").unwrap(); diff --git a/tests/smoke.rs b/tests/smoke.rs index 3e81265..97384f2 100644 --- a/tests/smoke.rs +++ b/tests/smoke.rs @@ -1,5 +1,5 @@ -//! verifies the lib/main split worked: integration tests can see crate items -//! through the public API. +// verifies the lib/main split worked: integration tests can see crate items +// through the public API. #[test] fn lib_target_is_importable() {