|
1 | | -use super::{ConfigError, RbConfig}; |
2 | | -use super::locator::locate_config_file; |
3 | | -use std::fs; |
4 | | -use std::path::PathBuf; |
5 | | -use log::{debug, info}; |
6 | | - |
7 | | -/// Load configuration from file |
8 | | -/// Returns default config if no file is found |
9 | | -/// |
10 | | -/// # Arguments |
11 | | -/// * `override_path` - Optional path to explicitly load config from (for testing) |
12 | | -pub fn load_config(override_path: Option<PathBuf>) -> Result<RbConfig, ConfigError> { |
13 | | - if let Some(config_path) = locate_config_file(override_path.clone()) { |
14 | | - info!("Loading configuration from: {}", config_path.display()); |
15 | | - |
16 | | - let contents = fs::read_to_string(&config_path)?; |
17 | | - let config: RbConfig = toml::from_str(&contents)?; |
18 | | - |
19 | | - // Log what was loaded |
20 | | - debug!("Configuration file contents parsed successfully"); |
21 | | - if let Some(ref dir) = config.rubies_dir { |
22 | | - debug!(" rubies-dir: {}", dir.display()); |
23 | | - } |
24 | | - if let Some(ref version) = config.ruby_version { |
25 | | - debug!(" ruby-version: {}", version); |
26 | | - } |
27 | | - if let Some(ref home) = config.gem_home { |
28 | | - debug!(" gem-home: {}", home.display()); |
29 | | - } |
30 | | - |
31 | | - Ok(config) |
32 | | - } else { |
33 | | - if override_path.is_some() { |
34 | | - debug!("Specified configuration file not found, using defaults"); |
35 | | - } else { |
36 | | - debug!("No configuration file found in default locations, using defaults"); |
37 | | - } |
38 | | - Ok(RbConfig::default()) |
39 | | - } |
40 | | -} |
41 | | - |
42 | | -#[cfg(test)] |
43 | | -mod tests { |
44 | | - use super::*; |
45 | | - |
46 | | - #[test] |
47 | | - fn test_load_config_returns_default_when_no_file() { |
48 | | - // Should return default config when no file exists |
49 | | - let result = load_config(None); |
50 | | - assert!(result.is_ok()); |
51 | | - |
52 | | - let config = result.unwrap(); |
53 | | - assert!(config.rubies_dir.is_none()); |
54 | | - assert!(config.ruby_version.is_none()); |
55 | | - assert!(config.gem_home.is_none()); |
56 | | - } |
57 | | - |
58 | | - #[test] |
59 | | - fn test_load_config_with_custom_path() { |
60 | | - use std::fs; |
61 | | - use std::io::Write; |
62 | | - |
63 | | - let temp_dir = std::env::temp_dir(); |
64 | | - let config_path = temp_dir.join("test_rb_custom.toml"); |
65 | | - |
66 | | - // Create a test config file |
67 | | - let mut file = fs::File::create(&config_path).expect("Failed to create test config"); |
68 | | - writeln!(file, r#"ruby-version = "3.2.0""#).expect("Failed to write config"); |
69 | | - drop(file); |
70 | | - |
71 | | - // Load config from custom path |
72 | | - let result = load_config(Some(config_path.clone())); |
73 | | - assert!(result.is_ok()); |
74 | | - |
75 | | - let config = result.unwrap(); |
76 | | - assert_eq!(config.ruby_version, Some("3.2.0".to_string())); |
77 | | - |
78 | | - // Cleanup |
79 | | - let _ = fs::remove_file(&config_path); |
80 | | - } |
81 | | -} |
| 1 | +use super::locator::locate_config_file; |
| 2 | +use super::{ConfigError, RbConfig}; |
| 3 | +use log::{debug, info}; |
| 4 | +use std::fs; |
| 5 | +use std::path::PathBuf; |
| 6 | + |
| 7 | +/// Load configuration from file |
| 8 | +/// Returns default config if no file is found |
| 9 | +/// |
| 10 | +/// # Arguments |
| 11 | +/// * `override_path` - Optional path to explicitly load config from (for testing) |
| 12 | +pub fn load_config(override_path: Option<PathBuf>) -> Result<RbConfig, ConfigError> { |
| 13 | + if let Some(config_path) = locate_config_file(override_path.clone()) { |
| 14 | + info!("Loading configuration from: {}", config_path.display()); |
| 15 | + |
| 16 | + let contents = fs::read_to_string(&config_path)?; |
| 17 | + let config: RbConfig = toml::from_str(&contents)?; |
| 18 | + |
| 19 | + // Log what was loaded |
| 20 | + debug!("Configuration file contents parsed successfully"); |
| 21 | + if let Some(ref dir) = config.rubies_dir { |
| 22 | + debug!(" rubies-dir: {}", dir.display()); |
| 23 | + } |
| 24 | + if let Some(ref version) = config.ruby_version { |
| 25 | + debug!(" ruby-version: {}", version); |
| 26 | + } |
| 27 | + if let Some(ref home) = config.gem_home { |
| 28 | + debug!(" gem-home: {}", home.display()); |
| 29 | + } |
| 30 | + |
| 31 | + Ok(config) |
| 32 | + } else { |
| 33 | + if override_path.is_some() { |
| 34 | + debug!("Specified configuration file not found, using defaults"); |
| 35 | + } else { |
| 36 | + debug!("No configuration file found in default locations, using defaults"); |
| 37 | + } |
| 38 | + Ok(RbConfig::default()) |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +#[cfg(test)] |
| 43 | +mod tests { |
| 44 | + use super::*; |
| 45 | + |
| 46 | + #[test] |
| 47 | + fn test_load_config_returns_default_when_no_file() { |
| 48 | + // Should return default config when no file exists |
| 49 | + let result = load_config(None); |
| 50 | + assert!(result.is_ok()); |
| 51 | + |
| 52 | + let config = result.unwrap(); |
| 53 | + assert!(config.rubies_dir.is_none()); |
| 54 | + assert!(config.ruby_version.is_none()); |
| 55 | + assert!(config.gem_home.is_none()); |
| 56 | + } |
| 57 | + |
| 58 | + #[test] |
| 59 | + fn test_load_config_with_custom_path() { |
| 60 | + use std::fs; |
| 61 | + use std::io::Write; |
| 62 | + |
| 63 | + let temp_dir = std::env::temp_dir(); |
| 64 | + let config_path = temp_dir.join("test_rb_custom.toml"); |
| 65 | + |
| 66 | + // Create a test config file |
| 67 | + let mut file = fs::File::create(&config_path).expect("Failed to create test config"); |
| 68 | + writeln!(file, r#"ruby-version = "3.2.0""#).expect("Failed to write config"); |
| 69 | + drop(file); |
| 70 | + |
| 71 | + // Load config from custom path |
| 72 | + let result = load_config(Some(config_path.clone())); |
| 73 | + assert!(result.is_ok()); |
| 74 | + |
| 75 | + let config = result.unwrap(); |
| 76 | + assert_eq!(config.ruby_version, Some("3.2.0".to_string())); |
| 77 | + |
| 78 | + // Cleanup |
| 79 | + let _ = fs::remove_file(&config_path); |
| 80 | + } |
| 81 | +} |
0 commit comments