-
Notifications
You must be signed in to change notification settings - Fork 3
/
build_test.go
125 lines (103 loc) · 2.7 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
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
// Copyright (C) 2019 Storj Labs, Inc.
// Copyright (C) 2017 Space Monkey, Inc.
// See LICENSE for copying information.
package main
import (
"go/parser"
"go/token"
"os"
"path/filepath"
"runtime/debug"
"testing"
"golang.org/x/tools/go/packages"
"storj.io/dbx/testutil"
)
var buildConfig = &packages.Config{
Mode: 0 |
packages.NeedTypes |
packages.NeedImports |
packages.NeedDeps |
packages.NeedCompiledGoFiles,
}
func TestBuild(t *testing.T) {
tw := testutil.Wrap(t)
tw.Parallel()
data_dir := filepath.Join("testdata", "good")
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 TestBuildBad(t *testing.T) {
tw := testutil.Wrap(t)
tw.Parallel()
data_dir := filepath.Join("testdata", "bad")
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 := t.TempDir()
dbx_source, err := os.ReadFile(file)
t.AssertNoError(err)
t.Context("dbx", linedSource(dbx_source))
d := loadDirectives(t, dbx_source)
dialects := []string{"sqlite3", "pgx", "pgxcockroach"}
if other := d.lookup("dialects"); other != nil {
dialects = other
t.Logf("using dialects: %q", dialects)
}
type options struct {
userdata bool
}
runBuild := func(opts options) {
t.Logf("[%s] generating... %+v", file, opts)
err = newGlobal().golangCmd("", dialects, "", opts.userdata, []string{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 := os.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)
pkg, err := packages.Load(buildConfig, go_file)
if d.has("fail") {
t.AssertError(err, d.get("fail"))
} else {
t.AssertNoError(err)
if len(pkg[0].Errors) > 0 {
errMsg := ""
for _, err := range pkg[0].Errors {
errMsg += " " + err.Error()
}
t.Logf("[%s] errors:\n%s", file, errMsg)
}
t.Assert(!pkg[0].IllTyped)
}
}
runBuild(options{userdata: false})
runBuild(options{userdata: true})
}