Skip to content

Commit

Permalink
tests: test validity of example config
Browse files Browse the repository at this point in the history
  • Loading branch information
eladyn committed Feb 24, 2025
1 parent 2177c5f commit 89b76d6
Showing 1 changed file with 45 additions and 1 deletion.
46 changes: 45 additions & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>,

/// The volume controller to use
Expand Down Expand Up @@ -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");

Check failure on line 743 in src/config.rs

View workflow job for this annotation

GitHub Actions / lint

this expression creates a reference which is immediately dereferenced by the compiler

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::<Vec<&str>>()
.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"
);
}
}

0 comments on commit 89b76d6

Please sign in to comment.