test(cli): make CLI tests hermetic against inherited AMPERSEND_* env vars - #137
Open
pcarranzav wants to merge 1 commit into
Open
test(cli): make CLI tests hermetic against inherited AMPERSEND_* env vars#137pcarranzav wants to merge 1 commit into
pcarranzav wants to merge 1 commit into
Conversation
The CLI treats several env vars as hard bypasses — `AMPERSEND_API_URL` (overrides the resolved API URL), `AMPERSEND_CONTEXT` (active context), `AMPERSEND_AGENT_*` (agent credentials). When the SDK is checked out inside a host repo whose direnv/devenv exports e.g. `AMPERSEND_API_URL=http://localhost:3002`, those values leak into the test process: the CLI tests pass in CI (clean env) but fail locally because the non-prod URL changes context auto-naming and API resolution (e.g. `localhost:3002-ctx-1a2b` instead of the expected `ctx-1a2b`). Add a vitest setup file that clears all inherited `AMPERSEND_*` vars before the suite runs, keeping it hermetic regardless of where it runs. Tests that need a specific value still set it themselves. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
pcarranzav
force-pushed
the
pcv/hermetic-cli-tests
branch
from
July 1, 2026 19:40
99e7bcf to
b6e7dc7
Compare
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.
Problem
The CLI honors several env vars as hard bypasses —
AMPERSEND_API_URL(overrides the resolved API URL),AMPERSEND_CONTEXT(active context),AMPERSEND_AGENT_*(agent credentials). When this SDK is checked out inside a host repo whose direnv/devenv exports one of them (e.g. the Ampersend monorepo exportsAMPERSEND_API_URL=http://localhost:3002), the value leaks into the vitest process.The result:
tests/cli/setup.test.tsandtests/cli/card.test.tspass in CI (clean env) but fail locally — the non-prod API URL flips context auto-naming fromctx-1a2btolocalhost:3002-ctx-1a2band changes API resolution. (setup.test.tsclearedAMPERSEND_API_URLinafterEachbut notbeforeEach, so the first test still inherited it;card.test.tsnever cleared it.)Fix
Add a vitest
setupFilesentry that strips all inheritedAMPERSEND_*env vars before the suite runs, so the suite is hermetic regardless of the shell it's invoked from. Tests that need a specific value still set it themselves (the setup only removes inherited ones at startup).Verification
From a shell with
AMPERSEND_API_URL=http://localhost:3002exported:card+setup)🤖 Generated with Claude Code