github.com/solo-io/cue@v0.4.7/encoding/jsonschema/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 jsonschema 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/rogpeppe/go-internal/txtar" 28 "github.com/stretchr/testify/assert" 29 30 "github.com/solo-io/cue/cue" 31 "github.com/solo-io/cue/cue/ast" 32 "github.com/solo-io/cue/cue/errors" 33 "github.com/solo-io/cue/cue/format" 34 "github.com/solo-io/cue/cue/token" 35 "github.com/solo-io/cue/encoding/json" 36 "github.com/solo-io/cue/encoding/yaml" 37 "github.com/solo-io/cue/internal/astinternal" 38 "github.com/solo-io/cue/internal/cuetest" 39 _ "github.com/solo-io/cue/pkg" 40 ) 41 42 // TestDecode reads the testdata/*.txtar files, converts the contained 43 // JSON schema to CUE and compares it against the output. 44 // 45 // Set CUE_UPDATE=1 to update test files with the corresponding output. 46 func TestDecode(t *testing.T) { 47 err := filepath.Walk("testdata", func(fullpath string, info os.FileInfo, err error) error { 48 _ = err 49 if !strings.HasSuffix(fullpath, ".txtar") { 50 return nil 51 } 52 53 t.Run(fullpath, func(t *testing.T) { 54 a, err := txtar.ParseFile(fullpath) 55 if err != nil { 56 t.Fatal(err) 57 } 58 59 cfg := &Config{ID: fullpath} 60 61 if bytes.Contains(a.Comment, []byte("openapi")) { 62 cfg.Root = "#/components/schemas/" 63 cfg.Map = func(p token.Pos, a []string) ([]ast.Label, error) { 64 // Just for testing: does not validate the path. 65 return []ast.Label{ast.NewIdent("#" + a[len(a)-1])}, nil 66 } 67 } 68 69 r := &cue.Runtime{} 70 var in *cue.Instance 71 var out, errout []byte 72 outIndex := -1 73 errIndex := -1 74 75 for i, f := range a.Files { 76 switch path.Ext(f.Name) { 77 case ".json": 78 in, err = json.Decode(r, f.Name, f.Data) 79 case ".yaml": 80 in, err = yaml.Decode(r, f.Name, f.Data) 81 case ".cue": 82 out = f.Data 83 outIndex = i 84 case ".err": 85 errout = f.Data 86 errIndex = i 87 } 88 } 89 if err != nil { 90 t.Fatal(err) 91 } 92 93 updated := false 94 95 expr, err := Extract(in, cfg) 96 if err != nil { 97 got := []byte(errors.Details(err, nil)) 98 99 got = bytes.TrimSpace(got) 100 errout = bytes.TrimSpace(errout) 101 102 switch { 103 case !cmp.Equal(errout, got): 104 if cuetest.UpdateGoldenFiles { 105 a.Files[errIndex].Data = got 106 updated = true 107 break 108 } 109 t.Error(cmp.Diff(string(got), string(errout))) 110 } 111 } 112 113 if expr != nil { 114 b, err := format.Node(expr, format.Simplify()) 115 if err != nil { 116 t.Fatal(errors.Details(err, nil)) 117 } 118 119 // verify the generated CUE. 120 if !bytes.Contains(a.Comment, []byte("#noverify")) { 121 if _, err = r.Compile(fullpath, b); err != nil { 122 t.Fatal(errors.Details(err, nil)) 123 } 124 } 125 126 b = bytes.TrimSpace(b) 127 out = bytes.TrimSpace(out) 128 129 switch { 130 case !cmp.Equal(b, out): 131 if cuetest.UpdateGoldenFiles { 132 updated = true 133 a.Files[outIndex].Data = b 134 break 135 } 136 t.Error(cmp.Diff(string(out), string(b))) 137 } 138 } 139 140 if updated { 141 b := txtar.Format(a) 142 err = ioutil.WriteFile(fullpath, b, 0644) 143 if err != nil { 144 t.Fatal(err) 145 } 146 } 147 }) 148 return nil 149 }) 150 assert.NoError(t, err) 151 } 152 153 func TestX(t *testing.T) { 154 t.Skip() 155 data := ` 156 -- schema.json -- 157 ` 158 159 a := txtar.Parse([]byte(data)) 160 161 r := &cue.Runtime{} 162 var in *cue.Instance 163 var err error 164 for _, f := range a.Files { 165 switch path.Ext(f.Name) { 166 case ".json": 167 in, err = json.Decode(r, f.Name, f.Data) 168 if err != nil { 169 t.Fatal(err) 170 } 171 case ".yaml": 172 in, err = yaml.Decode(r, f.Name, f.Data) 173 if err != nil { 174 t.Fatal(err) 175 } 176 } 177 } 178 179 cfg := &Config{ID: "test"} 180 expr, err := Extract(in, cfg) 181 if err != nil { 182 t.Fatal(err) 183 } 184 185 t.Fatal(astinternal.DebugStr(expr)) 186 }