cuelang.org/go@v0.10.1/encoding/protobuf/protobuf_test.go (about) 1 // 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 15 package protobuf 16 17 import ( 18 "bytes" 19 "fmt" 20 "io/fs" 21 "os" 22 "path/filepath" 23 "strings" 24 "testing" 25 26 "github.com/google/go-cmp/cmp" 27 28 "cuelang.org/go/cue/ast" 29 "cuelang.org/go/cue/errors" 30 "cuelang.org/go/cue/format" 31 "cuelang.org/go/internal/cuetest" 32 ) 33 34 func TestExtractDefinitions(t *testing.T) { 35 testCases := []string{ 36 "networking/v1alpha3/gateway.proto", 37 "mixer/v1/attributes.proto", 38 "mixer/v1/config/client/client_config.proto", 39 "other/trailcomment.proto", 40 } 41 for _, file := range testCases { 42 t.Run(file, func(t *testing.T) { 43 root := "testdata/istio.io/api" 44 filename := filepath.Join(root, filepath.FromSlash(file)) 45 c := &Config{ 46 Paths: []string{"testdata", root}, 47 } 48 49 out := &bytes.Buffer{} 50 51 if f, err := Extract(filename, nil, c); err != nil { 52 fmt.Fprintln(out, err) 53 } else { 54 b, _ := format.Node(f, format.Simplify()) 55 out.Write(b) 56 } 57 58 wantFile := filepath.Join("testdata", filepath.Base(file)+".out.cue") 59 if cuetest.UpdateGoldenFiles { 60 _ = os.WriteFile(wantFile, out.Bytes(), 0644) 61 return 62 } 63 64 b, err := os.ReadFile(wantFile) 65 if err != nil { 66 t.Fatal(err) 67 } 68 69 if diff := cmp.Diff(out.String(), string(b)); diff != "" { 70 t.Error(diff) 71 } 72 }) 73 } 74 } 75 76 func 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@v0", 82 Paths: []string{ 83 root, 84 filepath.Join(cwd, "testdata"), 85 }, 86 } 87 88 b := NewExtractor(c) 89 _ = 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) 93 94 files, err := b.Files() 95 if err != nil { 96 t.Fatal(errors.Details(err, nil)) 97 } 98 99 if cuetest.UpdateGoldenFiles { 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 = os.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 124 _ = filepath.WalkDir("testdata/istio.io/api", func(path string, entry fs.DirEntry, err error) error { 125 if err != nil || entry.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 := os.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 }