Skip to content

Add runnable examples for mocking HOME and the clock - #18

Merged
tanglearncode merged 3 commits into
mainfrom
tests/community-problems
Sep 13, 2026
Merged

tanglearncode merged 3 commits into
mainfrom
tests/community-problems

Conversation

@tanglearncode

@tanglearncode tanglearncode commented Sep 13, 2026

Copy link
Copy Markdown
Contributor

Adds two runnable examples with tests:

  • examples/environment.rs: tests code that reads HOME by mocking env::var_os per thread, without set_var.
  • examples/clock.rs: tests time-based code by mocking SystemTime::now, without sleeping.

Both are declared with test = true, so CI runs their tests. tests/examples.rs fails if a new example is missing that setting. The coverage gate ignores examples/.

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.
@tanglearncode
tanglearncode merged commit b5d7f6a into main Sep 13, 2026
9 checks passed
@tanglearncode
tanglearncode deleted the tests/community-problems branch September 14, 2026 03:47
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.

1 participant