use std::fs;
use rialoman::RialoDirs;
#[test]
fn fresh_install_creates_layout() {
let tmp = tempfile::tempdir().unwrap();
let dirs = RialoDirs::new(Some(&tmp.path().to_path_buf())).unwrap();
dirs.ensure_layout().unwrap();
assert!(dirs.releases().exists());
assert!(dirs.toolchains().exists());
assert!(dirs.bin().exists());
assert!(dirs.downloads().exists());
assert!(dirs.tmp().exists());
}
#[test]
fn migrate_stable_channel() {
let tmp = tempfile::tempdir().unwrap();
let home = tmp.path();
let old_stable = home.join("toolchains/stable/0.1.9");
fs::create_dir_all(&old_stable).unwrap();
fs::write(old_stable.join("manifest.json"), "{}").unwrap();
let dirs = RialoDirs::new(Some(&home.to_path_buf())).unwrap();
dirs.ensure_layout().unwrap();
assert!(!home.join("toolchains/stable").exists());
let new_stable = home.join("releases/stable/0.1.9");
assert!(new_stable.exists());
assert!(new_stable.join("manifest.json").exists());
}
#[test]
fn migrate_commit_channel() {
let tmp = tempfile::tempdir().unwrap();
let home = tmp.path();
let old_commit = home.join("toolchains/commit/abc123");
fs::create_dir_all(&old_commit).unwrap();
fs::write(old_commit.join("manifest.json"), "{}").unwrap();
let dirs = RialoDirs::new(Some(&home.to_path_buf())).unwrap();
dirs.ensure_layout().unwrap();
assert!(!home.join("toolchains/commit").exists());
let new_commit = home.join("releases/commit/abc123");
assert!(new_commit.exists());
assert!(new_commit.join("manifest.json").exists());
}
#[test]
fn migrate_multiple_channels() {
let tmp = tempfile::tempdir().unwrap();
let home = tmp.path();
fs::create_dir_all(home.join("toolchains/stable/0.1.9")).unwrap();
fs::create_dir_all(home.join("toolchains/commit/abc123")).unwrap();
fs::write(home.join("toolchains/stable/0.1.9/marker"), "stable").unwrap();
fs::write(home.join("toolchains/commit/abc123/marker"), "commit").unwrap();
let dirs = RialoDirs::new(Some(&home.to_path_buf())).unwrap();
dirs.ensure_layout().unwrap();
assert!(home.join("releases/stable/0.1.9/marker").exists());
assert!(home.join("releases/commit/abc123/marker").exists());
assert!(!home.join("toolchains/stable").exists());
assert!(!home.join("toolchains/commit").exists());
}
#[test]
fn migrate_preserves_actual_toolchains() {
let tmp = tempfile::tempdir().unwrap();
let home = tmp.path();
fs::create_dir_all(home.join("toolchains/stable/0.1.9")).unwrap();
fs::create_dir_all(home.join("toolchains/rialo-rust-0.3.0/bin")).unwrap();
fs::write(home.join("toolchains/rialo-rust-0.3.0/bin/rustc"), "fake").unwrap();
let dirs = RialoDirs::new(Some(&home.to_path_buf())).unwrap();
dirs.ensure_layout().unwrap();
assert!(home.join("releases/stable/0.1.9").exists());
assert!(!home.join("toolchains/stable").exists());
assert!(home.join("toolchains/rialo-rust-0.3.0/bin/rustc").exists());
}
#[test]
fn no_migration_when_already_migrated() {
let tmp = tempfile::tempdir().unwrap();
let home = tmp.path();
fs::create_dir_all(home.join("releases/stable/0.1.9")).unwrap();
fs::write(home.join("releases/stable/0.1.9/marker"), "new").unwrap();
fs::create_dir_all(home.join("toolchains")).unwrap();
let dirs = RialoDirs::new(Some(&home.to_path_buf())).unwrap();
dirs.ensure_layout().unwrap();
let content = fs::read_to_string(home.join("releases/stable/0.1.9/marker")).unwrap();
assert_eq!(content, "new");
}
#[test]
fn migration_is_idempotent() {
let tmp = tempfile::tempdir().unwrap();
let home = tmp.path();
fs::create_dir_all(home.join("toolchains/stable/0.1.9")).unwrap();
fs::write(home.join("toolchains/stable/0.1.9/marker"), "test").unwrap();
let dirs = RialoDirs::new(Some(&home.to_path_buf())).unwrap();
dirs.ensure_layout().unwrap();
dirs.ensure_layout().unwrap();
assert!(home.join("releases/stable/0.1.9/marker").exists());
let content = fs::read_to_string(home.join("releases/stable/0.1.9/marker")).unwrap();
assert_eq!(content, "test");
}
#[test]
fn migrate_conflict_both_exist() {
let tmp = tempfile::tempdir().unwrap();
let home = tmp.path();
fs::create_dir_all(home.join("toolchains/stable/0.1.8")).unwrap();
fs::write(home.join("toolchains/stable/0.1.8/marker"), "old").unwrap();
fs::create_dir_all(home.join("releases/stable/0.1.9")).unwrap();
fs::write(home.join("releases/stable/0.1.9/marker"), "new").unwrap();
let dirs = RialoDirs::new(Some(&home.to_path_buf())).unwrap();
let result = dirs.ensure_layout();
let err = result.expect_err("Expected error when both dirs exist");
let io_err = err
.chain()
.find_map(|e| e.downcast_ref::<std::io::Error>())
.expect("Expected io::Error in chain");
assert_eq!(io_err.kind(), std::io::ErrorKind::DirectoryNotEmpty);
assert!(home.join("toolchains/stable/0.1.8/marker").exists());
assert!(home.join("releases/stable/0.1.9/marker").exists());
}