-
Notifications
You must be signed in to change notification settings - Fork 247
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add build tags support #796
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -62,7 +62,8 @@ replace github.com/ServiceWeaver/weaver => %s | |
// | ||
// If "weaver generate" succeeds, the produced weaver_gen.go file is written in | ||
// the provided directory with name ${filename}_weaver_gen.go. | ||
func runGenerator(t *testing.T, directory, filename, contents string, subdirs []string) (string, error) { | ||
func runGenerator(t *testing.T, directory, filename, contents string, subdirs []string, | ||
buildTags []string) (string, error) { | ||
// runGenerator creates a temporary directory, copies the file and all | ||
// subdirs into it, writes a go.mod file, runs "go mod tidy", and finally | ||
// runs "weaver generate". | ||
|
@@ -102,7 +103,8 @@ func runGenerator(t *testing.T, directory, filename, contents string, subdirs [] | |
|
||
// Run "weaver generate". | ||
opt := Options{ | ||
Warn: func(err error) { t.Log(err) }, | ||
Warn: func(err error) { t.Log(err) }, | ||
BuildTags: "ignoreWeaverGen" + "," + strings.Join(buildTags, ","), | ||
} | ||
if err := Generate(tmp, []string{tmp}, opt); err != nil { | ||
return "", err | ||
|
@@ -134,7 +136,7 @@ func runGenerator(t *testing.T, directory, filename, contents string, subdirs [] | |
if err := tidy.Run(); err != nil { | ||
t.Fatalf("go mod tidy: %v", err) | ||
} | ||
gobuild := exec.Command("go", "build") | ||
gobuild := exec.Command("go", "build", "-tags="+opt.BuildTags) | ||
gobuild.Dir = tmp | ||
gobuild.Stdout = os.Stdout | ||
gobuild.Stderr = os.Stderr | ||
|
@@ -218,7 +220,7 @@ func TestGenerator(t *testing.T) { | |
} | ||
|
||
// Run "weaver generate". | ||
output, err := runGenerator(t, dir, filename, contents, []string{"sub1", "sub2"}) | ||
output, err := runGenerator(t, dir, filename, contents, []string{"sub1", "sub2"}, nil) | ||
if err != nil { | ||
t.Fatalf("error running generator: %v", err) | ||
} | ||
|
@@ -237,6 +239,47 @@ func TestGenerator(t *testing.T) { | |
} | ||
} | ||
|
||
// TestGeneratorBuildWithTags runs "weaver generate" on all the files in | ||
// testdata/tags and checks if the command succeeds. Each file should have some build tags. | ||
func TestGeneratorBuildWithTags(t *testing.T) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why can't we use runGenerator for this test? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. runGenerator copies every single file in a temp directory, and tries to build it. I chose this approach because I wanted to run go build on all the files in the directory and check that weaver_gen contains things only for the good files. That adds a little bit of boilerplate code for sure. If you have a strong preference, I can switch to calling runGenerator instead. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am fine with you keeping what you have, but I don't quite understand the downside of using runGenerator. It would copy all of the files and build it. You can still check that weaver_gen.go does not contain the components defined in the bad file. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
const dir = "testdata/tags" | ||
files, err := os.ReadDir(dir) | ||
if err != nil { | ||
t.Fatalf("cannot list files in %q", dir) | ||
} | ||
|
||
for _, file := range files { | ||
filename := file.Name() | ||
if !strings.HasSuffix(filename, ".go") || strings.HasSuffix(filename, generatedCodeFile) { | ||
continue | ||
} | ||
t.Run(filename, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
// Read the test file. | ||
bits, err := os.ReadFile(filepath.Join(dir, filename)) | ||
if err != nil { | ||
t.Fatalf("cannot read %q: %v", filename, err) | ||
} | ||
contents := string(bits) | ||
// Run "weaver generate". | ||
output, err := runGenerator(t, dir, filename, contents, nil, []string{"good"}) | ||
|
||
if filename == "good.go" { | ||
// Verify that the error is nil and the weaver_gen.go contains generated code for the good service. | ||
if err != nil || !strings.Contains(output, "GoodService") { | ||
t.Fatalf("expected generated code for the good service") | ||
} | ||
return | ||
} | ||
// For the bad.go verify that the error is not nil and there is no output. | ||
if err == nil || len(output) > 0 { | ||
t.Fatalf("expected no generated code for the good service") | ||
} | ||
}) | ||
} | ||
} | ||
|
||
// TestGeneratorErrors runs "weaver generate" on all of the files in | ||
// testdata/errors. | ||
// Every file in testdata/errors must begin with a single line header that looks | ||
|
@@ -286,7 +329,7 @@ func TestGeneratorErrors(t *testing.T) { | |
} | ||
|
||
// Run "weaver generate". | ||
output, err := runGenerator(t, dir, filename, contents, []string{}) | ||
output, err := runGenerator(t, dir, filename, contents, nil, nil) | ||
errfile := strings.TrimSuffix(filename, ".go") + "_error.txt" | ||
if err == nil { | ||
os.Remove(filepath.Join(dir, errfile)) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// Copyright 2024 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 | ||
// | ||
// http://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. | ||
|
||
//go:build !good | ||
|
||
package tags | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/ServiceWeaver/weaver" | ||
) | ||
|
||
type BadService interface { | ||
DoSomething(context.Context) error | ||
} | ||
|
||
type badServiceImpl struct { | ||
weaver.Implements[BadService] | ||
} | ||
|
||
func (b *badServiceImpl) DoSomething(context.Context) error { | ||
Some code that does not compile | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Copyright 2024 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 | ||
// | ||
// http://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. | ||
|
||
//go:build good | ||
|
||
package tags | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/ServiceWeaver/weaver" | ||
) | ||
|
||
type GoodService interface { | ||
DoSomething(context.Context) error | ||
} | ||
|
||
type goodServiceImpl struct { | ||
weaver.Implements[GoodService] | ||
} | ||
|
||
func (g *goodServiceImpl) DoSomething(context.Context) error { | ||
fmt.Println("Hello world!") | ||
return nil | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems like the BuildTags fields includes the "-tags=" prefix. That seems a bit wrong to me. Could just contain the tags, and we would add the -tags= prefix below when setting BuildFlags:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point. Fixed