LemonCore.Testing.HermeticEnv (lemon_core v0.1.0)

View Source

Shared helpers for keeping non-integration tests isolated from ambient machine state.

The default unit-test lane should not accidentally inherit a developer's real provider or platform credentials. Tests that intentionally exercise live integrations must opt in explicitly with LEMON_TEST_ALLOW_LIVE_CREDENTIALS=1 or by passing allow_live?: true.

Summary

Functions

Returns env vars considered live provider/platform credentials in unit tests.

Restores env vars captured with snapshot_env/1.

Scrubs live provider/platform credentials for normal unit-test lanes.

Captures current values for env vars.

Runs fun and restores the provided env vars afterward.

Functions

credential_env_vars()

@spec credential_env_vars() :: [String.t()]

Returns env vars considered live provider/platform credentials in unit tests.

restore_env(snapshot)

@spec restore_env(%{required(String.t()) => String.t() | nil}) :: :ok

Restores env vars captured with snapshot_env/1.

scrub_unit_credentials!(opts \\ [])

@spec scrub_unit_credentials!(keyword()) ::
  :ok | {:skipped, :live_credentials_allowed}

Scrubs live provider/platform credentials for normal unit-test lanes.

Set LEMON_TEST_ALLOW_LIVE_CREDENTIALS=1 or pass allow_live?: true for an explicit live/integration run that should inherit credentials.

Returns :ok when credentials are scrubbed, or {:skipped, :live_credentials_allowed} when live credentials are explicitly allowed.

snapshot_env(keys)

@spec snapshot_env([String.t()]) :: %{required(String.t()) => String.t() | nil}

Captures current values for env vars.

with_restored_env(keys, fun)

@spec with_restored_env([String.t()], (-> result)) :: result when result: term()

Runs fun and restores the provided env vars afterward.

This helper is intentionally small and synchronous. Use it from async: false tests when mutating process-wide environment variables.