allan2 / dotenvy

A well-maintained fork of the Rust dotenv crate
MIT License
625 stars 39 forks source link

test: create test harness with locked environment #59

Closed sonro closed 1 year ago

sonro commented 1 year ago

Candidate for solution to #58 part one.

Setup single-process integration testing with a utility module. Designed so multiple tests can be run without the need for separate processes. In practice, this means new tests will not need a new file. It allows for easily repeating checks across similar functions e.g. dotenv() and from_filename(".env").

The heart of the utility module is the TestEnv struct which enables controlled setup of a temporary testing environment. When using the test_in_env functions, access to the process' environment is effectively locked using a mutex, reset, and then the TestEnv is created.

Also included:

Example tests using this new system

#[test]
fn envfile_default_location() {
    test_in_default_env(|| {
        assert_env_var_unset(TEST_KEY);
        dotenvy().expect("loading .env file");
        assert_env_var(TEST_KEY, TEST_VALUE);
    });
}

#[test]
fn ignore_bom() {
    let bom = "\u{feff}";

    let test_env = TestEnv::init_with_envfile(format!(
        "{}{}={}",
        bom,
        TEST_KEY,
        TEST_VALUE
    ));

    test_in_env(test_env, || {
        assert_env_var_unset(TEST_KEY);
        dotenvy().expect("loading .env file");
        assert_env_var(TEST_KEY, TEST_VALUE);
    });
}
sonro commented 1 year ago

Code Coverage

This is lowering the coverage as the test utility has no tests currently making use of it

github-actions[bot] commented 1 year ago

Code Coverage