wire: make tests work with Go modules (google/go-cloud#331)
Generates go.mod files for test cases to support Go modules.
This commit is contained in:
@@ -17,8 +17,7 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bar"
|
||||
|
||||
"example.com/bar"
|
||||
"github.com/google/go-cloud/wire"
|
||||
)
|
||||
|
||||
|
||||
2
internal/wire/testdata/BuildTagsMainOnly/pkg
vendored
2
internal/wire/testdata/BuildTagsMainOnly/pkg
vendored
@@ -1 +1 @@
|
||||
foo
|
||||
example.com/foo
|
||||
|
||||
@@ -17,8 +17,7 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bar"
|
||||
|
||||
"example.com/bar"
|
||||
"github.com/google/go-cloud/wire"
|
||||
)
|
||||
|
||||
|
||||
@@ -1 +1 @@
|
||||
./foo
|
||||
./example.com/foo
|
||||
|
||||
2
internal/wire/testdata/Chain/pkg
vendored
2
internal/wire/testdata/Chain/pkg
vendored
@@ -1 +1 @@
|
||||
foo
|
||||
example.com/foo
|
||||
|
||||
2
internal/wire/testdata/Cleanup/pkg
vendored
2
internal/wire/testdata/Cleanup/pkg
vendored
@@ -1 +1 @@
|
||||
foo
|
||||
example.com/foo
|
||||
|
||||
2
internal/wire/testdata/CopyOtherDecls/pkg
vendored
2
internal/wire/testdata/CopyOtherDecls/pkg
vendored
@@ -1 +1 @@
|
||||
foo
|
||||
example.com/foo
|
||||
|
||||
2
internal/wire/testdata/Cycle/pkg
vendored
2
internal/wire/testdata/Cycle/pkg
vendored
@@ -1 +1 @@
|
||||
foo
|
||||
example.com/foo
|
||||
|
||||
2
internal/wire/testdata/EmptyVar/pkg
vendored
2
internal/wire/testdata/EmptyVar/pkg
vendored
@@ -1 +1 @@
|
||||
foo
|
||||
example.com/foo
|
||||
|
||||
@@ -17,7 +17,7 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bar"
|
||||
"example.com/bar"
|
||||
"github.com/google/go-cloud/wire"
|
||||
)
|
||||
|
||||
|
||||
2
internal/wire/testdata/ExportedValue/pkg
vendored
2
internal/wire/testdata/ExportedValue/pkg
vendored
@@ -1 +1 @@
|
||||
foo
|
||||
example.com/foo
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
bar "bar"
|
||||
bar "example.com/bar"
|
||||
)
|
||||
|
||||
// Injectors from wire.go:
|
||||
|
||||
@@ -19,7 +19,7 @@ package main
|
||||
import (
|
||||
"os"
|
||||
|
||||
"bar"
|
||||
"example.com/bar"
|
||||
"github.com/google/go-cloud/wire"
|
||||
)
|
||||
|
||||
|
||||
@@ -1 +1 @@
|
||||
foo
|
||||
example.com/foo
|
||||
|
||||
@@ -17,7 +17,7 @@ package main
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"foo"
|
||||
"example.com/foo"
|
||||
"github.com/google/go-cloud/wire"
|
||||
)
|
||||
|
||||
|
||||
@@ -17,7 +17,7 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"foo"
|
||||
"example.com/foo"
|
||||
"github.com/google/go-cloud/wire"
|
||||
)
|
||||
|
||||
|
||||
@@ -1 +1 @@
|
||||
bar
|
||||
example.com/bar
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
foo "foo"
|
||||
foo "example.com/foo"
|
||||
)
|
||||
|
||||
// Injectors from wire.go:
|
||||
|
||||
2
internal/wire/testdata/InjectInput/pkg
vendored
2
internal/wire/testdata/InjectInput/pkg
vendored
@@ -1 +1 @@
|
||||
foo
|
||||
example.com/foo
|
||||
|
||||
@@ -1 +1 @@
|
||||
foo
|
||||
example.com/foo
|
||||
|
||||
2
internal/wire/testdata/InjectWithPanic/pkg
vendored
2
internal/wire/testdata/InjectWithPanic/pkg
vendored
@@ -1 +1 @@
|
||||
foo
|
||||
example.com/foo
|
||||
|
||||
2
internal/wire/testdata/InterfaceBinding/pkg
vendored
2
internal/wire/testdata/InterfaceBinding/pkg
vendored
@@ -1 +1 @@
|
||||
foo
|
||||
example.com/foo
|
||||
|
||||
@@ -1 +1 @@
|
||||
foo
|
||||
example.com/foo
|
||||
|
||||
2
internal/wire/testdata/InterfaceValue/pkg
vendored
2
internal/wire/testdata/InterfaceValue/pkg
vendored
@@ -1 +1 @@
|
||||
foo
|
||||
example.com/foo
|
||||
|
||||
@@ -1 +1 @@
|
||||
foo
|
||||
example.com/foo
|
||||
|
||||
@@ -1 +1 @@
|
||||
foo
|
||||
example.com/foo
|
||||
|
||||
@@ -1 +1 @@
|
||||
foo
|
||||
example.com/foo
|
||||
|
||||
@@ -1,2 +1,2 @@
|
||||
no provider found for foo.Foo
|
||||
no provider found for foo.Bar
|
||||
no provider found for example.com/foo.Foo
|
||||
no provider found for example.com/foo.Bar
|
||||
|
||||
2
internal/wire/testdata/NamingWorstCase/pkg
vendored
2
internal/wire/testdata/NamingWorstCase/pkg
vendored
@@ -1 +1 @@
|
||||
foo
|
||||
example.com/foo
|
||||
|
||||
@@ -1 +1 @@
|
||||
foo
|
||||
example.com/foo
|
||||
|
||||
2
internal/wire/testdata/NiladicIdentity/pkg
vendored
2
internal/wire/testdata/NiladicIdentity/pkg
vendored
@@ -1 +1 @@
|
||||
foo
|
||||
example.com/foo
|
||||
|
||||
2
internal/wire/testdata/NiladicValue/pkg
vendored
2
internal/wire/testdata/NiladicValue/pkg
vendored
@@ -1 +1 @@
|
||||
foo
|
||||
example.com/foo
|
||||
|
||||
@@ -1 +1 @@
|
||||
foo
|
||||
example.com/foo
|
||||
|
||||
@@ -1 +1 @@
|
||||
foo
|
||||
example.com/foo
|
||||
|
||||
2
internal/wire/testdata/NoopBuild/pkg
vendored
2
internal/wire/testdata/NoopBuild/pkg
vendored
@@ -1 +1 @@
|
||||
foo
|
||||
example.com/foo
|
||||
|
||||
2
internal/wire/testdata/PartialCleanup/pkg
vendored
2
internal/wire/testdata/PartialCleanup/pkg
vendored
@@ -1 +1 @@
|
||||
foo
|
||||
example.com/foo
|
||||
|
||||
2
internal/wire/testdata/PkgImport/foo/foo.go
vendored
2
internal/wire/testdata/PkgImport/foo/foo.go
vendored
@@ -17,7 +17,7 @@ package main
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"bar"
|
||||
"example.com/bar"
|
||||
"github.com/google/go-cloud/wire"
|
||||
)
|
||||
|
||||
|
||||
2
internal/wire/testdata/PkgImport/pkg
vendored
2
internal/wire/testdata/PkgImport/pkg
vendored
@@ -1 +1 @@
|
||||
foo
|
||||
example.com/foo
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
bar "bar"
|
||||
bar "example.com/bar"
|
||||
)
|
||||
|
||||
// Injectors from wire.go:
|
||||
|
||||
2
internal/wire/testdata/ReturnError/pkg
vendored
2
internal/wire/testdata/ReturnError/pkg
vendored
@@ -1 +1 @@
|
||||
foo
|
||||
example.com/foo
|
||||
|
||||
2
internal/wire/testdata/Struct/pkg
vendored
2
internal/wire/testdata/Struct/pkg
vendored
@@ -1 +1 @@
|
||||
foo
|
||||
example.com/foo
|
||||
|
||||
2
internal/wire/testdata/StructPointer/pkg
vendored
2
internal/wire/testdata/StructPointer/pkg
vendored
@@ -1 +1 @@
|
||||
foo
|
||||
example.com/foo
|
||||
|
||||
2
internal/wire/testdata/TwoDeps/pkg
vendored
2
internal/wire/testdata/TwoDeps/pkg
vendored
@@ -1 +1 @@
|
||||
foo
|
||||
example.com/foo
|
||||
|
||||
@@ -17,7 +17,7 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bar"
|
||||
"example.com/bar"
|
||||
"github.com/google/go-cloud/wire"
|
||||
)
|
||||
|
||||
|
||||
2
internal/wire/testdata/UnexportedValue/pkg
vendored
2
internal/wire/testdata/UnexportedValue/pkg
vendored
@@ -1 +1 @@
|
||||
foo
|
||||
example.com/foo
|
||||
|
||||
2
internal/wire/testdata/UnusedProviders/pkg
vendored
2
internal/wire/testdata/UnusedProviders/pkg
vendored
@@ -1 +1 @@
|
||||
foo
|
||||
example.com/foo
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
unused provider set "unusedSet"
|
||||
unused provider "provideUnused"
|
||||
unused value of type string
|
||||
unused interface binding to type foo.Fooer
|
||||
unused interface binding to type example.com/foo.Fooer
|
||||
|
||||
2
internal/wire/testdata/ValueChain/pkg
vendored
2
internal/wire/testdata/ValueChain/pkg
vendored
@@ -1 +1 @@
|
||||
foo
|
||||
example.com/foo
|
||||
|
||||
2
internal/wire/testdata/ValueConversion/pkg
vendored
2
internal/wire/testdata/ValueConversion/pkg
vendored
@@ -1 +1 @@
|
||||
foo
|
||||
example.com/foo
|
||||
|
||||
@@ -1 +1 @@
|
||||
foo
|
||||
example.com/foo
|
||||
|
||||
2
internal/wire/testdata/VarValue/pkg
vendored
2
internal/wire/testdata/VarValue/pkg
vendored
@@ -1 +1 @@
|
||||
foo
|
||||
example.com/foo
|
||||
|
||||
2
internal/wire/testdata/Vendor/foo/wire.go
vendored
2
internal/wire/testdata/Vendor/foo/wire.go
vendored
@@ -17,7 +17,7 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bar"
|
||||
"example.com/bar"
|
||||
"github.com/google/go-cloud/wire"
|
||||
)
|
||||
|
||||
|
||||
2
internal/wire/testdata/Vendor/pkg
vendored
2
internal/wire/testdata/Vendor/pkg
vendored
@@ -1 +1 @@
|
||||
foo
|
||||
example.com/foo
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
bar "bar"
|
||||
bar "example.com/bar"
|
||||
)
|
||||
|
||||
// Injectors from wire.go:
|
||||
|
||||
@@ -71,6 +71,10 @@ func TestWire(t *testing.T) {
|
||||
for _, test := range tests {
|
||||
test := test
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
if test.name == "Vendor" && os.Getenv("GO111MODULE") != "off" {
|
||||
// TODO: Remove the GO111MODULE check when it is not relevant (maybe after Go 1.12).
|
||||
t.Skip("Skipped testing for vendored package for Go module turned on, see https://github.com/google/go-cloud/issues/326")
|
||||
}
|
||||
t.Parallel()
|
||||
|
||||
// Run Wire from a fake build context.
|
||||
@@ -147,6 +151,9 @@ func goBuildCheck(test *testCase, wd string, bctx *build.Context, gen []byte) er
|
||||
return err
|
||||
}
|
||||
}
|
||||
if err := writeGoMod(gopath); err != nil {
|
||||
return err
|
||||
}
|
||||
testExePath := filepath.Join(gopath, "bin", "testprog")
|
||||
realBuildCtx := &build.Context{
|
||||
GOARCH: bctx.GOARCH,
|
||||
@@ -158,7 +165,8 @@ func goBuildCheck(test *testCase, wd string, bctx *build.Context, gen []byte) er
|
||||
BuildTags: bctx.BuildTags,
|
||||
ReleaseTags: bctx.ReleaseTags,
|
||||
}
|
||||
if err := runGo(realBuildCtx, "build", "-o", testExePath, genPkg.ImportPath); err != nil {
|
||||
buildDir := filepath.Join(gopath, "src", genPkg.ImportPath)
|
||||
if err := runGo(realBuildCtx, buildDir, "build", "-o", testExePath); err != nil {
|
||||
return fmt.Errorf("build: %v", err)
|
||||
}
|
||||
|
||||
@@ -342,7 +350,7 @@ func loadTestCase(root string, wireGoSrc []byte) (*testCase, error) {
|
||||
if !*setup.Record {
|
||||
wantWireOutput, err = ioutil.ReadFile(filepath.Join(root, "want", "wire_gen.go"))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("load test case %s: %v. If this is a new testcase, run with -record to generate the wire_gen.go file.", name, err)
|
||||
return nil, fmt.Errorf("load test case %s: %v, if this is a new testcase, run with -record to generate the wire_gen.go file", name, err)
|
||||
}
|
||||
}
|
||||
wantProgramOutput, err = ioutil.ReadFile(filepath.Join(root, "want", "program_out.txt"))
|
||||
@@ -368,7 +376,7 @@ func loadTestCase(root string, wireGoSrc []byte) (*testCase, error) {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
goFiles[rel] = data
|
||||
goFiles[filepath.Join("example.com", rel)] = data
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
@@ -576,10 +584,45 @@ func (test *testCase) materialize(gopath string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func runGo(bctx *build.Context, args ...string) error {
|
||||
// writeGoMod generates go.mod files for the test package and its dependency.
|
||||
// The file structure looks like:
|
||||
//
|
||||
// gopath/src/
|
||||
//
|
||||
// example.com/
|
||||
//
|
||||
// go.mod
|
||||
// replaces dependency with local copied one
|
||||
//
|
||||
// ... (Packages to be built and tested)
|
||||
// any Go files copied recursively
|
||||
//
|
||||
// github.com/google/go-cloud/
|
||||
//
|
||||
// go.mod
|
||||
//
|
||||
// ... (Dependency files copied)
|
||||
func writeGoMod(gopath string) error {
|
||||
importPath := "example.com"
|
||||
depPath := "github.com/google/go-cloud"
|
||||
depLoc := filepath.Join(gopath, "src", filepath.FromSlash(depPath))
|
||||
example := fmt.Sprintf("module %s\n\nreplace %s => %s\n", importPath, depPath, depLoc)
|
||||
gomod := filepath.Join(gopath, "src", importPath, "go.mod")
|
||||
if err := ioutil.WriteFile(gomod, []byte(example), 0666); err != nil {
|
||||
return fmt.Errorf("generate go.mod for %s: %v", gomod, err)
|
||||
}
|
||||
if err := ioutil.WriteFile(filepath.Join(depLoc, "go.mod"), []byte("module "+depPath), 0666); err != nil {
|
||||
return fmt.Errorf("generate go.mod for %s: %v", depPath, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// runGo runs a go command in dir.
|
||||
func runGo(bctx *build.Context, dir string, args ...string) error {
|
||||
exe := filepath.Join(bctx.GOROOT, "bin", "go")
|
||||
c := exec.Command(exe, args...)
|
||||
c.Env = append(os.Environ(), "GOROOT="+bctx.GOROOT, "GOARCH="+bctx.GOARCH, "GOOS="+bctx.GOOS, "GOPATH="+bctx.GOPATH)
|
||||
c.Dir = dir
|
||||
if bctx.CgoEnabled {
|
||||
c.Env = append(c.Env, "CGO_ENABLED=1")
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user