-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmain_test.go
119 lines (102 loc) · 3.58 KB
/
main_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
package main_test
import (
"os"
"os/exec"
"path"
"strings"
"testing"
"time"
)
func FuzzBadFile(f *testing.F) {
testbin := path.Join(f.TempDir(), "go-instrument-testbin")
exec.Command("go", "build", "-cover", "-o", testbin, "main.go").Run()
f.Fuzz(func(t *testing.T, orig string) {
t.Run("when bad go file, then error", func(t *testing.T) {
fname := path.Join(t.TempDir(), "fuzz-test-file.go")
os.WriteFile(fname, []byte(orig), 0644)
cmd := exec.Command(testbin, "-app", "app", "-w", "-filename", fname)
cmd.Env = append(cmd.Environ(), "GOCOVERDIR=./coverage")
if err := cmd.Run(); err == nil {
t.Fatal(err)
}
})
})
}
func TestApp(t *testing.T) {
testbin := path.Join(t.TempDir(), "go-instrument-testbin")
exec.Command("go", "build", "-cover", "-o", testbin, "main.go").Run()
t.Run("when basic, then ok", func(t *testing.T) {
f := copyFile(t, "./internal/testdata/basic.go")
cmd := exec.Command(testbin, "-app", "app", "-w", "-filename", f)
cmd.Env = append(cmd.Environ(), "GOCOVERDIR=./coverage")
if err := cmd.Run(); err != nil {
t.Errorf(err.Error())
}
assertEqFile(t, "./internal/testdata/instrumented/basic.go.exp", f)
})
t.Run("when include only, then ok", func(t *testing.T) {
f := copyFile(t, "./internal/testdata/basic_include_only.go")
cmd := exec.Command(testbin, "-app", "app", "-w", "-all=false", "-filename", f)
cmd.Env = append(cmd.Environ(), "GOCOVERDIR=./coverage")
if err := cmd.Run(); err != nil {
t.Errorf(err.Error())
}
assertEqFile(t, "./internal/testdata/instrumented/basic_include_only.go.exp", f)
})
t.Run("when generated file, then ok", func(t *testing.T) {
cmd := exec.Command(testbin, "-app", "app", "-w", "-skip-generated=true", "-filename", "./internal/testdata/skipped_generated.go")
cmd.Env = append(cmd.Environ(), "GOCOVERDIR=./coverage")
out, err := cmd.CombinedOutput()
if err != nil {
t.Errorf(err.Error())
}
if !strings.Contains(string(out), "skipping generated file") {
t.Errorf("expected skipping generated file")
}
})
t.Run("when cannot open file, then err", func(t *testing.T) {
cmd := exec.Command(testbin, "-filename", "asdf")
cmd.Env = append(cmd.Environ(), "GOCOVERDIR=./coverage")
if err := cmd.Run(); err == nil {
t.Errorf("expected exit code 1")
}
})
t.Run("when non go file, then err", func(t *testing.T) {
cmd := exec.Command(testbin, "-filename", "README.md")
cmd.Env = append(cmd.Environ(), "GOCOVERDIR=./coverage")
if err := cmd.Run(); err == nil {
t.Errorf("expected exit code 1")
}
})
t.Run("when non filename, then err", func(t *testing.T) {
cmd := exec.Command(testbin)
cmd.Env = append(cmd.Environ(), "GOCOVERDIR=./coverage")
if err := cmd.Run(); err == nil {
t.Errorf("expected exit code 1")
}
})
t.Run("when can not parse commands, then err", func(t *testing.T) {
cmd := exec.Command(testbin, "-app", "app", "-w", "-all=false", "-filename", "./internal/testdata/error_unkonwn_command.go")
cmd.Env = append(cmd.Environ(), "GOCOVERDIR=./coverage")
if err := cmd.Run(); err == nil {
t.Errorf("expected exit code 1")
}
})
}
func assertEqFile(t *testing.T, a, b string) {
fa, _ := os.ReadFile(a)
fb, _ := os.ReadFile(b)
la := strings.Split(string(fa), "\n")
lb := strings.Split(string(fb), "\n")
for i := 0; i < len(la) && i < len(lb); i++ {
if la[i] != lb[i] {
t.Errorf("files are different at line(%d)\n%s\n%s\n", i, la[i], lb[i])
}
}
}
func copyFile(t *testing.T, from string) string {
f := path.Join(t.TempDir(), time.Now().Format("20060102-150405-"))
fbytes, _ := os.ReadFile(from)
os.WriteFile(f, fbytes, 0644)
return f
}