From 5428336c51f18ad628d9c8d7e052a48ec5379590 Mon Sep 17 00:00:00 2001 From: Paul Cody Johnston Date: Wed, 21 Feb 2024 12:50:24 -0700 Subject: [PATCH] Fix proto_compiled_sources with strip_import_prefix (#365) * Migrate example tests to "test_content" attribute * Simplify Makefile * Switch to proto_compile_assets * be more tricky with unused imports * Augment test with strip_import_prefix case * Modify test such that package matches directory structure * Modify test to not include a package * Test README --- BUILD.bazel | 1 + Makefile | 33 ++++--- cmd/examplegen/config.go | 2 +- cmd/examplegen/generator.go | 2 +- cmd/examplegen/template.go | 15 +-- cmd/gencopy/BUILD.bazel | 18 +++- cmd/gencopy/gencopy.go | 20 +--- cmd/gencopy/gencopy_test.go | 85 ++++++++++++++++- example/golden/BUILD.bazel | 94 +++++++++++++++++-- example/golden/README.md | 4 +- .../testdata/proto_compiled_sources/.bazelrc | 1 + .../proto_compiled_sources/.bazelversion | 1 + .../testdata/proto_compiled_sources/BUILD.in | 15 +++ .../testdata/proto_compiled_sources/BUILD.out | 15 +++ .../testdata/proto_compiled_sources/README.md | 10 ++ .../testdata/proto_compiled_sources/WORKSPACE | 0 .../proto_compiled_sources/api/v1/BUILD.in | 0 .../proto_compiled_sources/api/v1/BUILD.out | 16 ++++ .../proto_compiled_sources/api/v1/v1.proto | 7 ++ .../proto_compiled_sources/src/idl/BUILD.in | 1 + .../proto_compiled_sources/src/idl/BUILD.out | 20 ++++ .../proto_compiled_sources/src/idl/svc.proto | 9 ++ rules/example.bzl | 19 +++- 23 files changed, 330 insertions(+), 58 deletions(-) create mode 100644 example/golden/testdata/proto_compiled_sources/.bazelrc create mode 100644 example/golden/testdata/proto_compiled_sources/.bazelversion create mode 100644 example/golden/testdata/proto_compiled_sources/BUILD.in create mode 100644 example/golden/testdata/proto_compiled_sources/BUILD.out create mode 100644 example/golden/testdata/proto_compiled_sources/README.md create mode 100644 example/golden/testdata/proto_compiled_sources/WORKSPACE create mode 100644 example/golden/testdata/proto_compiled_sources/api/v1/BUILD.in create mode 100644 example/golden/testdata/proto_compiled_sources/api/v1/BUILD.out create mode 100644 example/golden/testdata/proto_compiled_sources/api/v1/v1.proto create mode 100644 example/golden/testdata/proto_compiled_sources/src/idl/BUILD.in create mode 100644 example/golden/testdata/proto_compiled_sources/src/idl/BUILD.out create mode 100644 example/golden/testdata/proto_compiled_sources/src/idl/svc.proto diff --git a/BUILD.bazel b/BUILD.bazel index 006a1149b..6aaa0da54 100644 --- a/BUILD.bazel +++ b/BUILD.bazel @@ -64,6 +64,7 @@ filegroup( "WORKSPACE", "go_deps.bzl", "//cmd/gazelle:all_files", + "//cmd/gencopy:all_files", "//deps:all_files", "//language/protobuf:all_files", "//pkg:all_files", diff --git a/Makefile b/Makefile index bf4921aba..635832135 100644 --- a/Makefile +++ b/Makefile @@ -1,44 +1,47 @@ -BAZEL := bazel .PHONY: tidy tidy: deps - $(BAZEL) run @go_sdk//:bin/go -- mod tidy - $(BAZEL) run @go_sdk//:bin/go -- mod vendor + bazel run @go_sdk//:bin/go -- mod tidy + bazel run @go_sdk//:bin/go -- mod vendor find vendor -name 'BUILD.bazel' | xargs rm - $(BAZEL) run //:update_go_deps - $(BAZEL) run //:buildifier - $(BAZEL) run //:gazelle + bazel run //:update_go_deps + bazel run //:buildifier + bazel run //:gazelle .PHONY: gazelle gazelle: - $(BAZEL) run //:gazelle + bazel run //:gazelle .PHONY: deps deps: - $(BAZEL) build //deps:* + bazel build //deps:* cp -f ./bazel-bin/deps/*.bzl deps/ chmod 0644 deps/*.bzl - $(BAZEL) run //:buildifier -- deps/ + bazel run //:buildifier -- deps/ .PHONY: site site: - $(BAZEL) build //example/golden:* + bazel build //example/golden:* cp -f ./bazel-bin/example/golden/*.md docs/ .PHONY: golden_test golden_test: - $(BAZEL) test //example/golden:golden_test + bazel test //example/golden:golden_test --test_output=streamed + +.PHONY: example_test +example_test: + bazel test //example/golden:proto_compiled_sources_test --test_output=streamed .PHONY: test test: - $(BAZEL) test --keep_going //example/... //pkg/... //plugin/... //language/... //rules/... //toolchain/... \ + bazel test --keep_going //example/... //pkg/... //plugin/... //language/... //rules/... //toolchain/... \ --deleted_packages=//plugin/grpc-ecosystem/grpc-gateway .PHONY: get get: - $(BAZEL) run @go_sdk//:bin/go -- get github.com/bazelbuild/bazel-gazelle@v0.31.0 - $(BAZEL) run @go_sdk//:bin/go -- mod download github.com/bazelbuild/buildtools - $(BAZEL) run @go_sdk//:bin/go -- mod vendor + bazel run @go_sdk//:bin/go -- get github.com/bazelbuild/bazel-gazelle@v0.31.0 + bazel run @go_sdk//:bin/go -- mod download github.com/bazelbuild/buildtools + bazel run @go_sdk//:bin/go -- mod vendor update_pnpm_lock: # nvm use 18 diff --git a/cmd/examplegen/config.go b/cmd/examplegen/config.go index 5a042c43e..0b6cbd98d 100644 --- a/cmd/examplegen/config.go +++ b/cmd/examplegen/config.go @@ -10,7 +10,7 @@ type Config struct { Name string Label string TestOut string - TestHeader string + TestContent string MarkdownOut string WorkspaceIn string StripPrefix string diff --git a/cmd/examplegen/generator.go b/cmd/examplegen/generator.go index 0f23df631..bc867c1f0 100644 --- a/cmd/examplegen/generator.go +++ b/cmd/examplegen/generator.go @@ -87,7 +87,7 @@ func generateTest(c *Config) error { defer f.Close() fmt.Fprintln(f, testHeader) - fmt.Fprintln(f, c.TestHeader) + fmt.Fprintln(f, c.TestContent) fmt.Fprintln(f, "var txtar=`") diff --git a/cmd/examplegen/template.go b/cmd/examplegen/template.go index 5c5abd6b8..60930329e 100644 --- a/cmd/examplegen/template.go +++ b/cmd/examplegen/template.go @@ -5,8 +5,17 @@ package main import ( "testing" + "os" "github.com/bazelbuild/rules_go/go/tools/bazel_testing" + "github.com/google/go-cmp/cmp" +) + +var ( + // allow use of os package in other tests + _os_Remove = os.Remove + // allow use of cmp package in other tests + _cmp_Diff = cmp.Diff ) func TestMain(m *testing.M) { @@ -14,10 +23,4 @@ func TestMain(m *testing.M) { Main: txtar, }) } - -func TestBuild(t *testing.T) { - if err := bazel_testing.RunBazel("build", "..."); err != nil { - t.Fatal(err) - } -} ` diff --git a/cmd/gencopy/BUILD.bazel b/cmd/gencopy/BUILD.bazel index 8983fdc8a..2cfa362c5 100644 --- a/cmd/gencopy/BUILD.bazel +++ b/cmd/gencopy/BUILD.bazel @@ -20,5 +20,21 @@ go_test( name = "gencopy_test", srcs = ["gencopy_test.go"], embed = [":gencopy_lib"], - deps = ["@com_github_google_go_cmp//cmp"], + deps = [ + "@bazel_gazelle//testtools:go_default_library", + "@com_github_google_go_cmp//cmp", + ], +) + +filegroup( + name = "all_files", + testonly = True, + srcs = [ + "BUILD.bazel", + "gencopy.bash.in", + "gencopy.bzl", + "gencopy.go", + "gencopy_test.go", + ], + visibility = ["//:__pkg__"], ) diff --git a/cmd/gencopy/gencopy.go b/cmd/gencopy/gencopy.go index 0ef14f30e..b0980dd42 100644 --- a/cmd/gencopy/gencopy.go +++ b/cmd/gencopy/gencopy.go @@ -1,14 +1,12 @@ // gencopy is a utility program that copies bazel outputs back into the // workspace source tree. Ideally, you don't have any generated files committed // to VCS, but sometimes you do. -// package main import ( "encoding/json" "flag" "fmt" - "io/ioutil" "log" "os" "path/filepath" @@ -89,7 +87,7 @@ func copyFile(src, dst string, mode os.FileMode) error { // NOTE: for some reason the io.Copy approach was writing an empty file... // for now OK to copy in-memory - data, err := ioutil.ReadFile(src) + data, err := os.ReadFile(src) if err != nil { return err } @@ -98,28 +96,18 @@ func copyFile(src, dst string, mode os.FileMode) error { return err } - return ioutil.WriteFile(dst, data, mode) + return os.WriteFile(dst, data, mode) } // readFileAsString reads the given file assumed to be text func readFileAsString(filename string) (string, error) { - bytes, err := ioutil.ReadFile(filename) + bytes, err := os.ReadFile(filename) if err != nil { return "", fmt.Errorf("could not read %s: %v", filename, err) } return string(bytes), nil } -func usageHint(cfg *Config, pkg *PackageConfig) string { - return fmt.Sprintf(`You may need to regenerate the files (bazel run) using the '.%[2]s' target, -update the 'srcs = [...]' attribute to include the generated files and re-run the test: - -$ bazel run %[1]s.%[2]s -$ bazel test %[1]s - -`, pkg.TargetLabel, cfg.UpdateTargetLabelName) -} - func check(cfg *Config, pkg *PackageConfig, pairs []*SrcDst) error { for _, pair := range pairs { expected, err := readFileAsString(pair.Src) @@ -227,7 +215,7 @@ func run(cfg *Config) error { } func readConfig(workspaceRootDirectory string) (*Config, error) { - data, err := ioutil.ReadFile(*config) + data, err := os.ReadFile(*config) if err != nil { return nil, fmt.Errorf("could not read config file %s: %w", *config, err) } diff --git a/cmd/gencopy/gencopy_test.go b/cmd/gencopy/gencopy_test.go index 6a1054946..9db097a5b 100644 --- a/cmd/gencopy/gencopy_test.go +++ b/cmd/gencopy/gencopy_test.go @@ -1,13 +1,14 @@ // gencopy is a utility program that copies bazel outputs back into the // workspace source tree. Ideally, you don't have any generated files committed // to VCS, but sometimes you do. -// package main import ( "os" + "path/filepath" "testing" + "github.com/bazelbuild/bazel-gazelle/testtools" "github.com/google/go-cmp/cmp" ) @@ -56,6 +57,12 @@ func TestMakePkgSrcDstPair(t *testing.T) { dst: "file.txt", want: SrcDst{Src: "file.txt", Dst: "/home/file.txt"}, }, + "WorkspaceSubDirectory": { + cfg: Config{WorkspaceRootDirectory: "/home"}, + src: "subdir/file.txt", + dst: "subdir/file.txt", + want: SrcDst{Src: "subdir/file.txt", Dst: "/home/subdir/file.txt"}, + }, "TargetWorkspaceRoot": { cfg: Config{WorkspaceRootDirectory: "/home"}, pkg: PackageConfig{TargetWorkspaceRoot: "external/foo"}, @@ -73,3 +80,79 @@ func TestMakePkgSrcDstPair(t *testing.T) { }) } } + +func TestRunPkg(t *testing.T) { + for name, tc := range map[string]struct { + cfg Config + files, want []testtools.FileSpec + }{ + "degenerate": {}, + "simple": { + // {"extension":"","fileMode":"0644","mode":"update","packageConfigs":[{"generatedFiles":["api/v1/v1_pb2.py"],"sourceFiles":["api/v1/v1_pb2.py"],"targetLabel":"@//api/v1:api_v1_python_compiled_sources","targetPackage":"api/v1","targetWorkspaceRoot":""}],"updateTargetLabelName":"api_v1_python_compiled_sources.update"} + cfg: Config{ + Extension: "", + FileMode: "0644", + Mode: "update", + WorkspaceRootDirectory: "workspace", + UpdateTargetLabelName: "api_v1_python_compiled_sources.update", + PackageConfigs: []*PackageConfig{ + { + GeneratedFiles: []string{"api/v1/v1_pb2.py"}, + SourceFiles: []string{"api/v1/v1_pb2.py"}, + TargetLabel: "@//api/v1:api_v1_python_compiled_sources", + TargetPackage: "api/v1", + TargetWorkspaceRoot: "gen", + }, + }, + }, + files: []testtools.FileSpec{ + { + Path: "workspace/api/v1/v1_pb2.py", + Content: "# generated file api/v1/v1_pb2.py", + }, + }, + want: []testtools.FileSpec{ + { + Path: "api/v1/v1_pb2.py", + Content: "# generated file api/v1/v1_pb2.py", + }, + }, + }, + } { + t.Run(name, func(t *testing.T) { + dir, cleanup := testtools.CreateFiles(t, tc.files) + defer cleanup() + + if err := os.Chdir(dir); err != nil { + t.Fatal(err) + } + listFiles(t, ".") + if err := run(&tc.cfg, t.Logf); err != nil { + t.Fatal(err) + } + + testtools.CheckFiles(t, dir, tc.want) + }) + } +} + +// listFiles - convenience debugging function to log the files under a given dir +func listFiles(t *testing.T, dir string) error { + return filepath.Walk(dir, func(path string, info os.FileInfo, err error) error { + if err != nil { + t.Logf("%v\n", err) + return err + } + if info.Mode()&os.ModeSymlink > 0 { + link, err := os.Readlink(path) + if err != nil { + return err + } + t.Logf("%s -> %s", path, link) + return nil + } + + t.Log(path) + return nil + }) +} diff --git a/example/golden/BUILD.bazel b/example/golden/BUILD.bazel index ed91eaa09..ebeb5093d 100644 --- a/example/golden/BUILD.bazel +++ b/example/golden/BUILD.bazel @@ -18,11 +18,6 @@ go_test( deps = ["//pkg/goldentest"], ) -# gazelle_testdata_example( -# name = "builtins", -# srcs = glob(["testdata/builtins/*"]), -# ) - gazelle_testdata_example( name = "cpp", srcs = glob(["testdata/cpp/*"]), @@ -101,7 +96,8 @@ gazelle_testdata_example( gazelle_testdata_example( name = "proto_repository", srcs = glob(["testdata/proto_repository/**/*"]), - extra_test_content = """ + strip_prefix = "example/golden/testdata/proto_repository", + test_content = """ func TestRunGazelle(t *testing.T) { if err := bazel_testing.RunBazel("run", "//:gazelle"); err != nil { t.Fatal(err) @@ -114,7 +110,6 @@ func TestRunGazelle(t *testing.T) { } } """, - strip_prefix = "example/golden/testdata/proto_repository", workspace_template = "prebuilt.WORKSPACE", ) @@ -142,13 +137,94 @@ filegroup( gazelle_testdata_example( name = "strip_import_prefix", srcs = glob(["testdata/strip_import_prefix/**/*"]), - extra_test_content = """ + strip_prefix = "example/golden/testdata/strip_import_prefix", + test_content = """ +func TestBuild(t *testing.T) { + if err := bazel_testing.RunBazel("build", "..."); err != nil { + t.Fatal(err) + } +} + func TestRunPyApp(t *testing.T) { if err := bazel_testing.RunBazel("run", "//module_app/app:app_py"); err != nil { t.Fatal(err) } } """, - strip_prefix = "example/golden/testdata/strip_import_prefix", + workspace_template = "prebuilt.WORKSPACE", +) + +gazelle_testdata_example( + name = "proto_compiled_sources", + srcs = glob(["testdata/proto_compiled_sources/**/*"]), + strip_prefix = "example/golden/testdata/proto_compiled_sources", + test_content = """ + +func TestUpdate(t *testing.T) { + var want = `proto_compile_gencopy_run rule //:assets +proto_library rule //api/v1:api_v1_proto +proto_compile rule //api/v1:api_v1_python_compiled_sources +proto_compile_gencopy_run rule //api/v1:api_v1_python_compiled_sources.update +proto_compile_gencopy_test rule //api/v1:api_v1_python_compiled_sources_test +proto_library rule //src/idl:svc_proto +proto_compile rule //src/idl:svc_python_compiled_sources +proto_compile_gencopy_run rule //src/idl:svc_python_compiled_sources.update +proto_compile_gencopy_test rule //src/idl:svc_python_compiled_sources_test +` + + // document query outputs + if got, err := bazel_testing.BazelOutput("query", "--output=label_kind", "..."); err != nil { + t.Fatal(err) + } else { + if diff := cmp.Diff(want, string(got)); diff != "" { + t.Fatalf("query mismatch (-want +got): %s", diff) + } + } + + // generated files should not initially exist + if err := os.Remove("api/v1/v1_pb2.py"); err == nil { + t.Fatal("remove should have failed") + } + if err := os.Remove("src/idl/svc_pb2.py"); err == nil { + t.Fatal("remove should have failed") + } + + // assert test initially fails + if err := bazel_testing.RunBazel("test", "//api/v1:api_v1_python_compiled_sources_test"); err == nil { + t.Fatal("expected initial test to fail!") + } + + // copy the files in place + if err := bazel_testing.RunBazel("run", "//api/v1:api_v1_python_compiled_sources.update"); err != nil { + t.Fatal(err) + } + if err := bazel_testing.RunBazel("run", "//src/idl:svc_python_compiled_sources.update"); err != nil { + t.Fatal(err) + } + + // tests should now pass + if err := bazel_testing.RunBazel("test", "..."); err != nil { + t.Fatal(err) + } + + // delete the generated files + if err := os.Remove("api/v1/v1_pb2.py"); err != nil { + t.Fatal(err) + } + if err := os.Remove("src/idl/svc_pb2.py"); err != nil { + t.Fatal(err) + } + + // document that running the proto_compile_assets rule also copies both files in place + if err := bazel_testing.RunBazel("run", "//:assets"); err != nil { + t.Fatal(err) + } + + // test should still pass + if err := bazel_testing.RunBazel("test", "..."); err != nil { + t.Fatal(err) + } +} +""", workspace_template = "prebuilt.WORKSPACE", ) diff --git a/example/golden/README.md b/example/golden/README.md index a55456d16..73d9cd1a4 100644 --- a/example/golden/README.md +++ b/example/golden/README.md @@ -31,9 +31,7 @@ Try and build from the main test workspace. To do that, open up rules_go in the external tree and disable the cleanup function. For example: ``` -$ bazel info output_base -/private/var/tmp/_bazel_pcj/092d6dadaf86f07590903c45033f576e -$ (cd /private/var/tmp/_bazel_pcj/092d6dadaf86f07590903c45033f576e/external/io_bazel_rules_go && code .) +$ (cd $(bazel info output_base)/external/io_bazel_rules_go && code .) $ code go/tools/bazel_testing/bazel_testing.go ``` diff --git a/example/golden/testdata/proto_compiled_sources/.bazelrc b/example/golden/testdata/proto_compiled_sources/.bazelrc new file mode 100644 index 000000000..2fe48bb4f --- /dev/null +++ b/example/golden/testdata/proto_compiled_sources/.bazelrc @@ -0,0 +1 @@ +build --proto_compiler=@build_stack_rules_proto//toolchain:protoc.exe diff --git a/example/golden/testdata/proto_compiled_sources/.bazelversion b/example/golden/testdata/proto_compiled_sources/.bazelversion new file mode 100644 index 000000000..19b860c18 --- /dev/null +++ b/example/golden/testdata/proto_compiled_sources/.bazelversion @@ -0,0 +1 @@ +6.4.0 diff --git a/example/golden/testdata/proto_compiled_sources/BUILD.in b/example/golden/testdata/proto_compiled_sources/BUILD.in new file mode 100644 index 000000000..c0765c442 --- /dev/null +++ b/example/golden/testdata/proto_compiled_sources/BUILD.in @@ -0,0 +1,15 @@ +load("@build_stack_rules_proto//rules:proto_compile_assets.bzl", "proto_compile_assets") + +# gazelle:proto_plugin python implementation builtin:python +# gazelle:proto_rule proto_compiled_sources implementation stackb:rules_proto:proto_compiled_sources +# gazelle:proto_rule proto_compiled_sources visibility //visibility:public +# gazelle:proto_language python rule proto_compiled_sources +# gazelle:proto_language python plugin python + +proto_compile_assets( + name = "assets", + deps = [ + "//api/v1:api_v1_python_compiled_sources", + "//src/idl:svc_python_compiled_sources", + ], +) diff --git a/example/golden/testdata/proto_compiled_sources/BUILD.out b/example/golden/testdata/proto_compiled_sources/BUILD.out new file mode 100644 index 000000000..c0765c442 --- /dev/null +++ b/example/golden/testdata/proto_compiled_sources/BUILD.out @@ -0,0 +1,15 @@ +load("@build_stack_rules_proto//rules:proto_compile_assets.bzl", "proto_compile_assets") + +# gazelle:proto_plugin python implementation builtin:python +# gazelle:proto_rule proto_compiled_sources implementation stackb:rules_proto:proto_compiled_sources +# gazelle:proto_rule proto_compiled_sources visibility //visibility:public +# gazelle:proto_language python rule proto_compiled_sources +# gazelle:proto_language python plugin python + +proto_compile_assets( + name = "assets", + deps = [ + "//api/v1:api_v1_python_compiled_sources", + "//src/idl:svc_python_compiled_sources", + ], +) diff --git a/example/golden/testdata/proto_compiled_sources/README.md b/example/golden/testdata/proto_compiled_sources/README.md new file mode 100644 index 000000000..d066a6162 --- /dev/null +++ b/example/golden/testdata/proto_compiled_sources/README.md @@ -0,0 +1,10 @@ +# proto_compiled_sources + +This test case demonstrates usage of proto_compiled_sources and +proto_compile_assets. + +The api/v1 subdirectory contains a typical proto. + +The src/idl subdirectory contains a proto that also uses +strip_import_prefix (see +[#358](https://github.com/stackb/rules_proto/issues/358)). \ No newline at end of file diff --git a/example/golden/testdata/proto_compiled_sources/WORKSPACE b/example/golden/testdata/proto_compiled_sources/WORKSPACE new file mode 100644 index 000000000..e69de29bb diff --git a/example/golden/testdata/proto_compiled_sources/api/v1/BUILD.in b/example/golden/testdata/proto_compiled_sources/api/v1/BUILD.in new file mode 100644 index 000000000..e69de29bb diff --git a/example/golden/testdata/proto_compiled_sources/api/v1/BUILD.out b/example/golden/testdata/proto_compiled_sources/api/v1/BUILD.out new file mode 100644 index 000000000..4b5d4ce46 --- /dev/null +++ b/example/golden/testdata/proto_compiled_sources/api/v1/BUILD.out @@ -0,0 +1,16 @@ +load("@rules_proto//proto:defs.bzl", "proto_library") +load("@build_stack_rules_proto//rules:proto_compiled_sources.bzl", "proto_compiled_sources") + +proto_library( + name = "api_v1_proto", + srcs = ["v1.proto"], + visibility = ["//visibility:public"], +) + +proto_compiled_sources( + name = "api_v1_python_compiled_sources", + srcs = ["v1_pb2.py"], + plugins = ["@build_stack_rules_proto//plugin/builtin:python"], + proto = "api_v1_proto", + visibility = ["//visibility:public"], +) diff --git a/example/golden/testdata/proto_compiled_sources/api/v1/v1.proto b/example/golden/testdata/proto_compiled_sources/api/v1/v1.proto new file mode 100644 index 000000000..de9ca3b1b --- /dev/null +++ b/example/golden/testdata/proto_compiled_sources/api/v1/v1.proto @@ -0,0 +1,7 @@ +syntax = "proto3"; + +package api.v1; + +message Data { + string id = 1; +} diff --git a/example/golden/testdata/proto_compiled_sources/src/idl/BUILD.in b/example/golden/testdata/proto_compiled_sources/src/idl/BUILD.in new file mode 100644 index 000000000..f01a3ace9 --- /dev/null +++ b/example/golden/testdata/proto_compiled_sources/src/idl/BUILD.in @@ -0,0 +1 @@ +# gazelle:proto_strip_import_prefix /src diff --git a/example/golden/testdata/proto_compiled_sources/src/idl/BUILD.out b/example/golden/testdata/proto_compiled_sources/src/idl/BUILD.out new file mode 100644 index 000000000..4cc3fd69d --- /dev/null +++ b/example/golden/testdata/proto_compiled_sources/src/idl/BUILD.out @@ -0,0 +1,20 @@ +load("@rules_proto//proto:defs.bzl", "proto_library") +load("@build_stack_rules_proto//rules:proto_compiled_sources.bzl", "proto_compiled_sources") + +# gazelle:proto_strip_import_prefix /src + +proto_library( + name = "svc_proto", + srcs = ["svc.proto"], + strip_import_prefix = "/src", + visibility = ["//visibility:public"], +) + +proto_compiled_sources( + name = "svc_python_compiled_sources", + srcs = ["svc_pb2.py"], + output_mappings = ["svc_pb2.py=/idl/svc_pb2.py"], + plugins = ["@build_stack_rules_proto//plugin/builtin:python"], + proto = "svc_proto", + visibility = ["//visibility:public"], +) \ No newline at end of file diff --git a/example/golden/testdata/proto_compiled_sources/src/idl/svc.proto b/example/golden/testdata/proto_compiled_sources/src/idl/svc.proto new file mode 100644 index 000000000..d851660d4 --- /dev/null +++ b/example/golden/testdata/proto_compiled_sources/src/idl/svc.proto @@ -0,0 +1,9 @@ +syntax = "proto3"; + +message Request { + string id = 1; +} + +message Response { + string id = 1; +} diff --git a/rules/example.bzl b/rules/example.bzl index f604bddbc..22692bc2f 100644 --- a/rules/example.bzl +++ b/rules/example.bzl @@ -11,7 +11,7 @@ def _examplegen_impl(ctx): name = ctx.label.name, label = str(ctx.label), testOut = output_test.path, - testHeader = ctx.attr.extra_test_content, + testContent = ctx.attr.test_content, markdownOut = output_markdown.path, workspaceIn = ctx.file.workspace_template.path, stripPrefix = ctx.attr.strip_prefix, @@ -46,8 +46,15 @@ _examplegen = rule( "strip_prefix": attr.string( doc = "path prefix to remove from test files in the txtar", ), - "extra_test_content": attr.string( - doc = "optional chunk of content that will be written into the _test.go file", + "test_content": attr.string( + doc = "optional chunk of golang test content. Default behavior is 'bazel build ...'", + default = """ +func TestBuild(t *testing.T) { + if err := bazel_testing.RunBazel("build", "..."); err != nil { + t.Fatal(err) + } +} +""", ), "workspace_template": attr.label( doc = "Template for the test WORKSPACE", @@ -78,22 +85,24 @@ def gazelle_testdata_example(**kwargs): """ name = kwargs.pop("name") srcs = kwargs.pop("srcs", []) + deps = kwargs.pop("deps", []) strip_prefix = kwargs.pop("strip_prefix", "") - extra_test_content = kwargs.pop("extra_test_content", "") + test_content = kwargs.pop("test_content", None) rule_files = kwargs.pop("rule_files", ["//:all_files"]) _examplegen( name = name, srcs = srcs, strip_prefix = strip_prefix, - extra_test_content = extra_test_content, + test_content = test_content, workspace_template = kwargs.pop("workspace_template", ""), ) go_bazel_test( name = name + "_test", srcs = [name + "_test.go"], + deps = deps + ["@com_github_google_go_cmp//cmp"], rule_files = rule_files, **kwargs )