Skip to content

Commit 26ec2b5

Browse files
committed
Add runtine and validation tests
1 parent 6cf2d5f commit 26ec2b5

9 files changed

Lines changed: 267 additions & 109 deletions

pkg/app/app.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ const (
1313
type AddCommandContext struct {
1414
Verbose bool
1515
NoColor bool
16-
Debug bool
1716
Concurrent int
1817
}
1918

pkg/app/app_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package app
2+
3+
import (
4+
"flag"
5+
"github.com/stretchr/testify/assert"
6+
"github.com/urfave/cli"
7+
"testing"
8+
)
9+
10+
func TestNewAddCommandContextFromCli(t *testing.T) {
11+
set := flag.NewFlagSet("verbose", 0)
12+
set.Bool("verbose", true, "")
13+
set.Bool("no-color", true, "")
14+
set.Int("concurrent", 5, "")
15+
16+
context := &cli.Context{}
17+
ctx := cli.NewContext(nil, set, context)
18+
19+
r := NewAddContextFromCli(ctx)
20+
21+
assert.True(t, r.Verbose)
22+
assert.True(t, r.NoColor)
23+
assert.Equal(t, 5, r.Concurrent)
24+
}

pkg/app/test_command_test.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,30 @@
11
package app
22

33
import (
4+
"bytes"
45
"github.com/stretchr/testify/assert"
6+
"io"
7+
"log"
8+
"os"
59
"runtime"
10+
"sync"
611
"testing"
712
)
813

14+
func Test_TestCommand_Verbose(t *testing.T) {
15+
out := captureOutput(func() {
16+
TestCommand("commander.yaml", "", AddCommandContext{Verbose: true})
17+
log.Println("test test test")
18+
})
19+
20+
assert.Contains(t, out, "test test test")
21+
}
22+
23+
func Test_TestCommand_DefaultFile(t *testing.T) {
24+
err := TestCommand("", "", AddCommandContext{Verbose: true})
25+
assert.Contains(t, err.Error(), "commander.yaml")
26+
}
27+
928
func Test_TestCommand(t *testing.T) {
1029
err := TestCommand("commander.yaml", "", AddCommandContext{})
1130

@@ -25,3 +44,33 @@ func Test_TestCommand_ShouldUseCustomFile(t *testing.T) {
2544
assert.Equal(t, "Error open my-test.yaml: no such file or directory", err.Error())
2645
}
2746
}
47+
48+
func captureOutput(f func()) string {
49+
reader, writer, err := os.Pipe()
50+
if err != nil {
51+
panic(err)
52+
}
53+
stdout := os.Stdout
54+
stderr := os.Stderr
55+
defer func() {
56+
os.Stdout = stdout
57+
os.Stderr = stderr
58+
log.SetOutput(os.Stderr)
59+
}()
60+
os.Stdout = writer
61+
os.Stderr = writer
62+
log.SetOutput(writer)
63+
out := make(chan string)
64+
wg := new(sync.WaitGroup)
65+
wg.Add(1)
66+
go func() {
67+
var buf bytes.Buffer
68+
wg.Done()
69+
io.Copy(&buf, reader)
70+
out <- buf.String()
71+
}()
72+
wg.Wait()
73+
f()
74+
writer.Close()
75+
return <-out
76+
}

pkg/runtime/runtime.go

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -199,22 +199,13 @@ func runTest(test TestCase) TestResult {
199199
Stderr: strings.TrimSpace(strings.Replace(cut.Stderr(), "\r\n", "\n", -1)),
200200
}
201201

202-
log.Println("title: '" + test.Title + "'", " ExitCode: ", test.Result.ExitCode)
203-
log.Println("title: '" + test.Title + "'", " Stdout: ", test.Result.Stdout)
204-
log.Println("title: '" + test.Title + "'", " Stderr: ", test.Result.Stderr)
202+
log.Println("title: '"+test.Title+"'", " ExitCode: ", test.Result.ExitCode)
203+
log.Println("title: '"+test.Title+"'", " Stdout: ", test.Result.Stdout)
204+
log.Println("title: '"+test.Title+"'", " Stderr: ", test.Result.Stderr)
205205

206206
return Validate(test)
207207
}
208208

209-
// trimSpace implementation to trim CLRF off
210-
func trimSpace(s string) string {
211-
result := strings.TrimSpace(s)
212-
if runtime.GOOS == "windows" {
213-
return strings.Trim(s, "\r\n")
214-
}
215-
return result
216-
}
217-
218209
func createEnvVarsOption(test TestCase) func(c *cmd.Command) {
219210
return func(c *cmd.Command) {
220211
// Add all env variables from parent process

pkg/runtime/runtime_darwin_test.go

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,44 @@
11
package runtime
22

33
import (
4-
"github.com/stretchr/testify/assert"
5-
"os"
6-
"testing"
4+
"github.com/stretchr/testify/assert"
5+
"os"
6+
"testing"
77
)
88

99
func TestRuntime_WithInheritFromShell(t *testing.T) {
10-
os.Setenv("TEST_COMMANDER", "test")
11-
defer os.Unsetenv("TEST_COMMANDER")
10+
os.Setenv("TEST_COMMANDER", "test")
11+
defer os.Unsetenv("TEST_COMMANDER")
1212

13-
test := TestCase{
14-
Command: CommandUnderTest{
15-
Cmd: "echo $TEST_COMMANDER",
16-
InheritEnv: true,
17-
},
18-
}
13+
test := TestCase{
14+
Command: CommandUnderTest{
15+
Cmd: "echo $TEST_COMMANDER",
16+
InheritEnv: true,
17+
},
18+
}
1919

20-
got := runTest(test)
20+
got := runTest(test)
2121

22-
assert.Equal(t, "test", got.TestCase.Result.Stdout)
22+
assert.Equal(t, "test", got.TestCase.Result.Stdout)
2323
}
2424

2525
func TestRuntime_WithInheritFromShell_Overwrite(t *testing.T) {
26-
os.Setenv("TEST_COMMANDER", "test")
27-
os.Setenv("ANOTHER_ENV", "from-parent")
28-
defer func() {
29-
os.Unsetenv("TEST_COMMANDER")
30-
os.Unsetenv("ANOTHER_ENV")
31-
}()
32-
33-
test := TestCase{
34-
Command: CommandUnderTest{
35-
Cmd: "echo $TEST_COMMANDER $ANOTHER_ENV",
36-
InheritEnv: true,
37-
Env: map[string]string{"TEST_COMMANDER": "overwrite"},
38-
},
39-
}
40-
41-
got := runTest(test)
42-
43-
assert.Equal(t, "overwrite from-parent", got.TestCase.Result.Stdout)
44-
}
26+
os.Setenv("TEST_COMMANDER", "test")
27+
os.Setenv("ANOTHER_ENV", "from-parent")
28+
defer func() {
29+
os.Unsetenv("TEST_COMMANDER")
30+
os.Unsetenv("ANOTHER_ENV")
31+
}()
32+
33+
test := TestCase{
34+
Command: CommandUnderTest{
35+
Cmd: "echo $TEST_COMMANDER $ANOTHER_ENV",
36+
InheritEnv: true,
37+
Env: map[string]string{"TEST_COMMANDER": "overwrite"},
38+
},
39+
}
40+
41+
got := runTest(test)
42+
43+
assert.Equal(t, "overwrite from-parent", got.TestCase.Result.Stdout)
44+
}

pkg/runtime/runtime_linux_test.go

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,44 @@
11
package runtime
22

33
import (
4-
"github.com/stretchr/testify/assert"
5-
"os"
6-
"testing"
4+
"github.com/stretchr/testify/assert"
5+
"os"
6+
"testing"
77
)
88

99
func TestRuntime_WithInheritFromShell(t *testing.T) {
10-
os.Setenv("TEST_COMMANDER", "test")
11-
defer os.Unsetenv("TEST_COMMANDER")
10+
os.Setenv("TEST_COMMANDER", "test")
11+
defer os.Unsetenv("TEST_COMMANDER")
1212

13-
test := TestCase{
14-
Command: CommandUnderTest{
15-
Cmd: "echo $TEST_COMMANDER",
16-
InheritEnv: true,
17-
},
18-
}
13+
test := TestCase{
14+
Command: CommandUnderTest{
15+
Cmd: "echo $TEST_COMMANDER",
16+
InheritEnv: true,
17+
},
18+
}
1919

20-
got := runTest(test)
20+
got := runTest(test)
2121

22-
assert.Equal(t, "test", got.TestCase.Result.Stdout)
22+
assert.Equal(t, "test", got.TestCase.Result.Stdout)
2323
}
2424

2525
func TestRuntime_WithInheritFromShell_Overwrite(t *testing.T) {
26-
os.Setenv("TEST_COMMANDER", "test")
27-
os.Setenv("ANOTHER_ENV", "from-parent")
28-
defer func() {
29-
os.Unsetenv("TEST_COMMANDER")
30-
os.Unsetenv("ANOTHER_ENV")
31-
}()
32-
33-
test := TestCase{
34-
Command: CommandUnderTest{
35-
Cmd: "echo $TEST_COMMANDER $ANOTHER_ENV",
36-
InheritEnv: true,
37-
Env: map[string]string{"TEST_COMMANDER": "overwrite"},
38-
},
39-
}
40-
41-
got := runTest(test)
42-
43-
assert.Equal(t, "overwrite from-parent", got.TestCase.Result.Stdout)
44-
}
26+
os.Setenv("TEST_COMMANDER", "test")
27+
os.Setenv("ANOTHER_ENV", "from-parent")
28+
defer func() {
29+
os.Unsetenv("TEST_COMMANDER")
30+
os.Unsetenv("ANOTHER_ENV")
31+
}()
32+
33+
test := TestCase{
34+
Command: CommandUnderTest{
35+
Cmd: "echo $TEST_COMMANDER $ANOTHER_ENV",
36+
InheritEnv: true,
37+
Env: map[string]string{"TEST_COMMANDER": "overwrite"},
38+
},
39+
}
40+
41+
got := runTest(test)
42+
43+
assert.Equal(t, "overwrite from-parent", got.TestCase.Result.Stdout)
44+
}

pkg/runtime/runtime_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func TestRuntime_WithRetriesAndInterval(t *testing.T) {
4949
s[0].Command.Interval = "50ms"
5050

5151
start := time.Now()
52-
got := Start(s, 1)
52+
got := Start(s, 0)
5353

5454
var counter = 0
5555
for r := range got {
Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,44 @@
11
package runtime
22

33
import (
4-
"github.com/stretchr/testify/assert"
5-
"os"
6-
"testing"
4+
"github.com/stretchr/testify/assert"
5+
"os"
6+
"testing"
77
)
88

99
func TestRuntime_WithInheritFromShell(t *testing.T) {
10-
os.Setenv("TEST_COMMANDER", "test")
11-
defer os.Unsetenv("TEST_COMMANDER")
10+
os.Setenv("TEST_COMMANDER", "test")
11+
defer os.Unsetenv("TEST_COMMANDER")
1212

13-
test := TestCase{
14-
Command: CommandUnderTest{
15-
Cmd: "echo %TEST_COMMANDER%",
16-
InheritEnv: true,
17-
},
18-
}
13+
test := TestCase{
14+
Command: CommandUnderTest{
15+
Cmd: "echo %TEST_COMMANDER%",
16+
InheritEnv: true,
17+
},
18+
}
1919

20-
got := runTest(test)
20+
got := runTest(test)
2121

22-
assert.Equal(t, "test", got.TestCase.Result.Stdout)
22+
assert.Equal(t, "test", got.TestCase.Result.Stdout)
2323
}
2424

2525
func TestRuntime_WithInheritFromShell_Overwrite(t *testing.T) {
26-
os.Setenv("TEST_COMMANDER", "test")
27-
os.Setenv("ANOTHER_ENV", "from-parent")
28-
defer func() {
29-
os.Unsetenv("TEST_COMMANDER")
30-
os.Unsetenv("ANOTHER_ENV")
31-
}()
32-
33-
test := TestCase{
34-
Command: CommandUnderTest{
35-
Cmd: "echo %TEST_COMMANDER% %ANOTHER_ENV%",
36-
InheritEnv: true,
37-
Env: map[string]string{"TEST_COMMANDER": "overwrite"},
38-
},
39-
}
40-
41-
got := runTest(test)
42-
43-
assert.Equal(t, "overwrite from-parent", got.TestCase.Result.Stdout)
26+
os.Setenv("TEST_COMMANDER", "test")
27+
os.Setenv("ANOTHER_ENV", "from-parent")
28+
defer func() {
29+
os.Unsetenv("TEST_COMMANDER")
30+
os.Unsetenv("ANOTHER_ENV")
31+
}()
32+
33+
test := TestCase{
34+
Command: CommandUnderTest{
35+
Cmd: "echo %TEST_COMMANDER% %ANOTHER_ENV%",
36+
InheritEnv: true,
37+
Env: map[string]string{"TEST_COMMANDER": "overwrite"},
38+
},
39+
}
40+
41+
got := runTest(test)
42+
43+
assert.Equal(t, "overwrite from-parent", got.TestCase.Result.Stdout)
4444
}

0 commit comments

Comments
 (0)