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

Conversation

sonro
Copy link
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
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

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