Skip to content

Commit b322e49

Browse files
authored
Merge pull request #520 from neild/dev.alias
[dev] protoc-gen-go: reorganize, fix testdata directory
2 parents e6af52b + 055d7b0 commit b322e49

34 files changed

+2461
-1498
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,4 @@ core
1212
_obj
1313
_test
1414
_testmain.go
15-
protoc-gen-go/testdata/multi/*.pb.go
1615
_conformance/_conformance

protoc-gen-go/Makefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,11 @@
2929
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
3030
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3131

32+
all: test
33+
3234
test:
35+
go test
3336
cd testdata && make test
37+
38+
regenerate:
39+
go test --regenerate

protoc-gen-go/golden_test.go

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
package main
2+
3+
import (
4+
"bytes"
5+
"flag"
6+
"io/ioutil"
7+
"os"
8+
"os/exec"
9+
"path/filepath"
10+
"regexp"
11+
"strings"
12+
"testing"
13+
)
14+
15+
// Set --regenerate to regenerate the golden files.
16+
var regenerate = flag.Bool("regenerate", false, "regenerate golden files")
17+
18+
// When the environment variable RUN_AS_PROTOC_GEN_GO is set, we skip running
19+
// tests and instead act as protoc-gen-go. This allows the test binary to
20+
// pass itself to protoc.
21+
func init() {
22+
if os.Getenv("RUN_AS_PROTOC_GEN_GO") != "" {
23+
main()
24+
os.Exit(0)
25+
}
26+
}
27+
28+
func TestGolden(t *testing.T) {
29+
workdir, err := ioutil.TempDir("", "proto-test")
30+
if err != nil {
31+
t.Fatal(err)
32+
}
33+
defer os.RemoveAll(workdir)
34+
35+
// Find all the proto files we need to compile. We assume that each directory
36+
// contains the files for a single package.
37+
packages := map[string][]string{}
38+
err = filepath.Walk("testdata", func(path string, info os.FileInfo, err error) error {
39+
if !strings.HasSuffix(path, ".proto") {
40+
return nil
41+
}
42+
dir := filepath.Dir(path)
43+
packages[dir] = append(packages[dir], path)
44+
return nil
45+
})
46+
if err != nil {
47+
t.Fatal(err)
48+
}
49+
50+
// Compile each package, using this binary as protoc-gen-go.
51+
//
52+
// We set the RUN_AS_PROTOC_GEN_GO environment variable to indicate that
53+
// the subprocess should act as a proto compiler rather than a test.
54+
for _, sources := range packages {
55+
cmd := exec.Command(
56+
"protoc",
57+
"--plugin=protoc-gen-go="+os.Args[0],
58+
"-Itestdata",
59+
"--go_out=plugins=grpc:"+workdir,
60+
)
61+
cmd.Args = append(cmd.Args, sources...)
62+
cmd.Env = append(os.Environ(), "RUN_AS_PROTOC_GEN_GO=1")
63+
t.Log(strings.Join(cmd.Args, " "))
64+
out, err := cmd.CombinedOutput()
65+
if len(out) > 0 {
66+
t.Log(string(out))
67+
}
68+
if err != nil {
69+
t.Fatalf("failed to compile: %v", sources)
70+
}
71+
}
72+
73+
// Compare each generated file to the golden version.
74+
relRoot := filepath.Join(workdir, "github.com/golang/protobuf/protoc-gen-go/testdata")
75+
filepath.Walk(workdir, func(genPath string, info os.FileInfo, _ error) error {
76+
if info.IsDir() {
77+
return nil
78+
}
79+
80+
// For each generated file, figure out the path to the corresponding
81+
// golden file in the testdata directory.
82+
relPath, err := filepath.Rel(relRoot, genPath)
83+
if err != nil {
84+
t.Errorf("filepath.Rel(%q, %q): %v", relRoot, genPath, err)
85+
return nil
86+
}
87+
if filepath.SplitList(relPath)[0] == ".." {
88+
t.Errorf("generated file %q is not relative to %q", genPath, relRoot)
89+
}
90+
goldenPath := filepath.Join("testdata", relPath)
91+
92+
got, err := ioutil.ReadFile(genPath)
93+
if err != nil {
94+
t.Error(err)
95+
return nil
96+
}
97+
if *regenerate {
98+
// If --regenerate set, just rewrite the golden files.
99+
err := ioutil.WriteFile(goldenPath, got, 0666)
100+
if err != nil {
101+
t.Error(err)
102+
}
103+
return nil
104+
}
105+
106+
want, err := ioutil.ReadFile(goldenPath)
107+
if err != nil {
108+
t.Error(err)
109+
return nil
110+
}
111+
112+
want = fdescRE.ReplaceAll(want, nil)
113+
got = fdescRE.ReplaceAll(got, nil)
114+
if bytes.Equal(got, want) {
115+
return nil
116+
}
117+
118+
cmd := exec.Command("diff", "-u", goldenPath, genPath)
119+
out, _ := cmd.CombinedOutput()
120+
t.Errorf("golden file differs: %v\n%v", relPath, string(out))
121+
return nil
122+
})
123+
}
124+
125+
var fdescRE = regexp.MustCompile(`(?ms)^var fileDescriptor.*}`)

protoc-gen-go/testdata/Makefile

Lines changed: 2 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -29,56 +29,7 @@
2929
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
3030
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3131

32-
all:
33-
@echo run make test
32+
all: test
3433

35-
include ../../Make.protobuf
36-
37-
test: golden testbuild
38-
39-
#test: golden testbuild extension_test
40-
# ./extension_test
41-
# @echo PASS
42-
43-
my_test/test.pb.go: my_test/test.proto
44-
protoc --go_out=Mmulti/multi1.proto=github.com/golang/protobuf/protoc-gen-go/testdata/multi:. $<
45-
46-
golden:
47-
make -B my_test/test.pb.go
48-
sed -i -e '/return.*fileDescriptor/d' my_test/test.pb.go
49-
sed -i -e '/^var fileDescriptor/,/^}/d' my_test/test.pb.go
50-
sed -i -e '/proto.RegisterFile.*fileDescriptor/d' my_test/test.pb.go
51-
gofmt -w my_test/test.pb.go
52-
diff -w my_test/test.pb.go my_test/test.pb.go.golden
53-
54-
make -B deprecated/deprecated.pb.go
55-
sed -i -e '/return.*fileDescriptor/d' deprecated/deprecated.pb.go
56-
sed -i -e '/^var fileDescriptor/,/^}/d' deprecated/deprecated.pb.go
57-
sed -i -e '/proto.RegisterFile.*fileDescriptor/d' deprecated/deprecated.pb.go
58-
gofmt -w deprecated/deprecated.pb.go
59-
diff -w deprecated/deprecated.pb.go deprecated/deprecated.pb.go.golden
60-
61-
62-
deprecated/deprecated.pb.go: deprecated/deprecated.proto
63-
protoc --go_out=plugins=grpc,import_path=Mdeprecated/deprecated.proto=github.com/golang/protobuf/protoc-gen-go/testdata/deprecated:. $<
64-
65-
nuke: clean
66-
67-
testbuild: regenerate
34+
test:
6835
go test
69-
70-
regenerate:
71-
# Invoke protoc once to generate three independent .pb.go files in the same package.
72-
protoc --go_out=. multi/multi1.proto multi/multi2.proto multi/multi3.proto
73-
74-
#extension_test: extension_test.$O
75-
# $(LD) -L. -o $@ $<
76-
77-
#multi.a: multi3.pb.$O multi2.pb.$O multi1.pb.$O
78-
# rm -f multi.a
79-
# $(QUOTED_GOBIN)/gopack grc $@ $<
80-
81-
#test.pb.go: imp.pb.go
82-
#multi1.pb.go: multi2.pb.go multi3.pb.go
83-
#main.$O: imp.pb.$O test.pb.$O multi.a
84-
#extension_test.$O: extension_base.pb.$O extension_extra.pb.$O extension_user.pb.$O

protoc-gen-go/testdata/deprecated/deprecated.pb.go

Lines changed: 31 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)