use envish::{ApplyEnvironmentFile, ApplyOptions, EnvironmentFile}; #[test] fn it_parses_basic_dotenv_file() { let file_contents = r#" # This value won't be set in the test, and this comment will be ignored MY_BEST_VARIABLE=some_value # This variable is also not defined, and it'll still be a string, # because all environment variables are strings without being converted to other data types SOME_OTHER_VARIABLE=1234 "#; std::env::var("MY_BEST_VARIABLE").expect_err("MY_BEST_VARIABLE should not be set"); std::env::var("SOME_OTHER_VARIABLE").expect_err("SOME_OTHER_VARIABLE should not be set"); let file = EnvironmentFile::parse(file_contents).expect("Failed to parse environment file"); file.apply(Default::default()); assert_eq!(std::env::var("MY_BEST_VARIABLE").unwrap(), "some_value"); assert_eq!(std::env::var("SOME_OTHER_VARIABLE").unwrap(), "1234"); } #[test] fn it_parses_dotenv_file_with_interpolation() { let file_contents = r#" # This value won't be set in the test, and this comment will be ignored MY_BEST_VARIABLE=some_value # This variable is also not defined, and it'll still be a string, # because all environment variables are strings without being converted to other data types SOME_OTHER_VARIABLE=1234 # This variable contains an interpolated value INTERPOLATED_VARIABLE=${SOME_OTHER_VARIABLE}567 "#; std::env::var("MY_BEST_VARIABLE").expect_err("MY_BEST_VARIABLE should not be set"); std::env::var("SOME_OTHER_VARIABLE").expect_err("SOME_OTHER_VARIABLE should not be set"); std::env::var("INTERPOLATED_VARIABLE").expect_err("INTERPOLATED_VARIABLE should not be set"); let file = EnvironmentFile::parse(file_contents).expect("Failed to parse environment file"); file.apply(Default::default()); assert_eq!(std::env::var("MY_BEST_VARIABLE").unwrap(), "some_value"); assert_eq!(std::env::var("SOME_OTHER_VARIABLE").unwrap(), "1234"); assert_eq!(std::env::var("INTERPOLATED_VARIABLE").unwrap(), "1234567"); } #[test] fn it_parses_dotenv_file_with_interpolation_and_prefix_option() { let file_contents = r#" # This value won't be set in the test, and this comment will be ignored MY_BEST_VARIABLE=some_value # This variable is also not defined, and it'll still be a string, # because all environment variables are strings without being converted to other data types SOME_OTHER_VARIABLE=1234 # This variable contains an interpolated value INTERPOLATED_VARIABLE=${SOME_OTHER_VARIABLE}567 "#; std::env::var("MY_BEST_VARIABLE").expect_err("MY_BEST_VARIABLE should not be set"); std::env::var("SOME_OTHER_VARIABLE").expect_err("SOME_OTHER_VARIABLE should not be set"); std::env::var("INTERPOLATED_VARIABLE").expect_err("INTERPOLATED_VARIABLE should not be set"); let file = EnvironmentFile::parse(file_contents).expect("Failed to parse environment file"); file.apply(ApplyOptions::with_prefix("APP_")); assert_eq!(std::env::var("APP_MY_BEST_VARIABLE").unwrap(), "some_value"); assert_eq!(std::env::var("APP_SOME_OTHER_VARIABLE").unwrap(), "1234"); assert_eq!(std::env::var("APP_INTERPOLATED_VARIABLE").unwrap(), "1234567"); }