-
Notifications
You must be signed in to change notification settings - Fork 261
Add build tags support #796
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
Changes from 2 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 |
---|---|---|
|
@@ -102,7 +102,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", | ||
} | ||
if err := Generate(tmp, []string{tmp}, opt); err != nil { | ||
return "", err | ||
|
@@ -237,6 +238,80 @@ 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) | ||
} | ||
|
||
tmp := t.TempDir() | ||
save := func(f, data string) { | ||
if err := os.WriteFile(filepath.Join(tmp, f), []byte(data), 0644); err != nil { | ||
t.Fatalf("error writing %s: %v", f, err) | ||
} | ||
} | ||
|
||
// Copy the files from dir to the temp directory. | ||
for _, file := range files { | ||
filename := file.Name() | ||
if !strings.HasSuffix(filename, ".go") || strings.HasSuffix(filename, generatedCodeFile) { | ||
continue | ||
} | ||
|
||
// 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) | ||
save(filename, contents) | ||
} | ||
save("go.mod", goModFile) | ||
|
||
// Run "go mod tidy" | ||
tidy := exec.Command("go", "mod", "tidy") | ||
tidy.Dir = tmp | ||
tidy.Stdout = os.Stdout | ||
tidy.Stderr = os.Stderr | ||
if err := tidy.Run(); err != nil { | ||
t.Fatalf("go mod tidy: %v", err) | ||
} | ||
|
||
// Run the "weaver generate" command with no build tags. Verify that the command | ||
// doesn't succeed because bad.go does not compile. | ||
err = Generate(tmp, []string{tmp}, Options{Warn: func(err error) { t.Log(err) }}) | ||
if err == nil { | ||
t.Fatal("expected generator to return an error; got nil error") | ||
} | ||
// Verify that no weaver_gen.go file was generated. | ||
_, err = os.ReadFile(filepath.Join(tmp, generatedCodeFile)) | ||
if err == nil { | ||
t.Fatal("expected no generated file") | ||
} | ||
|
||
// Run the "weaver generate" command with the build tag "good". Verify that the | ||
// command succeeds because the bad.go file is ignored, and the weaver_gen.go | ||
// contains only generated code for the good.go. | ||
err = Generate(tmp, []string{tmp}, Options{Warn: func(err error) { t.Log(err) }, BuildTags: "good"}) | ||
if err != nil { | ||
t.Fatalf("unexpected generator error: %v", err) | ||
} | ||
// Verify that the weaver_gen.go file doesn't contain generated code for the bad service. | ||
output, err := os.ReadFile(filepath.Join(tmp, generatedCodeFile)) | ||
if err != nil { | ||
t.Fatalf("unable to read the weaver_gen.go file: %v", err) | ||
} | ||
if strings.Contains(string(output), "bad") { | ||
t.Fatalf("unexpected generated code for the bad service") | ||
} | ||
if !strings.Contains(string(output), "good") { | ||
t.Fatalf("expected 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 | ||
|
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