Skip to content

test: create test harness with locked environment#59

Merged
allan2 merged 4 commits intoallan2:masterfrom
sonro:test-org
Dec 20, 2022
Merged

test: create test harness with locked environment#59
allan2 merged 4 commits intoallan2:masterfrom
sonro:test-org

Conversation

@sonro
Copy link
Copy Markdown
Collaborator

@sonro sonro commented Dec 19, 2022

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:

  • a set of default keys and values
  • valid and invalid default envfile contents
  • assertions with contextual output when tests fail

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);
    });
}

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:

- a set of default keys and values
- valid and invalid default envfile contents
- assertions with contextual output when tests fail
@sonro sonro mentioned this pull request Dec 19, 2022
@sonro
Copy link
Copy Markdown
Collaborator Author

sonro commented Dec 19, 2022

Code Coverage

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

@github-actions
Copy link
Copy Markdown

Code Coverage

@allan2 allan2 merged commit 423a7b0 into allan2:master Dec 20, 2022
@sonro sonro deleted the test-org branch July 18, 2024 08:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants