Repository: googleapis/gapic-generator-go
Pull request #18 (2018-09-27 22:14:54)

sample: test whole file
including copyright and imports.

pongad: @beccasaurus PTAL

Patch:
diff --git a/cmd/gen-go-sample/main.go b/cmd/gen-go-sample/main.go
index c77adea..35ff17b 100644
--- a/cmd/gen-go-sample/main.go
+++ b/cmd/gen-go-sample/main.go
@@ -19,6 +19,7 @@ import (
 	"flag"
 	"fmt"
 	"go/format"
+	"io"
 	"io/ioutil"
 	"log"
 	"os"
@@ -99,11 +100,10 @@ func main() {
 					}
 
 					gen.reset()
-					gen.imports[gen.clientPkg] = true
 					if err := gen.genSample(iface.Name, meth.Name, sam.RegionTag, vs); err != nil {
 						log.Fatal(errors.E(err, "value set: %q", vs))
 					}
-					if err := gen.commit(!*nofmt); err != nil {
+					if err := gen.commit(!*nofmt, time.Now().Year(), os.Stdout); err != nil {
 						log.Fatal(errors.E(err, "can't commit value set: %q", vs))
 					}
 				}
@@ -130,7 +130,7 @@ func (g *generator) reset() {
 	}
 }
 
-func (g *generator) commit(gofmt bool) error {
+func (g *generator) commit(gofmt bool, year int, w io.Writer) error {
 	// We'll gofmt unless user asks us to not, so no need to think too hard about sorting
 	// "correctly". We just want a deterministic output here.
 	var imports []pbinfo.ImportSpec
@@ -154,7 +154,7 @@ func (g *generator) commit(gofmt bool) error {
 	firstNonStd := sort.Search(len(imports), func(i int) bool { return strings.IndexByte(imports[i].Path, '.') >= 0 })
 
 	var file bytes.Buffer
-	fmt.Fprintf(&file, license.Apache, time.Now().Year())
+	fmt.Fprintf(&file, license.Apache, year)
 	file.WriteString("package main\n")
 	file.WriteString("import(\n")
 	for i, imp := range imports {
@@ -174,11 +174,12 @@ func (g *generator) commit(gofmt bool) error {
 		}
 		b = b2
 	}
-	_, err := os.Stdout.Write(b)
+	_, err := w.Write(b)
 	return err
 }
 
 func (g *generator) genSample(ifaceName, methName, regTag string, valSet SampleValueSet) error {
+	g.imports[g.clientPkg] = true
 	serv := g.descInfo.Serv["."+ifaceName]
 	if serv == nil {
 		return errors.E(nil, "can't find service: %q", ifaceName)


Test patch:
diff --git a/cmd/gen-go-sample/main_test.go b/cmd/gen-go-sample/main_test.go
index 63c76db..45c3c84 100644
--- a/cmd/gen-go-sample/main_test.go
+++ b/cmd/gen-go-sample/main_test.go
@@ -18,6 +18,7 @@ import (
 	"flag"
 	"io/ioutil"
 	"path/filepath"
+	"strings"
 	"testing"
 
 	"github.com/golang/protobuf/proto"
@@ -45,7 +46,6 @@ func diff(t *testing.T, name, got, goldenFile string) {
 	}
 }
 
-// TODO(pongad): maybe we should baseline test the whole file.
 func TestSample(t *testing.T) {
 	inType := &descriptor.DescriptorProto{
 		Name: proto.String("InputType"),
@@ -80,12 +80,12 @@ func TestSample(t *testing.T) {
 	}
 	file := &descriptor.FileDescriptorProto{
 		Options: &descriptor.FileOptions{
-			GoPackage: proto.String("path/to/foo;foo"),
+			GoPackage: proto.String("path/to/pb/foo;foo"),
 		},
 	}
 
 	g := generator{
-		clientPkg: pbinfo.ImportSpec{Path: "path/to/foo", Name: "foo"},
+		clientPkg: pbinfo.ImportSpec{Path: "path/to/client/foo", Name: "foo"},
 		imports:   map[pbinfo.ImportSpec]bool{},
 		descInfo: pbinfo.Info{
 			Serv: map[string]*descriptor.ServiceDescriptorProto{
@@ -122,5 +122,14 @@ func TestSample(t *testing.T) {
 	if err := g.genSample("MyService", "MyMethod", "awesome_region", vs); err != nil {
 		t.Fatal(err)
 	}
-	diff(t, "TestSample", string(g.pt.Bytes()), filepath.Join("testdata", "sample.want"))
+
+	var sb strings.Builder
+
+	// Don't format. Format can change with Go version.
+	gofmt := false
+	year := 2018
+	if err := g.commit(gofmt, year, &sb); err != nil {
+		t.Fatal(err)
+	}
+	diff(t, "TestSample", sb.String(), filepath.Join("testdata", "sample.want"))
 }
diff --git a/cmd/gen-go-sample/testdata/sample.want b/cmd/gen-go-sample/testdata/sample.want
index 1abede9..8cbabfb 100644
--- a/cmd/gen-go-sample/testdata/sample.want
+++ b/cmd/gen-go-sample/testdata/sample.want
@@ -1,3 +1,26 @@
+// Copyright 2018 Google LLC
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     https://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+// AUTO-GENERATED CODE. DO NOT EDIT.
+
+package main
+import(
+ "context"
+ "fmt"
+foo "path/to/client/foo"
+foopb "path/to/pb/foo"
+)
 // [START awesome_region]
 
 func sampleMyMethod() {