github.com/solo-io/cue@v0.4.7/encoding/openapi/decode_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 openapi_test 16 17 import ( 18 "bytes" 19 "io/ioutil" 20 "os" 21 "path" 22 "path/filepath" 23 "strings" 24 "testing" 25 26 "github.com/google/go-cmp/cmp" 27 "github.com/stretchr/testify/assert" 28 "golang.org/x/tools/txtar" 29 30 "github.com/solo-io/cue/cue" 31 "github.com/solo-io/cue/cue/errors" 32 "github.com/solo-io/cue/cue/format" 33 "github.com/solo-io/cue/encoding/json" 34 "github.com/solo-io/cue/encoding/openapi" 35 "github.com/solo-io/cue/encoding/yaml" 36 "github.com/solo-io/cue/internal/cuetest" 37 ) 38 39 // TestDecode reads the testdata/*.txtar files, converts the contained 40 // JSON schema to CUE and compares it against the output. 41 // 42 // Set CUE_UPDATE=1 to update test files with the corresponding output. 43 func TestDecode(t *testing.T) { 44 err := filepath.Walk("testdata/script", func(fullpath string, info os.FileInfo, err error) error { 45 _ = err 46 if !strings.HasSuffix(fullpath, ".txtar") { 47 return nil 48 } 49 50 t.Run(fullpath, func(t *testing.T) { 51 a, err := txtar.ParseFile(fullpath) 52 if err != nil { 53 t.Fatal(err) 54 } 55 56 cfg := &openapi.Config{PkgName: "foo"} 57 58 r := &cue.Runtime{} 59 var in *cue.Instance 60 var out, errout []byte 61 outIndex := -1 62 63 for i, f := range a.Files { 64 switch path.Ext(f.Name) { 65 case ".json": 66 in, err = json.Decode(r, f.Name, f.Data) 67 case ".yaml": 68 in, err = yaml.Decode(r, f.Name, f.Data) 69 case ".cue": 70 out = f.Data 71 outIndex = i 72 case ".err": 73 errout = f.Data 74 } 75 } 76 if err != nil { 77 t.Fatal(err) 78 } 79 80 expr, err := openapi.Extract(in, cfg) 81 if err != nil && errout == nil { 82 t.Fatal(errors.Details(err, nil)) 83 } 84 got := []byte(nil) 85 if err != nil { 86 got = []byte(err.Error()) 87 } 88 if !cmp.Equal(errout, got) { 89 t.Error(cmp.Diff(string(got), string(errout))) 90 } 91 92 if expr != nil { 93 b, err := format.Node(expr, format.Simplify()) 94 if err != nil { 95 t.Fatal(err) 96 } 97 98 // verify the generated CUE. 99 if _, err = r.Compile(fullpath, b); err != nil { 100 t.Fatal(errors.Details(err, nil)) 101 } 102 103 b = bytes.TrimSpace(b) 104 out = bytes.TrimSpace(out) 105 106 if !cmp.Equal(b, out) { 107 if cuetest.UpdateGoldenFiles { 108 a.Files[outIndex].Data = b 109 b = txtar.Format(a) 110 err = ioutil.WriteFile(fullpath, b, 0644) 111 if err != nil { 112 t.Fatal(err) 113 } 114 return 115 } 116 t.Error(cmp.Diff(string(out), string(b))) 117 } 118 } 119 }) 120 return nil 121 }) 122 assert.NoError(t, err) 123 }