-
Notifications
You must be signed in to change notification settings - Fork 23
/
command_test.go
132 lines (102 loc) · 2.79 KB
/
command_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package cmd
import (
"bytes"
"fmt"
"os"
"runtime"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestCommand_NewCommand(t *testing.T) {
cmd := NewCommand("echo hello")
cmd.Execute()
assertEqualWithLineBreak(t, "hello", cmd.Combined())
assertEqualWithLineBreak(t, "hello", cmd.Stdout())
}
func TestCommand_Execute(t *testing.T) {
cmd := NewCommand("echo hello")
err := cmd.Execute()
assert.Nil(t, err)
assert.True(t, cmd.Executed())
assertEqualWithLineBreak(t, "hello", cmd.Stdout())
}
func TestCommand_ExitCode(t *testing.T) {
cmd := NewCommand("exit 120")
err := cmd.Execute()
assert.Nil(t, err)
assert.Equal(t, 120, cmd.ExitCode())
}
func TestCommand_WithEnvVariables(t *testing.T) {
envVar := "$TEST"
if runtime.GOOS == "windows" {
envVar = "%TEST%"
}
cmd := NewCommand(fmt.Sprintf("echo %s", envVar))
cmd.Env = []string{"TEST=hey"}
_ = cmd.Execute()
assertEqualWithLineBreak(t, "hey", cmd.Stdout())
}
func TestCommand_Executed(t *testing.T) {
defer func() {
r := recover()
if r != nil {
assert.Contains(t, r, "Can not read Stdout if command was not executed")
}
assert.NotNil(t, r)
}()
c := NewCommand("echo will not be executed")
_ = c.Stdout()
}
func TestCommand_AddEnv(t *testing.T) {
c := NewCommand("echo test")
c.AddEnv("key", "value")
assert.Equal(t, []string{"key=value"}, c.Env)
}
func TestCommand_AddEnvWithShellVariable(t *testing.T) {
const TestEnvKey = "COMMANDER_TEST_SOME_KEY"
os.Setenv(TestEnvKey, "test from shell")
defer os.Unsetenv(TestEnvKey)
c := NewCommand(getCommand())
c.AddEnv("SOME_KEY", fmt.Sprintf("${%s}", TestEnvKey))
err := c.Execute()
assert.Nil(t, err)
assertEqualWithLineBreak(t, "test from shell", c.Stdout())
}
func TestCommand_AddMultipleEnvWithShellVariable(t *testing.T) {
const TestEnvKeyPlanet = "CMD_TEST_PLANET"
const TestEnvKeyName = "CMD_TEST_NAME"
os.Setenv(TestEnvKeyPlanet, "world")
os.Setenv(TestEnvKeyName, "Simon")
defer func() {
os.Unsetenv(TestEnvKeyPlanet)
os.Unsetenv(TestEnvKeyName)
}()
c := NewCommand(getCommand())
envValue := fmt.Sprintf("Hello ${%s}, I am ${%s}", TestEnvKeyPlanet, TestEnvKeyName)
c.AddEnv("SOME_KEY", envValue)
err := c.Execute()
assert.Nil(t, err)
assertEqualWithLineBreak(t, "Hello world, I am Simon", c.Stdout())
}
func getCommand() string {
command := "echo $SOME_KEY"
if runtime.GOOS == "windows" {
command = "echo %SOME_KEY%"
}
return command
}
func TestCommand_SetOptions(t *testing.T) {
writer := &bytes.Buffer{}
setWriter := func(c *Command) {
c.StdoutWriter = writer
}
setTimeout := func(c *Command) {
c.Timeout = 1 * time.Second
}
c := NewCommand("echo test", setTimeout, setWriter)
err := c.Execute()
assert.Nil(t, err)
assert.Equal(t, time.Duration(1000000000), c.Timeout)
assertEqualWithLineBreak(t, "test", writer.String())
}