this repo has no description
1// Copyright 2024 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//go:build ignore
16
17// This command prints a summary of which external tests are passing and failing.
18package main
19
20import (
21 "flag"
22 "fmt"
23 "io"
24 "log"
25 "maps"
26 "os"
27 "path"
28 "slices"
29 "strings"
30
31 "cuelang.org/go/cue/token"
32 "cuelang.org/go/encoding/jsonschema/internal/externaltest"
33)
34
35const testDir = "testdata/external"
36
37func main() {
38 flag.Usage = func() {
39 fmt.Fprintf(os.Stderr, "usage: teststats version\n")
40 fmt.Fprintf(os.Stderr, `
41List all failed tests for the given evaluator version (e.g. v3).
42If multiple versions are given, it prints tests that fail for all
43those versions. If a version starts with a ! character,
44it excludes tests that do not fail for that version.
45`)
46 os.Exit(2)
47 }
48 flag.Parse()
49 tests, err := externaltest.ReadTestDir(testDir)
50 if err != nil {
51 log.Fatal(err)
52 }
53 if flag.NArg() < 1 {
54 flag.Usage()
55 }
56 listFailures(os.Stdout, flag.Args(), tests)
57}
58
59func listFailures(outw io.Writer, versions []string, tests map[string][]*externaltest.Schema) {
60 for _, filename := range slices.Sorted(maps.Keys(tests)) {
61 schemas := tests[filename]
62 for _, schema := range schemas {
63 if match(schema.Skip, versions) {
64 fmt.Fprintf(outw, "%s: schema fail (%s)\n", testdataPos(schema), schema.Description)
65 continue
66 }
67 for _, test := range schema.Tests {
68 if !match(test.Skip, versions) {
69 continue
70 }
71 reason := "fail"
72 if !test.Valid {
73 reason = "unexpected success"
74 }
75 fmt.Fprintf(outw, "%s: %s (%s; %s)\n", testdataPos(test), reason, schema.Description, test.Description)
76 }
77 }
78 }
79}
80
81func match(skip map[string]string, versions []string) bool {
82 for _, v := range versions {
83 v, wantSuccess := strings.CutPrefix(v, "!")
84 _, failed := skip[v]
85 if failed != !wantSuccess {
86 return false
87 }
88 }
89 return true
90}
91
92type positioner interface {
93 Pos() token.Pos
94}
95
96func testdataPos(p positioner) token.Position {
97 pp := p.Pos().Position()
98 pp.Filename = path.Join(testDir, pp.Filename)
99 return pp
100}