protogen, cmd/protoc-gen-go: initial commit
Package protogen provides support for writing protoc plugins.
A "plugin" in this case is a program run by protoc to generate output.
The protoc-gen-go command is a protoc plugin to generate Go code.
cmd/protoc-gen-go/golden_test.go is mostly a straight copy from
the golden test in github.com/golang/protobuf.
Change-Id: I332d0df1e4b60bb8cd926320b8721e16b99a4b71
Reviewed-on: https://go-review.googlesource.com/130175
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
diff --git a/cmd/protoc-gen-go/golden_test.go b/cmd/protoc-gen-go/golden_test.go
new file mode 100644
index 0000000..d0ed8ea
--- /dev/null
+++ b/cmd/protoc-gen-go/golden_test.go
@@ -0,0 +1,145 @@
+// Copyright 2018 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package main
+
+import (
+ "bytes"
+ "flag"
+ "go/build"
+ "io/ioutil"
+ "os"
+ "os/exec"
+ "path/filepath"
+ "regexp"
+ "strings"
+ "testing"
+)
+
+// Set --regenerate to regenerate the golden files.
+var regenerate = flag.Bool("regenerate", false, "regenerate golden files")
+
+// When the environment variable RUN_AS_PROTOC_GEN_GO is set, we skip running
+// tests and instead act as protoc-gen-go. This allows the test binary to
+// pass itself to protoc.
+func init() {
+ if os.Getenv("RUN_AS_PROTOC_GEN_GO") != "" {
+ main()
+ os.Exit(0)
+ }
+}
+
+func TestGolden(t *testing.T) {
+ workdir, err := ioutil.TempDir("", "proto-test")
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer os.RemoveAll(workdir)
+
+ // Find all the proto files we need to compile. We assume that each directory
+ // contains the files for a single package.
+ supportTypeAliases := hasReleaseTag("go1.9")
+ packages := map[string][]string{}
+ err = filepath.Walk("testdata", func(path string, info os.FileInfo, err error) error {
+ if filepath.Base(path) == "import_public" && !supportTypeAliases {
+ // Public imports require type alias support.
+ return filepath.SkipDir
+ }
+ if !strings.HasSuffix(path, ".proto") {
+ return nil
+ }
+ dir := filepath.Dir(path)
+ packages[dir] = append(packages[dir], path)
+ return nil
+ })
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ // Compile each package, using this binary as protoc-gen-go.
+ for _, sources := range packages {
+ args := []string{"-Itestdata", "--go_out=plugins=grpc,paths=source_relative:" + workdir}
+ args = append(args, sources...)
+ protoc(t, args)
+ }
+
+ // Compare each generated file to the golden version.
+ filepath.Walk(workdir, func(genPath string, info os.FileInfo, _ error) error {
+ if info.IsDir() {
+ return nil
+ }
+
+ // For each generated file, figure out the path to the corresponding
+ // golden file in the testdata directory.
+ relPath, err := filepath.Rel(workdir, genPath)
+ if err != nil {
+ t.Errorf("filepath.Rel(%q, %q): %v", workdir, genPath, err)
+ return nil
+ }
+ if filepath.SplitList(relPath)[0] == ".." {
+ t.Errorf("generated file %q is not relative to %q", genPath, workdir)
+ }
+ goldenPath := filepath.Join("testdata", relPath)
+
+ got, err := ioutil.ReadFile(genPath)
+ if err != nil {
+ t.Error(err)
+ return nil
+ }
+ if *regenerate {
+ // If --regenerate set, just rewrite the golden files.
+ err := ioutil.WriteFile(goldenPath, got, 0666)
+ if err != nil {
+ t.Error(err)
+ }
+ return nil
+ }
+
+ want, err := ioutil.ReadFile(goldenPath)
+ if err != nil {
+ t.Error(err)
+ return nil
+ }
+
+ want = fdescRE.ReplaceAll(want, nil)
+ got = fdescRE.ReplaceAll(got, nil)
+ if bytes.Equal(got, want) {
+ return nil
+ }
+
+ cmd := exec.Command("diff", "-u", goldenPath, genPath)
+ out, _ := cmd.CombinedOutput()
+ t.Errorf("golden file differs: %v\n%v", relPath, string(out))
+ return nil
+ })
+}
+
+var fdescRE = regexp.MustCompile(`(?ms)^var fileDescriptor.*}`)
+
+func protoc(t *testing.T, args []string) {
+ cmd := exec.Command("protoc", "--plugin=protoc-gen-go="+os.Args[0])
+ cmd.Args = append(cmd.Args, args...)
+ // We set the RUN_AS_PROTOC_GEN_GO environment variable to indicate that
+ // the subprocess should act as a proto compiler rather than a test.
+ cmd.Env = append(os.Environ(), "RUN_AS_PROTOC_GEN_GO=1")
+ out, err := cmd.CombinedOutput()
+ if len(out) > 0 || err != nil {
+ t.Log("RUNNING: ", strings.Join(cmd.Args, " "))
+ }
+ if len(out) > 0 {
+ t.Log(string(out))
+ }
+ if err != nil {
+ t.Fatalf("protoc: %v", err)
+ }
+}
+
+func hasReleaseTag(want string) bool {
+ for _, tag := range build.Default.ReleaseTags {
+ if tag == want {
+ return true
+ }
+ }
+ return false
+}
diff --git a/cmd/protoc-gen-go/main.go b/cmd/protoc-gen-go/main.go
new file mode 100644
index 0000000..9ea1420
--- /dev/null
+++ b/cmd/protoc-gen-go/main.go
@@ -0,0 +1,35 @@
+// Copyright 2018 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// The protoc-gen-go binary is a protoc plugin to generate a Go protocol
+// buffer package.
+package main
+
+import (
+ "strings"
+
+ "google.golang.org/proto/protogen"
+)
+
+func main() {
+ protogen.Run(func(gen *protogen.Plugin) error {
+ for _, f := range gen.Files {
+ if !f.Generate {
+ continue
+ }
+ genFile(gen, f)
+ }
+ return nil
+ })
+}
+
+func genFile(gen *protogen.Plugin, f *protogen.File) {
+ g := gen.NewGeneratedFile(strings.TrimSuffix(f.Desc.GetName(), ".proto") + ".pb.go")
+ g.P("// Code generated by protoc-gen-go. DO NOT EDIT.")
+ g.P("// source: ", f.Desc.GetName())
+ g.P()
+ g.P("package TODO")
+
+ // TODO: Everything.
+}
diff --git a/cmd/protoc-gen-go/testdata/proto2/proto2.pb.go b/cmd/protoc-gen-go/testdata/proto2/proto2.pb.go
new file mode 100644
index 0000000..558363d
--- /dev/null
+++ b/cmd/protoc-gen-go/testdata/proto2/proto2.pb.go
@@ -0,0 +1,4 @@
+// Code generated by protoc-gen-go. DO NOT EDIT.
+// source: proto2/proto2.proto
+
+package TODO
diff --git a/cmd/protoc-gen-go/testdata/proto2/proto2.proto b/cmd/protoc-gen-go/testdata/proto2/proto2.proto
new file mode 100644
index 0000000..055bc71
--- /dev/null
+++ b/cmd/protoc-gen-go/testdata/proto2/proto2.proto
@@ -0,0 +1,12 @@
+// Copyright 2018 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+syntax = "proto2";
+
+package goproto.protoc.proto2;
+
+option go_package = "google.golang.org/proto/cmd/protoc-gen-go/testdata/proto2";
+
+message Message {
+}