blob: 2ce5d5f9c4e846e12bdc9d081d9b9fe53d98532d [file] [log] [blame]
Marcel van Lohuizen5274e982019-04-28 17:51:43 +02001// Copyright 2019 CUE Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package protobuf
16
17import (
18 "bytes"
19 "flag"
20 "fmt"
21 "io/ioutil"
Marcel van Lohuizen93e95972019-06-27 16:47:52 +020022 "os"
Marcel van Lohuizen5274e982019-04-28 17:51:43 +020023 "path/filepath"
Marcel van Lohuizen93e95972019-06-27 16:47:52 +020024 "strings"
Marcel van Lohuizen5274e982019-04-28 17:51:43 +020025 "testing"
26
Koichi Shiraishi30a4bee2019-12-26 03:06:48 +090027 "github.com/kr/pretty"
28
Marcel van Lohuizen93e95972019-06-27 16:47:52 +020029 "cuelang.org/go/cue/ast"
Marcel van Lohuizene7abb202019-10-08 11:17:17 +020030 "cuelang.org/go/cue/errors"
Marcel van Lohuizen5274e982019-04-28 17:51:43 +020031 "cuelang.org/go/cue/format"
Marcel van Lohuizen5274e982019-04-28 17:51:43 +020032)
33
Marcel van Lohuizenfbcb3392019-06-25 11:02:21 +020034var update = flag.Bool("update", false, "update the test output")
Marcel van Lohuizen5274e982019-04-28 17:51:43 +020035
Marcel van Lohuizenf94a5482019-07-02 20:15:03 +020036func TestExtractDefinitions(t *testing.T) {
Marcel van Lohuizen5274e982019-04-28 17:51:43 +020037 testCases := []string{
38 "networking/v1alpha3/gateway.proto",
39 "mixer/v1/attributes.proto",
40 "mixer/v1/config/client/client_config.proto",
41 }
42 for _, file := range testCases {
43 t.Run(file, func(t *testing.T) {
Marcel van Lohuizen93e95972019-06-27 16:47:52 +020044 root := "testdata/istio.io/api"
45 filename := filepath.Join(root, filepath.FromSlash(file))
Marcel van Lohuizen5274e982019-04-28 17:51:43 +020046 c := &Config{
Marcel van Lohuizen93e95972019-06-27 16:47:52 +020047 Paths: []string{"testdata", root},
Marcel van Lohuizen5274e982019-04-28 17:51:43 +020048 }
49
50 out := &bytes.Buffer{}
51
Marcel van Lohuizenf94a5482019-07-02 20:15:03 +020052 if f, err := Extract(filename, nil, c); err != nil {
Marcel van Lohuizen5274e982019-04-28 17:51:43 +020053 fmt.Fprintln(out, err)
54 } else {
Marcel van Lohuizen49544912019-06-26 13:16:48 +020055 b, _ := format.Node(f, format.Simplify())
56 out.Write(b)
Marcel van Lohuizen5274e982019-04-28 17:51:43 +020057 }
58
59 wantFile := filepath.Join("testdata", filepath.Base(file)+".out.cue")
60 if *update {
Marcel van Lohuizenafd1cc52019-08-07 00:28:13 +020061 _ = ioutil.WriteFile(wantFile, out.Bytes(), 0644)
Marcel van Lohuizen5274e982019-04-28 17:51:43 +020062 return
63 }
64
65 b, err := ioutil.ReadFile(wantFile)
66 if err != nil {
67 t.Fatal(err)
68 }
69
70 if desc := pretty.Diff(out.String(), string(b)); len(desc) > 0 {
71 t.Errorf("files differ:\n%v", desc)
72 }
73 })
74 }
75}
Marcel van Lohuizen93e95972019-06-27 16:47:52 +020076
77func TestBuild(t *testing.T) {
78 cwd, _ := os.Getwd()
79 root := filepath.Join(cwd, "testdata/istio.io/api")
80 c := &Config{
81 Root: root,
82 Module: "istio.io/api",
83 Paths: []string{
84 root,
85 filepath.Join(cwd, "testdata"),
86 },
87 }
88
Marcel van Lohuizenf94a5482019-07-02 20:15:03 +020089 b := NewExtractor(c)
Marcel van Lohuizenafd1cc52019-08-07 00:28:13 +020090 _ = b.AddFile("networking/v1alpha3/gateway.proto", nil)
91 _ = b.AddFile("mixer/v1/attributes.proto", nil)
92 _ = b.AddFile("mixer/v1/mixer.proto", nil)
93 _ = b.AddFile("mixer/v1/config/client/client_config.proto", nil)
Marcel van Lohuizen93e95972019-06-27 16:47:52 +020094
95 files, err := b.Files()
96 if err != nil {
Marcel van Lohuizene7abb202019-10-08 11:17:17 +020097 t.Fatal(errors.Details(err, nil))
Marcel van Lohuizen93e95972019-06-27 16:47:52 +020098 }
99
100 if *update {
101 for _, f := range files {
102 b, err := format.Node(f)
103 if err != nil {
104 t.Fatal(err)
105 }
106 _ = os.MkdirAll(filepath.Dir(f.Filename), 0755)
107 err = ioutil.WriteFile(f.Filename, b, 0644)
108 if err != nil {
109 t.Fatal(err)
110 }
111 }
112 return
113 }
114
115 gotFiles := map[string]*ast.File{}
116
117 for _, f := range files {
118 rel, err := filepath.Rel(cwd, f.Filename)
119 if err != nil {
120 t.Fatal(err)
121 }
122 gotFiles[rel] = f
123 }
124
Marcel van Lohuizen64cb20a2019-08-06 21:24:14 +0200125 _ = filepath.Walk("testdata/istio.io/api", func(path string, fi os.FileInfo, err error) error {
Marcel van Lohuizen93e95972019-06-27 16:47:52 +0200126 if err != nil || fi.IsDir() || !strings.HasSuffix(path, ".cue") {
127 return err
128 }
129
130 f := gotFiles[path]
131 if f == nil {
132 t.Errorf("did not produce file %q", path)
133 return nil
134 }
135 delete(gotFiles, path)
136
137 got, err := format.Node(f)
138 if err != nil {
139 t.Fatal(err)
140 }
141
142 want, err := ioutil.ReadFile(path)
143 if err != nil {
144 t.Fatal(err)
145 }
146
147 if !bytes.Equal(got, want) {
148 t.Errorf("%s: files differ", path)
149 }
150 return nil
151 })
152
153 for filename := range gotFiles {
154 t.Errorf("did not expect file %q", filename)
155 }
156}