Configurable Variables Resolved Before Test Setup: ballerina/test setEnv and Config.toml Precedence
0 reputation · 04 Apr 2025, 02:55 UTC
0 reputation · 04 Apr 2025, 02:55 UTC
Ballerina configurable variables are resolved during module initialization. In an integration test that must avoid production credentials, a common approach is to use ballerina/test's setEnv and unsetEnv functions, or a test-specific Config.toml, to supply substitute values. A failure condition arises when test setup runs after configurable resolution: a test that calls test:setEnv in @test:BeforeSuite or @test:BeforeEach may find the configurable value already fixed.
The unresolved decision is precedence and timing. When both an environment variable and a Config.toml entry are present, which source wins? More specifically, can test:setEnv called inside a test override a configurable variable that was already initialized, or must the value be present before the module loads? These behaviors are version-sensitive across Swan Lake distributions.
test:setEnv affect configurable variables after module initialization?Config.toml for configurable values?29775 reputation · 04 Apr 2025, 09:20 UTC
Calling test:setEnv after a module that contains a configurable has been loaded does **not** change the resolved value. The variable is fixed during module initialization, and later environment‑variable changes have no effect.
Config.toml (or Config.yaml) → default values.Config.toml entry exist for the same key, the environment variable wins.test:setEnv, the environment variable must be set **before** the module is first imported.@test:BeforeSuite block, call test:setEnv for each required key (or ensure the variables are already set in the process environment).setEnv calls, e.g., inside a test function or a @test:BeforeEach that runs after the suite‑level setup.Config.toml in the test resources directory and ensure no conflicting environment variables are present (otherwise the env var will override it).setEnv call or the test Config.toml.If you observe that a configurable still returns a production value despite using test:setEnv in @test:BeforeSuite, verify whether any dependency or test utility imports the module before your suite block runs. Identifying the earliest import point determines whether the environment variable was set early enough.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.