diff --git a/cmd/cue/cmd/script_test.go b/cmd/cue/cmd/script_test.go index 45b99f7bd81..48c87699a31 100644 --- a/cmd/cue/cmd/script_test.go +++ b/cmd/cue/cmd/script_test.go @@ -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" @@ -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, @@ -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 }, })) diff --git a/cmd/cue/cmd/testdata/script/sortfields.txtar b/cmd/cue/cmd/testdata/script/sortfields.txtar index f586ffe4f89..e29ed2fd124 100644 --- a/cmd/cue/cmd/testdata/script/sortfields.txtar +++ b/cmd/cue/cmd/testdata/script/sortfields.txtar @@ -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 @@ -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: