@@ -20,36 +20,50 @@ package main
20
20
import (
21
21
"flag"
22
22
"fmt"
23
+ "io"
23
24
"log"
25
+ "os"
24
26
"path"
25
27
"sort"
26
28
27
29
"cuelang.org/go/cue/token"
28
30
"cuelang.org/go/encoding/jsonschema/internal/externaltest"
29
31
)
30
32
31
- var list = flag .String ("list" , "" , "list all failed tests for a given evaluator version" )
33
+ var (
34
+ list = flag .String ("list" , "" , "list all failed tests for a given evaluator version" )
35
+ out = flag .String ("o" , "" , "write output to a file" )
36
+ )
32
37
33
38
const testDir = "testdata/external"
34
39
35
40
func main () {
41
+ flag .Parse ()
36
42
tests , err := externaltest .ReadTestDir (testDir )
37
43
if err != nil {
38
44
log .Fatal (err )
39
45
}
40
- flag .Parse ()
46
+ outw := os .Stdout
47
+ if * out != "" {
48
+ var err error
49
+ outw , err = os .Create (* out )
50
+ if err != nil {
51
+ log .Fatal (err )
52
+ }
53
+ fmt .Fprintf (outw , "# Generated by teststats. DO NOT EDIT\n " )
54
+ }
41
55
if * list != "" {
42
- listFailures (* list , tests )
56
+ listFailures (outw , * list , tests )
43
57
} else {
44
- fmt .Printf ( "v2:\n " )
45
- showStats ("v2" , tests )
46
- fmt .Println ( )
47
- fmt .Printf ( "v3:\n " )
48
- showStats ("v3" , tests )
58
+ fmt .Fprintf ( outw , "v2:\n " )
59
+ showStats (outw , "v2" , tests )
60
+ fmt .Fprintf ( outw , " \n " )
61
+ fmt .Fprintf ( outw , "v3:\n " )
62
+ showStats (outw , "v3" , tests )
49
63
}
50
64
}
51
65
52
- func showStats (version string , tests map [string ][]* externaltest.Schema ) {
66
+ func showStats (outw io. Writer , version string , tests map [string ][]* externaltest.Schema ) {
53
67
schemaOK := 0
54
68
schemaTot := 0
55
69
testOK := 0
@@ -76,17 +90,17 @@ func showStats(version string, tests map[string][]*externaltest.Schema) {
76
90
}
77
91
}
78
92
}
79
- fmt .Printf ( "\t schema extract (pass / total): %d / %d = %.1f%%\n " , schemaOK , schemaTot , percent (schemaOK , schemaTot ))
80
- fmt .Printf ( "\t tests (pass / total): %d / %d = %.1f%%\n " , testOK , testTot , percent (testOK , testTot ))
81
- fmt .Printf ( "\t tests on extracted schemas (pass / total): %d / %d = %.1f%%\n " , schemaOKTestOK , schemaOKTestTot , percent (schemaOKTestOK , schemaOKTestTot ))
93
+ fmt .Fprintf ( outw , "\t schema extract (pass / total): %d / %d = %.1f%%\n " , schemaOK , schemaTot , percent (schemaOK , schemaTot ))
94
+ fmt .Fprintf ( outw , "\t tests (pass / total): %d / %d = %.1f%%\n " , testOK , testTot , percent (testOK , testTot ))
95
+ fmt .Fprintf ( outw , "\t tests on extracted schemas (pass / total): %d / %d = %.1f%%\n " , schemaOKTestOK , schemaOKTestTot , percent (schemaOKTestOK , schemaOKTestTot ))
82
96
}
83
97
84
- func listFailures (version string , tests map [string ][]* externaltest.Schema ) {
98
+ func listFailures (outw io. Writer , version string , tests map [string ][]* externaltest.Schema ) {
85
99
for _ , filename := range sortedKeys (tests ) {
86
100
schemas := tests [filename ]
87
101
for _ , schema := range schemas {
88
102
if schema .Skip [version ] != "" {
89
- fmt .Printf ( "%s: schema fail (%s)\n " , testdataPos (schema ), schema .Description )
103
+ fmt .Fprintf ( outw , "%s: schema fail (%s)\n " , testdataPos (schema ), schema .Description )
90
104
continue
91
105
}
92
106
for _ , test := range schema .Tests {
@@ -95,7 +109,7 @@ func listFailures(version string, tests map[string][]*externaltest.Schema) {
95
109
if ! test .Valid {
96
110
reason = "unexpected success"
97
111
}
98
- fmt .Printf ( "%s: %s (%s; %s)\n " , testdataPos (test ), reason , schema .Description , test .Description )
112
+ fmt .Fprintf ( outw , "%s: %s (%s; %s)\n " , testdataPos (test ), reason , schema .Description , test .Description )
99
113
}
100
114
}
101
115
}
0 commit comments