Skip to content

Commit

Permalink
Add tests for Config module
Browse files Browse the repository at this point in the history
  • Loading branch information
WombatFromHell committed Feb 16, 2025
1 parent 024112d commit d55e9e8
Show file tree
Hide file tree
Showing 6 changed files with 250 additions and 9 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
target
result
.direnv
test_config.toml
veridian-controller.toml
84 changes: 82 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@ signal-hook = { version = "0.3.17", features = ["extended-siginfo"] }
toml = "0.8.20"
chrono = "0.4.38"
nix = { version = "0.29.0", features = ["user"] }

[dev-dependencies]
tempfile = "3.16.0"
14 changes: 7 additions & 7 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,15 @@ impl fmt::Display for ConfigError {
}
impl std::error::Error for ConfigError {}

fn expand_tilde(path: &str) -> Result<PathBuf, ConfigError> {
if path.starts_with("~") {
// Try to get the home directory
pub fn expand_tilde(path: &str) -> Result<PathBuf, ConfigError> {
if path.starts_with("~/") {
let home_dir = env::var("HOME").map_err(|_| ConfigError::MissingHomeDir)?;
// Strip the `~` and join with the home directory
let stripped_path = path.trim_start_matches('~');
Ok(PathBuf::from(home_dir).join(stripped_path))
let stripped_path = &path
.strip_prefix("~/")
.ok_or("Path does not start with '~/'!");
Ok(PathBuf::from(home_dir).join(stripped_path.unwrap()))
} else {
// If the path doesn't start with `~`, return it as-is
// If the path doesn't start with `~/`, return it as-is
Ok(PathBuf::from(path))
}
}
Expand Down
155 changes: 155 additions & 0 deletions src/config_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
use std::env;
use std::fs;
use std::path::PathBuf;
use tempfile::TempDir;

use crate::config;

#[test]
fn test_default_config() {
let config = config::Config::default();
assert_eq!(config.gpu_id, 0);
assert_eq!(config.temp_thresholds, vec![40, 50, 60, 78, 84]);
assert_eq!(config.fan_speeds, vec![46, 55, 62, 80, 100]);
assert_eq!(config.fan_speed_floor, 46);
assert_eq!(config.fan_speed_ceiling, 100);
assert_eq!(config.sampling_window_size, 10);
assert_eq!(config.hysteresis, 3);
assert_eq!(config.global_delay, 2);
assert_eq!(config.fan_dwell_time, 10);
assert!(config.smooth_mode);
assert_eq!(config.smooth_mode_incr_weight, 1.0);
assert_eq!(config.smooth_mode_decr_weight, 4.0);
assert_eq!(config.smooth_mode_max_fan_step, 5);
}

#[test]
fn test_expand_tilde() {
// Temporarily store the original HOME value
let original_home = env::var("HOME").ok();

// Set a controlled home directory for testing
env::set_var("HOME", "/home/test");

let cases = vec![
("~/config.toml", "/home/test/config.toml"),
("~/dir/config.toml", "/home/test/dir/config.toml"),
("/absolute/path/config.toml", "/absolute/path/config.toml"),
("relative/path/config.toml", "relative/path/config.toml"),
];

for (input, expected) in cases {
let result = config::expand_tilde(input).unwrap();
assert_eq!(result, PathBuf::from(expected));
}

// Restore the original HOME value
if let Some(home) = original_home {
env::set_var("HOME", home);
} else {
env::remove_var("HOME");
}
}

#[test]
fn test_resolve_path() {
let temp_dir = TempDir::new().unwrap();
let base_path = temp_dir.path();

// Test absolute path
let abs_path = base_path.join("config.toml");
let resolved = config::resolve_path(abs_path.to_str().unwrap()).unwrap();
assert!(resolved.is_absolute());
assert!(resolved.exists());

// Test relative path
let relative_path = "test_config.toml";
let resolved = config::resolve_path(relative_path).unwrap();
assert!(resolved.is_absolute());
assert!(resolved.exists());

// Test nested path creation
let nested_path = base_path.join("nested/config/test.toml");
let resolved = config::resolve_path(nested_path.to_str().unwrap()).unwrap();
assert!(resolved.is_absolute());
assert!(resolved.exists());
assert!(resolved.parent().unwrap().exists());
}

#[test]
fn test_config_serialization() {
let temp_dir = TempDir::new().unwrap();
let config_path = temp_dir.path().join("test_config.toml");

let config = config::Config::default();
config
.write_to_file(Some(config_path.to_str().unwrap().to_string()))
.unwrap();

// Read back the config and verify it matches
let read_config = config::Config::new(Some(config_path.to_str().unwrap().to_string())).unwrap();
assert_eq!(read_config.gpu_id, config.gpu_id);
assert_eq!(read_config.temp_thresholds, config.temp_thresholds);
assert_eq!(read_config.fan_speeds, config.fan_speeds);
assert_eq!(read_config.fan_speed_floor, config.fan_speed_floor);
assert_eq!(read_config.fan_speed_ceiling, config.fan_speed_ceiling);
}

#[test]
fn test_invalid_config() {
let temp_dir = TempDir::new().unwrap();
let config_path = temp_dir.path().join("invalid_config.toml");

// Write invalid TOML
fs::write(&config_path, "invalid = toml [ content").unwrap();

let result = config::Config::new(Some(config_path.to_str().unwrap().to_string()));
assert!(matches!(result, Err(config::ConfigError::Toml(_))));
}

#[test]
fn test_mismatched_arrays() {
let temp_dir = TempDir::new().unwrap();
let config_path = temp_dir.path().join("mismatched_config.toml");

// Create config with mismatched arrays
let config_content = r#"
gpu_id = 0
temp_thresholds = [40, 50, 60]
fan_speeds = [46, 55]
fan_speed_floor = 46
fan_speed_ceiling = 100
sampling_window_size = 10
hysteresis = 3
global_delay = 2
fan_dwell_time = 10
smooth_mode = true
smooth_mode_incr_weight = 1.0
smooth_mode_decr_weight = 4.0
smooth_mode_max_fan_step = 5
"#;

fs::write(&config_path, config_content).unwrap();

let result = config::Config::new(Some(config_path.to_str().unwrap().to_string()));
assert!(matches!(
result,
Err(config::ConfigError::InvalidArrayFormat)
));
}

#[test]
fn test_load_config_from_env() {
let temp_dir = TempDir::new().unwrap();
let config_path = temp_dir.path().join("env_config.toml");

// Test with non-existent file (should create default)
let config =
config::load_config_from_env(Some(config_path.to_str().unwrap().to_string())).unwrap();
assert_eq!(config.gpu_id, config::Config::default().gpu_id);

// Test with existing valid file
let config =
config::load_config_from_env(Some(config_path.to_str().unwrap().to_string())).unwrap();
assert_eq!(config.gpu_id, config::Config::default().gpu_id);
}
2 changes: 2 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ mod config;
mod filelock;
mod thermalmanager;

#[cfg(test)]
mod config_test;
#[cfg(test)]
mod thermalmanager_test;

Expand Down

0 comments on commit d55e9e8

Please sign in to comment.