...

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

Documentation: cuelang.org/go/encoding/jsonschema

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

View as plain text