...

Source file src/cuelang.org/go/encoding/openapi/decode_test.go

Documentation: cuelang.org/go/encoding/openapi

     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/fs"
    20  	"os"
    21  	"path"
    22  	"path/filepath"
    23  	"strings"
    24  	"testing"
    25  
    26  	"github.com/go-quicktest/qt"
    27  	"github.com/google/go-cmp/cmp"
    28  	"golang.org/x/tools/txtar"
    29  
    30  	"cuelang.org/go/cue"
    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"
    36  	"cuelang.org/go/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.WalkDir("testdata/script", func(fullpath string, entry fs.DirEntry, err error) error {
    45  		if err != nil {
    46  			return err
    47  		}
    48  		if !strings.HasSuffix(fullpath, ".txtar") {
    49  			return nil
    50  		}
    51  
    52  		t.Run(fullpath, func(t *testing.T) {
    53  			a, err := txtar.ParseFile(fullpath)
    54  			if err != nil {
    55  				t.Fatal(err)
    56  			}
    57  
    58  			cfg := &openapi.Config{PkgName: "foo"}
    59  
    60  			r := &cue.Runtime{}
    61  			var in *cue.Instance
    62  			var out, errout []byte
    63  			outIndex := -1
    64  
    65  			for i, f := range a.Files {
    66  				switch path.Ext(f.Name) {
    67  				case ".json":
    68  					in, err = json.Decode(r, f.Name, f.Data)
    69  				case ".yaml":
    70  					in, err = yaml.Decode(r, f.Name, f.Data)
    71  				case ".cue":
    72  					out = f.Data
    73  					outIndex = i
    74  				case ".err":
    75  					errout = f.Data
    76  				}
    77  			}
    78  			if err != nil {
    79  				t.Fatal(err)
    80  			}
    81  
    82  			expr, err := openapi.Extract(in, cfg)
    83  			if err != nil && errout == nil {
    84  				t.Fatal(errors.Details(err, nil))
    85  			}
    86  			got := []byte(nil)
    87  			if err != nil {
    88  				got = []byte(err.Error())
    89  			}
    90  			if diff := cmp.Diff(errout, got); diff != "" {
    91  				t.Error(diff)
    92  			}
    93  
    94  			if expr != nil {
    95  				b, err := format.Node(expr, format.Simplify())
    96  				if err != nil {
    97  					t.Fatal(err)
    98  				}
    99  
   100  				// verify the generated CUE.
   101  				if _, err = r.Compile(fullpath, b); err != nil {
   102  					t.Fatal(errors.Details(err, nil))
   103  				}
   104  
   105  				b = bytes.TrimSpace(b)
   106  				out = bytes.TrimSpace(out)
   107  
   108  				if diff := cmp.Diff(b, out); diff != "" {
   109  					t.Error(diff)
   110  					if cuetest.UpdateGoldenFiles {
   111  						a.Files[outIndex].Data = b
   112  						b = txtar.Format(a)
   113  						err = os.WriteFile(fullpath, b, 0644)
   114  						if err != nil {
   115  							t.Fatal(err)
   116  						}
   117  						return
   118  					}
   119  					t.Error(cmp.Diff(string(out), string(b)))
   120  				}
   121  			}
   122  		})
   123  		return nil
   124  	})
   125  	qt.Assert(t, qt.IsNil(err))
   126  }
   127  

View as plain text