From a80ddcc0544cd7a16f7d42fa32b2a097d873f5cb Mon Sep 17 00:00:00 2001 From: Patrice Ferlet Date: Tue, 26 Nov 2024 17:45:57 +0100 Subject: [PATCH] test(main): More tests in main command package --- cmd/katenary/main_test.go | 55 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 54 insertions(+), 1 deletion(-) diff --git a/cmd/katenary/main_test.go b/cmd/katenary/main_test.go index eb530ac..679fee7 100644 --- a/cmd/katenary/main_test.go +++ b/cmd/katenary/main_test.go @@ -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) + } +}