1
Fork 0
mirror of https://github.com/Steffo99/micronfig.git synced 2024-11-25 17:44:18 +00:00

Write a couple tests for the core library

This commit is contained in:
Steffo 2024-01-04 17:25:56 +01:00
parent e8e17647cb
commit 5c7a6e90dd
Signed by: steffo
GPG key ID: 2A24051445686895
7 changed files with 70 additions and 2 deletions

View file

@ -4,6 +4,7 @@
<exclude-output /> <exclude-output />
<content url="file://$MODULE_DIR$"> <content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/micronfig/src" isTestSource="false" /> <sourceFolder url="file://$MODULE_DIR$/micronfig/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/micronfig/tests" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/target" /> <excludeFolder url="file://$MODULE_DIR$/target" />
</content> </content>
<orderEntry type="inheritedJdk" /> <orderEntry type="inheritedJdk" />

View file

@ -22,3 +22,6 @@ envdot = ["regex"]
[dependencies] [dependencies]
micronfig_macros = { version = "0.3.0", path = "../micronfig_macros" } micronfig_macros = { version = "0.3.0", path = "../micronfig_macros" }
regex = { version = "1.10.2", optional = true } regex = { version = "1.10.2", optional = true }
[dev-dependencies]
tempfile = { version = "3.9.0" }

View file

@ -21,3 +21,34 @@ pub fn get(key: &OsStr) -> Option<String> {
Some(data) 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);
}
}

View file

@ -6,3 +6,23 @@ use std::ffi::OsStr;
pub fn get(key: &OsStr) -> Option<String> { pub fn get(key: &OsStr) -> Option<String> {
std::env::var(key).ok() 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);
}
}

View file

@ -6,5 +6,7 @@ pub mod envvars;
pub mod envfiles; pub mod envfiles;
#[cfg(feature = "envdot")] #[cfg(feature = "envdot")]
pub mod envdot; pub mod envdot;
#[cfg(test)]
mod testing;
pub use micronfig_macros::config; pub use micronfig_macros::config;

11
micronfig/src/testing.rs Normal file
View file

@ -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()
}

View file

@ -182,8 +182,8 @@ pub fn config(input: TokenStream) -> TokenStream {
#[allow(non_snake_case)] #[allow(non_snake_case)]
pub(crate) fn #identifier() -> &'static #type_final_option { pub(crate) fn #identifier() -> &'static #type_final_option {
#identifier::_lock.get_or_init(|| { #identifier::_lock.get_or_init(|| {
let key: std::ffi::OsString = #identifier_string.into(); let key = #identifier_string.as_ref();
let value: Option<std::string::String> = _cache().get(&key); let value: Option<std::string::String> = _cache().get(key);
#require_code #require_code
#conversion_code #conversion_code