Skip to content

Commit 4258e9a

Browse files
committed
cmd/cue: add a testscript for CUE_DEBUG=sortfields working with the Go API
It currently does not; we will fix this in the following commit. We write this test as part of the cmd/cue testscript suite given that we already have multiple scripts which smoke test whether CUE_EXPERIMENT and CUE_DEBUG are wired through properly, such as sortfields.txtar and also dev.txtar for CUE_EXPERIMENT=evalv3. While here, since the export output with sortfields is the same on evalv2 and evalv3, we can deduplicate one of the output files in sortfields.txtar. Signed-off-by: Daniel Martí <[email protected]> Change-Id: I0895966789bec41db859e635ecacb7d08d0d8318 Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1205201 TryBot-Result: CUEcueckoo <[email protected]> Unity-Result: CUE porcuepine <[email protected]> Reviewed-by: Matthew Sackman <[email protected]>
1 parent 5ac1b8a commit 4258e9a

File tree

2 files changed

+35
-15
lines changed

2 files changed

+35
-15
lines changed

cmd/cue/cmd/script_test.go

+28-4
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ import (
4545
"golang.org/x/oauth2"
4646
"golang.org/x/tools/txtar"
4747

48+
"cuelang.org/go/cue"
49+
"cuelang.org/go/cue/cuecontext"
4850
"cuelang.org/go/cue/errors"
4951
"cuelang.org/go/cue/parser"
5052
"cuelang.org/go/internal/cuetest"
@@ -388,6 +390,12 @@ func TestX(t *testing.T) {
388390
}
389391

390392
func TestMain(m *testing.M) {
393+
check := func(err error) {
394+
if err != nil {
395+
fmt.Fprintln(os.Stderr, err)
396+
os.Exit(1)
397+
}
398+
}
391399
os.Exit(testscript.RunMain(m, map[string]func() int{
392400
"cue": Main,
393401
// Until https://github.com/rogpeppe/go-internal/issues/93 is fixed,
@@ -411,10 +419,26 @@ func TestMain(m *testing.M) {
411419
return 0
412420
},
413421
"testcmd": func() int {
414-
if err := testCmd(); err != nil {
415-
fmt.Fprintln(os.Stderr, err)
416-
return 1
417-
}
422+
err := testCmd()
423+
check(err)
424+
return 0
425+
},
426+
// Like `cue export`, but as a standalone Go program which doesn't
427+
// go through cmd/cue's setup of cuecontext and the evaluator.
428+
// Useful to check what the export behavior is for Go API users,
429+
// for example in relation to env vars like CUE_EXPERIMENT or CUE_DEBUG.
430+
// Only works with cue stdin and json stdout for simplicity.
431+
"cuectx_export": func() int {
432+
input, err := io.ReadAll(os.Stdin)
433+
check(err)
434+
ctx := cuecontext.New()
435+
v := ctx.CompileBytes(input)
436+
err = v.Validate(cue.Concrete(true))
437+
check(err)
438+
enc := json.NewEncoder(os.Stdout)
439+
enc.SetIndent("", " ")
440+
err = enc.Encode(v)
441+
check(err)
418442
return 0
419443
},
420444
}))

cmd/cue/cmd/testdata/script/sortfields.txtar

+7-11
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,13 @@ cmp stdout eval.stdout
2323
# Just to double check, ensure it also works for evalv3.
2424
env CUE_EXPERIMENT=evalv3
2525
exec cue export input.cue
26-
cmp stdout export-evalv3.stdout
26+
cmp stdout export.stdout
27+
28+
# Also ensure that it's wired up for the Go API.
29+
# TODO: it is not, currently.
30+
stdin input.cue
31+
exec cuectx_export
32+
cmp stdout export-unsorted.stdout
2733

2834
-- input.cue --
2935
c: true
@@ -53,16 +59,6 @@ b: y: true
5359
},
5460
"c": true
5561
}
56-
-- export-evalv3.stdout --
57-
{
58-
"a": true,
59-
"b": {
60-
"x": true,
61-
"y": true,
62-
"z": true
63-
},
64-
"c": true
65-
}
6662
-- export-yaml.stdout --
6763
a: true
6864
b:

0 commit comments

Comments
 (0)