Skip to content
Snippets Groups Projects
integration.rs 3.09 KiB
Newer Older
use envish::{ApplyEnvironmentFile, ApplyOptions, EnvironmentFile};

#[test]
fn it_parses_basic_dotenv_file() {
Louis's avatar
Louis committed
	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
    "#;

Louis's avatar
Louis committed
	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");
Louis's avatar
Louis committed
	let file = EnvironmentFile::parse(file_contents).expect("Failed to parse environment file");
	file.apply(Default::default());
Louis's avatar
Louis committed
	assert_eq!(std::env::var("MY_BEST_VARIABLE").unwrap(), "some_value");
	assert_eq!(std::env::var("SOME_OTHER_VARIABLE").unwrap(), "1234");
fn it_parses_dotenv_file_with_interpolation() {
Louis's avatar
Louis committed
	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
    "#;

Louis's avatar
Louis committed
	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");
Louis's avatar
Louis committed
	let file = EnvironmentFile::parse(file_contents).expect("Failed to parse environment file");
	file.apply(Default::default());
Louis's avatar
Louis committed
	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() {
Louis's avatar
Louis committed
	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
    "#;

Louis's avatar
Louis committed
	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");
Louis's avatar
Louis committed
	let file = EnvironmentFile::parse(file_contents).expect("Failed to parse environment file");
	file.apply(ApplyOptions::with_prefix("APP_"));
Louis's avatar
Louis committed
	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");
}