Skip to content

Commit

Permalink
fix: inherit env (fixes #76)
Browse files Browse the repository at this point in the history
Co-Authored-By: eric <[email protected]>
  • Loading branch information
2 people authored and amonks committed Jan 25, 2024
1 parent 6f1225d commit 62ea8b4
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 5 deletions.
23 changes: 23 additions & 0 deletions example/tasks.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,26 @@
done
"""

# these examples illustrate the behavior of env vars in tasks
#
# the output of the two following tasks should be the same except for the TESTENV line
# the first task should be empty, the second should have the TESTENV line with the
# value "test". Both should include the env vars from the parent process.
[[task]]
id = "noenv"
desc = "with no env var provided, the task inherits (sic) the env from the parent process"
type = "short"
cmd = """
echo "TESTENV: $TESTENV"
echo "done"
"""

[[task]]
id = "withenv"
desc = "providing an env var overwrites the entire env"
type = "short"
env = { TESTENV = "test" }
cmd = """
echo "TESTENV: $TESTENV"
echo "done"
"""
7 changes: 7 additions & 0 deletions pkg/run/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ func TestIntegrationSnapshots(t *testing.T) {
}

func testExample(t *testing.T, name string) error {
// Set up environment variables here so we can validate it's
// still accessible to the task's command.
if name == "env" {
os.Setenv("INHERITED_ENV", "true")
defer os.Unsetenv("INHERITED_ENV")
}

dmp := diffmatchpatch.New()

changedFilePath := filepath.Join("testdata", "snapshots", name, "changed-file")
Expand Down
3 changes: 2 additions & 1 deletion pkg/run/script_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,8 @@ func (t *scriptTask) startCmd(stdout io.Writer) error {
t.cmd.Dir = t.dir
t.cmd.Stdout = stdout
t.cmd.Stderr = stdout
t.cmd.Env = t.env
t.cmd.Env = append(os.Environ(), t.env...)

if err := t.cmd.Start(); err != nil {
return err
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/run/testdata/snapshots/env/out.log
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
ok

test starting
ENVIRONMENT VARIABLE
ENV_FROM_TASK: 'some env var value'
INHERITED_ENV: 'true'
exit ok

run done
8 changes: 5 additions & 3 deletions pkg/run/testdata/snapshots/env/tasks.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
[[task]]
id = "test"
type = "short"
cmd = "echo $VALUE"
env = { VALUE = "ENVIRONMENT VARIABLE" }

env = { ENV_FROM_TASK = "some env var value" }
cmd = """
echo "ENV_FROM_TASK: '$ENV_FROM_TASK'"
echo "INHERITED_ENV: '$INHERITED_ENV'"
"""

0 comments on commit 62ea8b4

Please sign in to comment.