Fix env var restoration on error path to prevent test pollution#14245
Open
Fix env var restoration on error path to prevent test pollution#14245
Conversation
Move environment variable restoration from the try block to a finally block so env vars passed via the test harness are always cleaned up, even when the command fails and Deno's exit sanitizer intercepts the Deno.exit() call. Without this, a test that sets env vars (e.g. QUARTO_PDF_STANDARD) via TestContext.env could leak those values to subsequent tests when running in the same deno test process.
Collaborator
✅ Snyk checks have passed. No issues have been found so far.
💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse. |
Collaborator
|
This is another instance of the class of bugs "we should set up env variables first and never touch them again". Not something we'll be able to fully fix before Quarto 2, and this is a good PR - I'm just pointing out the general principle. |
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.
When tests pass environment variables via
TestContext.env, thequarto()function sets them globally withDeno.env.set()and restores the original values after execution. However, the restore code only runs on the success path — ifawait promisethrows (e.g. aCommandErrorfrom a failed render, or Deno's exit sanitizer intercepting aDeno.exit()call inside the command), execution jumps to thecatchblock and the env vars are never restored.In the sequential test runner (
deno testwith all files in one process), this means env vars from a failing test leak to all subsequent tests.Fix
Move the env restoration into a
finallyblock so it runs on both success and error paths.Context
Discovered while reviewing #14241, which adds a test that sets
QUARTO_PDF_STANDARDviaTestContext.env. Same class of env pollution as #14218, though #14218 is a separate issue wherecrossref.tssets env vars directly without any cleanup mechanism.Related: #12621 (broader
Deno.envcleanup effort)