refactor: reorganize source and tests

This commit is contained in:
objz committed 2026-07-27 11:28:57 +02:00
1 parent 15d76c18a0
commit c6205a337c
139 files changed
+5532 -5548

No files matched your search

+209
View File
@@ -0,0 +1,209 @@
use super::*;
#[test]
fn migration_backs_up_and_renames_instance_directories() {
let temp = tempfile::tempdir().unwrap();
let instances = temp.path().join("instances");
let meta = temp.path().join("meta");
let config = temp.path().join("config.toml");
let instance = instances.join("Example");
fs::create_dir_all(instance.join(".minecraft/saves/world")).unwrap();
fs::create_dir_all(instance.join(".rmcl/config-sync/local-config/config")).unwrap();
fs::write(instance.join(".minecraft/saves/world/level.dat"), b"world").unwrap();
fs::write(
instance.join(".rmcl/config-sync/local-config/options.txt"),
b"options",
)
.unwrap();
fs::write(&config, b"[paths]").unwrap();
fs::create_dir_all(meta.join("config-sync/profiles/main")).unwrap();
fs::write(
meta.join("config-sync/profiles/main/options.txt"),
b"profile",
)
.unwrap();
let mut progress = Vec::new();
let backup = run(&instances, &meta, &config, |update| progress.push(update)).unwrap();
assert_eq!(
fs::read(instance.join("minecraft/saves/world/level.dat")).unwrap(),
b"world"
);
assert_eq!(
fs::read(instance.join("rmcl/content/config/options.txt")).unwrap(),
b"options"
);
assert!(backup.join("instances/Example/.minecraft").exists());
assert!(backup.join("profiles/legacy/main/options.txt").exists());
assert!(
backup
.file_name()
.unwrap()
.to_string_lossy()
.starts_with("backup-")
);
assert!(!meta.join("config-sync").exists());
assert!(progress.iter().any(|update| {
update.item_total.is_some_and(|total| total > 0)
&& update.item_current.is_some_and(|current| current > 0)
}));
assert_eq!(
marker_version(&MetadataPaths::new(&meta).layout_marker()),
Some(LAYOUT_VERSION)
);
assert!(cache_rebuild_pending(&meta));
assert_eq!(run(&instances, &meta, &config, |_| {}).unwrap(), backup);
finish_cache_rebuild(&meta).unwrap();
assert!(!cache_rebuild_pending(&meta));
}
#[test]
fn migration_resumes_from_the_recorded_journal() {
let temp = tempfile::tempdir().unwrap();
let instances = temp.path().join("instances");
let meta = temp.path().join("meta");
let config = temp.path().join("config.toml");
let done = instances.join("Done");
let pending = instances.join("Pending");
fs::create_dir_all(done.join("minecraft")).unwrap();
fs::create_dir_all(pending.join(".minecraft/saves")).unwrap();
let metadata = MetadataPaths::new(&meta);
fs::create_dir_all(metadata.state()).unwrap();
fs::create_dir_all(metadata.backups()).unwrap();
let backup = metadata.backups().join("backup-resume");
fs::create_dir_all(&backup).unwrap();
write_json_atomic(
&metadata.migration_journal(),
&MigrationJournal {
version: LAYOUT_VERSION,
backup_dir: backup.clone(),
completed: vec!["backup".to_owned(), "instance:Done".to_owned()],
},
)
.unwrap();
assert_eq!(run(&instances, &meta, &config, |_| {}).unwrap(), backup);
assert!(done.join("minecraft").exists());
assert!(pending.join("minecraft/saves").exists());
assert!(!metadata.migration_journal().exists());
}
#[test]
fn stale_partial_backup_is_replaced() {
let temp = tempfile::tempdir().unwrap();
let instances = temp.path().join("instances");
let backup = temp.path().join("backups/backup-test");
let partial = backup.with_extension("partial");
fs::create_dir_all(instances.join("Example")).unwrap();
fs::write(instances.join("Example/data.txt"), b"current").unwrap();
fs::create_dir_all(&partial).unwrap();
fs::write(partial.join("stale.txt"), b"stale").unwrap();
backup_user_data(
&instances,
&temp.path().join("missing-config.toml"),
&temp.path().join("meta"),
&backup,
|_, _, _| {},
)
.unwrap();
assert_eq!(
fs::read(backup.join("instances/Example/data.txt")).unwrap(),
b"current"
);
assert!(!backup.join("stale.txt").exists());
assert!(!partial.exists());
}
#[test]
fn backup_rejects_overlap_and_insufficient_space() {
let temp = tempfile::tempdir().unwrap();
let instances = temp.path().join("instances");
fs::create_dir_all(&instances).unwrap();
let overlap =
validate_backup_destination(&instances, &instances.join("backup"), 0).unwrap_err();
assert!(matches!(overlap, MigrationError::BackupOverlap(_)));
let outside = temp.path().join("backups/backup");
let insufficient = validate_backup_destination(&instances, &outside, u64::MAX).unwrap_err();
assert!(matches!(
insufficient,
MigrationError::InsufficientSpace { .. }
));
}
#[test]
fn migration_need_follows_legacy_data_marker_and_pending_rebuild() {
let temp = tempfile::tempdir().unwrap();
let instances = temp.path().join("instances");
let meta = temp.path().join("meta");
fs::create_dir_all(instances.join("Example/.minecraft")).unwrap();
assert!(is_needed(&instances, &meta));
initialize_new_layout(&meta).unwrap();
assert!(!is_needed(&instances, &meta));
fs::write(MetadataPaths::new(&meta).layout_marker(), b"invalid marker").unwrap();
assert!(is_needed(&instances, &meta));
initialize_new_layout(&meta).unwrap();
fs::write(
MetadataPaths::new(&meta).cache_rebuild_pending(),
LAYOUT_VERSION.to_string(),
)
.unwrap();
assert!(is_needed(&instances, &meta));
}
#[cfg(unix)]
#[test]
fn backup_preserves_symlinks() {
let temp = tempfile::tempdir().unwrap();
let source = temp.path().join("source");
let destination = temp.path().join("destination");
fs::create_dir_all(&source).unwrap();
fs::write(source.join("target.txt"), b"target").unwrap();
std::os::unix::fs::symlink("target.txt", source.join("link.txt")).unwrap();
copy_dir_recursive_with_progress(&source, &destination, &mut |_, _| {}).unwrap();
assert_eq!(
fs::read_link(destination.join("link.txt")).unwrap(),
PathBuf::from("target.txt")
);
}
#[test]
fn conflicting_visible_and_hidden_directories_are_not_merged() {
let temp = tempfile::tempdir().unwrap();
let instance = temp.path().join("Example");
fs::create_dir_all(instance.join(".minecraft")).unwrap();
fs::create_dir_all(instance.join("minecraft")).unwrap();
let error = migrate_instance(&instance, "Example").unwrap_err();
assert!(matches!(error, MigrationError::PathConflict { .. }));
}
#[test]
fn merge_conflicts_do_not_overwrite_existing_profiles() {
let temp = tempfile::tempdir().unwrap();
let source = temp.path().join("legacy");
let destination = temp.path().join("current");
fs::create_dir_all(source.join("nested")).unwrap();
fs::create_dir_all(destination.join("nested")).unwrap();
fs::write(source.join("nested/options.txt"), b"legacy").unwrap();
fs::write(destination.join("nested/options.txt"), b"current").unwrap();
let error = move_or_merge(&source, &destination).unwrap_err();
assert!(matches!(error, MigrationError::MergeConflict { .. }));
assert_eq!(
fs::read(destination.join("nested/options.txt")).unwrap(),
b"current"
);
assert_eq!(
fs::read(source.join("nested/options.txt")).unwrap(),
b"legacy"
);
}
+122
View File
@@ -0,0 +1,122 @@
use super::*;
#[test]
fn rename_top_level_moves_when_only_old_exists() {
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();
rename_top_level(&old, &new);
assert!(!old.exists());
assert!(new.exists());
assert_eq!(fs::read(new.join("sub").join("f.txt")).unwrap(), b"hi");
}
#[test]
fn rename_top_level_skips_when_only_new_exists() {
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();
rename_top_level(&old, &new);
assert!(!old.exists());
assert_eq!(fs::read(new.join("marker.txt")).unwrap(), b"keep");
}
#[test]
fn rename_top_level_skips_when_both_exist() {
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();
fs::write(new.join("b"), b"new").unwrap();
rename_top_level(&old, &new);
assert!(old.exists(), "old should remain when both exist");
assert!(new.exists(), "new should remain when both exist");
assert_eq!(fs::read(old.join("a")).unwrap(), b"old");
assert_eq!(fs::read(new.join("b")).unwrap(), b"new");
}
#[test]
fn rename_top_level_noop_when_neither_exists() {
let tmp = tempfile::tempdir().unwrap();
let old = tmp.path().join("mcl");
let new = tmp.path().join("rmcl");
rename_top_level(&old, &new);
assert!(!old.exists());
assert!(!new.exists());
}
#[test]
fn cleanup_instance_leftovers_removes_shim_and_log4j() {
let tmp = tempfile::tempdir().unwrap();
let instances = tmp.path().join("instances");
let mc = instances
.join("Test")
.join(crate::storage::MINECRAFT_DIR_NAME);
fs::create_dir_all(&mc).unwrap();
fs::write(mc.join(".mcl-shim.jar"), b"jar").unwrap();
fs::write(mc.join(".mcl-log4j2.xml"), b"xml").unwrap();
fs::write(mc.join("keep.txt"), b"keep").unwrap();
cleanup_instance_leftovers(&instances);
assert!(!mc.join(".mcl-shim.jar").exists());
assert!(!mc.join(".mcl-log4j2.xml").exists());
assert!(mc.join("keep.txt").exists());
}
#[test]
#[cfg(target_os = "linux")]
fn rewrite_linux_desktop_entries_renames_and_rewrites_exec() {
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();
fs::create_dir_all(&apps).unwrap();
let old_entry = apps.join("mcl-MyPack.desktop");
fs::write(
&old_entry,
"[Desktop Entry]\nName=Test\nExec=mcl instance launch \"MyPack\"\n",
)
.unwrap();
rewrite_linux_desktop_entries(data, &instances);
let new_entry = apps.join("rmcl-MyPack.desktop");
assert!(!old_entry.exists(), "old .desktop should be removed");
assert!(new_entry.exists(), "new .desktop should exist");
let content = fs::read_to_string(&new_entry).unwrap();
assert!(content.contains("Exec=rmcl instance launch \"MyPack\""));
}
#[test]
fn copy_dir_recursive_copies_nested_tree() {
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();
copy_dir_recursive(&src, &dst).unwrap();
assert_eq!(fs::read(dst.join("top.txt")).unwrap(), b"top");
assert_eq!(
fs::read(dst.join("nested").join("inner.txt")).unwrap(),
b"inner"
);
}
+3
View File
@@ -0,0 +1,3 @@
use std::sync::Mutex;
pub(crate) static TEST_LOCK: Mutex<()> = Mutex::new(());
+45
View File
@@ -0,0 +1,45 @@
use super::*;
#[test]
fn instance_paths_use_visible_directories() {
let paths = InstancePaths::new("/instances/example");
assert_eq!(
paths.minecraft(),
PathBuf::from("/instances/example/minecraft")
);
assert_eq!(
paths.content_manifest(),
PathBuf::from("/instances/example/rmcl/content/manifest.json")
);
assert_eq!(
paths.local_config(),
PathBuf::from("/instances/example/rmcl/content/config")
);
}
#[test]
fn metadata_paths_separate_state_and_cache() {
let paths = MetadataPaths::new("/meta");
assert_eq!(paths.profiles(), PathBuf::from("/meta/state/profiles"));
assert_eq!(
paths.provider_icons("modrinth"),
PathBuf::from("/meta/cache/providers/modrinth/icons")
);
assert_eq!(
paths.versions(),
PathBuf::from("/meta/cache/minecraft/versions")
);
assert_eq!(
paths.migration_journal(),
PathBuf::from("/meta/state/migration.json")
);
}
#[test]
fn atomic_write_replaces_existing_file() {
let temp = tempfile::tempdir().unwrap();
let path = temp.path().join("state.json");
std::fs::write(&path, b"old").unwrap();
write_atomic(&path, b"new").unwrap();
assert_eq!(std::fs::read(path).unwrap(), b"new");
}