Compare commits

..

4 Commits

Author SHA1 Message Date
eae0207b0f added add_mod tests 2026-03-16 17:09:58 +01:00
52e48be57f added test game 2026-03-16 17:09:47 +01:00
defc4a5721 change root_config test data 2026-03-16 17:08:56 +01:00
55f9e3f6d6 use HashSet instead of Vec for file links 2026-03-16 17:08:10 +01:00
10 changed files with 71 additions and 9 deletions

View File

@@ -1,4 +1,4 @@
use std::ffi::OsStr;
use std::{collections::HashSet, ffi::OsStr};
use serde::{Deserialize, Serialize};
@@ -10,7 +10,7 @@ use crate::{
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
pub struct InstalledMod {
id: String,
files: Vec<Link>,
files: HashSet<Link>,
priority: isize,
}
@@ -18,13 +18,13 @@ impl InstalledMod {
pub fn new(root_mod_id: &str, priority: isize) -> Self {
Self {
id: root_mod_id.to_owned(),
files: Vec::new(),
files: HashSet::new(),
priority,
}
}
pub fn add_file(&mut self, file: &ModFile) {
self.files.push(Link::from_mod_file(file));
self.files.insert(Link::from_mod_file(file));
}
/// Get the id of the mod
@@ -38,7 +38,7 @@ impl InstalledMod {
}
/// The selected files
pub fn files(&self) -> &[Link] {
pub fn files(&self) -> &HashSet<Link> {
&self.files
}

View File

@@ -5,7 +5,7 @@ use serde::{Deserialize, Serialize};
use crate::types::mod_file::ModFile;
/// A link between a file from a mod and a destination in a ModdedInstance
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq, Hash)]
#[serde(from = "(PathBuf, PathBuf)", into = "(PathBuf,PathBuf)")]
pub struct Link {
src: PathBuf,

45
tests/add_mod_test.rs Normal file
View File

@@ -0,0 +1,45 @@
use std::{collections::HashSet, error::Error, path::PathBuf};
use fomod_manager::{
instance::{files_to_install_mod, insert_mod_to_instance},
types::{Link, RootConfig},
};
fn get_parent() -> PathBuf {
PathBuf::from(file!()).parent().unwrap().to_owned()
}
fn load_root() -> RootConfig {
RootConfig::load_from_file(get_parent().join("data/root_config_complex.toml")).unwrap()
}
#[test]
fn add_simple_mod() -> Result<(), Box<dyn Error>> {
let root_config = load_root();
let mut instance = root_config.load_instance_by_id("instance_minimal")?;
let mod_to_install = root_config.mod_by_id("add_test_1").expect("Mod not found");
let files_to_add = files_to_install_mod(&root_config, &instance, &mod_to_install)?;
insert_mod_to_instance(&mut instance, &mod_to_install, &files_to_add, 0)?;
let installed_mods = instance.mods();
assert_eq!(installed_mods.len(), 1);
let the_mod = installed_mods.first().expect("Asserted before");
assert_eq!(the_mod.mod_id(), "add_test_1");
assert_eq!(the_mod.priority(), 0);
let expected_files: HashSet<_> = [Link::new("plugin.esp", "Data/plugin.esp")]
.into_iter()
.collect();
assert_eq!(
*the_mod.files(),
expected_files,
"Installed files missmatch"
);
Ok(())
}

View File

@@ -0,0 +1 @@
Skyrim.esm

View File

@@ -0,0 +1 @@
Update.esm

View File

@@ -0,0 +1 @@
SkyrimSE.exe

View File

@@ -0,0 +1 @@
SkyrimSELauncher.exe

View File

@@ -0,0 +1 @@
plugin.esl-add_test_1

View File

@@ -2,13 +2,15 @@ mod_location = "mods"
download_location = "downloads"
nexus_api_key = "1234"
[games.sse]
[games.example_game]
path = "/home/user/games/sse"
[games.sse]
path = "games/sse"
[instances.example1]
path = "example1.toml"
[instances.example2]
path = "/home/user/example2.toml"
@@ -26,3 +28,6 @@ path = "mod2"
[mods.mod3]
path = "mod3"
[mods.add_test_1]
path = "add_test_1"

View File

@@ -34,11 +34,18 @@ fn parse_complex() {
assert!(
config
.game_by_id("sse")
.game_by_id("example_game")
.is_some_and(|e| e.install_location() == "/home/user/games/sse"),
"Installed game wrong path"
);
assert!(
config
.game_by_id("sse")
.is_some_and(|e| e.install_location().ends_with("games/sse")),
"Installed game wrong path"
);
assert!(config.game_by_id("starfield").is_none());
assert!(config.mod_by_id("mod1").is_some());