From 777c1c4331d0ea37a5369a7569f5ecc95926b8db Mon Sep 17 00:00:00 2001 From: Ross Light Date: Wed, 11 Jul 2018 09:21:23 -0700 Subject: [PATCH] wire: handle build tags with relative paths correctly (google/go-cloud#188) 5cfd0141dd95acd1a98a1b7f6de5b8b304077634 worked correctly with absolute import paths, but would fail with relative import paths (most commonly when passing "." in the default invocation of gowire). This commit introduces test infrastructure to allow relative import paths to be given as test inputs, along with a test case that addresses the regression. --- .../testdata/BuildTagsRelativePkg/bar/bar.go | 23 +++++++++++++++ .../BuildTagsRelativePkg/bar/bar_inject.go | 22 +++++++++++++++ .../testdata/BuildTagsRelativePkg/foo/foo.go | 21 ++++++++++++++ .../testdata/BuildTagsRelativePkg/foo/wire.go | 28 +++++++++++++++++++ .../testdata/BuildTagsRelativePkg/out.txt | 1 + .../wire/testdata/BuildTagsRelativePkg/pkg | 1 + internal/wire/wire.go | 17 ++++++----- internal/wire/wire_test.go | 22 +++++++++++---- 8 files changed, 122 insertions(+), 13 deletions(-) create mode 100644 internal/wire/testdata/BuildTagsRelativePkg/bar/bar.go create mode 100644 internal/wire/testdata/BuildTagsRelativePkg/bar/bar_inject.go create mode 100644 internal/wire/testdata/BuildTagsRelativePkg/foo/foo.go create mode 100644 internal/wire/testdata/BuildTagsRelativePkg/foo/wire.go create mode 100644 internal/wire/testdata/BuildTagsRelativePkg/out.txt create mode 100644 internal/wire/testdata/BuildTagsRelativePkg/pkg diff --git a/internal/wire/testdata/BuildTagsRelativePkg/bar/bar.go b/internal/wire/testdata/BuildTagsRelativePkg/bar/bar.go new file mode 100644 index 0000000..3cd29fd --- /dev/null +++ b/internal/wire/testdata/BuildTagsRelativePkg/bar/bar.go @@ -0,0 +1,23 @@ +// Copyright 2018 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +//+build !wireinject + +// Package bar includes both wireinject and non-wireinject variants. +package bar + +import "github.com/google/go-cloud/wire" + +// Set provides a friendly user greeting. +var Set = wire.NewSet(wire.Value("Hello, World!")) diff --git a/internal/wire/testdata/BuildTagsRelativePkg/bar/bar_inject.go b/internal/wire/testdata/BuildTagsRelativePkg/bar/bar_inject.go new file mode 100644 index 0000000..97c662b --- /dev/null +++ b/internal/wire/testdata/BuildTagsRelativePkg/bar/bar_inject.go @@ -0,0 +1,22 @@ +// Copyright 2018 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +//+build wireinject + +package bar + +import "github.com/google/go-cloud/wire" + +// Set provides an unfriendly user greeting. +var Set = wire.NewSet(wire.Value("Bah humbug! This is the wrong variant!")) diff --git a/internal/wire/testdata/BuildTagsRelativePkg/foo/foo.go b/internal/wire/testdata/BuildTagsRelativePkg/foo/foo.go new file mode 100644 index 0000000..f8216df --- /dev/null +++ b/internal/wire/testdata/BuildTagsRelativePkg/foo/foo.go @@ -0,0 +1,21 @@ +// Copyright 2018 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package main + +import "fmt" + +func main() { + fmt.Println(injectedMessage()) +} diff --git a/internal/wire/testdata/BuildTagsRelativePkg/foo/wire.go b/internal/wire/testdata/BuildTagsRelativePkg/foo/wire.go new file mode 100644 index 0000000..3e08683 --- /dev/null +++ b/internal/wire/testdata/BuildTagsRelativePkg/foo/wire.go @@ -0,0 +1,28 @@ +// Copyright 2018 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +//+build wireinject + +package main + +import ( + "bar" + + "github.com/google/go-cloud/wire" +) + +func injectedMessage() string { + wire.Build(bar.Set) + return "" +} diff --git a/internal/wire/testdata/BuildTagsRelativePkg/out.txt b/internal/wire/testdata/BuildTagsRelativePkg/out.txt new file mode 100644 index 0000000..8ab686e --- /dev/null +++ b/internal/wire/testdata/BuildTagsRelativePkg/out.txt @@ -0,0 +1 @@ +Hello, World! diff --git a/internal/wire/testdata/BuildTagsRelativePkg/pkg b/internal/wire/testdata/BuildTagsRelativePkg/pkg new file mode 100644 index 0000000..b10f96c --- /dev/null +++ b/internal/wire/testdata/BuildTagsRelativePkg/pkg @@ -0,0 +1 @@ +./foo diff --git a/internal/wire/wire.go b/internal/wire/wire.go index 7dd49b6..5acb5cb 100644 --- a/internal/wire/wire.go +++ b/internal/wire/wire.go @@ -51,18 +51,21 @@ func Generate(bctx *build.Context, wd string, pkg string) ([]byte, []error) { return path == mainPkg.ImportPath }, FindPackage: func(bctx *build.Context, importPath, fromDir string, mode build.ImportMode) (*build.Package, error) { - if importPath == mainPkg.ImportPath { - // Load in the generated package with the wireinject build tag - // to pick up the injector template. The imported packages - // should be imported as normal. Since the *build.Context is - // shared between calls to FindPackage, this uses a copy. + // Optimistically try to load in the package with normal build tags. + pkg, err := bctx.Import(importPath, fromDir, mode) + + // If this is the generated package, then load it in with the + // wireinject build tag to pick up the injector template. Since + // the *build.Context is shared between calls to FindPackage, this + // uses a copy. + if pkg != nil && pkg.ImportPath == mainPkg.ImportPath { bctx2 := new(build.Context) *bctx2 = *bctx n := len(bctx2.BuildTags) bctx2.BuildTags = append(bctx2.BuildTags[:n:n], "wireinject") - bctx = bctx2 + pkg, err = bctx2.Import(importPath, fromDir, mode) } - return bctx.Import(importPath, fromDir, mode) + return pkg, err }, } conf.Import(pkg) diff --git a/internal/wire/wire_test.go b/internal/wire/wire_test.go index 17f1af9..b596db5 100644 --- a/internal/wire/wire_test.go +++ b/internal/wire/wire_test.go @@ -34,10 +34,6 @@ import ( ) func TestWire(t *testing.T) { - wd, err := os.Getwd() - if err != nil { - t.Fatal(err) - } const testRoot = "testdata" testdataEnts, err := ioutil.ReadDir(testRoot) // ReadDir sorts by name. if err != nil { @@ -61,6 +57,7 @@ func TestWire(t *testing.T) { } tests = append(tests, test) } + wd := filepath.Join(magicGOPATH(), "src") t.Run("Generate", func(t *testing.T) { if _, err := os.Stat(filepath.Join(build.Default.GOROOT, "bin", "go")); err != nil { @@ -70,6 +67,8 @@ func TestWire(t *testing.T) { test := test t.Run(test.name, func(t *testing.T) { t.Parallel() + + // Run gowire from a fake build context. bctx := test.buildContext() gen, errs := Generate(bctx, wd, test.pkg) if len(gen) > 0 { @@ -93,6 +92,14 @@ func TestWire(t *testing.T) { t.Fatal("wirego succeeded; want error") } + // Find the absolute import path, since test.pkg may be a relative + // import path. + genPkg, err := bctx.Import(test.pkg, wd, build.FindOnly) + if err != nil { + t.Fatal(err) + } + + // Run a `go build` with the generated output. gopath, err := ioutil.TempDir("", "wire_test") if err != nil { t.Fatal(err) @@ -102,7 +109,7 @@ func TestWire(t *testing.T) { t.Fatal(err) } if len(gen) > 0 { - genPath := filepath.Join(gopath, "src", filepath.FromSlash(test.pkg), "wire_gen.go") + genPath := filepath.Join(gopath, "src", filepath.FromSlash(genPkg.ImportPath), "wire_gen.go") if err := ioutil.WriteFile(genPath, gen, 0666); err != nil { t.Fatal(err) } @@ -118,9 +125,12 @@ func TestWire(t *testing.T) { BuildTags: bctx.BuildTags, ReleaseTags: bctx.ReleaseTags, } - if err := runGo(realBuildCtx, "build", "-o", testExePath, test.pkg); err != nil { + if err := runGo(realBuildCtx, "build", "-o", testExePath, genPkg.ImportPath); err != nil { t.Fatal("build:", err) } + + // Run the resulting program and compare its output to the expected + // output. out, err := exec.Command(testExePath).Output() if err != nil { t.Error("run compiled program:", err)