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

New integration test suite #111

Draft
wants to merge 44 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
8270cb7
Refactor test harness structure
sonro Jul 12, 2024
23c6f9e
Test the test harness
sonro Jul 12, 2024
726c057
Rustfmt test harness
sonro Jul 12, 2024
588959d
Refactor dotenv usage into wrapper
sonro Jul 12, 2024
4833142
Fix envfile contents now represented as bytes
sonro Jul 12, 2024
48d4609
Add more test harness assertions
sonro Jul 12, 2024
461b9eb
Add API wrapper to test harness
sonro Jul 12, 2024
240d0f5
Test empty envfile in harness
sonro Jul 12, 2024
97e69b5
Add EnvFileBuilder to test harness
sonro Jul 12, 2024
88386ac
Test harness BOM handling
sonro Jul 12, 2024
d872f14
Move test harness tests to subdir
sonro Jul 14, 2024
e870d60
Clean harness utility layout
sonro Jul 14, 2024
799cfb6
Move test harness to separate crate
sonro Jul 14, 2024
cddf9bd
Fix const naming convension
sonro Jul 14, 2024
1a93d86
Refactor wrappers into module
sonro Jul 15, 2024
538e196
Flesh out test_util docs
sonro Jul 15, 2024
31cf074
Add more than one envfile to TestEnv
sonro Jul 15, 2024
443ab30
Fix parameter ownership in methods
sonro Jul 15, 2024
972162c
Fix testenv lifetime
sonro Jul 15, 2024
a1a554d
Reflect test harness changes in API docs
sonro Jul 15, 2024
3dfc394
Test adding envfiles to testenv
sonro Jul 15, 2024
c70aa93
Clean TestEnv::add_envfile
sonro Jul 16, 2024
9ba20dd
Test adding env vars to testenv
sonro Jul 16, 2024
0fce979
Simplify adding env vars to testenv
sonro Jul 16, 2024
6992416
Test rest of TestEnv
sonro Jul 16, 2024
55ed501
Test envfile functions
sonro Jul 17, 2024
3471211
Document test util with a readme
sonro Jul 17, 2024
6edeb9f
Fix macos working directory
sonro Jul 17, 2024
896c32f
Remove wrap module
sonro Jul 17, 2024
dc14ec5
Add extra assertions
sonro Jul 17, 2024
1bd15d0
Fix naming consistency
sonro Jul 17, 2024
9974e0b
Add EnvFileBuilder conversions
sonro Jul 17, 2024
2d13009
Fix canonicalize paths
sonro Jul 17, 2024
9219e80
Fix method signature consistancy
sonro Jul 23, 2024
59b4684
Add default existing var testenv
sonro Jul 23, 2024
0bb3f37
Test dotenv with new harness
sonro Jul 17, 2024
2218aa9
Test dotenv_iter with new harness
sonro Jul 23, 2024
b5fd2c8
Test dotenv_override with new harness
sonro Jul 23, 2024
60139ae
Test from_filename with new harness
sonro Jul 24, 2024
c846595
Test from_filename_iter with new harness
sonro Jul 24, 2024
992e111
Test from_filename_override with new harness
sonro Jul 24, 2024
d6a9d82
Test from_filename* return value
sonro Jul 24, 2024
fb8dbb0
Test from_path with new harness
sonro Jul 25, 2024
627ec75
Test from_path_iter with new harness
sonro Jul 25, 2024
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
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
[workspace]
resolver = "2"

members = ["dotenv", "dotenv_codegen"]
members = ["dotenv", "dotenv_codegen", "test_util"]
2 changes: 1 addition & 1 deletion dotenv/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ required-features = ["cli"]
clap = { version = "4.3.11", optional = true }

[dev-dependencies]
dotenvy_test_util = { path = "../test_util", version = "0.1.0" }
tempfile = "3.3.0"
once_cell = "1.16.0"

[features]
cli = ["clap"]
36 changes: 36 additions & 0 deletions dotenv/tests/integration/api/dotenv.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
use crate::util::*;
use dotenvy::dotenv;
use dotenvy_test_util::*;

#[test]
fn default_env_ok() {
test_in_default_env(|| {
dotenv().ok();
assert_default_keys();
});
}

#[test]
fn default_env_unwrap() {
test_in_default_env(|| {
dotenv().unwrap();
assert_default_keys();
});
}

#[test]
fn default_env_unwrap_path() {
let testenv = TestEnv::default();
test_default_envfile_path(&testenv);
}

#[test]
fn explicit_no_override() {
let mut testenv = TestEnv::init();
testenv.add_env_var("FOOO", "bar");
testenv.add_envfile(".env", "FOOO=notbar");
test_in_env(&testenv, || {
dotenv().unwrap();
assert_env_var("FOOO", "bar");
})
}
103 changes: 103 additions & 0 deletions dotenv/tests/integration/api/dotenv_iter.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
use crate::util::*;
use dotenvy::dotenv_iter;
use dotenvy_test_util::*;

#[test]
fn default_env_ok() {
test_in_default_env(|| {
dotenv_iter().ok();
assert_default_existing_var();
// the envfile shouldn't be loaded into the environment
assert_env_var_unset(DEFAULT_TEST_KEY);
});
}

#[test]
fn default_env_unwrap() {
test_in_default_env(|| {
dotenv_iter().unwrap();
assert_default_existing_var();
assert_env_var_unset(DEFAULT_TEST_KEY);
});
}

#[test]
fn no_envfile_ok() {
let testenv = TestEnv::init();
test_in_env(&testenv, || {
dotenv_iter().ok();
assert_default_keys_unset();
});
}

#[test]
fn no_envfile_err() {
let testenv = TestEnv::init();
test_in_env(&testenv, || match dotenv_iter() {
Ok(_) => panic!("should have failed"),
Err(err) => assert_err_not_found(err),
});
}

#[test]
fn no_vars() {
let testenv = TestEnv::init_with_envfile("");
test_in_env(&testenv, || {
dotenv_iter().unwrap().for_each(|_| {
panic!("should have no keys");
});
});
}

#[test]
fn one_var() {
let testenv = TestEnv::init_with_envfile("FOOO=bar");
test_in_env(&testenv, || {
let (key, value) = dotenv_iter_unwrap_one_item();
assert_eq!(key, "FOOO");
assert_eq!(value, "bar");
});
}

#[test]
fn one_var_only() {
let testenv = TestEnv::init_with_envfile("FOOO=bar");
test_in_env(&testenv, || {
let count = dotenv_iter().expect("valid file").count();
assert_eq!(1, count);
});
}

#[test]
fn one_var_empty() {
let testenv = TestEnv::init_with_envfile("FOOO=");
test_in_env(&testenv, || {
let (key, value) = dotenv_iter_unwrap_one_item();
assert_eq!(key, "FOOO");
assert_eq!(value, "");
});
}

#[test]
fn two_vars_into_hash_map() {
check_iter_default_envfile_into_hash_map(dotenv_iter);
}

#[test]
fn explicit_no_override() {
let mut testenv = TestEnv::init();
testenv.add_env_var("FOOO", "bar");
testenv.add_envfile(".env", "FOOO=notbar");
test_in_env(&testenv, || {
dotenv_iter().unwrap();
assert_env_var("FOOO", "bar");
})
}

fn dotenv_iter_unwrap_one_item() -> (String, String) {
dotenv_iter()
.expect("valid file")
.next()
.expect("one item")
.expect("valid item")
}
83 changes: 83 additions & 0 deletions dotenv/tests/integration/api/dotenv_override.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
use crate::util::*;
use dotenvy::dotenv_override;
use dotenvy_test_util::*;

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

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

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

#[test]
fn one_new_var() {
let testenv = TestEnv::init_with_envfile("FOOO=bar");
test_in_env(&testenv, || {
dotenv_override().unwrap();
assert_env_var("FOOO", "bar");
});
}

#[test]
fn one_old_var() {
let mut testenv = TestEnv::init_with_envfile("FOOO=from_file");
testenv.add_env_var("FOOO", "from_env");
test_in_env(&testenv, || {
assert_env_var("FOOO", "from_env");
dotenv_override().unwrap();
assert_env_var("FOOO", "from_file");
});
}

#[test]
fn one_old_var_one_new_var() {
let vars = [("FOOO", "from_file"), ("BARR", "new")];
let envfile = create_custom_envfile(&vars);
let mut testenv = TestEnv::init_with_envfile(envfile);
testenv.add_env_var("FOOO", "from_env");
test_in_env(&testenv, || {
assert_env_var_unset("BARR");
dotenv_override().unwrap();
assert_env_vars(&vars);
});
}

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

#[test]
fn substitute_self_twice() {
let mut testenv = TestEnv::init_with_envfile("FOOO=$FOOO+1\nFOOO=$FOOO+1");
testenv.add_env_var("FOOO", "test");
test_in_env(&testenv, || {
assert_env_var("FOOO", "test");
dotenv_override().unwrap();
assert_env_var("FOOO", "test+1+1");
});
}
117 changes: 117 additions & 0 deletions dotenv/tests/integration/api/from_filename.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
use crate::util::*;
use dotenvy::from_filename;
use dotenvy_test_util::*;

#[test]
fn no_file() {
let testenv = TestEnv::init();
test_in_env(&testenv, || {
let err = from_filename("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(".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(".custom.env").is_ok());
});
}

#[test]
fn return_path_valid() {
let testenv = TestEnv::default();
test_in_env(&testenv, || {
let actual = from_filename(".env").unwrap();
assert_default_envfile_path(&testenv, &actual);
});
}

#[test]
fn default_file_not_read_on_missing_file() {
test_in_default_env(|| {
let err = from_filename("nonexistent.env").unwrap_err();
assert_err_not_found(err);
assert_env_var_unset(DEFAULT_TEST_KEY);
})
}

#[test]
fn dotenv_then_custom() {
let mut testenv = TestEnv::default();
testenv.add_envfile("custom", KEYVAL_1);
test_in_env(&testenv, || {
dotenvy::dotenv().unwrap();
from_filename("custom").unwrap();
assert_env_var(KEY_1, VAL_1);
assert_default_keys();
});
}

#[test]
fn dotenv_then_custom_no_override() {
let mut testenv = TestEnv::default();
testenv.add_envfile("custom", format!("{DEFAULT_TEST_KEY}=from_custom"));
test_in_env(&testenv, || {
dotenvy::dotenv().unwrap();
from_filename("custom").unwrap();
assert_default_keys();
});
}

#[test]
fn explicit_no_override() {
let mut testenv = TestEnv::init();
testenv.add_env_var(KEY_1, VAL_1);
testenv.add_envfile("custom", format!("{KEY_1}=from_custom"));
test_in_env(&testenv, || {
from_filename("custom").unwrap();
assert_env_var(KEY_1, VAL_1);
});
}

#[test]
fn child_dir() {
let mut testenv = TestEnv::init();
testenv.add_child_dir("child");
testenv.add_envfile("child/custom", KEYVAL_1);
test_in_env(&testenv, || {
from_filename("child/custom").unwrap();
assert_env_var(KEY_1, VAL_1);
});
}

#[test]
fn parent_dir_relative_path() {
let mut testenv = TestEnv::init();
testenv.add_child_dir("child");
testenv.add_envfile("custom.env", KEYVAL_1);
testenv.set_work_dir("child");
test_in_env(&testenv, || {
from_filename("../custom.env").unwrap();
assert_env_var(KEY_1, VAL_1);
});
}

#[test]
fn parent_dir_absolute_path() {
let mut testenv = TestEnv::init();
testenv.add_child_dir("child");
testenv.add_envfile("custom.env", KEYVAL_1);
testenv.set_work_dir("child");
test_in_env(&testenv, || {
let path = canonicalize_envfile_path(&testenv, "custom.env");
from_filename(path).unwrap();
assert_env_var(KEY_1, VAL_1);
});
}
Loading
Loading