Skip to content

Commit 5315a69

Browse files
committed
interfaces: rework build test to support modules
1 parent 566c0f8 commit 5315a69

File tree

1 file changed

+26
-12
lines changed

1 file changed

+26
-12
lines changed

build_test.go

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package interfaces_test
33
import (
44
"bytes"
55
"fmt"
6+
"io"
67
"io/ioutil"
78
"os"
89
"os/exec"
@@ -30,7 +31,7 @@ func TestBuild(t *testing.T) {
3031
run: func(base string) error {
3132
args := []string{
3233
"-for", `os.File`,
33-
"-as", "mock.File",
34+
"-as", "interfacer.File",
3435
"-o", filepath.Join(base, "package.go"),
3536
}
3637

@@ -51,7 +52,7 @@ func TestBuild(t *testing.T) {
5152

5253
args := []string{
5354
"-tag", "json",
54-
"-as", "billing.Record",
55+
"-as", "structer.Record",
5556
"-format", "csv",
5657
"-o", filepath.Join(base, "package.go"),
5758
}
@@ -72,9 +73,24 @@ func TestBuild(t *testing.T) {
7273
},
7374
}
7475

75-
for name, cas := range cases {
76-
t.Run(name, func(t *testing.T) {
77-
genpkg := filepath.Join(src, name)
76+
gocommand := func(out io.Writer, pkg string, args ...string) *exec.Cmd {
77+
c := exec.Command("go", args...)
78+
c.Stderr = out
79+
c.Stdout = out
80+
c.Dir = filepath.Join(gopath, "src", pkg)
81+
c.Env = []string{
82+
"PATH=" + os.Getenv("PATH"),
83+
"GOROOT=" + os.Getenv("GOROOT"),
84+
"GOPATH=" + gopath,
85+
"GOCACHE=" + os.Getenv("GOCACHE"),
86+
"GO111MODULE=on",
87+
}
88+
return c
89+
}
90+
91+
for pkg, cas := range cases {
92+
t.Run(pkg, func(t *testing.T) {
93+
genpkg := filepath.Join(src, pkg)
7894

7995
if err := os.MkdirAll(genpkg, 0755); err != nil {
8096
t.Fatalf("MkdirAll()=%s", err)
@@ -86,15 +102,13 @@ func TestBuild(t *testing.T) {
86102

87103
var buf bytes.Buffer
88104

89-
gobuild := exec.Command("go", "build", name)
90-
gobuild.Stderr = &buf
91-
gobuild.Stdout = &buf
92-
gobuild.Env = []string{
93-
"GOROOT=" + os.Getenv("GOROOT"),
94-
"GOPATH=" + gopath,
105+
if err := gocommand(&buf, pkg, "mod", "init").Run(); err != nil {
106+
t.Fatalf("gomod.Run()=%s:\n%s", err, &buf)
95107
}
96108

97-
if err := gobuild.Run(); err != nil {
109+
buf.Reset()
110+
111+
if err := gocommand(&buf, pkg, "build", ".").Run(); err != nil {
98112
t.Fatalf("gobuild.Run()=%s:\n%s", err, &buf)
99113
}
100114
})

0 commit comments

Comments
 (0)