Skip to content

Commit 8241094

Browse files
committed
add additional helper functions for setting command's environment
1 parent 36eafd4 commit 8241094

File tree

3 files changed

+82
-0
lines changed

3 files changed

+82
-0
lines changed

builder_command.go

+30
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,13 @@ func (c *chain) WithInjections(sources ...io.Reader) CommandBuilder {
108108
return c
109109
}
110110

111+
func (c *chain) WithEmptyEnvironment() CommandBuilder {
112+
cmdDesc := c.cmdDescriptors[len(c.cmdDescriptors)-1]
113+
cmdDesc.command.Env = []string{}
114+
115+
return c
116+
}
117+
111118
func (c *chain) WithEnvironmentMap(envMap map[interface{}]interface{}) CommandBuilder {
112119
cmdDesc := c.cmdDescriptors[len(c.cmdDescriptors)-1]
113120

@@ -127,6 +134,17 @@ func (c *chain) WithEnvironment(envMap ...interface{}) CommandBuilder {
127134
for i := 0; i < len(envMap); i += 2 {
128135
cmdDesc.command.Env = append(cmdDesc.command.Env, fmt.Sprintf("%v=%v", envMap[i], envMap[i+1]))
129136
}
137+
138+
return c
139+
}
140+
141+
func (c *chain) WithEnvironmentPairs(envMap ...string) CommandBuilder {
142+
cmdDesc := c.cmdDescriptors[len(c.cmdDescriptors)-1]
143+
144+
for _, entry := range envMap {
145+
cmdDesc.command.Env = append(cmdDesc.command.Env, entry)
146+
}
147+
130148
return c
131149
}
132150

@@ -148,6 +166,18 @@ func (c *chain) WithAdditionalEnvironment(envMap ...interface{}) CommandBuilder
148166
return c.WithEnvironment(envMap...)
149167
}
150168

169+
func (c *chain) WithAdditionalEnvironmentPairs(envMap ...string) CommandBuilder {
170+
cmdDesc := c.cmdDescriptors[len(c.cmdDescriptors)-1]
171+
pairs := cmdDesc.command.Env
172+
173+
if len(pairs) == 0 {
174+
pairs = os.Environ()
175+
}
176+
pairs = append(pairs, envMap...)
177+
178+
return c.WithEnvironmentPairs(pairs...)
179+
}
180+
151181
func (c *chain) WithWorkingDirectory(workingDir string) CommandBuilder {
152182
cmdDesc := c.cmdDescriptors[len(c.cmdDescriptors)-1]
153183
cmdDesc.command.Dir = workingDir

chain_test.go

+40
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,19 @@ func TestSimple_WithMultiInput(t *testing.T) {
292292
runAndCompare(t, toTest, "2\n")
293293
}
294294

295+
func TestSimple_WithProcessEnvironment(t *testing.T) {
296+
chainWithEnv := Builder().Join(testHelper, "-pe")
297+
chainWithoutEnv := Builder().Join(testHelper, "-pe").WithEmptyEnvironment()
298+
299+
out1, _, err := chainWithEnv.Finalize().RunAndGet()
300+
assert.NoError(t, err)
301+
302+
out2, _, err := chainWithoutEnv.Finalize().RunAndGet()
303+
assert.NoError(t, err)
304+
305+
assert.NotEqual(t, out1, out2)
306+
}
307+
295308
func TestSimple_WithEnvironment(t *testing.T) {
296309
toTest := Builder().
297310
Join(testHelper, "-pe").WithEnvironment("TEST", "VALUE", "TEST2", 2)
@@ -307,6 +320,14 @@ func TestSimple_WithEnvironmentMap(t *testing.T) {
307320
runAndCompare(t, toTest, "GO_COMMAND_CHAIN_TEST2=2\nGO_COMMAND_CHAIN_TEST=VALUE\n")
308321
}
309322

323+
func TestSimple_WithEnvironmentPairs(t *testing.T) {
324+
toTest := Builder().
325+
Join(testHelper, "-pe").WithEnvironmentPairs("GO_COMMAND_CHAIN_TEST=VALUE", "GO_COMMAND_CHAIN_TEST2=2").
326+
Join("sort")
327+
328+
runAndCompare(t, toTest, "GO_COMMAND_CHAIN_TEST2=2\nGO_COMMAND_CHAIN_TEST=VALUE\n")
329+
}
330+
310331
func TestSimple_WithAdditionalEnvironment(t *testing.T) {
311332
toTest := Builder().
312333
Join(testHelper, "-pe").WithAdditionalEnvironment("GO_COMMAND_CHAIN_TEST", "VALUE", "GO_COMMAND_CHAIN_TEST2", 2).
@@ -325,6 +346,15 @@ func TestSimple_WithAdditionalEnvironmentMap(t *testing.T) {
325346
runAndCompare(t, toTest, "GO_COMMAND_CHAIN_TEST2=2\nGO_COMMAND_CHAIN_TEST=VALUE\n")
326347
}
327348

349+
func TestSimple_WithAdditionalEnvironmentPairs(t *testing.T) {
350+
toTest := Builder().
351+
Join(testHelper, "-pe").WithAdditionalEnvironmentPairs("GO_COMMAND_CHAIN_TEST=VALUE", "GO_COMMAND_CHAIN_TEST2=2").
352+
Join("grep", "GO_COMMAND_CHAIN_TEST").
353+
Join("sort")
354+
355+
runAndCompare(t, toTest, "GO_COMMAND_CHAIN_TEST2=2\nGO_COMMAND_CHAIN_TEST=VALUE\n")
356+
}
357+
328358
func TestSimple_WithAdditionalEnvironment_butNotProcessEnv(t *testing.T) {
329359
cmd := exec.Command(testHelper, "-pe")
330360
cmd.Env = []string{"TEST=VALUE"}
@@ -345,6 +375,16 @@ func TestSimple_WithAdditionalEnvironmentMap_butNotProcessEnv(t *testing.T) {
345375
runAndCompare(t, toTest, "TEST=VALUE\nTEST2=2\n")
346376
}
347377

378+
func TestSimple_WithAdditionalEnvironmentPairs_butNotProcessEnv(t *testing.T) {
379+
cmd := exec.Command(testHelper, "-pe")
380+
cmd.Env = []string{"TEST=VALUE"}
381+
382+
toTest := Builder().
383+
JoinCmd(cmd).WithAdditionalEnvironmentPairs("TEST2=2")
384+
385+
runAndCompare(t, toTest, "TEST=VALUE\nTEST2=2\n")
386+
}
387+
348388
func TestSimple_WithEnvironment_InvalidArguments(t *testing.T) {
349389
err := Builder().
350390
Join(testHelper, "-pe").WithEnvironment("TEST", "VALUE", "TEST2").

interfaces.go

+12
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,10 @@ type CommandBuilder interface {
109109
// If this behavior is not wanted, me the io.MultiReader is a better choice.
110110
WithInjections(sources ...io.Reader) CommandBuilder
111111

112+
// WithEmptyEnvironment will use an empty environment for the previously joined command. The default behavior is to
113+
// use the current process's environment.
114+
WithEmptyEnvironment() CommandBuilder
115+
112116
// WithEnvironment will configure the previously joined command to use the given environment variables. Key-value
113117
// pair(s) must be passed as arguments. Where the first represents the key and the second the value of the
114118
// environment variable.
@@ -117,6 +121,10 @@ type CommandBuilder interface {
117121
// WithEnvironmentMap will configure the previously joined command to use the given environment variables.
118122
WithEnvironmentMap(envMap map[interface{}]interface{}) CommandBuilder
119123

124+
// WithEnvironmentPairs will configure the previously joined command to use the given environment variables.
125+
// Each entry must have the form "key=value"
126+
WithEnvironmentPairs(envMap ...string) CommandBuilder
127+
120128
// WithAdditionalEnvironment will do almost the same thing as WithEnvironment expecting that the given key-value
121129
// pairs will be joined with the environment variables of the current process.
122130
WithAdditionalEnvironment(envMap ...interface{}) CommandBuilder
@@ -125,6 +133,10 @@ type CommandBuilder interface {
125133
// values will be joined with the environment variables of the current process.
126134
WithAdditionalEnvironmentMap(envMap map[interface{}]interface{}) CommandBuilder
127135

136+
// WithAdditionalEnvironmentPairs will do almost the same thing as WithEnvironmentPairs expecting that the given
137+
// values will be joined with the environment variables of the current process.
138+
WithAdditionalEnvironmentPairs(envMap ...string) CommandBuilder
139+
128140
// WithWorkingDirectory will configure the previously joined command to use the specifies the working directory.
129141
// Without setting the working directory, the calling process's current directory will be used.
130142
WithWorkingDirectory(workingDir string) CommandBuilder

0 commit comments

Comments
 (0)