Skip to content

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

Merged
merged 4 commits into from
Sep 10, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 33 additions & 60 deletions internal/tool/generate/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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".
Expand Down Expand Up @@ -103,7 +104,7 @@ func runGenerator(t *testing.T, directory, filename, contents string, subdirs []
// Run "weaver generate".
opt := Options{
Warn: func(err error) { t.Log(err) },
BuildTags: "ignoreWeaverGen",
BuildTags: "ignoreWeaverGen" + "," + strings.Join(buildTags, ","),
}
if err := Generate(tmp, []string{tmp}, opt); err != nil {
return "", err
Expand Down Expand Up @@ -135,7 +136,12 @@ 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")

var buildTagsArg string
if len(buildTags) > 0 {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought the earlier code always added "ignoreWeaveGen". So can we supply -tags unconditionally?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great observation. Fixed

buildTagsArg = "-tags=" + strings.Join(buildTags, ",")
}
gobuild := exec.Command("go", "build", buildTagsArg)
gobuild.Dir = tmp
gobuild.Stdout = os.Stdout
gobuild.Stderr = os.Stderr
Expand Down Expand Up @@ -219,7 +225,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"}, []string{})
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you just pass nil for the empty slice?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

if err != nil {
t.Fatalf("error running generator: %v", err)
}
Expand Down Expand Up @@ -247,68 +253,35 @@ func TestGeneratorBuildWithTags(t *testing.T) {
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
}
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)
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")
}
// 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, []string{}, []string{"good"})

// 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")
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")
}
})
}
}

Expand Down Expand Up @@ -361,7 +334,7 @@ func TestGeneratorErrors(t *testing.T) {
}

// Run "weaver generate".
output, err := runGenerator(t, dir, filename, contents, []string{})
output, err := runGenerator(t, dir, filename, contents, []string{}, []string{})
errfile := strings.TrimSuffix(filename, ".go") + "_error.txt"
if err == nil {
os.Remove(filepath.Join(dir, errfile))
Expand Down
Loading