Skip to content

Commit 631fe7e

Browse files
committed
Fix tests
1 parent de29bb4 commit 631fe7e

File tree

2 files changed

+51
-48
lines changed

2 files changed

+51
-48
lines changed

command_linux_test.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package cmd
22

33
import (
4+
"bytes"
45
"github.com/stretchr/testify/assert"
56
"io/ioutil"
67
"os"
@@ -81,3 +82,52 @@ func TestCommand_WithInvalidDir(t *testing.T) {
8182
assert.NotNil(t, err)
8283
assert.Equal(t, "chdir /invalid: no such file or directory", err.Error())
8384
}
85+
86+
func TestWithInheritedEnvironment(t *testing.T) {
87+
os.Setenv("FROM_OS", "is on os")
88+
os.Setenv("OVERWRITE", "is on os but should be overwritten")
89+
defer func() {
90+
os.Unsetenv("FROM_OS")
91+
os.Unsetenv("OVERWRITE")
92+
}()
93+
94+
c := NewCommand(
95+
"echo $FROM_OS $OVERWRITE",
96+
WithInheritedEnvironment(map[string]string{"OVERWRITE": "overwritten"}))
97+
c.Execute()
98+
99+
assertEqualWithLineBreak(t, "is on os overwritten", c.Stdout())
100+
}
101+
102+
func TestWithCustomStderr(t *testing.T) {
103+
writer := bytes.Buffer{}
104+
c := NewCommand(">&2 echo stderr; sleep 0.01; echo stdout;", WithCustomStderr(&writer))
105+
c.Execute()
106+
107+
assertEqualWithLineBreak(t, "stderr", writer.String())
108+
assertEqualWithLineBreak(t, "stdout", c.Stdout())
109+
assertEqualWithLineBreak(t, "stderr", c.Stderr())
110+
assertEqualWithLineBreak(t, "stderr\nstdout", c.Combined())
111+
}
112+
113+
func TestWithCustomStdout(t *testing.T) {
114+
writer := bytes.Buffer{}
115+
c := NewCommand(">&2 echo stderr; sleep 0.01; echo stdout;", WithCustomStdout(&writer))
116+
c.Execute()
117+
118+
assertEqualWithLineBreak(t, "stdout", writer.String())
119+
assertEqualWithLineBreak(t, "stdout", c.Stdout())
120+
assertEqualWithLineBreak(t, "stderr", c.Stderr())
121+
assertEqualWithLineBreak(t, "stderr\nstdout", c.Combined())
122+
}
123+
124+
func TestWithStandardStreams(t *testing.T) {
125+
out, err := CaptureStandardOutput(func() interface{} {
126+
c := NewCommand(">&2 echo stderr; sleep 0.01; echo stdout;", WithStandardStreams)
127+
err := c.Execute()
128+
return err
129+
})
130+
131+
assertEqualWithLineBreak(t, "stderr\nstdout", out)
132+
assert.Nil(t, err)
133+
}

command_test.go

Lines changed: 1 addition & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -127,56 +127,9 @@ func TestCommand_SetOptions(t *testing.T) {
127127
assertEqualWithLineBreak(t, "test", writer.String())
128128
}
129129

130-
func TestWithCustomStderr(t *testing.T) {
131-
writer := bytes.Buffer{}
132-
c := NewCommand(">&2 echo stderr; sleep 0.01; echo stdout;", WithCustomStderr(&writer))
133-
c.Execute()
134-
135-
assertEqualWithLineBreak(t, "stderr", writer.String())
136-
assertEqualWithLineBreak(t, "stdout", c.Stdout())
137-
assertEqualWithLineBreak(t, "stderr", c.Stderr())
138-
assertEqualWithLineBreak(t, "stderr\nstdout", c.Combined())
139-
}
140-
141-
func TestWithCustomStdout(t *testing.T) {
142-
writer := bytes.Buffer{}
143-
c := NewCommand(">&2 echo stderr; sleep 0.01; echo stdout;", WithCustomStdout(&writer))
144-
c.Execute()
145-
146-
assertEqualWithLineBreak(t, "stdout", writer.String())
147-
assertEqualWithLineBreak(t, "stdout", c.Stdout())
148-
assertEqualWithLineBreak(t, "stderr", c.Stderr())
149-
assertEqualWithLineBreak(t, "stderr\nstdout", c.Combined())
150-
}
151-
152-
func TestWithStandardStreams(t *testing.T) {
153-
out, err := CaptureStandardOutput(func() interface{} {
154-
c := NewCommand(">&2 echo stderr; sleep 0.01; echo stdout;", WithStandardStreams)
155-
err := c.Execute()
156-
return err
157-
})
158-
159-
assertEqualWithLineBreak(t, "stderr\nstdout", out)
160-
assert.Nil(t, err)
161-
}
162-
163130
func TestWithEnvironmentVariables(t *testing.T) {
164131
c := NewCommand("echo $env", WithEnvironmentVariables(map[string]string{"env": "value"}))
165132
c.Execute()
166133

167-
assert.Equal(t, "value", c.Stdout())
168-
}
169-
170-
func TestWithInheritedEnvironment(t *testing.T) {
171-
os.Setenv("FROM_OS", "is on os")
172-
os.Setenv("OVERWRITE", "is on os but should be overwritten")
173-
defer func() {
174-
os.Unsetenv("FROM_OS")
175-
os.Unsetenv("OVERWRITE")
176-
}()
177-
178-
c := NewCommand("echo $FROM_OS $OVERWRITE", WithInheritedEnvironment(map[string]string{"OVERWRITE": "overwritten"}))
179-
c.Execute()
180-
181-
assertEqualWithLineBreak(t, "is on os overwritten", c.Stdout())
134+
assertEqualWithLineBreak(t, "value", c.Stdout())
182135
}

0 commit comments

Comments
 (0)