Skip to content

Commit

Permalink
Test from_filename_override with new harness
Browse files Browse the repository at this point in the history
  • Loading branch information
sonro committed Jul 24, 2024
1 parent c846595 commit 992e111
Showing 1 changed file with 93 additions and 0 deletions.
93 changes: 93 additions & 0 deletions dotenv/tests/integration/api/from_filename_override.rs
Original file line number Diff line number Diff line change
@@ -1 +1,94 @@
use crate::util::*;
use dotenvy::from_filename_override;
use dotenvy_test_util::*;

#[test]
fn no_file_ok() {
let testenv = TestEnv::init();
test_in_env(&testenv, || {
from_filename_override("nonexistent").ok();
});
}

#[test]
fn no_file_err() {
let testenv = TestEnv::init();
test_in_env(&testenv, || {
let err = from_filename_override("nonexistent.env").unwrap_err();
assert_err_not_found(err);
});
}

#[test]
fn empty_default_file() {
let testenv = TestEnv::init_with_envfile("");
test_in_env(&testenv, || {
assert!(from_filename_override(".env").is_ok());
});
}

#[test]
fn empty_custom_file() {
let mut testenv = TestEnv::init();
testenv.add_envfile(".custom.env", "");
test_in_env(&testenv, || {
assert!(from_filename_override(".custom.env").is_ok());
});
}

#[test]
fn one_var_custom_file() {
let mut testenv = TestEnv::init();
testenv.add_envfile(".custom.env", KEYVAL_1);
test_in_env(&testenv, || {
assert!(from_filename_override(".custom.env").is_ok());
assert_env_var(KEY_1, VAL_1);
});
}

#[test]
fn override_existing_var_custom_file() {
let mut testenv = TestEnv::init();
testenv.add_env_var("FOOO", "from_env");
testenv.add_envfile(".custom.env", "FOOO=from_file");
test_in_env(&testenv, || {
assert_env_var("FOOO", "from_env");
from_filename_override(".custom.env").unwrap();
assert_env_var("FOOO", "from_file");
});
}

#[test]
fn default_override() {
test_in_default_env(|| {
from_filename_override(".env").unwrap();
assert_env_var(DEFAULT_TEST_KEY, DEFAULT_TEST_VALUE);
assert_env_var(DEFAULT_EXISTING_KEY, DEFAULT_OVERRIDING_VALUE);
})
}

#[test]
fn substitute_self() {
let mut testenv = TestEnv::init();
testenv.add_env_var("FOOO", "test");
testenv.add_envfile(".custom.env", "FOOO=$FOOO+1");
test_in_env(&testenv, || {
assert_env_var("FOOO", "test");
from_filename_override(".custom.env").unwrap();
assert_env_var("FOOO", "test+1");
});
}

#[test]
fn substitute_self_two_files() {
let mut testenv = TestEnv::init();
testenv.add_env_var("FOOO", "test");
testenv.add_envfile(".custom1.env", "FOOO=$FOOO+1");
testenv.add_envfile(".custom2.env", "FOOO=$FOOO+1");
test_in_env(&testenv, || {
assert_env_var("FOOO", "test");
from_filename_override(".custom1.env").unwrap();
from_filename_override(".custom2.env").unwrap();
assert_env_var("FOOO", "test+1+1");
});
}

0 comments on commit 992e111

Please sign in to comment.