Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ Commands:

Use "pgschema [command] --help" for more information about a command.`,
version.App(), GitCommit, platform(), BuildDate),
Version: version.App(),
PersistentPreRun: func(cmd *cobra.Command, args []string) {
setupLogger()
globallogger.SetGlobal(logger, Debug)
Expand All @@ -45,6 +46,8 @@ Use "pgschema [command] --help" for more information about a command.`,

func init() {
RootCmd.PersistentFlags().BoolVar(&Debug, "debug", false, "Enable debug logging")
// Print a clean, machine-parseable version line, e.g. "1.12.3"
RootCmd.SetVersionTemplate("{{.Version}}\n")
RootCmd.CompletionOptions.DisableDefaultCmd = true
RootCmd.AddCommand(dump.DumpCmd)
RootCmd.AddCommand(plan.PlanCmd)
Expand Down
27 changes: 27 additions & 0 deletions cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"bytes"
"strings"
"testing"

"github.com/pgplex/pgschema/internal/version"
)

func TestRootCommand(t *testing.T) {
Expand Down Expand Up @@ -40,6 +42,31 @@ func TestRootCommandWithoutArgs(t *testing.T) {
}
}

func TestRootCommandVersionFlag(t *testing.T) {
// Reset flags that earlier tests may have set on the shared global RootCmd.
// pflag does not reset flag values between Parse calls, and cobra checks
// the help flag before the version flag.
if err := RootCmd.Flags().Set("help", "false"); err != nil {
t.Fatalf("failed to reset help flag: %v", err)
}
Comment on lines +46 to +51

var buf bytes.Buffer
RootCmd.SetOut(&buf)
RootCmd.SetErr(&buf)
RootCmd.SetArgs([]string{"--version"})

err := RootCmd.Execute()
if err != nil {
t.Errorf("root command with --version failed: %v", err)
}

output := buf.String()
expected := version.App() + "\n"
if output != expected {
t.Errorf("expected version output %q, got %q", expected, output)
}
}

func TestRootCommandHasSubcommands(t *testing.T) {
commands := RootCmd.Commands()

Expand Down