Add runnable examples for mocking HOME and the clock - #18
Merged
Merged
Conversation
Two common testing problems get their own integration tests, each written against business code that calls the standard library directly. tests/environment.rs mocks env::var. In the 2024 edition env::set_var is unsafe, and it changes the environment of the whole test process, so tests that set variables must share a lock or run one at a time. The tests read a log level and a database URL with values chosen per thread, cover a missing variable and a default port, check that three overlapping threads each see their own value, and check that a thread without a session still finds the variable unset. The file forbids unsafe code. tests/clock.rs mocks SystemTime::now. Code that reads the clock directly usually has to take a clock parameter before a test can choose the time. The tests run a morning check at chosen instants, step across both edges of the morning window, advance the clock 20 seconds per reading so a token expires without sleeping, and check that other threads keep the machine clock. Checked locally with cargo fmt and cargo clippy --workspace --all-targets -- -D warnings on x86_64-pc-windows-msvc, and cargo clippy -- -D warnings for the two new test targets on aarch64-unknown-linux-gnu, aarch64-apple-darwin, x86_64-apple-darwin and aarch64-pc-windows-msvc. The tests run in CI.
The environment tests now follow a case that keeps breaking CI in other projects: code that shortens paths under the home directory to ~, and tests that point HOME somewhere else with the unsafe env::set_var. Parallel tests then read each other's HOME, so projects add environment locks, serialize the tests, or change the function to take the home directory as a parameter. The business code reads HOME with env::var_os and stays unchanged. The tests mock env::var_os per thread and cover a path under home, home itself, a sibling directory that shares the prefix, an unset or empty HOME, three overlapping threads that each see their own HOME, and a thread without a session that still sees the real value. The file still forbids unsafe code. The log-level and database-URL examples from the previous commit are gone. The previous commit's tests passed CI on all seven jobs (run 34743354785). Checked this change locally with cargo fmt and cargo clippy -- -D warnings for the environment test target on x86_64-pc-windows-msvc, aarch64-unknown-linux-gnu, aarch64-apple-darwin, x86_64-apple-darwin and aarch64-pc-windows-msvc.
The HOME and clock scenarios are meant to be read on their own and linked to, so they now live in examples/environment.rs and examples/clock.rs. Each file keeps its business code at the top, adds a main that runs that code without mocks, and holds its tests in a cfg(test) module. Cargo builds examples during cargo test but runs their tests only when the example is declared with test = true, so Cargo.toml declares both. CI's cargo test --workspace and cargo clippy --all-targets therefore run and lint them in all seven required jobs. tests/examples.rs fails if a file in examples/ has no such entry, so a new example cannot drop out of CI unnoticed. The coverage gate already ignored test files; it now ignores examples/ too, because an example's main never runs under cargo llvm-cov and is not library code. The package include list gains examples/**, which tests/examples.rs reads.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds two runnable examples with tests:
examples/environment.rs: tests code that readsHOMEby mockingenv::var_osper thread, withoutset_var.examples/clock.rs: tests time-based code by mockingSystemTime::now, without sleeping.Both are declared with
test = true, so CI runs their tests.tests/examples.rsfails if a new example is missing that setting. The coverage gate ignoresexamples/.