| 1 |
#![allow(dead_code)] |
| 2 |
|
| 3 |
mod testenv; |
| 4 |
|
| 5 |
use std::env::{self, VarError}; |
| 6 |
|
| 7 |
pub use testenv::*; |
| 8 |
|
| 9 |
/// Default key used in envfile |
| 10 |
pub const TEST_KEY: &str = "TESTKEY"; |
| 11 |
/// Default value used in envfile |
| 12 |
pub const TEST_VALUE: &str = "test_val"; |
| 13 |
|
| 14 |
/// Default existing key set before test is run |
| 15 |
pub const TEST_EXISTING_KEY: &str = "TEST_EXISTING_KEY"; |
| 16 |
/// Default existing value set before test is run |
| 17 |
pub const TEST_EXISTING_VALUE: &str = "from_env"; |
| 18 |
/// Default overriding value in envfile |
| 19 |
pub const TEST_OVERRIDING_VALUE: &str = "from_file"; |
| 20 |
|
| 21 |
#[inline(always)] |
| 22 |
pub fn create_default_envfile() -> String { |
| 23 |
format!( |
| 24 |
"{}={}\n{}={}", |
| 25 |
TEST_KEY, TEST_VALUE, TEST_EXISTING_KEY, TEST_OVERRIDING_VALUE |
| 26 |
) |
| 27 |
} |
| 28 |
|
| 29 |
/// missing equals |
| 30 |
#[inline(always)] |
| 31 |
pub fn create_invalid_envfile() -> String { |
| 32 |
format!( |
| 33 |
"{}{}\n{}{}", |
| 34 |
TEST_KEY, TEST_VALUE, TEST_EXISTING_KEY, TEST_OVERRIDING_VALUE |
| 35 |
) |
| 36 |
} |
| 37 |
|
| 38 |
/// Assert that an environment variable is set and has the expected value. |
| 39 |
pub fn assert_env_var(key: &str, expected: &str) { |
| 40 |
match env::var(key) { |
| 41 |
Ok(actual) => assert_eq!( |
| 42 |
expected, actual, |
| 43 |
"\n\nFor Environment Variable `{}`:\n EXPECTED: `{}`\n ACTUAL: `{}`\n", |
| 44 |
key, expected, actual |
| 45 |
), |
| 46 |
Err(VarError::NotPresent) => panic!("env var `{}` not found", key), |
| 47 |
Err(VarError::NotUnicode(val)) => panic!( |
| 48 |
"env var `{}` currently has invalid unicode: `{}`", |
| 49 |
key, |
| 50 |
val.to_string_lossy() |
| 51 |
), |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
/// Assert that an environment variable is not currently set. |
| 56 |
pub fn assert_env_var_unset(key: &str) { |
| 57 |
match env::var(key) { |
| 58 |
Ok(actual) => panic!( |
| 59 |
"env var `{}` should not be set, currently it is: `{}`", |
| 60 |
key, actual |
| 61 |
), |
| 62 |
Err(VarError::NotUnicode(val)) => panic!( |
| 63 |
"env var `{}` should not be set, currently has invalid unicode: `{}`", |
| 64 |
key, |
| 65 |
val.to_string_lossy() |
| 66 |
), |
| 67 |
_ => (), |
| 68 |
} |
| 69 |
} |