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
|
package gtest
import (
"context"
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
"time"
"path"
"errors"
"golang.flu0r1ne.net/planr"
)
const GTEST_CMAKE = "CMakeLists.txt"
func mkUnit(tc *planr.TestCase) cmakeUnit {
cfg := tc.AdapterConfig().(*GtestConfig)
return cmakeUnit {
tc.Cname,
cfg.joinTests(*cfg.Testfile),
cfg.srcList(),
};
}
func chdir(dir string) {
if err := os.Chdir(dir); err != nil {
log.Fatal(err)
}
}
type GtestAdapter struct {}
func (a *GtestAdapter) Config() planr.AdapterConfig {
return planr.AdapterConfig {
Name: "gtest",
ParseConfig: ParseConfig,
ParseDefaultConfig: ParseDefaultConfig,
}
}
func (adapter *GtestAdapter) Build(tcs []*planr.TestCase) {
buildDir := adapter.Config().Dir()
cmakeFile := path.Join(buildDir, GTEST_CMAKE)
units := make([]cmakeUnit, 0)
for _, tc := range tcs {
fmt.Printf("[R] Building %s (%s)\n", tc.Cname, tc.Path)
cfg := tc.AdapterConfig().(*GtestConfig)
cfg.ensureSatisfied(tc.Path)
units = append(units, mkUnit(tc))
}
genCmake(cmakeFile, units)
chdir(buildDir)
planr.RunCmd("cmake", "-S", ".", "-B", ".")
}
type ResultFromId map[string] planr.TestResult
func (adapter *GtestAdapter) execTests(cnames []string) ResultFromId {
buildDir := adapter.Config().Dir()
lut := make(ResultFromId, 0)
for _, exe := range cnames {
fmt.Printf("[R] Evaluating %s\n", exe)
exePath := path.Join(buildDir, exe)
f, err := ioutil.TempFile(buildDir, "gtest_adapter_*.json")
if err != nil {
log.Fatal(err)
}
ctx, cancel := context.WithTimeout(context.Background(), 9999*time.Millisecond)
cmd := exec.CommandContext(ctx, exePath, "--gtest_output=json:" + f.Name())
defer cancel()
defer os.Remove(f.Name())
exitFail := false
if err := cmd.Run(); err != nil {
var exiterr *exec.ExitError
if errors.As(err, &exiterr) && exiterr.ExitCode() == 1{
exitFail = true
} else {
log.Printf("%v\n", err)
os.Exit(exiterr.ExitCode())
}
}
if exitFail {
fmt.Printf("")
}
for _, r := range decodeResults(f) {
lut[exe + "." + r.id] = r.result
}
}
return lut
}
func exes(tcs []*planr.TestCase) []string {
set := make(map[string] bool, 0)
for _, tc := range tcs {
if(!set[tc.Cname]) {
set[tc.Cname] = true
}
}
exes := make([]string, 0)
for k := range set {
exes = append(exes, k)
}
return exes
}
func id(tc *planr.TestCase) string {
cfg := tc.AdapterConfig().(*GtestConfig)
return tc.Cname + "." + *cfg.Suite + "." + *cfg.Name
}
func (adapter *GtestAdapter) Evaluate(tcs []*planr.TestCase) {
buildDir := adapter.Config().Dir()
chdir(buildDir)
planr.RunCmd("make", "-k")
files := exes(tcs)
resultById := adapter.execTests(files)
for _, tc := range tcs {
result := resultById[id(tc)]
tc.Result = &result
}
}
|