-
Notifications
You must be signed in to change notification settings - Fork 0
/
app_test.go
124 lines (115 loc) · 2.89 KB
/
app_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
package gowatch
import (
"fmt"
"os"
"testing"
)
func TestStartApp(t *testing.T) {
pwd, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
w := AppRunner{
dir: fmt.Sprintf("%s/testdata/helloworld", pwd),
binaryName: fmt.Sprintf("%s/testdata/helloworld/helloworld", pwd),
}
if err := w.Compile(); err != nil {
t.Fatal(err)
}
if _, err := w.Start(); err != nil {
t.Errorf(unexpectedErrorMsg, err)
}
}
func TestCompileApp(t *testing.T) {
pwd, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
w := AppRunner{
dir: fmt.Sprintf("%s/testdata/helloworld", pwd),
binaryName: fmt.Sprintf("%s/testdata/helloworld/helloworld", pwd),
}
if err := w.Compile(); err != nil {
t.Errorf(unexpectedErrorMsg, err)
}
}
func TestCompileAppWithFlags(t *testing.T) {
pwd, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
w := AppRunner{
dir: fmt.Sprintf("%s/testdata/helloworld", pwd),
binaryName: fmt.Sprintf("%s/testdata/helloworld/helloworld", pwd),
buildFlags: []string{"-x", "-v"},
}
if err := w.Compile(); err != nil {
t.Errorf(unexpectedErrorMsg, err)
}
}
func TestRestartApp(t *testing.T) {
w := AppRunner{
binaryName: "http-server",
dir: "./testdata/http-server",
}
if err := w.Compile(); err != nil {
t.Fatal(err)
}
cmd := cmdRunBinary(w.dir, w.binaryName, w.runFlags...)
if err := cmd.Start(); err != nil {
t.Fatal(err)
}
if err := w.Restart(cmd); err != nil {
t.Fatalf(unexpectedErrorMsg, err)
}
if err := cmd.Process.Kill(); err != nil {
t.Fatal(err)
}
}
func TestCmdRunBinary(t *testing.T) {
dir := "/home/unittest/gowatch/testcase"
binaryName := "testcase"
pathExpected := fmt.Sprintf("./%s", binaryName)
cmd := cmdRunBinary(dir, binaryName)
if cmd.Path != pathExpected {
t.Errorf(assertErrorMsg, pathExpected, cmd.Path)
}
}
func TestCmdRunBinaryPrefix(t *testing.T) {
dir := "/home/unittest/gowatch/testcase"
binaryName := "/home/unittest/gowatch/testcase/testcase"
cmd := cmdRunBinary(dir, binaryName)
if cmd.Path != binaryName {
t.Errorf(assertErrorMsg, binaryName, cmd.Path)
}
}
func TestCmdRunBase(t *testing.T) {
dir := "/home/unittest/gowatch/testcase"
command := "testcase"
args := []string{"unittest", "--foobar"}
argsExpected := []string{"testcase", "unittest", "--foobar"}
cmd := newCmd(dir, command, args...)
if cmd.Dir != dir {
t.Errorf(assertErrorMsg, dir, cmd.Dir)
}
if cmd.Path != command {
t.Errorf(assertErrorMsg, command, cmd.Path)
}
if cmd.Stderr != os.Stderr {
t.Errorf(assertErrorMsg, "os.Stderr", cmd.Stderr)
}
if cmd.Stdout != os.Stdout {
t.Errorf(assertErrorMsg, "os.Stdout", cmd.Stdout)
}
if cmd.Stdin != os.Stdin {
t.Errorf(assertErrorMsg, "os.Stdin", cmd.Stdin)
}
if len(cmd.Args) != len(argsExpected) {
t.Fatalf(assertErrorMsg, len(argsExpected), len(cmd.Args))
}
for i, arg := range cmd.Args {
if arg != argsExpected[i] {
t.Errorf(assertErrorMsg, argsExpected[i], arg)
}
}
}