Skip to content

Commit

Permalink
Add unit tests for #30
Browse files Browse the repository at this point in the history
Part of a move to introduce testing (issue #42)
  • Loading branch information
superatomic committed Apr 15, 2022
1 parent 7202f7a commit 0a78e4a
Show file tree
Hide file tree
Showing 3 changed files with 190 additions and 0 deletions.
63 changes: 63 additions & 0 deletions Cargo.lock

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

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,7 @@ shellexpand = "2.1.0"
human-panic = "1.0.3"
log = "0.4.16"
env_logger = "0.9.0"

[dev-dependencies]
indoc = "1.0.4"
pretty_assertions = "1.2.1"
123 changes: 123 additions & 0 deletions src/config_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,126 @@ pub(crate) enum EnvVariableValue {
String(String),
Array(Vec<String>),
}

#[cfg(test)]
mod tests {
use indexmap::indexmap;
use indoc::indoc;
use pretty_assertions::assert_eq;

use super::*;
use EnvVariableOption::*;

/// A function to get a ConfigFile from a str more easily.
fn to_config(toml_str: &str) -> ConfigFile {
toml::from_str(toml_str).unwrap()
}

/// Used to compare TOML to its expected representation in all other tests.
fn assert_config_value(toml_str: &str, map: EnvironmentVariables) {
assert_eq!(to_config(toml_str).vars, map);
}

#[test]
fn test_config_file_load_string() {
assert_config_value(
indoc! {r#"
FOO = "Bar"
"#},
indexmap! {
"FOO".into() => General(EnvVariableValue::String("Bar".into())),
},
)
}

#[test]
fn test_config_file_load_path() {
assert_config_value(
indoc! {r#"
PATH = ["/usr/local/bin", "/usr/bin", "/bin", "/usr/sbin", "/sbin"]
"#},
indexmap! {
"PATH".into() => General(EnvVariableValue::Array(vec![
"/usr/local/bin".into(),
"/usr/bin".into(),
"/bin".into(),
"/usr/sbin".into(),
"/sbin".into(),
])),
},
)
}

#[test]
fn test_config_file_load_specific() {
assert_config_value(
indoc! {r#"
ONLY_FOR_BASH.bash = "Do people read test cases?"
"#},
indexmap! {
"ONLY_FOR_BASH".into() => Specific(indexmap! {
"bash".into() => EnvVariableValue::String("Do people read test cases?".into()),
}),
},
)
}

#[test]
fn test_config_file_load_specific_other() {
assert_config_value(
indoc! {r#"
SOME_VARIABLE.fish = "you're pretty"
SOME_VARIABLE._ = '[ACCESS DENIED]'
"#},
indexmap! {
"SOME_VARIABLE".into() => Specific(indexmap! {
"fish".into() => EnvVariableValue::String("you're pretty".into()),
"_".into() => EnvVariableValue::String("[ACCESS DENIED]".into()),
})
},
)
}

#[test]
fn test_config_file_load_specific_other_alt() {
assert_config_value(
indoc! {r#"
[SOME_VARIABLE]
fish = "you're pretty"
_ = '[ACCESS DENIED]'
[ANOTHER_VARIABLE]
zsh = 'Zzz'
"#},
indexmap! {
"SOME_VARIABLE".into() => Specific(indexmap! {
"fish".into() => EnvVariableValue::String("you're pretty".into()),
"_".into() => EnvVariableValue::String("[ACCESS DENIED]".into()),
}),
"ANOTHER_VARIABLE".into() => Specific(indexmap! {
"zsh".into() => EnvVariableValue::String("Zzz".into()),
}),
},
)
}

#[test]
fn test_config_file_everything() {
assert_config_value(
indoc! {r#"
FOO = 'bar'
BAZ.zsh = 'zž'
BAZ.fish = ['gone', 'fishing']
BAZ._ = 'other'
"#},
indexmap! {
"FOO".into() => General(EnvVariableValue::String("bar".into())),
"BAZ".into() => Specific(indexmap! {
"zsh".into() => EnvVariableValue::String("zž".into()),
"fish".into() => EnvVariableValue::Array(vec!["gone".into(), "fishing".into()]),
"_".into() => EnvVariableValue::String("other".into()),
})
},
)
}
}

0 comments on commit 0a78e4a

Please # to comment.