cmd/wire: add a --header_file flag to the "gen" and "diff" commands (#175)

This commit is contained in:
Robert van Gent
2019-05-16 09:56:42 -07:00
committed by GitHub
parent d76a979091
commit fe01654174
10 changed files with 133 additions and 11 deletions

View File

@@ -52,7 +52,7 @@ trap cleanup EXIT
git clone -b "$UPSTREAM_BRANCH" . "$MASTER_CLONE_DIR" &> /dev/null
incompatible_change_pkgs=()
PKGS=$(cd "$MASTER_CLONE_DIR"; go list ./... | grep -v test)
PKGS=$(cd "$MASTER_CLONE_DIR"; go list ./... | grep -v test | grep -v internal)
for pkg in $PKGS; do
echo " Testing ${pkg}..."

View File

@@ -0,0 +1,29 @@
// Copyright 2019 The Wire Authors
//
// 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(injectFoo())
}
type Foo int
func provideFoo() Foo {
return 41
}

View File

@@ -0,0 +1,26 @@
// Copyright 2019 The Wire Authors
//
// 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 (
"github.com/google/wire"
)
func injectFoo() Foo {
wire.Build(provideFoo)
return Foo(0)
}

2
internal/wire/testdata/Header/header vendored Normal file
View File

@@ -0,0 +1,2 @@
// This is a sample header file.
//

1
internal/wire/testdata/Header/pkg vendored Normal file
View File

@@ -0,0 +1 @@
example.com/foo

View File

@@ -0,0 +1 @@
41

View File

@@ -0,0 +1,15 @@
// This is a sample header file.
//
// Code generated by Wire. DO NOT EDIT.
//go:generate wire
//+build !wireinject
package main
// Injectors from wire.go:
func injectFoo() Foo {
foo := provideFoo()
return foo
}

View File

@@ -60,6 +60,12 @@ func (gen GenerateResult) Commit() error {
return ioutil.WriteFile(gen.OutputPath, gen.Content, 0666)
}
// GenerateOptions holds options for Generate.
type GenerateOptions struct {
// Header will be inserted at the start of each generated file.
Header []byte
}
// Generate performs dependency injection for the packages that match the given
// patterns, return a GenerateResult for each package. The package pattern is
// defined by the underlying build system. For the go tool, this is described at
@@ -72,7 +78,10 @@ func (gen GenerateResult) Commit() error {
// takes precedence.
//
// Generate may return one or more errors if it failed to load the packages.
func Generate(ctx context.Context, wd string, env []string, patterns []string) ([]GenerateResult, []error) {
func Generate(ctx context.Context, wd string, env []string, patterns []string, opts *GenerateOptions) ([]GenerateResult, []error) {
if opts == nil {
opts = &GenerateOptions{}
}
pkgs, errs := load(ctx, wd, env, patterns)
if len(errs) > 0 {
return nil, errs
@@ -94,6 +103,9 @@ func Generate(ctx context.Context, wd string, env []string, patterns []string) (
}
copyNonInjectorDecls(g, injectorFiles, pkg.TypesInfo)
goSrc := g.frame()
if len(opts.Header) > 0 {
goSrc = append(opts.Header, goSrc...)
}
fmtSrc, err := format.Source(goSrc)
if err != nil {
// This is likely a bug from a poorly generated source file.

View File

@@ -90,7 +90,7 @@ func TestWire(t *testing.T) {
t.Fatal(err)
}
wd := filepath.Join(gopath, "src", "example.com")
gens, errs := Generate(ctx, wd, append(os.Environ(), "GOPATH="+gopath), []string{test.pkg})
gens, errs := Generate(ctx, wd, append(os.Environ(), "GOPATH="+gopath), []string{test.pkg}, &GenerateOptions{Header: test.header})
var gen GenerateResult
if len(gens) > 1 {
t.Fatalf("got %d generated files, want 0 or 1", len(gens))
@@ -428,6 +428,7 @@ func scrubLineColumn(s string) (replacement string, n int) {
type testCase struct {
name string
pkg string
header []byte
goFiles map[string][]byte
wantProgramOutput []byte
wantWireOutput []byte
@@ -471,6 +472,7 @@ func loadTestCase(root string, wireGoSrc []byte) (*testCase, error) {
if err != nil {
return nil, fmt.Errorf("load test case %s: %v", name, err)
}
header, _ := ioutil.ReadFile(filepath.Join(root, "header"))
var wantProgramOutput []byte
var wantWireOutput []byte
wireErrb, err := ioutil.ReadFile(filepath.Join(root, "want", "wire_errs.txt"))
@@ -521,6 +523,7 @@ func loadTestCase(root string, wireGoSrc []byte) (*testCase, error) {
return &testCase{
name: name,
pkg: string(bytes.TrimSpace(pkg)),
header: header,
goFiles: goFiles,
wantWireOutput: wantWireOutput,
wantProgramOutput: wantProgramOutput,