blob: b824e0521f637ea14bf83eaec48bfc6413414987 [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
Marcel van Lohuizen93e95972019-06-27 16:47:52 +020027 "cuelang.org/go/cue/ast"
Marcel van Lohuizene7abb202019-10-08 11:17:17 +020028 "cuelang.org/go/cue/errors"
Marcel van Lohuizen5274e982019-04-28 17:51:43 +020029 "cuelang.org/go/cue/format"
30 "github.com/kr/pretty"
31)
32
Marcel van Lohuizenfbcb3392019-06-25 11:02:21 +020033var update = flag.Bool("update", false, "update the test output")
Marcel van Lohuizen5274e982019-04-28 17:51:43 +020034
Marcel van Lohuizenf94a5482019-07-02 20:15:03 +020035func TestExtractDefinitions(t *testing.T) {
Marcel van Lohuizen5274e982019-04-28 17:51:43 +020036 testCases := []string{
37 "networking/v1alpha3/gateway.proto",
38 "mixer/v1/attributes.proto",
39 "mixer/v1/config/client/client_config.proto",
40 }
41 for _, file := range testCases {
42 t.Run(file, func(t *testing.T) {
Marcel van Lohuizen93e95972019-06-27 16:47:52 +020043 root := "testdata/istio.io/api"
44 filename := filepath.Join(root, filepath.FromSlash(file))
Marcel van Lohuizen5274e982019-04-28 17:51:43 +020045 c := &Config{
Marcel van Lohuizen93e95972019-06-27 16:47:52 +020046 Paths: []string{"testdata", root},
Marcel van Lohuizen5274e982019-04-28 17:51:43 +020047 }
48
49 out := &bytes.Buffer{}
50
Marcel van Lohuizenf94a5482019-07-02 20:15:03 +020051 if f, err := Extract(filename, nil, c); err != nil {
Marcel van Lohuizen5274e982019-04-28 17:51:43 +020052 fmt.Fprintln(out, err)
53 } else {
Marcel van Lohuizen49544912019-06-26 13:16:48 +020054 b, _ := format.Node(f, format.Simplify())
55 out.Write(b)
Marcel van Lohuizen5274e982019-04-28 17:51:43 +020056 }
57
58 wantFile := filepath.Join("testdata", filepath.Base(file)+".out.cue")
59 if *update {
Marcel van Lohuizenafd1cc52019-08-07 00:28:13 +020060 _ = ioutil.WriteFile(wantFile, out.Bytes(), 0644)
Marcel van Lohuizen5274e982019-04-28 17:51:43 +020061 return
62 }
63
64 b, err := ioutil.ReadFile(wantFile)
65 if err != nil {
66 t.Fatal(err)
67 }
68
69 if desc := pretty.Diff(out.String(), string(b)); len(desc) > 0 {
70 t.Errorf("files differ:\n%v", desc)
71 }
72 })
73 }
74}
Marcel van Lohuizen93e95972019-06-27 16:47:52 +020075
76func TestBuild(t *testing.T) {
77 cwd, _ := os.Getwd()
78 root := filepath.Join(cwd, "testdata/istio.io/api")
79 c := &Config{
80 Root: root,
81 Module: "istio.io/api",
82 Paths: []string{
83 root,
84 filepath.Join(cwd, "testdata"),
85 },
86 }
87
Marcel van Lohuizenf94a5482019-07-02 20:15:03 +020088 b := NewExtractor(c)
Marcel van Lohuizenafd1cc52019-08-07 00:28:13 +020089 _ = b.AddFile("networking/v1alpha3/gateway.proto", nil)
90 _ = b.AddFile("mixer/v1/attributes.proto", nil)
91 _ = b.AddFile("mixer/v1/mixer.proto", nil)
92 _ = b.AddFile("mixer/v1/config/client/client_config.proto", nil)
Marcel van Lohuizen93e95972019-06-27 16:47:52 +020093
94 files, err := b.Files()
95 if err != nil {
Marcel van Lohuizene7abb202019-10-08 11:17:17 +020096 t.Fatal(errors.Details(err, nil))
Marcel van Lohuizen93e95972019-06-27 16:47:52 +020097 }
98
99 if *update {
100 for _, f := range files {
101 b, err := format.Node(f)
102 if err != nil {
103 t.Fatal(err)
104 }
105 _ = os.MkdirAll(filepath.Dir(f.Filename), 0755)
106 err = ioutil.WriteFile(f.Filename, b, 0644)
107 if err != nil {
108 t.Fatal(err)
109 }
110 }
111 return
112 }
113
114 gotFiles := map[string]*ast.File{}
115
116 for _, f := range files {
117 rel, err := filepath.Rel(cwd, f.Filename)
118 if err != nil {
119 t.Fatal(err)
120 }
121 gotFiles[rel] = f
122 }
123
Marcel van Lohuizen64cb20a2019-08-06 21:24:14 +0200124 _ = filepath.Walk("testdata/istio.io/api", func(path string, fi os.FileInfo, err error) error {
Marcel van Lohuizen93e95972019-06-27 16:47:52 +0200125 if err != nil || fi.IsDir() || !strings.HasSuffix(path, ".cue") {
126 return err
127 }
128
129 f := gotFiles[path]
130 if f == nil {
131 t.Errorf("did not produce file %q", path)
132 return nil
133 }
134 delete(gotFiles, path)
135
136 got, err := format.Node(f)
137 if err != nil {
138 t.Fatal(err)
139 }
140
141 want, err := ioutil.ReadFile(path)
142 if err != nil {
143 t.Fatal(err)
144 }
145
146 if !bytes.Equal(got, want) {
147 t.Errorf("%s: files differ", path)
148 }
149 return nil
150 })
151
152 for filename := range gotFiles {
153 t.Errorf("did not expect file %q", filename)
154 }
155}