Skip to content

test: add unit tests for environment variable bindings#747

Open
Molter73 wants to merge 2 commits into
mainfrom
mauro/chore/add-env-var-unit-tests
Open

test: add unit tests for environment variable bindings#747
Molter73 wants to merge 2 commits into
mainfrom
mauro/chore/add-env-var-unit-tests

Conversation

@Molter73
Copy link
Copy Markdown
Collaborator

@Molter73 Molter73 commented Jun 1, 2026

Description

Adds coverage for all FACT_* env-var bindings in FactCli:

  • env_vars: each env var populates the correct FactConfig field
  • env_vars_override_yaml: env vars take precedence over YAML config
  • env_vars_invalid_values: invalid values produce ValueValidation errors

Introduces EnvVar and EnvVarGuard helper types that together acquire ENV_MUTEX, set the variable, and guarantee removal on drop — even if the test panics. std::env::set_var is not safe in multi-threaded programs; the mutex serialises all env-var mutations within the suite as the least invasive available mitigation.

Closes #730

Assisted-by: Claude Sonnet 4.6

Checklist

  • Patch has a change log entry OR does not need one.
  • Investigated and inspected CI test results
  • Updated documentation accordingly

Automated testing

  • Added unit tests
  • Added integration tests
  • Added regression tests

If any of these don't apply, please comment below.

Testing Performed

Run the tests manually.

Summary by CodeRabbit

  • Tests
    • Added comprehensive tests for environment-variable configuration with concurrency-safe isolation and automatic cleanup of temporary env vars.
    • Validates parsing of multiple variables (paths, numbers, booleans, URL, socket), verifies env-vars override YAML settings, and checks clear error messages for invalid values.

Adds coverage for all FACT_* env-var bindings in FactCli:
- env_vars: each env var populates the correct FactConfig field
- env_vars_override_yaml: env vars take precedence over YAML config
- env_vars_invalid_values: invalid values produce ValueValidation errors

Introduces EnvVar and EnvVarGuard helper types that together acquire
ENV_MUTEX, set the variable, and guarantee removal on drop — even if
the test panics. std::env::set_var is not safe in multi-threaded
programs; the mutex serialises all env-var mutations within the suite
as the least invasive available mitigation.

Closes #730

Assisted-by: Claude Sonnet 4.6
@Molter73 Molter73 requested a review from a team as a code owner June 1, 2026 14:09
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Jun 1, 2026

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yml

Review profile: CHILL

Plan: Enterprise

Run ID: 992383d3-561b-4f3f-a7f8-be12bd5c928d

📥 Commits

Reviewing files that changed from the base of the PR and between b9ed127 and cedeabe.

📒 Files selected for processing (1)
  • fact/src/config/tests.rs
🚧 Files skipped from review as they are similar to previous changes (1)
  • fact/src/config/tests.rs

📝 Walkthrough

Walkthrough

This PR adds concurrency-safe test infrastructure and three tests that verify FACT_* environment variables populate FactConfig fields, override YAML configuration when applied, and produce clap parse/validation errors for invalid values.

Changes

Environment Variable Test Coverage

Layer / File(s) Summary
Thread-safe test harness and helpers
fact/src/config/tests.rs
Imports Display and Mutex/MutexGuard; defines global ENV_MUTEX, EnvVarGuard RAII cleanup, EnvVar wrapper with a locked set() method and Display; adds with_env_var() helper that sets an env var and parses CLI into FactConfig while holding the mutex.
Environment variable binding tests
fact/src/config/tests.rs
Adds env_vars which iterates FACT_* env vars and asserts parsed FactConfig fields; env_vars_override_yaml which parses YAML, applies an env var via config.update(...), and asserts the env var overrides YAML; env_vars_invalid_values which asserts invalid env var values produce clap::Error messages with expected first-line diagnostics.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐰 I hop in tests with mutex tight,
set an env var, parse it right,
then gently drop and wipe the trace —
no races in this tranquil place. 🥕

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 66.67% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately and concisely describes the main change: adding unit tests for environment variable bindings in the codebase.
Description check ✅ Passed The PR description covers the key changes, includes all required checklist items (with reasoning for unchecked items), and documents testing approach. Minor: 'Updated documentation accordingly' is unchecked but appropriate since tests don't require doc changes.
Linked Issues check ✅ Passed The PR fully implements all requirements from issue #730: comprehensive unit tests for env-var bindings [#730], precedence over YAML config [#730], invalid-value error handling [#730], mutex-based serialization to prevent race conditions [#730], and cleanup via EnvVarGuard RAII [#730].
Out of Scope Changes check ✅ Passed All changes are directly aligned with issue #730 requirements: test harness (EnvVar/EnvVarGuard/ENV_MUTEX), env-var binding tests, YAML precedence tests, and invalid-value tests. No extraneous changes detected.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch mauro/chore/add-env-var-unit-tests

Comment @coderabbitai help to get the list of available commands and usage tips.

@Molter73
Copy link
Copy Markdown
Collaborator Author

Molter73 commented Jun 2, 2026

/retest fact-on-push

@Molter73 Molter73 enabled auto-merge (squash) June 2, 2026 09:35
Extends env_vars_override_yaml test to cover all configuration
variables that can be set via environment variables:
- FACT_CERTS
- FACT_ENDPOINT_ADDRESS
- FACT_ENDPOINT_EXPOSE_METRICS
- FACT_ENDPOINT_HEALTH_CHECK
- FACT_SKIP_PRE_FLIGHT
- FACT_HOTRELOAD

Extends env_vars_invalid_values test to cover boolean flags and
improves error message assertions by checking the full first line
of the error output instead of just the error source.

All 13 environment variables from FactCli are now comprehensively
tested across env_vars, env_vars_override_yaml, and
env_vars_invalid_values test functions.

Assisted-by: Claude Code (claude-sonnet-4-5@20250929)
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.

test: add unit tests for environment variable bindings (e.g. FACT_INODES_MAX)

1 participant