forked from rjeczalik/interfaces
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build_test.go
122 lines (101 loc) · 2.39 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
package interfaces_test
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"testing"
)
func TestBuild(t *testing.T) {
gopath, err := ioutil.TempDir("", "interfaces_test")
if err != nil {
t.Fatalf("TempDir()=%s", err)
}
defer os.RemoveAll(gopath)
os.Setenv("GOCACHE", gopath)
src := filepath.Join(gopath, "src")
if err := os.MkdirAll(src, 0755); err != nil {
t.Fatalf("MkdirAll()=%s", err)
}
cases := map[string]struct {
run func(string) error
}{
"interfacer": {
run: func(base string) error {
args := []string{
"run",
"./cmd/interfacer",
"-for", `os.File`,
"-as", "interfacer.File",
"-o", filepath.Join(base, "package.go"),
}
p, err := exec.Command("go", args...).CombinedOutput()
if err != nil {
return fmt.Errorf("%s:\n%s", err, p)
}
return nil
},
},
"structer": {
run: func(base string) error {
testdata, err := ioutil.ReadFile(filepath.FromSlash("testdata/aws-billing.csv"))
if err != nil {
return err
}
args := []string{
"run",
"./cmd/structer",
"-tag", "json",
"-as", "structer.Record",
"-format", "csv",
"-o", filepath.Join(base, "package.go"),
}
var buf bytes.Buffer
cmd := exec.Command("go", args...)
cmd.Stdin = bytes.NewReader(testdata)
cmd.Stdout = &buf
cmd.Stderr = &buf
if err := cmd.Run(); err != nil {
return fmt.Errorf("%s:\n%s", err, &buf)
}
return nil
},
},
}
gocommand := func(out io.Writer, pkg string, args ...string) *exec.Cmd {
c := exec.Command("go", args...)
c.Stderr = out
c.Stdout = out
c.Dir = filepath.Join(gopath, "src", pkg)
c.Env = []string{
"PATH=" + os.Getenv("PATH"),
"GOROOT=" + os.Getenv("GOROOT"),
"GOPATH=" + gopath,
"GOCACHE=" + os.Getenv("GOCACHE"),
"GO111MODULE=on",
}
return c
}
for pkg, cas := range cases {
t.Run(pkg, func(t *testing.T) {
genpkg := filepath.Join(src, pkg)
if err := os.MkdirAll(genpkg, 0755); err != nil {
t.Fatalf("MkdirAll()=%s", err)
}
if err := cas.run(genpkg); err != nil {
t.Fatalf("run()=%s", err)
}
var buf bytes.Buffer
if err := gocommand(&buf, pkg, "mod", "init").Run(); err != nil {
t.Fatalf("gomod.Run()=%s:\n%s", err, &buf)
}
buf.Reset()
if err := gocommand(&buf, pkg, "build", ".").Run(); err != nil {
t.Fatalf("gobuild.Run()=%s:\n%s", err, &buf)
}
})
}
}