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