-
Notifications
You must be signed in to change notification settings - Fork 377
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 a go_test directive to enable generating go_test targets per _test.go file #1597
Merged
Merged
Changes from 8 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
9ab60bc
add a go_test directive to enable generating go_test targets per _tes…
shahms fe7b983
Merge branch 'master' into per-file-go_test
shahms 518c99d
Merge branch 'master' into per-file-go_test
shahms 68fcc75
Merge branch 'master' into per-file-go_test
shahms 3f4cea8
address comments
shahms fbe9226
Merge branch 'master' into per-file-go_test
shahms 8e98986
Merge branch 'master' into per-file-go_test
shahms cf24f22
add new tests for updating to per-file mode
shahms 6448481
Merge branch 'master' into per-file-go_test
fmeum f583aa5
Merge branch 'master' into per-file-go_test
fmeum 25da6b2
Merge branch 'master' into per-file-go_test
fmeum File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,7 +19,6 @@ import ( | |
"errors" | ||
"flag" | ||
"fmt" | ||
"github.com/bazelbuild/bazel-gazelle/internal/module" | ||
"go/build" | ||
"io/ioutil" | ||
"log" | ||
|
@@ -30,6 +29,8 @@ import ( | |
"strconv" | ||
"strings" | ||
|
||
"github.com/bazelbuild/bazel-gazelle/internal/module" | ||
|
||
"github.com/bazelbuild/bazel-gazelle/config" | ||
gzflag "github.com/bazelbuild/bazel-gazelle/flag" | ||
"github.com/bazelbuild/bazel-gazelle/internal/version" | ||
|
@@ -126,18 +127,55 @@ type goConfig struct { | |
// in internal packages. | ||
submodules []moduleRepo | ||
|
||
// testMode determines how go_test targets are generated. | ||
testMode testMode | ||
|
||
// buildDirectives, buildExternalAttr, buildExtraArgsAttr, | ||
// buildFileGenerationAttr, buildFileNamesAttr, buildFileProtoModeAttr and | ||
// buildTagsAttr are attributes for go_repository rules, set on the command | ||
// line. | ||
buildDirectivesAttr, buildExternalAttr, buildExtraArgsAttr, buildFileGenerationAttr, buildFileNamesAttr, buildFileProtoModeAttr, buildTagsAttr string | ||
} | ||
|
||
// testMode determines how go_test rules are generated. | ||
type testMode int | ||
|
||
const ( | ||
// defaultTestMode generates a go_test for the primary package in a directory. | ||
defaultTestMode = iota | ||
|
||
// fileTestMode generates a go_test for each Go test file. | ||
fileTestMode | ||
) | ||
|
||
var ( | ||
defaultGoProtoCompilers = []string{"@io_bazel_rules_go//proto:go_proto"} | ||
defaultGoGrpcCompilers = []string{"@io_bazel_rules_go//proto:go_grpc"} | ||
) | ||
|
||
func (m testMode) String() string { | ||
switch m { | ||
case defaultTestMode: | ||
return "default" | ||
case fileTestMode: | ||
return "file" | ||
default: | ||
log.Panicf("unknown mode %d", m) | ||
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. Don't panic on a |
||
return "" | ||
} | ||
} | ||
|
||
func testModeFromString(s string) (testMode, error) { | ||
switch s { | ||
case "default": | ||
return defaultTestMode, nil | ||
case "file": | ||
return fileTestMode, nil | ||
default: | ||
return 0, fmt.Errorf("unrecognized go_test mode: %q", s) | ||
} | ||
} | ||
|
||
func newGoConfig() *goConfig { | ||
gc := &goConfig{ | ||
goProtoCompilers: defaultGoProtoCompilers, | ||
|
@@ -351,6 +389,7 @@ func (*goLang) KnownDirectives() []string { | |
"go_naming_convention", | ||
"go_naming_convention_external", | ||
"go_proto_compilers", | ||
"go_test", | ||
"go_visibility", | ||
"importmap_prefix", | ||
"prefix", | ||
|
@@ -592,6 +631,14 @@ Update io_bazel_rules_go to a newer version in your WORKSPACE file.` | |
gc.goProtoCompilers = splitValue(d.Value) | ||
} | ||
|
||
case "go_test": | ||
mode, err := testModeFromString(d.Value) | ||
if err != nil { | ||
log.Print(err) | ||
continue | ||
} | ||
gc.testMode = mode | ||
|
||
case "go_visibility": | ||
gc.goVisibility = append(gc.goVisibility, strings.TrimSpace(d.Value)) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# gazelle:go_test file |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") | ||
|
||
go_library( | ||
name = "tests_per_file", | ||
srcs = ["lib.go"], | ||
_gazelle_imports = [], | ||
importpath = "example.com/repo/tests_per_file", | ||
visibility = ["//visibility:public"], | ||
) | ||
|
||
go_test( | ||
name = "bar_test", | ||
srcs = ["bar_test.go"], | ||
_gazelle_imports = ["testing"], | ||
embed = [":tests_per_file"], | ||
) | ||
|
||
go_test( | ||
name = "external_test", | ||
srcs = ["external_test.go"], | ||
_gazelle_imports = [ | ||
"example.com/repo/tests_per_file", | ||
"testing", | ||
], | ||
) | ||
|
||
go_test( | ||
name = "foo_test", | ||
srcs = ["foo_test.go"], | ||
_gazelle_imports = [ | ||
"github.com/bazelbuild/bazel-gazelle/testtools", | ||
"testing", | ||
], | ||
embed = [":tests_per_file"], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package tests_per_file | ||
|
||
import "testing" | ||
|
||
func TestStuff(t *testing.T) {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package tests_per_file_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"example.com/repo/tests_per_file" | ||
) | ||
|
||
func TestStuff(t *testing.T) { | ||
var _ tests_per_file.Type | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package tests_per_file | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/bazelbuild/bazel-gazelle/testtools" | ||
) | ||
|
||
type fileSpec testtools.FileSpec | ||
|
||
func TestStuff(t *testing.T) {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package tests_per_file | ||
|
||
type Type int |
29 changes: 29 additions & 0 deletions
29
language/go/testdata/tests_per_file_from_default/BUILD.old
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# gazelle:go_test file | ||
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") | ||
|
||
go_library( | ||
name = "tests_per_file_from_default", | ||
srcs = ["lib.go"], | ||
_gazelle_imports = [], | ||
importpath = "example.com/repo/tests_per_file_from_default", | ||
visibility = ["//visibility:public"], | ||
) | ||
|
||
go_test( | ||
name = "test_per_file_from_default_test", | ||
srcs = [ | ||
"bar_test.go", | ||
"foo_test.go", | ||
], | ||
_gazelle_imports = ["testing"], | ||
embed = [":tests_per_file_from_default"], | ||
) | ||
|
||
go_test( | ||
name = "external_test", | ||
srcs = ["external_test.go"], | ||
_gazelle_imports = [ | ||
"example.com/repo/tests_per_file_from_default", | ||
"testing", | ||
], | ||
) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
nit