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