driftctl/pkg/cmd/version_test.go

45 lines
953 B
Go
Raw Normal View History

package cmd
import (
2021-11-25 14:08:33 +00:00
"fmt"
"testing"
2021-12-06 13:29:39 +00:00
"github.com/snyk/driftctl/pkg/version"
"github.com/snyk/driftctl/test"
"github.com/spf13/cobra"
)
func TestVersionCmd(t *testing.T) {
rootCmd := &cobra.Command{Use: "root"}
rootCmd.AddCommand(NewVersionCmd())
output, err := test.Execute(rootCmd, "version")
if output == "" {
t.Errorf("Unexpected output: %v", output)
}
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
2021-11-25 14:08:33 +00:00
expected := fmt.Sprintf("%s\n", version.Current())
if output != expected {
t.Errorf("Expected %v, got %v", expected, output)
}
}
func TestVersionCmd_Invalid(t *testing.T) {
rootCmd := &cobra.Command{Use: "root"}
rootCmd.AddCommand(NewVersionCmd())
_, err := test.Execute(rootCmd, "version", "test")
if err == nil {
t.Errorf("Invalid arg should generate error")
}
expected := `unknown command "test" for "root version"`
if err.Error() != expected {
t.Errorf("Expected %v, got %v", expected, err)
}
}