forked from matrix-org/complement
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sytest_coverage.go
123 lines (116 loc) · 3.19 KB
/
sytest_coverage.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
// go run ./sytest_coverage.go
// Add -v to see verbose output.
package main
import (
"fmt"
"go/parser"
"go/token"
"io/ioutil"
"os"
"regexp"
"sort"
"strings"
)
// to generate test.list, clone sytest then:
// $ grep -r 'test "' ./tests > test.list
var (
testNameRegexp = regexp.MustCompile(`.*"(.*)"`)
testFilenameRegexp = regexp.MustCompile(`/tests/(.*)\.pl`)
)
// Maps test names to filenames then looks for:
// sytest: $test_name
// in all files in ./tests - if there's a match it marks that test as converted.
func main() {
verbose := len(os.Args) == 2 && os.Args[1] == "-v"
body, err := ioutil.ReadFile("./sytest.list")
if err != nil {
panic(err)
}
testLines := strings.Split(string(body), "\n")
filenameToTestName := make(map[string][]string)
testNameToFilename := make(map[string]string)
for _, line := range testLines {
name, filename := extract(line)
if name == "" || filename == "" {
continue
}
name = "sytest: " + strings.TrimSpace(name)
filenameToTestName[filename] = append(filenameToTestName[filename], name)
testNameToFilename[name] = strings.TrimSpace(filename)
}
total := len(testNameToFilename)
files, err := ioutil.ReadDir("./tests")
if err != nil {
panic(err)
}
convertedTests := make(map[string]bool)
for _, file := range files {
fset := token.NewFileSet()
astFile, err := parser.ParseFile(fset, "./tests/"+file.Name(), nil, parser.ParseComments)
if err != nil {
panic(err)
}
for _, cmt := range astFile.Comments {
comment := strings.TrimSpace(cmt.Text())
lines := strings.Split(comment, "\n")
for _, line := range lines {
_, ok := testNameToFilename[line]
if !ok {
continue
}
convertedTests[line] = true
}
}
}
numComplementTests := len(convertedTests)
for _, fname := range sorted(filenameToTestName) {
testNames := filenameToTestName[fname]
convertedTestsInFile := 0
// try to find the filename
for i := range testNames {
// see if this test was converted
if convertedTests[testNames[i]] {
convertedTestsInFile++
testNames[i] = "✓ " + strings.TrimPrefix(testNames[i], "sytest: ")
} else {
testNames[i] = "× " + strings.TrimPrefix(testNames[i], "sytest: ")
}
}
fmt.Printf("%s %d/%d tests\n", fname, convertedTestsInFile, len(testNames))
if !verbose || convertedTestsInFile == 0 {
continue
}
for _, tn := range testNames {
fmt.Printf(" %s\n", tn)
}
fmt.Println()
}
fmt.Printf("\nTOTAL: %d/%d tests converted\n", numComplementTests, total)
}
func sorted(in map[string][]string) []string {
out := make([]string, len(in))
i := 0
for k := range in {
out[i] = k
i++
}
sort.Strings(out)
return out
}
// ./tests/49ignore.pl:test "Ignore invite in incremental sync",
// ./tests/31sync/16room-summary.pl:test "Room summary counts change when membership changes",
func extract(line string) (string, string) {
line = strings.TrimSpace(line)
if len(line) == 0 {
return "", ""
}
nameGroups := testNameRegexp.FindStringSubmatch(line)
filenameGroups := testFilenameRegexp.FindStringSubmatch(line)
if nameGroups == nil {
panic("Cannot find name: " + line)
}
if filenameGroups == nil {
panic("Cannot find filename: " + line)
}
return nameGroups[1], filenameGroups[1]
}