Skip to content

Commit 5428336

Browse files
authored
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
1 parent 23cc080 commit 5428336

23 files changed

+330
-58
lines changed

BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ filegroup(
6464
"WORKSPACE",
6565
"go_deps.bzl",
6666
"//cmd/gazelle:all_files",
67+
"//cmd/gencopy:all_files",
6768
"//deps:all_files",
6869
"//language/protobuf:all_files",
6970
"//pkg:all_files",

Makefile

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,47 @@
1-
BAZEL := bazel
21

32
.PHONY: tidy
43
tidy: deps
5-
$(BAZEL) run @go_sdk//:bin/go -- mod tidy
6-
$(BAZEL) run @go_sdk//:bin/go -- mod vendor
4+
bazel run @go_sdk//:bin/go -- mod tidy
5+
bazel run @go_sdk//:bin/go -- mod vendor
76
find vendor -name 'BUILD.bazel' | xargs rm
8-
$(BAZEL) run //:update_go_deps
9-
$(BAZEL) run //:buildifier
10-
$(BAZEL) run //:gazelle
7+
bazel run //:update_go_deps
8+
bazel run //:buildifier
9+
bazel run //:gazelle
1110

1211
.PHONY: gazelle
1312
gazelle:
14-
$(BAZEL) run //:gazelle
13+
bazel run //:gazelle
1514

1615
.PHONY: deps
1716
deps:
18-
$(BAZEL) build //deps:*
17+
bazel build //deps:*
1918
cp -f ./bazel-bin/deps/*.bzl deps/
2019
chmod 0644 deps/*.bzl
21-
$(BAZEL) run //:buildifier -- deps/
20+
bazel run //:buildifier -- deps/
2221

2322
.PHONY: site
2423
site:
25-
$(BAZEL) build //example/golden:*
24+
bazel build //example/golden:*
2625
cp -f ./bazel-bin/example/golden/*.md docs/
2726

2827
.PHONY: golden_test
2928
golden_test:
30-
$(BAZEL) test //example/golden:golden_test
29+
bazel test //example/golden:golden_test --test_output=streamed
30+
31+
.PHONY: example_test
32+
example_test:
33+
bazel test //example/golden:proto_compiled_sources_test --test_output=streamed
3134

3235
.PHONY: test
3336
test:
34-
$(BAZEL) test --keep_going //example/... //pkg/... //plugin/... //language/... //rules/... //toolchain/... \
37+
bazel test --keep_going //example/... //pkg/... //plugin/... //language/... //rules/... //toolchain/... \
3538
--deleted_packages=//plugin/grpc-ecosystem/grpc-gateway
3639

3740
.PHONY: get
3841
get:
39-
$(BAZEL) run @go_sdk//:bin/go -- get github.com/bazelbuild/bazel-gazelle@v0.31.0
40-
$(BAZEL) run @go_sdk//:bin/go -- mod download github.com/bazelbuild/buildtools
41-
$(BAZEL) run @go_sdk//:bin/go -- mod vendor
42+
bazel run @go_sdk//:bin/go -- get github.com/bazelbuild/bazel-gazelle@v0.31.0
43+
bazel run @go_sdk//:bin/go -- mod download github.com/bazelbuild/buildtools
44+
bazel run @go_sdk//:bin/go -- mod vendor
4245

4346
update_pnpm_lock:
4447
# nvm use 18

cmd/examplegen/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ type Config struct {
1010
Name string
1111
Label string
1212
TestOut string
13-
TestHeader string
13+
TestContent string
1414
MarkdownOut string
1515
WorkspaceIn string
1616
StripPrefix string

cmd/examplegen/generator.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ func generateTest(c *Config) error {
8787
defer f.Close()
8888

8989
fmt.Fprintln(f, testHeader)
90-
fmt.Fprintln(f, c.TestHeader)
90+
fmt.Fprintln(f, c.TestContent)
9191

9292
fmt.Fprintln(f, "var txtar=`")
9393

cmd/examplegen/template.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,22 @@ package main
55
66
import (
77
"testing"
8+
"os"
89
910
"github.com/bazelbuild/rules_go/go/tools/bazel_testing"
11+
"github.com/google/go-cmp/cmp"
12+
)
13+
14+
var (
15+
// allow use of os package in other tests
16+
_os_Remove = os.Remove
17+
// allow use of cmp package in other tests
18+
_cmp_Diff = cmp.Diff
1019
)
1120
1221
func TestMain(m *testing.M) {
1322
bazel_testing.TestMain(m, bazel_testing.Args{
1423
Main: txtar,
1524
})
1625
}
17-
18-
func TestBuild(t *testing.T) {
19-
if err := bazel_testing.RunBazel("build", "..."); err != nil {
20-
t.Fatal(err)
21-
}
22-
}
2326
`

cmd/gencopy/BUILD.bazel

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,21 @@ go_test(
2020
name = "gencopy_test",
2121
srcs = ["gencopy_test.go"],
2222
embed = [":gencopy_lib"],
23-
deps = ["@com_github_google_go_cmp//cmp"],
23+
deps = [
24+
"@bazel_gazelle//testtools:go_default_library",
25+
"@com_github_google_go_cmp//cmp",
26+
],
27+
)
28+
29+
filegroup(
30+
name = "all_files",
31+
testonly = True,
32+
srcs = [
33+
"BUILD.bazel",
34+
"gencopy.bash.in",
35+
"gencopy.bzl",
36+
"gencopy.go",
37+
"gencopy_test.go",
38+
],
39+
visibility = ["//:__pkg__"],
2440
)

cmd/gencopy/gencopy.go

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
// gencopy is a utility program that copies bazel outputs back into the
22
// workspace source tree. Ideally, you don't have any generated files committed
33
// to VCS, but sometimes you do.
4-
//
54
package main
65

76
import (
87
"encoding/json"
98
"flag"
109
"fmt"
11-
"io/ioutil"
1210
"log"
1311
"os"
1412
"path/filepath"
@@ -89,7 +87,7 @@ func copyFile(src, dst string, mode os.FileMode) error {
8987
// NOTE: for some reason the io.Copy approach was writing an empty file...
9088
// for now OK to copy in-memory
9189

92-
data, err := ioutil.ReadFile(src)
90+
data, err := os.ReadFile(src)
9391
if err != nil {
9492
return err
9593
}
@@ -98,28 +96,18 @@ func copyFile(src, dst string, mode os.FileMode) error {
9896
return err
9997
}
10098

101-
return ioutil.WriteFile(dst, data, mode)
99+
return os.WriteFile(dst, data, mode)
102100
}
103101

104102
// readFileAsString reads the given file assumed to be text
105103
func readFileAsString(filename string) (string, error) {
106-
bytes, err := ioutil.ReadFile(filename)
104+
bytes, err := os.ReadFile(filename)
107105
if err != nil {
108106
return "", fmt.Errorf("could not read %s: %v", filename, err)
109107
}
110108
return string(bytes), nil
111109
}
112110

113-
func usageHint(cfg *Config, pkg *PackageConfig) string {
114-
return fmt.Sprintf(`You may need to regenerate the files (bazel run) using the '.%[2]s' target,
115-
update the 'srcs = [...]' attribute to include the generated files and re-run the test:
116-
117-
$ bazel run %[1]s.%[2]s
118-
$ bazel test %[1]s
119-
120-
`, pkg.TargetLabel, cfg.UpdateTargetLabelName)
121-
}
122-
123111
func check(cfg *Config, pkg *PackageConfig, pairs []*SrcDst) error {
124112
for _, pair := range pairs {
125113
expected, err := readFileAsString(pair.Src)
@@ -227,7 +215,7 @@ func run(cfg *Config) error {
227215
}
228216

229217
func readConfig(workspaceRootDirectory string) (*Config, error) {
230-
data, err := ioutil.ReadFile(*config)
218+
data, err := os.ReadFile(*config)
231219
if err != nil {
232220
return nil, fmt.Errorf("could not read config file %s: %w", *config, err)
233221
}

cmd/gencopy/gencopy_test.go

Lines changed: 84 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
// gencopy is a utility program that copies bazel outputs back into the
22
// workspace source tree. Ideally, you don't have any generated files committed
33
// to VCS, but sometimes you do.
4-
//
54
package main
65

76
import (
87
"os"
8+
"path/filepath"
99
"testing"
1010

11+
"github.com/bazelbuild/bazel-gazelle/testtools"
1112
"github.com/google/go-cmp/cmp"
1213
)
1314

@@ -56,6 +57,12 @@ func TestMakePkgSrcDstPair(t *testing.T) {
5657
dst: "file.txt",
5758
want: SrcDst{Src: "file.txt", Dst: "/home/file.txt"},
5859
},
60+
"WorkspaceSubDirectory": {
61+
cfg: Config{WorkspaceRootDirectory: "/home"},
62+
src: "subdir/file.txt",
63+
dst: "subdir/file.txt",
64+
want: SrcDst{Src: "subdir/file.txt", Dst: "/home/subdir/file.txt"},
65+
},
5966
"TargetWorkspaceRoot": {
6067
cfg: Config{WorkspaceRootDirectory: "/home"},
6168
pkg: PackageConfig{TargetWorkspaceRoot: "external/foo"},
@@ -73,3 +80,79 @@ func TestMakePkgSrcDstPair(t *testing.T) {
7380
})
7481
}
7582
}
83+
84+
func TestRunPkg(t *testing.T) {
85+
for name, tc := range map[string]struct {
86+
cfg Config
87+
files, want []testtools.FileSpec
88+
}{
89+
"degenerate": {},
90+
"simple": {
91+
// {"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"}
92+
cfg: Config{
93+
Extension: "",
94+
FileMode: "0644",
95+
Mode: "update",
96+
WorkspaceRootDirectory: "workspace",
97+
UpdateTargetLabelName: "api_v1_python_compiled_sources.update",
98+
PackageConfigs: []*PackageConfig{
99+
{
100+
GeneratedFiles: []string{"api/v1/v1_pb2.py"},
101+
SourceFiles: []string{"api/v1/v1_pb2.py"},
102+
TargetLabel: "@//api/v1:api_v1_python_compiled_sources",
103+
TargetPackage: "api/v1",
104+
TargetWorkspaceRoot: "gen",
105+
},
106+
},
107+
},
108+
files: []testtools.FileSpec{
109+
{
110+
Path: "workspace/api/v1/v1_pb2.py",
111+
Content: "# generated file api/v1/v1_pb2.py",
112+
},
113+
},
114+
want: []testtools.FileSpec{
115+
{
116+
Path: "api/v1/v1_pb2.py",
117+
Content: "# generated file api/v1/v1_pb2.py",
118+
},
119+
},
120+
},
121+
} {
122+
t.Run(name, func(t *testing.T) {
123+
dir, cleanup := testtools.CreateFiles(t, tc.files)
124+
defer cleanup()
125+
126+
if err := os.Chdir(dir); err != nil {
127+
t.Fatal(err)
128+
}
129+
listFiles(t, ".")
130+
if err := run(&tc.cfg, t.Logf); err != nil {
131+
t.Fatal(err)
132+
}
133+
134+
testtools.CheckFiles(t, dir, tc.want)
135+
})
136+
}
137+
}
138+
139+
// listFiles - convenience debugging function to log the files under a given dir
140+
func listFiles(t *testing.T, dir string) error {
141+
return filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
142+
if err != nil {
143+
t.Logf("%v\n", err)
144+
return err
145+
}
146+
if info.Mode()&os.ModeSymlink > 0 {
147+
link, err := os.Readlink(path)
148+
if err != nil {
149+
return err
150+
}
151+
t.Logf("%s -> %s", path, link)
152+
return nil
153+
}
154+
155+
t.Log(path)
156+
return nil
157+
})
158+
}

0 commit comments

Comments
 (0)