From 44bc1ab9d15d05916e36e4a21ed56267f11fd7f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Mart=C3=AD?= Date: Thu, 1 Aug 2024 12:44:06 +0100 Subject: [PATCH] encoding/jsonschema: port txtar tests to cuetxtar MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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í Change-Id: I3f9a368391c4510d2f5a389da3ac7babb376e036 Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1198734 Reviewed-by: Roger Peppe TryBot-Result: CUEcueckoo Unity-Result: CUE porcuepine --- encoding/jsonschema/decode_test.go | 158 ++++++------------ encoding/jsonschema/testdata/basic.txtar | 2 +- encoding/jsonschema/testdata/def.txtar | 2 +- encoding/jsonschema/testdata/emptyanyof.txtar | 2 +- encoding/jsonschema/testdata/emptyobj.txtar | 3 +- encoding/jsonschema/testdata/err.txtar | 4 +- encoding/jsonschema/testdata/list.txtar | 2 +- encoding/jsonschema/testdata/num.txtar | 2 +- encoding/jsonschema/testdata/object.txtar | 2 +- encoding/jsonschema/testdata/openapi.txtar | 4 +- encoding/jsonschema/testdata/ref.txtar | 2 +- encoding/jsonschema/testdata/refroot.txtar | 2 +- encoding/jsonschema/testdata/refroot2.txtar | 2 +- encoding/jsonschema/testdata/type.txtar | 2 +- encoding/jsonschema/testdata/typedis.txtar | 4 +- .../jsonschema/testdata/unsupported.txtar | 2 +- encoding/jsonschema/testdata/used.txtar | 2 +- 17 files changed, 68 insertions(+), 129 deletions(-) diff --git a/encoding/jsonschema/decode_test.go b/encoding/jsonschema/decode_test.go index 63f07bc751d..a653df5840e 100644 --- a/encoding/jsonschema/decode_test.go +++ b/encoding/jsonschema/decode_test.go @@ -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" @@ -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" ) @@ -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) { @@ -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) } diff --git a/encoding/jsonschema/testdata/basic.txtar b/encoding/jsonschema/testdata/basic.txtar index 45d8d741470..f4eb29180c7 100644 --- a/encoding/jsonschema/testdata/basic.txtar +++ b/encoding/jsonschema/testdata/basic.txtar @@ -37,7 +37,7 @@ } } --- out.cue -- +-- out/decode/cue -- import "strings" // Main schema diff --git a/encoding/jsonschema/testdata/def.txtar b/encoding/jsonschema/testdata/def.txtar index 35ea0a3de2d..cc11c99020c 100644 --- a/encoding/jsonschema/testdata/def.txtar +++ b/encoding/jsonschema/testdata/def.txtar @@ -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"] diff --git a/encoding/jsonschema/testdata/emptyanyof.txtar b/encoding/jsonschema/testdata/emptyanyof.txtar index 0313e6d34d8..c32734221d8 100644 --- a/encoding/jsonschema/testdata/emptyanyof.txtar +++ b/encoding/jsonschema/testdata/emptyanyof.txtar @@ -20,7 +20,7 @@ } } --- out.cue -- +-- out/decode/cue -- _ #shell: (string | ("bash" | "sh" | "cmd" | "powershell")) & string diff --git a/encoding/jsonschema/testdata/emptyobj.txtar b/encoding/jsonschema/testdata/emptyobj.txtar index 61ddea165f1..c3b4a4994d2 100644 --- a/encoding/jsonschema/testdata/emptyobj.txtar +++ b/encoding/jsonschema/testdata/emptyobj.txtar @@ -22,8 +22,7 @@ } } } --- out.err -- --- out.cue -- +-- out/decode/cue -- @jsonschema(schema="http://json-schema.org/draft-07/schema") _ diff --git a/encoding/jsonschema/testdata/err.txtar b/encoding/jsonschema/testdata/err.txtar index 4b16985c2eb..90527e951c9 100644 --- a/encoding/jsonschema/testdata/err.txtar +++ b/encoding/jsonschema/testdata/err.txtar @@ -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 diff --git a/encoding/jsonschema/testdata/list.txtar b/encoding/jsonschema/testdata/list.txtar index e2695bfb168..36f3fe662e4 100644 --- a/encoding/jsonschema/testdata/list.txtar +++ b/encoding/jsonschema/testdata/list.txtar @@ -35,7 +35,7 @@ properties: additionalProperties: false --- out.cue -- +-- out/decode/cue -- import "list" foo?: [...string] diff --git a/encoding/jsonschema/testdata/num.txtar b/encoding/jsonschema/testdata/num.txtar index 0b53a7a8fb4..2dad4a44042 100644 --- a/encoding/jsonschema/testdata/num.txtar +++ b/encoding/jsonschema/testdata/num.txtar @@ -38,7 +38,7 @@ "additionalProperties": false } --- out.cue -- +-- out/decode/cue -- import ( "strings" "math" diff --git a/encoding/jsonschema/testdata/object.txtar b/encoding/jsonschema/testdata/object.txtar index 1e6644e57a8..0f791b86966 100644 --- a/encoding/jsonschema/testdata/object.txtar +++ b/encoding/jsonschema/testdata/object.txtar @@ -67,7 +67,7 @@ "additionalProperties": false } --- out.cue -- +-- out/decode/cue -- import "struct" // Main schema diff --git a/encoding/jsonschema/testdata/openapi.txtar b/encoding/jsonschema/testdata/openapi.txtar index b241922df10..b1f81475b4a 100644 --- a/encoding/jsonschema/testdata/openapi.txtar +++ b/encoding/jsonschema/testdata/openapi.txtar @@ -1,4 +1,4 @@ -openapi +#openapi -- type.yaml -- components: @@ -18,7 +18,7 @@ components: description: "The number to dial." type: string --- out.cue -- +-- out/decode/cue -- // A User uses something. #User: { id?: int diff --git a/encoding/jsonschema/testdata/ref.txtar b/encoding/jsonschema/testdata/ref.txtar index bd406a95b44..60ba0e4d85c 100644 --- a/encoding/jsonschema/testdata/ref.txtar +++ b/encoding/jsonschema/testdata/ref.txtar @@ -63,7 +63,7 @@ } } --- out.cue -- +-- out/decode/cue -- import ( "acme.com/external.json:external" "acme.com/external-foo.json:schema" diff --git a/encoding/jsonschema/testdata/refroot.txtar b/encoding/jsonschema/testdata/refroot.txtar index 6f33439e5cd..f3ca5934587 100644 --- a/encoding/jsonschema/testdata/refroot.txtar +++ b/encoding/jsonschema/testdata/refroot.txtar @@ -12,7 +12,7 @@ } } --- out.cue -- +-- out/decode/cue -- _schema _schema: { @jsonschema(schema="http://json-schema.org/draft-07/schema#") diff --git a/encoding/jsonschema/testdata/refroot2.txtar b/encoding/jsonschema/testdata/refroot2.txtar index e4a162c0aee..1440aaa8dae 100644 --- a/encoding/jsonschema/testdata/refroot2.txtar +++ b/encoding/jsonschema/testdata/refroot2.txtar @@ -10,7 +10,7 @@ } } --- out.cue -- +-- out/decode/cue -- _schema _schema: { @jsonschema(schema="http://json-schema.org/draft-07/schema#") diff --git a/encoding/jsonschema/testdata/type.txtar b/encoding/jsonschema/testdata/type.txtar index 52d660560c5..2ad36ca6e24 100644 --- a/encoding/jsonschema/testdata/type.txtar +++ b/encoding/jsonschema/testdata/type.txtar @@ -29,7 +29,7 @@ "additionalProperties": false } --- out.cue -- +-- out/decode/cue -- // Main schema intString?: null | bool | int | string | [...] object?: { diff --git a/encoding/jsonschema/testdata/typedis.txtar b/encoding/jsonschema/testdata/typedis.txtar index 3c74a1b6a4d..5095e39442e 100644 --- a/encoding/jsonschema/testdata/typedis.txtar +++ b/encoding/jsonschema/testdata/typedis.txtar @@ -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 diff --git a/encoding/jsonschema/testdata/unsupported.txtar b/encoding/jsonschema/testdata/unsupported.txtar index a6f8f36c771..99ce39db17e 100644 --- a/encoding/jsonschema/testdata/unsupported.txtar +++ b/encoding/jsonschema/testdata/unsupported.txtar @@ -34,7 +34,7 @@ } --- out.cue -- +-- out/decode/cue -- @jsonschema(schema="http://json-schema.org/draft-07/schema") _ diff --git a/encoding/jsonschema/testdata/used.txtar b/encoding/jsonschema/testdata/used.txtar index b22c92fd789..25250346ef5 100644 --- a/encoding/jsonschema/testdata/used.txtar +++ b/encoding/jsonschema/testdata/used.txtar @@ -40,7 +40,7 @@ } } } --- out.cue -- +-- out/decode/cue -- _ #enum: "a" | "b" | "c"