Skip to content

Allow setting environment variables in setup hook - #44

Merged
imjasonh merged 6 commits into
mainfrom
copilot/support-env-vars-in-setup
Dec 30, 2025
Merged

Allow setting environment variables in setup hook#44
imjasonh merged 6 commits into
mainfrom
copilot/support-env-vars-in-setup

Conversation

Copilot AI commented Dec 30, 2025

Copy link
Copy Markdown
Contributor

Plan: Support passing env vars into test environment in setup

  • Update SetupFn type alias in params.rs to accept &mut TestEnvironment instead of &TestEnvironment
  • Update setup method signature in params.rs to accept Fn(&mut TestEnvironment) instead of Fn(&TestEnvironment)
  • Update setup method signature in lib.rs Builder to accept Fn(&mut TestEnvironment) instead of Fn(&TestEnvironment)
  • Update execution in execution.rs to pass &mut env to setup function
  • Update existing test in setup_hook.rs to test the new functionality (make it pass)
  • Add a new focused test to validate setting environment variables in setup hook
  • Run tests to validate changes - all tests pass
  • Update documentation examples to show the new capability
  • Address code review feedback - fix trailing newline inconsistency
  • Add comprehensive test for environment variable presence/absence to ensure proper isolation
  • Enhance tests to demonstrate env vars work without sh -c wrapper (using printenv/set)
  • Format assert statement to single line per style guidelines
Original prompt

This section details on the original issue you should resolve

<issue_title>support passing env vars into the test environment in setup</issue_title>
<issue_description>```
.setup(|env| => {
env.set("FOO", "bar")
}


(or similar)

this should make `FOO` have the value of `bar` in the test environment</issue_description>

## Comments on the Issue (you are @copilot in this section)

<comments>
</comments>


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI self-assigned this Dec 30, 2025
Copilot AI and others added 2 commits December 30, 2025 01:46
Co-authored-by: imjasonh <210737+imjasonh@users.noreply.github.com>
Co-authored-by: imjasonh <210737+imjasonh@users.noreply.github.com>
Copilot AI changed the title [WIP] Support passing env vars into the test environment in setup Allow setting environment variables in setup hook Dec 30, 2025
Copilot AI requested a review from imjasonh December 30, 2025 01:53
@imjasonh

Copy link
Copy Markdown
Owner

@copilot Ensure there's a test that covers this behavior, to ensure envs which are set in setup are present in the test, and those that aren't set, are not available

Co-authored-by: imjasonh <210737+imjasonh@users.noreply.github.com>

Copilot AI commented Dec 30, 2025

Copy link
Copy Markdown
Contributor Author

@copilot Ensure there's a test that covers this behavior, to ensure envs which are set in setup are present in the test, and those that aren't set, are not available

Added test_setup_env_vars_presence_and_absence() which verifies:

  1. Environment variables set in setup (SET_VAR) are available in the test
  2. Environment variables not set in setup (UNSET_VAR) are not available in the test

This ensures proper isolation of the test environment. Commit: 063cd77

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • 1.1.1.1
    • Triggering command: REDACTED, pid is -1 (packet block)

If you need me to access, download, or install something from one of these locations, you can either:

@imjasonh imjasonh left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot I want to mimic as much as possible (idiomatically for Rust) the behavior of https://pkg.go.dev/github.com/rogpeppe/go-internal/testscript#Env

Does that require sh -c to be able to get the values of the env? That library also supports env which just prints the full env vars, do we do that too?

Co-authored-by: imjasonh <210737+imjasonh@users.noreply.github.com>

Copilot AI commented Dec 30, 2025

Copy link
Copy Markdown
Contributor Author

@copilot I want to mimic as much as possible (idiomatically for Rust) the behavior of https://pkg.go.dev/github.com/rogpeppe/go-internal/testscript#Env

Does that require sh -c to be able to get th...

Environment variables set in setup (or via env command) are passed directly to executed commands via Rust's .envs() - no sh -c needed for programs to access them (see updated test using printenv/set directly).

However, shell variable expansion syntax like $VAR requires a shell interpreter (sh -c or cmd /c), which matches idiomatic Rust behavior.

The env command already supports both behaviors from Go testscript:

  • env (no args) prints all environment variables
  • env VAR=value sets environment variables

Commit: 7282682

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • 1.1.1.1
    • Triggering command: REDACTED, pid is -1 (packet block)

If you need me to access, download, or install something from one of these locations, you can either:

@imjasonh
imjasonh marked this pull request as ready for review December 30, 2025 02:15
@imjasonh
imjasonh enabled auto-merge (squash) December 30, 2025 02:15
@imjasonh

Copy link
Copy Markdown
Owner

@copilot

Diff in /home/runner/work/testscript-rs/testscript-rs/tests/setup_hook.rs:253:
     });
 
     let result = run_script(&script_path, &params);
-    assert!(
-        result.is_ok(),
-        "Env command test should pass: {:?}",
-        result
-    );
+    assert!(result.is_ok(), "Env command test should pass: {:?}", result);
 }

Co-authored-by: imjasonh <210737+imjasonh@users.noreply.github.com>
auto-merge was automatically disabled December 30, 2025 02:19

Head branch was pushed to by a user without write access

Copilot AI commented Dec 30, 2025

Copy link
Copy Markdown
Contributor Author

@copilot

...

Fixed formatting to single line. Commit: 229e22d

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • 1.1.1.1
    • Triggering command: REDACTED, pid is -1 (packet block)

If you need me to access, download, or install something from one of these locations, you can either:

@imjasonh
imjasonh merged commit a69a295 into main Dec 30, 2025
3 checks passed
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.

support passing env vars into the test environment in setup

2 participants