Skip to content

Commit

Permalink
encoding/jsonschema: port txtar tests to cuetxtar
Browse files Browse the repository at this point in the history
This saves about 60 lines of code to deal with loading the txtar files
as well as updating output files following CUE_UPDATE and so on,
but most importantly, it unlocks the ability to test with evalv3 too.

For #3351.

Signed-off-by: Daniel Martí <mvdan@mvdan.cc>
Change-Id: I3f9a368391c4510d2f5a389da3ac7babb376e036
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1198734
Reviewed-by: Roger Peppe <rogpeppe@gmail.com>
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>
  • Loading branch information
mvdan committed Aug 1, 2024
1 parent 91929bd commit 44bc1ab
Show file tree
Hide file tree
Showing 17 changed files with 68 additions and 129 deletions.
158 changes: 49 additions & 109 deletions encoding/jsonschema/decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,13 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package jsonschema
package jsonschema_test

import (
"bytes"
"io/fs"
"os"
"path"
"path/filepath"
"strings"
"testing"

"github.com/go-quicktest/qt"
"github.com/google/go-cmp/cmp"
"golang.org/x/tools/txtar"

"cuelang.org/go/cue"
Expand All @@ -34,9 +28,10 @@ import (
"cuelang.org/go/cue/format"
"cuelang.org/go/cue/token"
"cuelang.org/go/encoding/json"
"cuelang.org/go/encoding/jsonschema"
"cuelang.org/go/encoding/yaml"
"cuelang.org/go/internal/astinternal"
"cuelang.org/go/internal/cuetest"
"cuelang.org/go/internal/cuetxtar"
_ "cuelang.org/go/pkg"
)

Expand All @@ -45,121 +40,66 @@ import (
//
// Set CUE_UPDATE=1 to update test files with the corresponding output.
func TestDecode(t *testing.T) {
err := filepath.WalkDir("testdata", func(fullpath string, entry fs.DirEntry, err error) error {
if err != nil {
return err
}
if !strings.HasSuffix(fullpath, ".txtar") {
return nil
}

t.Run(fullpath, func(t *testing.T) {
a, err := txtar.ParseFile(fullpath)
if err != nil {
t.Fatal(err)
test := cuetxtar.TxTarTest{
Root: "./testdata",
Name: "decode",
}
test.Run(t, func(t *cuetxtar.Test) {
cfg := &jsonschema.Config{}

if t.HasTag("openapi") {
cfg.Root = "#/components/schemas/"
cfg.Map = func(p token.Pos, a []string) ([]ast.Label, error) {
// Just for testing: does not validate the path.
return []ast.Label{ast.NewIdent("#" + a[len(a)-1])}, nil
}
}

cfg := &Config{ID: fullpath}
ctx := t.Context()
var v cue.Value

if bytes.Contains(a.Comment, []byte("openapi")) {
cfg.Root = "#/components/schemas/"
cfg.Map = func(p token.Pos, a []string) ([]ast.Label, error) {
// Just for testing: does not validate the path.
return []ast.Label{ast.NewIdent("#" + a[len(a)-1])}, nil
for _, f := range t.Archive.Files {
switch path.Ext(f.Name) {
case ".json":
expr, err := json.Extract(f.Name, f.Data)
if err != nil {
t.Fatal(err)
}
}

ctx := cuecontext.New()
var v cue.Value
var out, errout []byte
outIndex := -1
errIndex := -1

for i, f := range a.Files {
switch path.Ext(f.Name) {
case ".json":
expr, err := json.Extract(f.Name, f.Data)
if err != nil {
t.Fatal(err)
}
v = ctx.BuildExpr(expr)
case ".yaml":
file, err := yaml.Extract(f.Name, f.Data)
if err != nil {
t.Fatal(err)
}
v = ctx.BuildFile(file)
case ".cue":
out = f.Data
outIndex = i
case ".err":
errout = f.Data
errIndex = i
v = ctx.BuildExpr(expr)
case ".yaml":
file, err := yaml.Extract(f.Name, f.Data)
if err != nil {
t.Fatal(err)
}
v = ctx.BuildFile(file)
}
if err != nil {
t.Fatal(err)
}
}

updated := false
expr, err := jsonschema.Extract(v, cfg)
if err != nil {
got := []byte(errors.Details(err, nil))
got = append(bytes.TrimSpace(got), '\n')
t.Writer("err").Write(got)
}

expr, err := Extract(v, cfg)
if expr != nil {
b, err := format.Node(expr, format.Simplify())
if err != nil {
got := []byte(errors.Details(err, nil))

got = bytes.TrimSpace(got)
errout = bytes.TrimSpace(errout)

switch {
case !cmp.Equal(errout, got):
if cuetest.UpdateGoldenFiles {
a.Files[errIndex].Data = got
updated = true
break
}
t.Error(cmp.Diff(string(got), string(errout)))
}
t.Fatal(errors.Details(err, nil))
}

if expr != nil {
b, err := format.Node(expr, format.Simplify())
if err != nil {
// verify the generated CUE.
if !t.HasTag("noverify") {
v := ctx.CompileBytes(b, cue.Filename("generated.cue"))
if err := v.Err(); err != nil {
t.Fatal(errors.Details(err, nil))
}

// verify the generated CUE.
if !bytes.Contains(a.Comment, []byte("#noverify")) {
v := ctx.CompileBytes(b, cue.Filename(fullpath))
if err := v.Err(); err != nil {
t.Fatal(errors.Details(err, nil))
}
}

b = bytes.TrimSpace(b)
out = bytes.TrimSpace(out)

switch {
case !cmp.Equal(b, out):
if cuetest.UpdateGoldenFiles {
updated = true
a.Files[outIndex].Data = b
break
}
t.Error(cmp.Diff(string(out), string(b)))
}
}

if updated {
b := txtar.Format(a)
err = os.WriteFile(fullpath, b, 0644)
if err != nil {
t.Fatal(err)
}
}
})
return nil
b = append(bytes.TrimSpace(b), '\n')
t.Writer("cue").Write(b)
}
})
qt.Assert(t, qt.IsNil(err))
}

func TestX(t *testing.T) {
Expand Down Expand Up @@ -190,8 +130,8 @@ func TestX(t *testing.T) {
}
}

cfg := &Config{ID: "test"}
expr, err := Extract(v, cfg)
cfg := &jsonschema.Config{ID: "test"}
expr, err := jsonschema.Extract(v, cfg)
if err != nil {
t.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion encoding/jsonschema/testdata/basic.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
}
}

-- out.cue --
-- out/decode/cue --
import "strings"

// Main schema
Expand Down
2 changes: 1 addition & 1 deletion encoding/jsonschema/testdata/def.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
}
}

-- out.cue --
-- out/decode/cue --
@jsonschema(schema="http://json-schema.org/draft-07/schema#")
@jsonschema(id="http://cuelang.org/go/encoding/openapi/testdata/order.json")
person?: #["per-son"]
Expand Down
2 changes: 1 addition & 1 deletion encoding/jsonschema/testdata/emptyanyof.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
}
}

-- out.cue --
-- out/decode/cue --
_

#shell: (string | ("bash" | "sh" | "cmd" | "powershell")) & string
3 changes: 1 addition & 2 deletions encoding/jsonschema/testdata/emptyobj.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@
}
}
}
-- out.err --
-- out.cue --
-- out/decode/cue --
@jsonschema(schema="http://json-schema.org/draft-07/schema")
_

Expand Down
4 changes: 2 additions & 2 deletions encoding/jsonschema/testdata/err.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
"additionalProperties": false
}

-- out.err --
-- out/decode/err --
constraint not allowed because type string is excluded:
type.json:9:9
-- out.cue --
-- out/decode/cue --
multi?: int & >=2 & <=3
2 changes: 1 addition & 1 deletion encoding/jsonschema/testdata/list.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ properties:

additionalProperties: false

-- out.cue --
-- out/decode/cue --
import "list"

foo?: [...string]
Expand Down
2 changes: 1 addition & 1 deletion encoding/jsonschema/testdata/num.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"additionalProperties": false
}

-- out.cue --
-- out/decode/cue --
import (
"strings"
"math"
Expand Down
2 changes: 1 addition & 1 deletion encoding/jsonschema/testdata/object.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
"additionalProperties": false
}

-- out.cue --
-- out/decode/cue --
import "struct"

// Main schema
Expand Down
4 changes: 2 additions & 2 deletions encoding/jsonschema/testdata/openapi.txtar
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
openapi
#openapi

-- type.yaml --
components:
Expand All @@ -18,7 +18,7 @@ components:
description: "The number to dial."
type: string

-- out.cue --
-- out/decode/cue --
// A User uses something.
#User: {
id?: int
Expand Down
2 changes: 1 addition & 1 deletion encoding/jsonschema/testdata/ref.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
}
}

-- out.cue --
-- out/decode/cue --
import (
"acme.com/external.json:external"
"acme.com/external-foo.json:schema"
Expand Down
2 changes: 1 addition & 1 deletion encoding/jsonschema/testdata/refroot.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
}
}

-- out.cue --
-- out/decode/cue --
_schema
_schema: {
@jsonschema(schema="http://json-schema.org/draft-07/schema#")
Expand Down
2 changes: 1 addition & 1 deletion encoding/jsonschema/testdata/refroot2.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
}
}

-- out.cue --
-- out/decode/cue --
_schema
_schema: {
@jsonschema(schema="http://json-schema.org/draft-07/schema#")
Expand Down
2 changes: 1 addition & 1 deletion encoding/jsonschema/testdata/type.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"additionalProperties": false
}

-- out.cue --
-- out/decode/cue --
// Main schema
intString?: null | bool | int | string | [...]
object?: {
Expand Down
4 changes: 2 additions & 2 deletions encoding/jsonschema/testdata/typedis.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@
}
}
}
-- out.err --
-- out/decode/err --
constraint not allowed because type string is excluded:
type.json:39:15
-- out.cue --
-- out/decode/cue --
// Main schema
intOrString1?: int | string
intOrString2?: int | string
Expand Down
2 changes: 1 addition & 1 deletion encoding/jsonschema/testdata/unsupported.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
}


-- out.cue --
-- out/decode/cue --
@jsonschema(schema="http://json-schema.org/draft-07/schema")
_

Expand Down
2 changes: 1 addition & 1 deletion encoding/jsonschema/testdata/used.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
}
}
}
-- out.cue --
-- out/decode/cue --
_

#enum: "a" | "b" | "c"
Expand Down

0 comments on commit 44bc1ab

Please sign in to comment.