This repository has been archived by the owner on Jul 13, 2018. It is now read-only.
/
main.go
604 lines (569 loc) · 14.3 KB
/
main.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
package main
import (
"bytes"
"context"
"fmt"
"go/ast"
"go/build"
"go/format"
"go/parser"
"go/printer"
"go/token"
"io/ioutil"
"net/url"
"os"
"os/exec"
"path/filepath"
"strings"
"text/template"
"github.com/gernest/mad"
"golang.org/x/tools/go/ast/astutil"
"github.com/gernest/mad/annotate"
"github.com/gernest/mad/config"
"github.com/gernest/mad/launcher/chrome"
"github.com/gernest/mad/report/console"
"github.com/gernest/mad/tools"
"github.com/urfave/cli"
)
const (
testsDir = "tests"
testsOutDir = "madness"
localhost = "http://localhost"
resourcePath = "/resource"
projectDescription = "Inter galactic test runner for Go frontend projects"
serviceName = "madtitan"
// hardcoded import paths
madImportPath = "github.com/gernest/mad"
integrationImportPath = "github.com/gernest/mad/integration"
coverImportPath = "github.com/gernest/mad/cover"
websocketImportPath = "github.com/gernest/mad/ws"
)
// precompile templates
var (
integrationTpl = template.Must(template.New("i").Parse(mainIntegrationTpl))
indexHTMLTpl = template.Must(template.New("idx").Parse(idxTpl))
mainUnitTestTpl = template.Must(template.New("main").Parse(mainUnitTpl))
mainCoverTpl = template.Must(template.New("cover").Parse(coverTpl))
)
func main() {
a := cli.NewApp()
a.Name = serviceName
a.Usage = projectDescription
a.Commands = []cli.Command{
{
Name: "test",
Usage: "runs the test suites",
Flags: config.FLags(),
Action: runTestsCommand,
},
{
Name: "coverage",
Usage: "calculate code coverage",
Flags: []cli.Flag{
cli.BoolFlag{
Name: "text",
Usage: "Formats output to be like what go test -cover does",
},
},
Action: runCoverage,
},
}
if err := a.Run(os.Args); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
func runTestsCommand(ctx *cli.Context) error {
cfg, err := config.Load(ctx)
if err != nil {
return err
}
executionContext, cancel := context.WithTimeout(context.Background(), cfg.Timeout)
defer cancel()
os.RemoveAll(cfg.OutputPath)
os.MkdirAll(cfg.OutputPath, 0755)
if err = generateTestPackage(executionContext, cfg); err != nil {
return err
}
if cfg.Dry {
return nil
}
if cfg.Build {
o := filepath.Join(cfg.OutputPath, "main.js")
err = buildPackage(executionContext, o, cfg.OutputMainPkg)
if err != nil {
return err
}
q := make(url.Values)
q.Set("src", "main.js.map")
mainFIle := fmt.Sprintf("%s:%d%s?%s",
localhost, cfg.Port, resourcePath, q.Encode())
if err = addSourcemap(o, mainFIle); err != nil {
return err
}
}
for _, v := range cfg.Browsers {
switch v {
case "chrome":
chrome, err := chrome.New(chrome.Options{
Verbose: cfg.Verbose,
Port: cfg.DevtoolPort,
ChromeFlags: []string{"--headless"},
})
if err != nil {
return err
}
var h respHandler
if cfg.JSON != "" {
h = console.NewJSON(cfg.JSON)
} else {
h = console.New(cfg.Verbose)
}
err = streamResponse(executionContext,
cfg, chrome, h)
if err != nil {
return err
}
}
}
return nil
}
type respHandler interface {
Handle(*mad.SpecResult)
Done() error
}
// generateTestPackage process the test directory and generate processed files
// in the promtest directory.
func generateTestPackage(ctx context.Context, cfg *config.Config) error {
for _, v := range cfg.TestInfo {
err := createTestPackage(cfg, v)
if err != nil {
return err
}
}
cfg.ResolvePackageConflict()
wsImport := cfg.ImportMap[websocketImportPath]
if wsImport == "" {
wsImport = websocketImportPath
}
madImport := cfg.ImportMap[madImportPath]
if madImport == "" {
madImport = madImportPath
}
err := writeMain(cfg.OutputPath, map[string]interface{}{
"config": cfg,
"wsImport": wsImport,
"madImport": madImport,
})
if err != nil {
return err
}
err = generateIntegrationPackages(ctx, cfg)
if err != nil {
return err
}
return writeIndex(cfg)
}
// This loads files found in path, process them and generate processed package
// into the directory specified by cfg.OutputPath.
func createTestPackage(cfg *config.Config, out *config.Info) error {
var files []*ast.File
os.MkdirAll(out.OutputPath, 0755)
set := token.NewFileSet()
// we need to keep track of the defined unit and integration test functions.
// This collects functions from all files.
funcs := &tools.TestNames{}
if cfg.Cover {
for _, v := range out.Package.Imports {
err := instrumentPackage(cfg, v)
if err != nil {
return err
}
}
if cfg.Info.ImportPath == madImportPath {
//WORKAROUND : when we testing the mad package
imports := []string{
integrationImportPath,
}
for _, v := range imports {
err := instrumentPackage(cfg, v)
if err != nil {
return err
}
}
}
}
for _, v := range out.Package.GoFiles {
f, err := parser.ParseFile(set, filepath.Join(out.Package.Dir, v), nil, 0)
if err != nil {
return err
}
fn := tools.AddFileNumber(set, f)
funcs.Integration = append(funcs.Integration, fn.Integration...)
funcs.Unit = append(funcs.Unit, fn.Unit...)
for old, newImport := range cfg.ImportMap {
astutil.RewriteImport(set, f, old, newImport)
}
files = append(files, f)
}
for _, v := range files {
err := writeFile(out.OutputPath, set, v)
if err != nil {
return err
}
}
funcs.Filter(cfg.Run)
hasTests := len(funcs.Unit) > 0 || len(funcs.Integration) > 0
if hasTests {
cfg.TestNames[out] = funcs
}
return nil
}
const coverTpl = `
package {{.pkgName}}
import(
"github.com/gernest/mad/cover"
)
func coverage()[]cover.Profile {
return []cover.Profile{
{{- $mode:=.mode}}
{{- range $k,$v:=.vars}}
cover.File("{{$k}}","{{$mode}}", {{$v}}.Count[:], {{$v}}.Pos[:], {{$v}}.NumStmt[:]),
{{- end}}
}
}
func init() {
cover.Register("{{.pkg}}",coverage)
}
`
// instrumentPackage processes pkg and adds instrumentation for coverage analysis.
func instrumentPackage(cfg *config.Config, pkg string) error {
if !strings.HasPrefix(pkg, cfg.Info.ImportPath) {
return nil
}
if _, ok := cfg.ImportMap[pkg]; ok {
return nil
}
if pkg == coverImportPath {
cfg.ImportMap[pkg] = pkg
return nil
}
info := cfg.Info
if pkg != cfg.Info.ImportPath {
path := strings.TrimPrefix(pkg, cfg.Info.ImportPath)
dir := filepath.Join(cfg.Info.Dir, path)
newPkg, err := build.ImportDir(dir, 0)
if err != nil {
return err
}
info = newPkg
}
for _, v := range info.Imports {
err := instrumentPackage(cfg, v)
if err != nil {
return err
}
}
set := token.NewFileSet()
base := filepath.Base(pkg)
if pkg != cfg.Info.ImportPath {
rel, err := filepath.Rel(cfg.Info.ImportPath, pkg)
if err != nil {
return err
}
base = rel
}
out := filepath.Join(cfg.OutputPath, base)
outPkg := filepath.Join(cfg.Info.ImportPath, cfg.OutputDirName, base)
os.MkdirAll(out, 0755)
var buf bytes.Buffer
coverVarNames := make(map[string]string)
for k, v := range info.GoFiles {
f, err := parser.ParseFile(set, filepath.Join(info.Dir, v), nil, parser.ParseComments)
if err != nil {
return err
}
buf.Reset()
varName := fmt.Sprintf("coverStats%d", k)
fullName := info.ImportPath + "/" + v
coverVarNames[fullName] = varName
err = annotate.Annotate(&buf, set, f, annotate.Options{
Name: fullName,
VarName: varName,
Mode: cfg.Covermode,
})
if err != nil {
return err
}
varDef := buf.String()
buf.Reset()
for old, newImport := range cfg.ImportMap {
astutil.RewriteImport(set, f, old, newImport)
}
filename := filepath.Join(out, v)
err = printer.Fprint(&buf, set, f)
if err != nil {
return err
}
buf.WriteString(varDef)
err = ioutil.WriteFile(filename, buf.Bytes(), 0600)
if err != nil {
return err
}
}
buf.Reset()
data := map[string]interface{}{
"pkgName": info.Name,
"vars": coverVarNames,
"mode": cfg.Covermode,
"pkg": pkg,
}
err := mainCoverTpl.Execute(&buf, data)
if err != nil {
return err
}
filename := filepath.Join(out, "coverrage_stats.go")
b, err := format.Source(buf.Bytes())
if err != nil {
return err
}
err = ioutil.WriteFile(filename, b, 0600)
if err != nil {
return err
}
cfg.ImportMap[pkg] = outPkg
return nil
}
// This genegrates and compiles packages with integration tests. Each
// integration test lives in it's own separate package.
func generateIntegrationPackages(ctx context.Context, cfg *config.Config) error {
for info, funcs := range cfg.TestNames {
if len(funcs.Integration) > 0 {
data := make(map[string]interface{})
data["config"] = cfg
data["info"] = info
madImport := cfg.ImportMap[madImportPath]
if madImport == "" {
madImport = madImportPath
}
interImport := cfg.ImportMap[integrationImportPath]
if interImport == "" {
interImport = integrationImportPath
}
data["madImport"] = madImport
data["interImport"] = interImport
var buf bytes.Buffer
for _, v := range funcs.Integration {
name := strings.ToLower(v)
data["PkgName"] = name
pkg := info.ImportPath + "/" + name
data["IntegrationPkg"] = pkg
e := filepath.Join(info.OutputPath, name)
os.MkdirAll(e, 0755)
data["FuncName"] = v
buf.Reset()
err := integrationTpl.Execute(&buf, data)
if err != nil {
return err
}
err = ioutil.WriteFile(filepath.Join(e, "main.go"), buf.Bytes(), 0600)
if err != nil {
return err
}
q := make(url.Values)
q.Set("src", filepath.Join(info.RelativePath, name, "main.js"))
mainFIle := fmt.Sprintf("%s:%d%s?%s",
localhost, cfg.Port, resourcePath, q.Encode())
var buf bytes.Buffer
err = indexHTMLTpl.Execute(&buf, map[string]interface{}{
"mainFile": mainFIle,
"config": cfg,
})
m := filepath.Join(e, "index.html")
err = ioutil.WriteFile(m, buf.Bytes(), 0600)
if err != nil {
return err
}
query := make(url.Values)
query.Set("src", filepath.Join(info.RelativePath, name, "index.html"))
cfg.IntegrationIndexPages = append(cfg.IntegrationIndexPages,
fmt.Sprintf("%s:%d%s?%s",
localhost, cfg.Port, resourcePath, query.Encode()))
if cfg.Build {
err = buildPackage(ctx, filepath.Join(e, "main.js"), pkg)
if err != nil {
return err
}
}
}
}
}
return nil
}
// writeFile prints the ast for f using the printer package. The file name is
// obtained from the fset.
//
// The file is created in the to directory.
func writeFile(to string, fset *token.FileSet, f *ast.File) error {
var buf bytes.Buffer
err := printer.Fprint(&buf, fset, f)
if err != nil {
return err
}
file := fset.File(f.Pos())
o := filepath.Join(to, filepath.Base(file.Name()))
// println(o)
err = ioutil.WriteFile(o, buf.Bytes(), 0600)
if err != nil {
return err
}
return nil
}
// writeMain creates main.go file which wraps the compiled test functions with
// extra logic for running the tests.
func writeMain(dst string, ctx interface{}) error {
var buf bytes.Buffer
err := mainUnitTestTpl.Execute(&buf, ctx)
if err != nil {
return err
}
m := filepath.Join(dst, "main.go")
b, err := format.Source(buf.Bytes())
if err != nil {
return err
}
return ioutil.WriteFile(m, b, 0600)
}
//creates index.html file which loads the generated test suite js file.
func writeIndex(cfg *config.Config) error {
q := make(url.Values)
q.Set("src", "main.js")
mainFIle := fmt.Sprintf("%s:%d%s?%s",
localhost, cfg.Port, resourcePath, q.Encode())
ctx := map[string]interface{}{
"mainFile": mainFIle,
"config": cfg,
}
o := cfg.OutputPath
var buf bytes.Buffer
err := indexHTMLTpl.Execute(&buf, ctx)
m := filepath.Join(o, "index.html")
err = ioutil.WriteFile(m, buf.Bytes(), 0600)
if err != nil {
return err
}
query := make(url.Values)
query.Set("src", "index.html")
cfg.UnitIndexPage = fmt.Sprintf("%s:%d%s?%s",
localhost, cfg.Port, resourcePath, query.Encode())
return nil
}
// This is the template for the main entrypoint of the generated unit test
// package.
//
// This goes to the madness/main.go what will eventual be compiled by gopherjs.
// And loaded for execution in the browser.
var mainUnitTpl = `package main
import(
{{range $k,$v:=.config.TestNames}}
{{with $k.FixImport}}{{.}}{{end}} "{{$k.ImportPath}}"
{{- end}}
"{{.wsImport}}"
"{{.madImport}}"
{{if .config.Cover}}
"github.com/gernest/mad/cover"
{{end}}
)
func main() {
startTest()
}
const testID ="{{.config.UUID}}"
const testPkg ="{{.config.Info.ImportPath}}"
func startTest(){
go func () {
{{if .config.Cover}}
defer func () {
println(cover.Key+cover.JSON())
}()
{{end}}
w,err:=ws.New(testID)
if err!=nil{
panic(err)
}
defer w.Close()
for _,ts:=range allTests(){
v:=mad.Exec(ts)
err=w.Report(v,testPkg,testID)
if err!=nil{
println("error "+testID+" "+testPkg+" "+err.Error())
}
}
}()
}
{{$n:=.config.TestDirName}}
func allTests()[]mad.Test {
return []mad.Test{
{{- range $k, $v:=.config.TestNames}}
{{- range $v.Unit}}
mad.Describe("{{$k.Desc .}}",{{$k.FormatName .}}()),
{{- end}}
{{- end}}
}
}
`
var mainIntegrationTpl = `package main
import (
"{{.info.ImportPath}}"
"{{.interImport}}"
"{{.madImport}}"
"github.com/gopherjs/vecty"
)
const testID ="{{.config.UUID}}"
const testPkg ="{{.config.Info.ImportPath}}"
{{$n:=.config.TestDirName}}
func main() {
vecty.RenderBody(
&integration.Integration{
UUID: testID,
Pkg: testPkg,
Cover: {{.config.Cover}},
Component: testFunc().(*mad.Component),
},
)
}
func testFunc() mad.Integration {
return mad.SetupIntegration("{{.info.Desc .FuncName}}",{{.info.FormatName .FuncName}}() )
}
`
const idxTpl = `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>mad test runner</title>
</head>
<body>
</body>
<script src="{{.mainFile}}"></script>
</html>`
func buildPackage(ctx context.Context, out, pkg string) error {
cmd := exec.CommandContext(ctx, "gopherjs", "build", "-o", out, pkg)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stdout
return cmd.Run()
}
func addSourcemap(o string, srcMap string) error {
b, err := ioutil.ReadFile(o)
if err != nil {
return err
}
b = bytes.TrimSpace(b)
p := bytes.Split(b, []byte("\n"))
last := p[len(p)-1]
if bytes.HasPrefix(last, []byte("//# sourceMappingURL=")) {
p[len(p)-1] = []byte(fmt.Sprintf("//# sourceMappingURL=%s", srcMap))
return ioutil.WriteFile(o, bytes.Join(p, []byte("\n")), 0600)
}
return nil
}