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)