Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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");
}