use super::super::import_dats;
use super::super::import_roms;
use super::super::xso::XsoType;
use super::*;
use std::path::{Path, PathBuf};
use tempfile::{NamedTempFile, TempDir};
use tokio::fs;
#[tokio::test]
async fn test() {
let _guard = MUTEX.lock().await;
let test_directory = Path::new("tests");
let progress_bar = ProgressBar::hidden();
let db_file = NamedTempFile::new().unwrap();
let pool = establish_connection(db_file.path().to_str().unwrap()).await;
let mut connection = pool.acquire().await.unwrap();
let rom_directory = TempDir::new_in(test_directory).unwrap();
set_rom_directory(&mut connection, PathBuf::from(rom_directory.path())).await;
let tmp_directory = TempDir::new_in(test_directory).unwrap();
let tmp_directory =
set_tmp_directory(&mut connection, PathBuf::from(tmp_directory.path())).await;
let matches = import_dats::subcommand()
.get_matches_from(["import-dats", "tests/Test System (20200721).dat"]);
import_dats::main(&mut connection, &matches, &progress_bar)
.await
.unwrap();
let romfile_path = tmp_directory.join("Test Game (USA, Europe).cso");
fs::copy(
test_directory.join("Test Game (USA, Europe).cso"),
&romfile_path,
)
.await
.unwrap();
let system = find_systems(&mut connection).await.remove(0);
let system_directory = get_system_directory(&mut connection, &system)
.await
.unwrap();
let matches = import_roms::subcommand()
.get_matches_from(["import-roms", romfile_path.as_os_str().to_str().unwrap()]);
import_roms::main(&mut connection, &matches, &progress_bar)
.await
.unwrap();
let roms = find_roms_with_romfile_by_system_id(&mut connection, system.id).await;
let romfile = find_romfile_by_id(&mut connection, roms[0].romfile_id.unwrap()).await;
let old_mtime = fs::metadata(&romfile.as_common(&mut connection).await.unwrap().path)
.await
.unwrap()
.modified()
.unwrap();
let mut roms_by_game_id: IndexMap<i64, Vec<Rom>> = IndexMap::new();
roms_by_game_id.insert(roms[0].game_id, roms);
let mut romfiles_by_id: HashMap<i64, Romfile> = HashMap::new();
romfiles_by_id.insert(romfile.id, romfile);
to_xso(
&mut connection,
&progress_bar,
roms_by_game_id,
romfiles_by_id,
true,
true,
true,
XsoType::Cso,
)
.await
.unwrap();
let roms = find_roms_with_romfile_by_system_id(&mut connection, system.id).await;
assert_eq!(roms.len(), 1);
let romfiles = find_romfiles(&mut connection).await;
assert_eq!(romfiles.len(), 1);
let rom = roms.first().unwrap();
assert_eq!(rom.name, "Test Game (USA, Europe) (ISO).iso");
let romfile = romfiles.first().unwrap();
let new_mtime = fs::metadata(&romfile.as_common(&mut connection).await.unwrap().path)
.await
.unwrap()
.modified()
.unwrap();
assert_eq!(
romfile.path,
system_directory
.join("Test Game (USA, Europe) (ISO).cso")
.strip_prefix(&rom_directory)
.unwrap()
.as_os_str()
.to_str()
.unwrap(),
);
assert!(rom_directory.path().join(&romfile.path).is_file());
assert_eq!(rom.romfile_id, Some(romfile.id));
assert_ne!(old_mtime, new_mtime);
}