Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
feat(cli-test): add cli test framework (#660)
qsliub1f6cad8190e
diff --git a/bin/bb/cmd/root_test.go b/bin/bb/cmd/root_test.gonew file mode 100644index 0000000000..38d34c0968--- /dev/null+++ b/bin/bb/cmd/root_test.go@@ -0,0 +1,60 @@+package cmd++import (+ "bytes"+ "strings"+ "testing"++ "github.com/spf13/cobra"+)++func execute(t *testing.T, cmd *cobra.Command, args ...string) (string, error) {+ t.Helper()++ var buf bytes.Buffer+ cmd.SetOut(&buf)+ cmd.SetErr(&buf)+ cmd.SetArgs(args)++ err := cmd.Execute()+ return buf.String(), err+}++type testTable struct {+ args []string+ expectedErr error+ expected string+}++func tableTest(t *testing.T, tables []testTable) {+ t.Helper()+ for _, tc := range tables {+ actual, err := execute(t, rootCmd, tc.args...)++ if err != tc.expectedErr {+ t.Errorf(`"> bb %v"+got unexpected error:+>>>>>>>>>>+%v+<<<<<<<<<<+want:+>>>>>>>>>>+%v+<<<<<<<<<<+`, strings.Join(tc.args, " "), err, tc.expectedErr)+ }++ if actual != tc.expected {+ t.Errorf(`"> bb %v"+got unexpected output:+>>>>>>>>>>+%v+<<<<<<<<<<+want:+>>>>>>>>>>+%v+<<<<<<<<<<+`, strings.Join(tc.args, " "), actual, tc.expected)+ }+ }+}diff --git a/bin/bb/cmd/version.go b/bin/bb/cmd/version.goindex 43bcbb44d9..e95349a983 100644--- a/bin/bb/cmd/version.go+++ b/bin/bb/cmd/version.go@@ -21,10 +21,11 @@ var versionCmd = &cobra.Command{Use: "version",Short: "Print the version of bb",Run: func(cmd *cobra.Command, args []string) {- fmt.Printf("bb version: %s\n", version)- fmt.Printf("Golang version: %s\n", goversion)- fmt.Printf("Git commit hash: %s\n", gitcommit)- fmt.Printf("Built on: %s\n", buildtime)- fmt.Printf("Built by: %s\n", builduser)+ out := cmd.OutOrStdout()+ fmt.Fprintf(out, "bb version: %s\n", version)+ fmt.Fprintf(out, "Golang version: %s\n", goversion)+ fmt.Fprintf(out, "Git commit hash: %s\n", gitcommit)+ fmt.Fprintf(out, "Built on: %s\n", buildtime)+ fmt.Fprintf(out, "Built by: %s\n", builduser)},}diff --git a/bin/bb/cmd/version_test.go b/bin/bb/cmd/version_test.gonew file mode 100644index 0000000000..e104425537--- /dev/null+++ b/bin/bb/cmd/version_test.go@@ -0,0 +1,32 @@+package cmd++import (+ "testing"+)++func TestVersion(t *testing.T) {+ tt := []testTable{+ {+ args: []string{"version"},+ expected: `bb version: development+Golang version: unknown+Git commit hash: unknown+Built on: unknown+Built by: unknown+`,+ },+ {+ args: []string{"version", "--help"},+ expected: `Print the version of bb++Usage:+ bb version [flags]++Flags:+ -h, --help help for version+`,+ },+ }++ tableTest(t, tt)+}