From 89b76d6e17af69e54adc49d473d46519596dcf11 Mon Sep 17 00:00:00 2001 From: eladyn Date: Mon, 24 Feb 2025 20:52:23 +0100 Subject: [PATCH] tests: test validity of example config --- src/config.rs | 46 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/src/config.rs b/src/config.rs index 7a31a097..29468efd 100644 --- a/src/config.rs +++ b/src/config.rs @@ -285,7 +285,7 @@ pub struct SharedConfigValues { /// The audio backend to use #[arg(long, short, value_parser = possible_backends())] - #[serde(deserialize_with = "deserialize_backend")] + #[serde(deserialize_with = "deserialize_backend", default)] backend: Option, /// The volume controller to use @@ -734,4 +734,48 @@ mod tests { spotifyd_section.device_name = Some("spotifyd-test".to_string()); assert_eq!(merged_config, spotifyd_section); } + + #[test] + fn test_example_config() { + let example_config = include_str!("../contrib/spotifyd.conf"); + + let config: FileConfig = + toml::from_str(&example_config).expect("Commented example config should be valid"); + + assert_eq!( + (config.global, config.spotifyd), + (Some(SharedConfigValues::default()), None), + "example config should not do anything by default, but contain the global section" + ); + + let uncommented_example_config = example_config + .lines() + .map(|line| { + // uncomment any line starting with #[a-zA-Z] + line.strip_prefix("#") + .filter(|rest| { + // uncomment if the rest is a valid config line + // if alsa_backend is not enabled, ignore any problematic lines + rest.starts_with(char::is_alphabetic) + && (cfg!(feature = "alsa_backend") || !rest.contains("alsa")) + }) + .unwrap_or(line) + }) + .collect::>() + .join("\n"); + + let config: FileConfig = toml::from_str(&uncommented_example_config) + .expect("Uncommented example config should be valid"); + + assert!( + config.spotifyd.is_none(), + "example config should not have a spotifyd section" + ); + assert!( + config + .global + .is_some_and(|global| global != SharedConfigValues::default()), + "uncommented example config should contain some values" + ); + } }