cmd/cue/cmd: add tests for export command
Change-Id: If02359b85b1264e0e2aac077b9db4f7129d5d3e8
Reviewed-on: https://cue-review.googlesource.com/c/1527
Reviewed-by: Marcel van Lohuizen <mpvl@google.com>
diff --git a/cmd/cue/cmd/export.go b/cmd/cue/cmd/export.go
index d4f8fa0..7557eae 100644
--- a/cmd/cue/cmd/export.go
+++ b/cmd/cue/cmd/export.go
@@ -16,6 +16,7 @@
import (
"encoding/json"
+ "fmt"
"github.com/spf13/cobra"
)
@@ -72,14 +73,19 @@
defined by the files in the current directory.
`,
- Run: func(cmd *cobra.Command, args []string) {
+ RunE: func(cmd *cobra.Command, args []string) error {
instances := buildFromArgs(cmd, args)
- e := json.NewEncoder(cmd.OutOrStdout())
+ w := cmd.OutOrStdout()
+ e := json.NewEncoder(w)
e.SetIndent("", " ")
e.SetEscapeHTML(*escape)
root := instances[0].Value()
- must(e.Encode(root))
+ err := e.Encode(root)
+ if err != nil {
+ fmt.Fprintln(w, err)
+ }
+ return nil
},
}
diff --git a/cmd/cue/cmd/export_test.go b/cmd/cue/cmd/export_test.go
new file mode 100644
index 0000000..4431cb1
--- /dev/null
+++ b/cmd/cue/cmd/export_test.go
@@ -0,0 +1,21 @@
+// Copyright 2019 CUE Authors
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package cmd
+
+import "testing"
+
+func TestExport(t *testing.T) {
+ runCommand(t, exportCmd.RunE, "export")
+}
diff --git a/cmd/cue/cmd/testdata/export/export.cue b/cmd/cue/cmd/testdata/export/export.cue
new file mode 100644
index 0000000..7155601
--- /dev/null
+++ b/cmd/cue/cmd/testdata/export/export.cue
@@ -0,0 +1,11 @@
+package export
+
+def: *1 | int
+
+a: string
+a: "foo"
+
+s t u: "bar"
+s t v: {a: 1} | *{b: 2}
+
+l: [1, 2, 3]
diff --git a/cmd/cue/cmd/testdata/export/export.out b/cmd/cue/cmd/testdata/export/export.out
new file mode 100644
index 0000000..5911b5c
--- /dev/null
+++ b/cmd/cue/cmd/testdata/export/export.out
@@ -0,0 +1,17 @@
+{
+ "def": 1,
+ "a": "foo",
+ "s": {
+ "t": {
+ "u": "bar",
+ "v": {
+ "b": 2
+ }
+ }
+ },
+ "l": [
+ 1,
+ 2,
+ 3
+ ]
+}