encoding/protobuf: add Builder API

A Builder parses a collection of .proto file and
organizes them according to a CUE package
layout. The exising Parse function is expressed
in this new API.

Issue #5

Change-Id: I980e7654d2b666dd1c637ad6d80f513096907a0b
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/2364
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
diff --git a/encoding/protobuf/protobuf_test.go b/encoding/protobuf/protobuf_test.go
index 9864728..8e4a45b 100644
--- a/encoding/protobuf/protobuf_test.go
+++ b/encoding/protobuf/protobuf_test.go
@@ -19,9 +19,12 @@
 	"flag"
 	"fmt"
 	"io/ioutil"
+	"os"
 	"path/filepath"
+	"strings"
 	"testing"
 
+	"cuelang.org/go/cue/ast"
 	"cuelang.org/go/cue/format"
 	"github.com/kr/pretty"
 )
@@ -36,9 +39,10 @@
 	}
 	for _, file := range testCases {
 		t.Run(file, func(t *testing.T) {
-			filename := filepath.Join("testdata", filepath.FromSlash(file))
+			root := "testdata/istio.io/api"
+			filename := filepath.Join(root, filepath.FromSlash(file))
 			c := &Config{
-				Paths: []string{"testdata"},
+				Paths: []string{"testdata", root},
 			}
 
 			out := &bytes.Buffer{}
@@ -67,3 +71,84 @@
 		})
 	}
 }
+
+func TestBuild(t *testing.T) {
+	cwd, _ := os.Getwd()
+	root := filepath.Join(cwd, "testdata/istio.io/api")
+	c := &Config{
+		Root:   root,
+		Module: "istio.io/api",
+		Paths: []string{
+			root,
+			filepath.Join(cwd, "testdata"),
+		},
+	}
+
+	b := NewBuilder(c)
+	b.AddFile("networking/v1alpha3/gateway.proto", nil)
+	b.AddFile("mixer/v1/attributes.proto", nil)
+	b.AddFile("mixer/v1/mixer.proto", nil)
+	b.AddFile("mixer/v1/config/client/client_config.proto", nil)
+
+	files, err := b.Files()
+	if err != nil {
+		t.Fatal(err)
+	}
+
+	if *update {
+		for _, f := range files {
+			b, err := format.Node(f)
+			if err != nil {
+				t.Fatal(err)
+			}
+			_ = os.MkdirAll(filepath.Dir(f.Filename), 0755)
+			err = ioutil.WriteFile(f.Filename, b, 0644)
+			if err != nil {
+				t.Fatal(err)
+			}
+		}
+		return
+	}
+
+	gotFiles := map[string]*ast.File{}
+
+	for _, f := range files {
+		rel, err := filepath.Rel(cwd, f.Filename)
+		if err != nil {
+			t.Fatal(err)
+		}
+		gotFiles[rel] = f
+	}
+
+	filepath.Walk("testdata/istio.io/api", func(path string, fi os.FileInfo, err error) error {
+		if err != nil || fi.IsDir() || !strings.HasSuffix(path, ".cue") {
+			return err
+		}
+
+		f := gotFiles[path]
+		if f == nil {
+			t.Errorf("did not produce file %q", path)
+			return nil
+		}
+		delete(gotFiles, path)
+
+		got, err := format.Node(f)
+		if err != nil {
+			t.Fatal(err)
+		}
+
+		want, err := ioutil.ReadFile(path)
+		if err != nil {
+			t.Fatal(err)
+		}
+
+		if !bytes.Equal(got, want) {
+			t.Errorf("%s: files differ", path)
+		}
+		return nil
+	})
+
+	for filename := range gotFiles {
+		t.Errorf("did not expect file %q", filename)
+	}
+}