-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathcourtney_test.go
196 lines (169 loc) · 4.06 KB
/
courtney_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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package main
import (
"fmt"
"testing"
"io/ioutil"
"path/filepath"
"bytes"
"strings"
"os"
"github.com/dave/courtney/shared"
"github.com/dave/patsy"
"github.com/dave/patsy/builder"
"github.com/dave/patsy/vos"
)
func TestRun(t *testing.T) {
for _, gomod := range []bool{true, false} {
t.Run(fmt.Sprintf("gomod=%v", gomod), func(t *testing.T) {
name := "run"
env := vos.Mock()
b, err := builder.New(env, "ns", gomod)
if err != nil {
t.Fatalf("Error creating builder in %s: %s", name, err)
}
defer b.Cleanup()
_, pdir, err := b.Package("a", map[string]string{
"a.go": `package a
func Foo(i int) int {
i++
return i
}
func Bar(i int) int {
i++
return i
}
`,
"a_test.go": `package a
import "testing"
func TestFoo(t *testing.T) {
i := Foo(1)
if i != 2 {
t.Fail()
}
}
`,
})
if err != nil {
t.Fatalf("Error creating builder in %s: %s", name, err)
}
if err := env.Setwd(pdir); err != nil {
t.Fatalf("Error in Setwd in %s: %s", name, err)
}
sout := &bytes.Buffer{}
serr := &bytes.Buffer{}
env.Setstdout(sout)
env.Setstderr(serr)
setup := &shared.Setup{
Env: env,
Paths: patsy.NewCache(env),
Enforce: true,
Verbose: true,
}
err = Run(setup)
if err == nil {
t.Fatalf("Error in %s. Run should error.", name)
}
expected := `Error - untested code:
ns/a/a.go:8-11:
func Bar(i int) int {
i++
return i
}`
if !strings.Contains(err.Error(), expected) {
t.Fatalf("Error in %s err. Got: \n%s\nExpected to contain: \n%s\n", name, err.Error(), expected)
}
coverage, err := ioutil.ReadFile(filepath.Join(pdir, "coverage.out"))
if err != nil {
t.Fatalf("Error reading coverage file in %s: %s", name, err)
}
expected = `mode: set
ns/a/a.go:3.24,6.5 2 1
ns/a/a.go:8.24,11.5 2 0
`
if string(coverage) != expected {
t.Fatalf("Error in %s coverage. Got: \n%s\nExpected: \n%s\n", name, string(coverage), expected)
}
setup = &shared.Setup{
Env: env,
Paths: patsy.NewCache(env),
}
if err := Run(setup); err != nil {
t.Fatalf("Error running program (second try) in %s: %s", name, err)
}
})
}
}
func TestRun_load(t *testing.T) {
for _, gomod := range []bool{true, false} {
t.Run(fmt.Sprintf("gomod=%v", gomod), func(t *testing.T) {
name := "load"
env := vos.Mock()
b, err := builder.New(env, "ns", gomod)
if err != nil {
t.Fatalf("Error creating builder in %s: %s", name, err)
}
defer b.Cleanup()
_, pdir, err := b.Package("a", map[string]string{
"a.go": `package a
func Foo(i int) int {
i++
return i
}
func Bar(i int) int {
i++
return i
}
`,
"a_test.go": `package a
import "testing"
func TestFoo(t *testing.T) {
// In "load" mode, this test will not run.
t.Fail()
}
`,
"a.out": `mode: set
ns/a/a.go:3.24,6.5 2 1
ns/a/a.go:8.24,11.5 2 0
`,
"b.out": `mode: set
ns/a/a.go:3.24,6.5 2 0
ns/a/a.go:8.24,11.5 2 1
`,
})
if err != nil {
t.Fatalf("Error creating builder in %s: %s", name, err)
}
if err := env.Setwd(pdir); err != nil {
t.Fatalf("Error in Setwd in %s: %s", name, err)
}
// annoyingly, filepath.Glob in "Load" method does not respect the mocked
// vos working directory
if err := os.Chdir(pdir); err != nil {
t.Fatalf("Error in os.Chdir in %s: %s", name, err)
}
sout := &bytes.Buffer{}
serr := &bytes.Buffer{}
env.Setstdout(sout)
env.Setstderr(serr)
setup := &shared.Setup{
Env: env,
Paths: patsy.NewCache(env),
Load: "*.out",
}
if err := Run(setup); err != nil {
t.Fatalf("Error running program in %s: %s", name, err)
}
coverage, err := ioutil.ReadFile(filepath.Join(pdir, "coverage.out"))
if err != nil {
t.Fatalf("Error reading coverage file in %s: %s", name, err)
}
expected := `mode: set
ns/a/a.go:3.24,6.5 2 1
ns/a/a.go:8.24,11.5 2 1
`
if string(coverage) != expected {
t.Fatalf("Error in %s coverage. Got: \n%s\nExpected: \n%s\n", name, string(coverage), expected)
}
})
}
}