Skip to content

Commit a80ddcc

Browse files
committed
test(main): More tests in main command package
1 parent 921eaff commit a80ddcc

File tree

1 file changed

+54
-1
lines changed

1 file changed

+54
-1
lines changed

cmd/katenary/main_test.go

+54-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
package main
22

3-
import "testing"
3+
import (
4+
"bytes"
5+
"encoding/json"
6+
"io"
7+
"os"
8+
"strings"
9+
"testing"
10+
)
411

512
func TestBuildCommand(t *testing.T) {
613
rootCmd := buildRootCmd()
@@ -15,3 +22,49 @@ func TestBuildCommand(t *testing.T) {
1522
t.Errorf("Expected %d command, got %d", numCommands, len(rootCmd.Commands()))
1623
}
1724
}
25+
26+
func TestGetVersion(t *testing.T) {
27+
cmd := buildRootCmd()
28+
if cmd == nil {
29+
t.Errorf("Expected cmd to be defined")
30+
}
31+
version := generateVersionCommand()
32+
old := os.Stdout
33+
r, w, _ := os.Pipe()
34+
os.Stdout = w
35+
36+
version.Run(cmd, nil)
37+
38+
w.Close()
39+
os.Stdout = old
40+
41+
var buf bytes.Buffer
42+
io.Copy(&buf, r)
43+
output := buf.String()
44+
if !strings.Contains(output, "(devel)") {
45+
t.Errorf("Expected output to contain '(devel)', got %s", output)
46+
}
47+
}
48+
49+
func TestSchemaCommand(t *testing.T) {
50+
cmd := buildRootCmd()
51+
if cmd == nil {
52+
t.Errorf("Expected cmd to be defined")
53+
}
54+
schema := generateSchemaCommand()
55+
old := os.Stdout
56+
r, w, _ := os.Pipe()
57+
os.Stdout = w
58+
schema.Run(cmd, nil)
59+
w.Close()
60+
os.Stdout = old
61+
var buf bytes.Buffer
62+
io.Copy(&buf, r)
63+
output := buf.String()
64+
65+
// try to parse json
66+
schemaContent := make(map[string]interface{})
67+
if err := json.Unmarshal([]byte(output), &schemaContent); err != nil {
68+
t.Errorf("Expected valid json, got %s", output)
69+
}
70+
}

0 commit comments

Comments
 (0)