-
-
Notifications
You must be signed in to change notification settings - Fork 326
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add WithEnv
method for setting a task's environment variables
#208
Changes from 7 commits
6d05dbc
3661071
2ea0daf
56ce4f3
3c0b782
f206b47
2208a26
b556398
ed2103b
56b09cf
93de3a0
9bb06c6
be908be
d628930
929de18
dc02161
b174908
54e80aa
638156d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ import ( | |
"os" | ||
"path/filepath" | ||
"regexp" | ||
"slices" | ||
"strings" | ||
"testing" | ||
"testing/iotest" | ||
|
@@ -1768,6 +1769,57 @@ func TestWithStdout_SetsSpecifiedWriterAsStdout(t *testing.T) { | |
} | ||
} | ||
|
||
func TestWithEnv_SetEmptyEnvironment(t *testing.T) { | ||
bitfield marked this conversation as resolved.
Show resolved
Hide resolved
|
||
t.Parallel() | ||
buf := new(bytes.Buffer) | ||
env := []string{} | ||
|
||
_, err := script.NewPipe().WithStdout(buf).WithEnv(env).Exec("printenv").Stdout() | ||
bitfield marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if len(buf.String()) != 0 { | ||
got := strings.Split(strings.TrimRight(buf.String(), "\n"), "\n") | ||
t.Errorf("expected 0 environment variables, got %d %v", len(got), got) | ||
bitfield marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
|
||
func TestWithEnv_SetMultipleEnvVars(t *testing.T) { | ||
bitfield marked this conversation as resolved.
Show resolved
Hide resolved
|
||
t.Parallel() | ||
buf := new(bytes.Buffer) | ||
env := []string{"ENV1=test1", "ENV2=test2"} | ||
|
||
_, err := script.NewPipe().WithStdout(buf).WithEnv(env).Exec("printenv").Stdout() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It seems a shame to add an implicit dependency on the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @bitfield would you please be able to elaborate on this for me? i'm not sure what it means to use the shell instead There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well, you'll notice that If we could stick to those, without adding a new dependency that we don't absolutely need, that would be a nice bonus. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @bitfield there is the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we could avoid adding a new external dependency, that would be great! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @bitfield using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, this is a bit of a subtle one. If you did something like this: Exec("echo ENV1=$ENV1") that wouldn't work, because On the other hand, if we exec a shell instead: Exec("sh -c 'echo ENV1=$ENV1'") this works as expected, because the shell expands We do use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's really cool! Thanks for the explanation @bitfield |
||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
got := strings.Split(strings.TrimRight(buf.String(), "\n"), "\n") | ||
bitfield marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if !slices.Equal(got, env) { | ||
t.Errorf("expected %v, got %v", env, got) | ||
} | ||
} | ||
|
||
func TestNotSettingEnvFallsBackToDefaultEnvironment(t *testing.T) { | ||
bitfield marked this conversation as resolved.
Show resolved
Hide resolved
|
||
t.Parallel() | ||
buf := new(bytes.Buffer) | ||
|
||
_, err := script.NewPipe().WithStdout(buf).Exec("printenv").Stdout() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
// not setting the environment should simply use the task's default environment | ||
expected := os.Environ() | ||
bitfield marked this conversation as resolved.
Show resolved
Hide resolved
|
||
got := strings.Split(strings.TrimRight(buf.String(), "\n"), "\n") | ||
|
||
if !slices.Equal(got, expected) { | ||
t.Errorf("expected %v, got %v", expected, got) | ||
} | ||
} | ||
|
||
func TestErrorReturnsErrorSetByPreviousPipeStage(t *testing.T) { | ||
t.Parallel() | ||
p := script.File("testdata/nonexistent.txt") | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's worth pointing out also that if
env
is an empty array, it will remove everything, and commands will be run with a completely empty environment. Sincenil
and empty arrays usually have the same semantics, this would otherwise be slightly surprising (but completely correct) behaviour.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The place to say this, of course, would be in the doc comment for
WithEnv
—in fact, we don't really need to say anything much about the struct field itself.