File: test-variable-substitution.rs

package info (click to toggle)
rust-dotenvy 0.15.7-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 268 kB
  • sloc: makefile: 2
file content (64 lines) | stat: -rw-r--r-- 1,491 bytes parent folder | download
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
mod common;

use dotenvy::*;
use std::{env, error::Error, result::Result};

use crate::common::*;

#[test]
fn test_variable_substitutions() -> Result<(), Box<dyn Error>> {
    std::env::set_var("KEY", "value");
    std::env::set_var("KEY1", "value1");

    let substitutions_to_test = [
        "$ZZZ", "$KEY", "$KEY1", "${KEY}1", "$KEY_U", "${KEY_U}", "\\$KEY",
    ];

    let common_string = substitutions_to_test.join(">>");
    let dir = tempdir_with_dotenv(&format!(
        r#"
KEY1=new_value1
KEY_U=$KEY+valueU

SUBSTITUTION_FOR_STRONG_QUOTES='{}'
SUBSTITUTION_FOR_WEAK_QUOTES="{}"
SUBSTITUTION_WITHOUT_QUOTES={}
"#,
        common_string, common_string, common_string
    ))?;

    assert_eq!(var("KEY")?, "value");
    assert_eq!(var("KEY1")?, "value1");
    assert_eq!(var("KEY_U")?, "value+valueU");
    assert_eq!(var("SUBSTITUTION_FOR_STRONG_QUOTES")?, common_string);
    assert_eq!(
        var("SUBSTITUTION_FOR_WEAK_QUOTES")?,
        [
            "",
            "value",
            "value1",
            "value1",
            "value_U",
            "value+valueU",
            "$KEY"
        ]
        .join(">>")
    );
    assert_eq!(
        var("SUBSTITUTION_WITHOUT_QUOTES")?,
        [
            "",
            "value",
            "value1",
            "value1",
            "value_U",
            "value+valueU",
            "$KEY"
        ]
        .join(">>")
    );

    env::set_current_dir(dir.path().parent().unwrap())?;
    dir.close()?;
    Ok(())
}