test(main): More tests in main command package

This commit is contained in:
2024-11-26 17:45:57 +01:00
parent 921eaff367
commit a80ddcc054

View File

@@ -1,6 +1,13 @@
package main
import "testing"
import (
"bytes"
"encoding/json"
"io"
"os"
"strings"
"testing"
)
func TestBuildCommand(t *testing.T) {
rootCmd := buildRootCmd()
@@ -15,3 +22,49 @@ func TestBuildCommand(t *testing.T) {
t.Errorf("Expected %d command, got %d", numCommands, len(rootCmd.Commands()))
}
}
func TestGetVersion(t *testing.T) {
cmd := buildRootCmd()
if cmd == nil {
t.Errorf("Expected cmd to be defined")
}
version := generateVersionCommand()
old := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w
version.Run(cmd, nil)
w.Close()
os.Stdout = old
var buf bytes.Buffer
io.Copy(&buf, r)
output := buf.String()
if !strings.Contains(output, "(devel)") {
t.Errorf("Expected output to contain '(devel)', got %s", output)
}
}
func TestSchemaCommand(t *testing.T) {
cmd := buildRootCmd()
if cmd == nil {
t.Errorf("Expected cmd to be defined")
}
schema := generateSchemaCommand()
old := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w
schema.Run(cmd, nil)
w.Close()
os.Stdout = old
var buf bytes.Buffer
io.Copy(&buf, r)
output := buf.String()
// try to parse json
schemaContent := make(map[string]interface{})
if err := json.Unmarshal([]byte(output), &schemaContent); err != nil {
t.Errorf("Expected valid json, got %s", output)
}
}