Skip to content
Snippets Groups Projects
integration.rs 2.01 KiB
Newer Older
use envish::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().expect("Failed to apply environment file");

    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() {
    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().expect("Failed to apply environment file");

    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");
}