-
Notifications
You must be signed in to change notification settings - Fork 15
/
coverage_test.go
276 lines (250 loc) · 6.71 KB
/
coverage_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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package main
import (
"bytes"
"container/list"
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"strings"
"sync"
"testing"
)
var modules = []mod{
mod{name: "acodec", decl: buildRegex("ALLEGRO_ACODEC_FUNC")},
mod{name: "audio", decl: buildRegex("ALLEGRO_KCM_AUDIO_FUNC")},
mod{name: "color", decl: buildRegex("ALLEGRO_COLOR_FUNC")},
mod{name: "dialog", decl: buildRegex("ALLEGRO_DIALOG_FUNC"), header: "native_dialog"},
mod{name: "font", decl: buildRegex("ALLEGRO_FONT_FUNC")},
mod{name: "image", decl: buildRegex("ALLEGRO_IIO_FUNC")},
mod{name: "memfile", decl: buildRegex("ALLEGRO_MEMFILE_FUNC")},
mod{name: "physfs", decl: buildRegex("ALLEGRO_PHYSFS_FUNC")},
mod{name: "primitives", decl: buildRegex("ALLEGRO_PRIM_FUNC")},
mod{name: "ttf", decl: buildRegex("ALLEGRO_TTF_FUNC"), path: "font/ttf"},
}
type mod struct {
name, path, header string
decl *decl
}
func (m *mod) Header() string {
if m.header != "" {
return "allegro_" + m.header + ".h"
} else {
return "allegro_" + m.name + ".h"
}
}
func (m *mod) Path() string {
if m.path != "" {
return m.path
} else {
return m.name
}
}
type decl struct {
macro string
regex *regexp.Regexp
}
// regexes for various function macros
var alFunc = buildRegex("AL_FUNC")
func buildRegex(macro string) *decl {
return &decl{macro, regexp.MustCompile(macro + `\((?P<type>.*), (?P<name>.*), \((?P<params>.*)\)\)`)}
}
func getSource(dirs ...string) ([]byte, error) {
var (
buf bytes.Buffer
err error
)
for _, dir := range dirs {
if err = filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if info.IsDir() && path != dir {
return filepath.SkipDir
} else if !strings.HasSuffix(info.Name(), ".go") {
return nil
}
data, err2 := ioutil.ReadFile(path)
if err2 != nil {
return fmt.Errorf("can't read Go source file \"%s\": %s", path, err2.Error())
}
buf.Write(data)
return nil
}); err != nil {
return nil, err
}
}
return buf.Bytes(), err
}
type missingFunc struct {
Name string
Type string
Params string
Header string
Module string
}
func scanHeaders(packageRoot string, missingFuncs chan *missingFunc, errs chan error) {
var (
source []byte
sourceErr error
headerRoot string = os.Getenv("ALLEGRO_INCLUDE")
)
if headerRoot == "" {
headerRoot = filepath.Join("/", "usr", "include", "allegro5")
}
defer func() {
close(missingFuncs)
close(errs)
}()
if _, err := os.Stat(headerRoot); os.IsNotExist(err) {
return
}
// First walk the full root, looking for standard allegro functions.
// Also include subpackages that include platform-specific functionality,
// but aren't separate modules.
source, sourceErr = getSource(packageRoot, filepath.Join(packageRoot, "x11"), filepath.Join(packageRoot, "windows"))
if sourceErr != nil {
errs <- sourceErr
return
}
filepath.Walk(headerRoot, func(header string, info os.FileInfo, err error) error {
if info == nil {
return errors.New("no file info! headerRoot = " + headerRoot + ", header = " + header)
}
if info.IsDir() && info.Name() == "internal" {
return filepath.SkipDir
} else if info.IsDir() || !strings.HasSuffix(info.Name(), ".h") {
return nil
}
data, err2 := ioutil.ReadFile(header)
if err2 != nil {
errs <- err2
return nil
}
findMissingFuncs(data, source, header, alFunc, "", missingFuncs)
return nil
})
// Now iterate through all known modules.
for _, m := range modules {
root := filepath.Join(packageRoot, m.Path())
header := filepath.Join(headerRoot, m.Header())
if _, err := os.Stat(header); os.IsNotExist(err) {
errs <- fmt.Errorf("Module header not found at '%s'", header)
continue
}
if info, err := os.Stat(root); os.IsNotExist(err) || !info.IsDir() {
errs <- fmt.Errorf("Source not found at '%s'", root)
continue
}
data, err := ioutil.ReadFile(header)
if err != nil {
errs <- err
continue
}
source, sourceErr = getSource(root)
if sourceErr != nil {
errs <- sourceErr
return
}
findMissingFuncs(data, source, header, m.decl, m.name, missingFuncs)
}
}
// findMissingFuncs() is a customized iteration method used to ensure that multi-line function declarations are found.
func findMissingFuncs(data, source []byte, header string, d *decl, modName string, missingFuncs chan *missingFunc) {
ch := make(chan string)
go func() {
defer func() {
close(ch)
if r := recover(); r != nil {
fmt.Fprintf(os.Stderr, "Header file '%s' is malformed.\n", header)
os.Exit(1)
}
}()
var buf bytes.Buffer
lines := strings.Split(string(data), "\n")
for i := 0; i < len(lines); i++ {
line := strings.TrimSpace(lines[i])
buf.WriteString(line)
if strings.HasPrefix(line, d.macro) {
for !strings.HasSuffix(line, ";") {
i++
line = strings.TrimSpace(lines[i])
buf.WriteString(line)
}
}
ch <- buf.String()
buf.Reset()
}
}()
for line := range ch {
vals := d.regex.FindStringSubmatch(line)
if vals == nil {
// no match
continue
}
name := strings.TrimSpace(vals[2])
if strings.HasPrefix(name, "_") {
// function names starting with an underscore are private
continue
}
typ := strings.TrimSpace(vals[1])
params := strings.TrimSpace(vals[3])
if !bytes.Contains(source, []byte("C."+name)) {
missingFuncs <- &missingFunc{Name: name, Type: typ, Params: params, Module: modName, Header: header}
}
}
}
// Ignore functions that I either don't know how to implement,
// are redundant, or are based on Allegro features that Go
// already provides, like file I/O and fixed point math.
func getBlacklist() map[string]bool {
blacklist := make(map[string]bool)
data, err := ioutil.ReadFile("blacklist")
if err != nil {
panic(err)
}
for _, line := range strings.Split(string(data), "\n") {
i := strings.IndexRune(line, '#')
if i != -1 {
line = line[0:i]
}
blacklist[strings.TrimSpace(line)] = true
}
return blacklist
}
func TestCoverage(t *testing.T) {
cwd, err := os.Getwd()
if err != nil {
t.Fatal(err.Error())
}
packageRoot := filepath.Join(cwd, "allegro")
blacklist := getBlacklist()
missingFuncs := make(chan *missingFunc)
errs := make(chan error)
go scanHeaders(packageRoot, missingFuncs, errs)
var wg sync.WaitGroup
wg.Add(2)
go func() {
for f := range missingFuncs {
if blacklist[f.Name] {
continue
}
if f.Module == "" {
t.Errorf("Missing allegro function '%s' in file '%s'", f.Name, f.Header)
} else {
t.Errorf("Module '%s' missing function '%s' [%s %s(%s)]", f.Module, f.Name, f.Type, f.Name, f.Params)
}
}
wg.Done()
}()
go func() {
errorList := list.New()
for err := range errs {
errorList.PushBack(err)
}
for e := errorList.Front(); e != nil; e = e.Next() {
t.Error("Error: " + e.Value.(error).Error())
}
wg.Done()
}()
wg.Wait()
}