forked from volatiletech/sqlboiler
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsqlboiler_test.go
140 lines (115 loc) · 2.64 KB
/
sqlboiler_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
package main
import (
"bufio"
"bytes"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"regexp"
"strconv"
"testing"
)
var state *State
var rgxHasSpaces = regexp.MustCompile(`^\s+`)
func TestNew(t *testing.T) {
if testing.Short() {
t.SkipNow()
}
var err error
out, err := ioutil.TempDir("", "boil_templates")
if err != nil {
t.Fatalf("unable to create tempdir: %s", err)
}
// Defer cleanup of the tmp folder
defer func() {
if t.Failed() {
t.Log("template test output:", state.Config.OutFolder)
return
}
os.RemoveAll(state.Config.OutFolder)
}()
config := &Config{
DriverName: "mock",
PkgName: "models",
OutFolder: out,
BlacklistTables: []string{"hangars"},
}
state, err = New(config)
if err != nil {
t.Fatalf("Unable to create State using config: %s", err)
}
if err = state.Run(false); err != nil {
t.Errorf("Unable to execute State.Run: %s", err)
}
buf := &bytes.Buffer{}
cmd := exec.Command("go", "test", "-c")
cmd.Dir = state.Config.OutFolder
cmd.Stderr = buf
if err = cmd.Run(); err != nil {
t.Errorf("go test cmd execution failed: %s", err)
outputCompileErrors(buf, state.Config.OutFolder)
fmt.Println()
}
}
func outputCompileErrors(buf *bytes.Buffer, outFolder string) {
type errObj struct {
errMsg string
fileName string
lineNumber int
}
var errObjects []errObj
lineBuf := &bytes.Buffer{}
bufLines := bytes.Split(buf.Bytes(), []byte{'\n'})
for i := 0; i < len(bufLines); i++ {
lineBuf.Reset()
if !bytes.HasPrefix(bufLines[i], []byte("./")) {
continue
}
fmt.Fprintf(lineBuf, "%s\n", bufLines[i])
splits := bytes.Split(bufLines[i], []byte{':'})
lineNum, err := strconv.Atoi(string(splits[1]))
if err != nil {
panic(fmt.Sprintf("Cant convert line number to int: %s", bufLines[i]))
}
eObj := errObj{
fileName: string(splits[0]),
lineNumber: lineNum,
}
for y := i; y < len(bufLines); y++ {
if !rgxHasSpaces.Match(bufLines[y]) {
break
}
fmt.Fprintf(lineBuf, "%s\n", bufLines[y])
i++
}
eObj.errMsg = lineBuf.String()
errObjects = append(errObjects, eObj)
}
for _, eObj := range errObjects {
fmt.Printf("-----------------\n")
fmt.Println(eObj.errMsg)
filePath := filepath.Join(outFolder, eObj.fileName)
fh, err := os.Open(filePath)
if err != nil {
panic(fmt.Sprintf("Cant open the file: %#v", eObj))
}
scanner := bufio.NewScanner(fh)
throwaway := eObj.lineNumber - 5
for throwaway > 0 && scanner.Scan() {
throwaway--
}
for i := 0; i < 6; i++ {
if scanner.Scan() {
b := scanner.Bytes()
if len(b) != 0 {
fmt.Printf("%s\n", b)
} else {
i--
}
}
}
fh.Close()
}
}