Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: create test harness with locked environment #59

Merged
merged 4 commits into from
Dec 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions dotenv/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ clap = { version = "3.2", optional = true }

[dev-dependencies]
tempfile = "3.3.0"
once_cell = "1.16.0"

[features]
cli = ["clap"]
1 change: 1 addition & 0 deletions dotenv/tests/integration/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mod util;
69 changes: 69 additions & 0 deletions dotenv/tests/integration/util/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#![allow(dead_code)]

mod testenv;

use std::env::{self, VarError};

pub use testenv::*;

/// Default key used in envfile
pub const TEST_KEY: &str = "TESTKEY";
/// Default value used in envfile
pub const TEST_VALUE: &str = "test_val";

/// Default existing key set before test is run
pub const TEST_EXISTING_KEY: &str = "TEST_EXISTING_KEY";
/// Default existing value set before test is run
pub const TEST_EXISTING_VALUE: &str = "from_env";
/// Default overriding value in envfile
pub const TEST_OVERRIDING_VALUE: &str = "from_file";

#[inline(always)]
pub fn create_default_envfile() -> String {
format!(
"{}={}\\n{}={}",
TEST_KEY, TEST_VALUE, TEST_EXISTING_KEY, TEST_OVERRIDING_VALUE
)
}

/// missing equals
#[inline(always)]
pub fn create_invalid_envfile() -> String {
format!(
"{}{}\\n{}{}",
TEST_KEY, TEST_VALUE, TEST_EXISTING_KEY, TEST_OVERRIDING_VALUE
)
}

/// Assert that an environment variable is set and has the expected value.
pub fn assert_env_var(key: &str, expected: &str) {
match env::var(key) {
Ok(actual) => assert_eq!(
expected, actual,
"\n\nFor Environment Variable `{}`:\n EXPECTED: `{}`\n ACTUAL: `{}`\n",
key, expected, actual
),
Err(VarError::NotPresent) => panic!("env var `{}` not found", key),
Err(VarError::NotUnicode(val)) => panic!(
"env var `{}` currently has invalid unicode: `{}`",
key,
val.to_string_lossy()
),
}
}

/// Assert that an environment variable is not currently set.
pub fn assert_env_var_unset(key: &str) {
match env::var(key) {
Ok(actual) => panic!(
"env var `{}` should not be set, currently it is: `{}`",
key, actual
),
Err(VarError::NotUnicode(val)) => panic!(
"env var `{}` should not be set, currently has invalid unicode: `{}`",
key,
val.to_string_lossy()
),
_ => (),
}
}
Loading