Repository: googleapis/api-linter
Pull request #258 (2019-10-24 21:04:14)

Used `LookupImport` for common protos.


Patch:
diff --git a/go.mod b/go.mod
index 4b61ad8..a02c180 100644
--- a/go.mod
+++ b/go.mod
@@ -8,7 +8,7 @@ require (
 	github.com/gertd/go-pluralize v0.1.1
 	github.com/golang/protobuf v1.3.2
 	github.com/google/go-cmp v0.3.1
-	github.com/jhump/protoreflect v1.5.0
+	github.com/jhump/protoreflect v1.5.1-0.20191024142022-1d0024204e58
 	github.com/lithammer/dedent v1.1.0
 	github.com/stoewer/go-strcase v1.0.2
 	github.com/urfave/cli v1.22.1
diff --git a/go.sum b/go.sum
index 3195b53..76c4fe9 100644
--- a/go.sum
+++ b/go.sum
@@ -24,6 +24,8 @@ github.com/google/go-cmp v0.3.1 h1:Xye71clBPdm5HgqGwUkwhbynsUJZhDbS20FvLhQ2izg=
 github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
 github.com/jhump/protoreflect v1.5.0 h1:NgpVT+dX71c8hZnxHof2M7QDK7QtohIJ7DYycjnkyfc=
 github.com/jhump/protoreflect v1.5.0/go.mod h1:eaTn3RZAmMBcV0fifFvlm6VHNz3wSkYyXYWUh7ymB74=
+github.com/jhump/protoreflect v1.5.1-0.20191024142022-1d0024204e58 h1:RHSRszt3DIDwIwV1fPXtZI74OL36BkDIzsp2IOdp+Xc=
+github.com/jhump/protoreflect v1.5.1-0.20191024142022-1d0024204e58/go.mod h1:eaTn3RZAmMBcV0fifFvlm6VHNz3wSkYyXYWUh7ymB74=
 github.com/lithammer/dedent v1.1.0 h1:VNzHMVCBNG1j0fh3OrsFRkVUwStdDArbgBWoPAffktY=
 github.com/lithammer/dedent v1.1.0/go.mod h1:jrXYCQtgg0nJiN+StA2KgR7w6CiQNv9Fd/Z9BP0jIOc=
 github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=


Test patch:
diff --git a/rules/internal/testutils/parse.go b/rules/internal/testutils/parse.go
index be78cc8..2610a67 100644
--- a/rules/internal/testutils/parse.go
+++ b/rules/internal/testutils/parse.go
@@ -16,18 +16,13 @@ package testutils
 
 import (
 	"bytes"
-	"compress/gzip"
 	"fmt"
 	"html/template"
-	"io/ioutil"
 	"strings"
 	"testing"
 
-	"github.com/golang/protobuf/proto"
-	dpb "github.com/golang/protobuf/protoc-gen-go/descriptor"
 	"github.com/jhump/protoreflect/desc"
 	"github.com/jhump/protoreflect/desc/protoparse"
-	"github.com/jhump/protoreflect/desc/protoprint"
 	"github.com/lithammer/dedent"
 
 	// These imports cause the common protos to be registered with
@@ -51,14 +46,12 @@ func ParseProtoString(t *testing.T, src string) *desc.FileDescriptor {
 	fileContents := map[string]string{
 		"test.proto": strings.TrimSpace(dedent.Dedent(src)),
 	}
-	for fn, c := range commonProtos {
-		fileContents[fn] = c
-	}
 
 	// Parse the file.
 	parser := protoparse.Parser{
 		Accessor:              protoparse.FileContentsFromMap(fileContents),
 		IncludeSourceCodeInfo: true,
+		LookupImport:          desc.LoadFileDescriptor,
 	}
 	fds, err := parser.ParseFiles("test.proto")
 	if err != nil {
@@ -102,84 +95,3 @@ func ParseProto3Tmpl(t *testing.T, src string, data interface{}) *desc.FileDescr
 	// Parse the proto as a string.
 	return ParseProto3String(t, protoBytes.String())
 }
-
-// decompile reads compiled Go protos and returns a map of strings
-// corresponding to the original protos.
-//
-// This is used to handle google.api common protos, which some tests need
-// to use.
-func decompile(filenames ...string) (map[string]string, error) {
-	var fdps []*dpb.FileDescriptorProto
-	for _, filename := range filenames {
-		// Compiled golang protos include a compressed descriptor, which must be
-		// uncompressed before it can be populated into a FileDescriptorProto.
-		compressedDesc := proto.FileDescriptor(filename)
-		r, err := gzip.NewReader(bytes.NewReader(compressedDesc))
-		if err != nil {
-			return nil, err
-		}
-		descBytes, err := ioutil.ReadAll(r)
-		if err != nil {
-			return nil, err
-		}
-
-		// Unmarshal the byte string into a FileDescriptorProto.
-		fdp := &dpb.FileDescriptorProto{}
-		if err := proto.Unmarshal(descBytes, fdp); err != nil {
-			return nil, err
-		}
-		fdps = append(fdps, fdp)
-	}
-	fds, err := desc.CreateFileDescriptors(fdps)
-	if err != nil {
-		return nil, fmt.Errorf(dedent.Dedent(`
-			Unable to create protoreflect descriptors
-			(missing import in args to decompile?): %v
-		`), err)
-	}
-
-	// Iterate over the values in the map and populate the original contents
-	// of the protos.
-	printer := &protoprint.Printer{}
-	answer := make(map[string]string)
-	for filename, fd := range fds {
-		protoStr, err := printer.PrintProtoToString(fd)
-		if err != nil {
-			return nil, err
-		}
-		answer[filename] = protoStr
-	}
-
-	// Done; return the answer.
-	return answer, nil
-}
-
-// Common protos likely to need to be imported in tests.
-// It is okay to add more here; however, this list **must not** include any
-// protos outside of what is defined in AIP-213 (https://aip.dev/213).
-//
-// Note that any file that needs to be included here must also have each
-// of its imports here, and the corresponding compiled Go protos must be
-// imported up top.
-var commonProtos map[string]string
-
-func init() {
-	cp, err := decompile(
-		"google/api/annotations.proto",
-		"google/api/client.proto",
-		"google/api/field_behavior.proto",
-		"google/api/http.proto",
-		"google/api/resource.proto",
-		"google/longrunning/operations.proto",
-		"google/protobuf/any.proto",
-		"google/protobuf/descriptor.proto",
-		"google/protobuf/duration.proto",
-		"google/protobuf/empty.proto",
-		"google/protobuf/timestamp.proto",
-		"google/rpc/status.proto",
-	)
-	if err != nil {
-		panic(err)
-	}
-	commonProtos = cp
-}
diff --git a/rules/internal/testutils/parse_test.go b/rules/internal/testutils/parse_test.go
index 65dce07..a8a4cad 100644
--- a/rules/internal/testutils/parse_test.go
+++ b/rules/internal/testutils/parse_test.go
@@ -173,23 +173,3 @@ func TestParseProto3TmplDataError(t *testing.T) {
 		t.Errorf("Expected missing data to cause a fatal error.")
 	}
 }
-
-func TestDecompileErrors(t *testing.T) {
-	// Note: I prefer to test only public interfaces, but because the common
-	// protos are added at module initialization, there is no way to test
-	// the failure cases without hitting the decompile method directly.
-	tests := []struct {
-		testName string
-		filename string
-	}{
-		{"MissingImport", "google/longrunning/operations.proto"},
-		{"BogusFile", "bogus.proto"},
-	}
-	for _, test := range tests {
-		t.Run(test.testName, func(t *testing.T) {
-			if _, err := decompile(test.filename); err == nil {
-				t.Errorf("Unexpected success: %q", test.filename)
-			}
-		})
-	}
-}