-
Notifications
You must be signed in to change notification settings - Fork 11
/
build_test.go
97 lines (79 loc) · 2.13 KB
/
build_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
// Copyright (C) 2017 Space Monkey, Inc.
package main
import (
"go/parser"
"go/token"
"io/ioutil"
"os"
"path/filepath"
"runtime/debug"
"testing"
"golang.org/x/tools/go/packages"
"gopkg.in/spacemonkeygo/dbx.v1/testutil"
)
func TestBuild(t *testing.T) {
tw := testutil.Wrap(t)
tw.Parallel()
data_dir := filepath.Join("testdata", "build")
names, err := filepath.Glob(filepath.Join(data_dir, "*.dbx"))
tw.AssertNoError(err)
for _, name := range names {
name := name
tw.Runp(filepath.Base(name), func(tw *testutil.T) {
testBuildFile(tw, name)
})
}
}
func testBuildFile(t *testutil.T, file string) {
defer func() {
if val := recover(); val != nil {
t.Fatalf("%s\n%s", val, string(debug.Stack()))
}
}()
dir, err := ioutil.TempDir("", "dbx")
t.AssertNoError(err)
defer os.RemoveAll(dir)
dbx_source, err := ioutil.ReadFile(file)
t.AssertNoError(err)
t.Context("dbx", linedSource(dbx_source))
d := loadDirectives(t, dbx_source)
dialects := []string{"postgres", "sqlite3"}
if other := d.lookup("dialects"); other != nil {
dialects = other
t.Logf("using dialects: %q", dialects)
}
type options struct {
rx bool
userdata bool
}
runBuild := func(opts options) {
t.Logf("[%s] generating... %+v", file, opts)
err = golangCmd("", dialects, "", opts.rx, opts.userdata, file, dir)
if d.has("fail_gen") {
t.AssertError(err, d.get("fail_gen"))
return
} else {
t.AssertNoError(err)
}
t.Logf("[%s] loading...", file)
go_file := filepath.Join(dir, filepath.Base(file)+".go")
go_source, err := ioutil.ReadFile(go_file)
t.AssertNoError(err)
t.Context("go", linedSource(go_source))
t.Logf("[%s] parsing...", file)
fset := token.NewFileSet()
_, err = parser.ParseFile(fset, go_file, go_source, parser.AllErrors)
t.AssertNoError(err)
t.Logf("[%s] compiling...", file)
_, err = packages.Load(nil, go_file)
if d.has("fail") {
t.AssertError(err, d.get("fail"))
} else {
t.AssertNoError(err)
}
}
runBuild(options{rx: false, userdata: false})
runBuild(options{rx: false, userdata: true})
runBuild(options{rx: true, userdata: false})
runBuild(options{rx: true, userdata: true})
}