Skip to content

Commit

Permalink
cmd/cue: add a testscript for CUE_DEBUG=sortfields working with the G…
Browse files Browse the repository at this point in the history
…o 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í <mvdan@mvdan.cc>
Change-Id: I0895966789bec41db859e635ecacb7d08d0d8318
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1205201
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>
Reviewed-by: Matthew Sackman <matthew@cue.works>
  • Loading branch information
mvdan committed Dec 4, 2024
1 parent 5ac1b8a commit 4258e9a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 15 deletions.
32 changes: 28 additions & 4 deletions cmd/cue/cmd/script_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ import (
"golang.org/x/oauth2"
"golang.org/x/tools/txtar"

"cuelang.org/go/cue"
"cuelang.org/go/cue/cuecontext"
"cuelang.org/go/cue/errors"
"cuelang.org/go/cue/parser"
"cuelang.org/go/internal/cuetest"
Expand Down Expand Up @@ -388,6 +390,12 @@ func TestX(t *testing.T) {
}

func TestMain(m *testing.M) {
check := func(err error) {
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
os.Exit(testscript.RunMain(m, map[string]func() int{
"cue": Main,
// Until https://github.com/rogpeppe/go-internal/issues/93 is fixed,
Expand All @@ -411,10 +419,26 @@ func TestMain(m *testing.M) {
return 0
},
"testcmd": func() int {
if err := testCmd(); err != nil {
fmt.Fprintln(os.Stderr, err)
return 1
}
err := testCmd()
check(err)
return 0
},
// Like `cue export`, but as a standalone Go program which doesn't
// go through cmd/cue's setup of cuecontext and the evaluator.
// Useful to check what the export behavior is for Go API users,
// for example in relation to env vars like CUE_EXPERIMENT or CUE_DEBUG.
// Only works with cue stdin and json stdout for simplicity.
"cuectx_export": func() int {
input, err := io.ReadAll(os.Stdin)
check(err)
ctx := cuecontext.New()
v := ctx.CompileBytes(input)
err = v.Validate(cue.Concrete(true))
check(err)
enc := json.NewEncoder(os.Stdout)
enc.SetIndent("", " ")
err = enc.Encode(v)
check(err)
return 0
},
}))
Expand Down
18 changes: 7 additions & 11 deletions cmd/cue/cmd/testdata/script/sortfields.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,13 @@ cmp stdout eval.stdout
# Just to double check, ensure it also works for evalv3.
env CUE_EXPERIMENT=evalv3
exec cue export input.cue
cmp stdout export-evalv3.stdout
cmp stdout export.stdout

# Also ensure that it's wired up for the Go API.
# TODO: it is not, currently.
stdin input.cue
exec cuectx_export
cmp stdout export-unsorted.stdout

-- input.cue --
c: true
Expand Down Expand Up @@ -53,16 +59,6 @@ b: y: true
},
"c": true
}
-- export-evalv3.stdout --
{
"a": true,
"b": {
"x": true,
"y": true,
"z": true
},
"c": true
}
-- export-yaml.stdout --
a: true
b:
Expand Down

0 comments on commit 4258e9a

Please sign in to comment.