diff --git a/_workspace.iml b/_workspace.iml index 9c3374c..84b5aaf 100644 --- a/_workspace.iml +++ b/_workspace.iml @@ -4,6 +4,7 @@ + diff --git a/micronfig/Cargo.toml b/micronfig/Cargo.toml index 3e7b5bf..281ab14 100644 --- a/micronfig/Cargo.toml +++ b/micronfig/Cargo.toml @@ -22,3 +22,6 @@ envdot = ["regex"] [dependencies] micronfig_macros = { version = "0.3.0", path = "../micronfig_macros" } regex = { version = "1.10.2", optional = true } + +[dev-dependencies] +tempfile = { version = "3.9.0" } \ No newline at end of file diff --git a/micronfig/src/envfiles.rs b/micronfig/src/envfiles.rs index 077083d..1d3135f 100644 --- a/micronfig/src/envfiles.rs +++ b/micronfig/src/envfiles.rs @@ -21,3 +21,34 @@ pub fn get(key: &OsStr) -> Option { Some(data) } + + +#[cfg(test)] +pub(crate) mod tests { + use super::*; + use crate::testing::tempfile_fixture; + + #[test] + fn it_works() { + let file = tempfile_fixture("XYZ"); + std::env::set_var("LETTERS_FILE", file.as_os_str()); + + let value = get("LETTERS".as_ref()); + assert_eq!(value, Some("XYZ".to_string())); + } + + #[test] + fn missing_envvar() { + std::env::remove_var("THIS_ENVVAR_DOES_NOT_EXIST_FILE"); + let value = get("THIS_ENVVAR_DOES_NOT_EXIST".as_ref()); + assert_eq!(value, None) + } + + #[test] + #[should_panic] + fn missing_file() { + std::env::set_var("NONEXISTENT_FILE", "/this/file/does/not/exist"); + let value = get("NONEXISTENT".as_ref()); + println!("{:?}", value); + } +} diff --git a/micronfig/src/envvars.rs b/micronfig/src/envvars.rs index bc1b974..5a9a8d0 100644 --- a/micronfig/src/envvars.rs +++ b/micronfig/src/envvars.rs @@ -6,3 +6,23 @@ use std::ffi::OsStr; pub fn get(key: &OsStr) -> Option { std::env::var(key).ok() } + + +#[cfg(test)] +pub(crate) mod tests { + use super::*; + + #[test] + fn it_works() { + std::env::set_var("LETTERS", "XYZ"); + let value = get("LETTERS".as_ref()); + assert_eq!(value, Some("XYZ".to_string())); + } + + #[test] + fn missing_envvar() { + std::env::remove_var("THIS_ENVVAR_DOES_NOT_EXIST"); + let value = get("THIS_ENVVAR_DOES_NOT_EXIST".as_ref()); + assert_eq!(value, None); + } +} diff --git a/micronfig/src/lib.rs b/micronfig/src/lib.rs index 3ae1096..17d1981 100644 --- a/micronfig/src/lib.rs +++ b/micronfig/src/lib.rs @@ -6,5 +6,7 @@ pub mod envvars; pub mod envfiles; #[cfg(feature = "envdot")] pub mod envdot; +#[cfg(test)] +mod testing; pub use micronfig_macros::config; diff --git a/micronfig/src/testing.rs b/micronfig/src/testing.rs new file mode 100644 index 0000000..58e40f4 --- /dev/null +++ b/micronfig/src/testing.rs @@ -0,0 +1,11 @@ +pub fn tempfile_fixture(content: &str) -> tempfile::TempPath { + use std::io::Write; + + let mut file = tempfile::NamedTempFile::new() + .expect("the tempfile fixture to be created successfully"); + write!(file, "{}", content) + .expect("to be able to write into the tempfile fixture"); + + file.into_temp_path() +} + diff --git a/micronfig_macros/src/lib.rs b/micronfig_macros/src/lib.rs index 3448e7a..40fe434 100644 --- a/micronfig_macros/src/lib.rs +++ b/micronfig_macros/src/lib.rs @@ -182,8 +182,8 @@ pub fn config(input: TokenStream) -> TokenStream { #[allow(non_snake_case)] pub(crate) fn #identifier() -> &'static #type_final_option { #identifier::_lock.get_or_init(|| { - let key: std::ffi::OsString = #identifier_string.into(); - let value: Option = _cache().get(&key); + let key = #identifier_string.as_ref(); + let value: Option = _cache().get(key); #require_code #conversion_code