From 2fcd86d1d74d66873c00dba2bed8b00775d6fa47 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Mon, 9 Mar 2026 00:45:34 +0000 Subject: [PATCH] test(cmd): share command UI test contexts --- internal/cmd/admin_test.go | 16 ++--------- internal/cmd/auth_alias_test.go | 10 +------ internal/cmd/auth_more_test.go | 38 +++++---------------------- internal/cmd/auth_tokens_more_test.go | 14 ++-------- internal/cmd/cmd_testutil_test.go | 30 +++++++++++++++++++++ 5 files changed, 41 insertions(+), 67 deletions(-) create mode 100644 internal/cmd/cmd_testutil_test.go diff --git a/internal/cmd/admin_test.go b/internal/cmd/admin_test.go index b5fade8..8aadea2 100644 --- a/internal/cmd/admin_test.go +++ b/internal/cmd/admin_test.go @@ -4,7 +4,6 @@ import ( "context" "encoding/json" "errors" - "io" "net/http" "net/http/httptest" "strings" @@ -12,9 +11,6 @@ import ( admin "google.golang.org/api/admin/directory/v1" "google.golang.org/api/option" - - "github.com/steipete/gogcli/internal/outfmt" - "github.com/steipete/gogcli/internal/ui" ) func TestRequireAdminAccount_ConsumerBlocked(t *testing.T) { @@ -94,11 +90,7 @@ func TestAdminUsersList_JSON_AllowsNilName(t *testing.T) { } newAdminDirectoryService = func(context.Context, string) (*admin.Service, error) { return svc, nil } - u, uiErr := ui.New(ui.Options{Stdout: io.Discard, Stderr: io.Discard, Color: "never"}) - if uiErr != nil { - t.Fatalf("ui.New: %v", uiErr) - } - ctx := outfmt.WithMode(ui.WithUI(context.Background(), u), outfmt.Mode{JSON: true}) + ctx := newCmdJSONContext(t) out := captureStdout(t, func() { if err := (&AdminUsersListCmd{Domain: "example.com"}).Run(ctx, &RootFlags{Account: "svc@example.com"}); err != nil { @@ -152,11 +144,7 @@ func TestAdminGroupsMembersAdd_JSON(t *testing.T) { } newAdminDirectoryService = func(context.Context, string) (*admin.Service, error) { return svc, nil } - u, uiErr := ui.New(ui.Options{Stdout: io.Discard, Stderr: io.Discard, Color: "never"}) - if uiErr != nil { - t.Fatalf("ui.New: %v", uiErr) - } - ctx := outfmt.WithMode(ui.WithUI(context.Background(), u), outfmt.Mode{JSON: true}) + ctx := newCmdJSONContext(t) out := captureStdout(t, func() { if err := (&AdminGroupsMembersAddCmd{ diff --git a/internal/cmd/auth_alias_test.go b/internal/cmd/auth_alias_test.go index bba6769..84c5c2e 100644 --- a/internal/cmd/auth_alias_test.go +++ b/internal/cmd/auth_alias_test.go @@ -1,14 +1,10 @@ package cmd import ( - "context" "encoding/json" "os" "path/filepath" "testing" - - "github.com/steipete/gogcli/internal/outfmt" - "github.com/steipete/gogcli/internal/ui" ) func TestAuthAliasSetListUnset_JSON(t *testing.T) { @@ -16,11 +12,7 @@ func TestAuthAliasSetListUnset_JSON(t *testing.T) { t.Setenv("HOME", home) t.Setenv("XDG_CONFIG_HOME", filepath.Join(home, "xdg-config")) - u, err := ui.New(ui.Options{Stdout: os.Stdout, Stderr: os.Stderr, Color: "never"}) - if err != nil { - t.Fatalf("ui.New: %v", err) - } - ctx := outfmt.WithMode(ui.WithUI(context.Background(), u), outfmt.Mode{JSON: true}) + ctx := newCmdJSONOutputContext(t, os.Stdout, os.Stderr) // set _ = captureStdout(t, func() { diff --git a/internal/cmd/auth_more_test.go b/internal/cmd/auth_more_test.go index 0a6b0c9..11288e2 100644 --- a/internal/cmd/auth_more_test.go +++ b/internal/cmd/auth_more_test.go @@ -10,8 +10,6 @@ import ( "github.com/steipete/gogcli/internal/config" "github.com/steipete/gogcli/internal/googleauth" - "github.com/steipete/gogcli/internal/outfmt" - "github.com/steipete/gogcli/internal/ui" ) func TestAuthKeepCmd_JSON(t *testing.T) { @@ -24,11 +22,7 @@ func TestAuthKeepCmd_JSON(t *testing.T) { t.Fatalf("write key: %v", err) } - u, err := ui.New(ui.Options{Stdout: os.Stdout, Stderr: os.Stderr, Color: "never"}) - if err != nil { - t.Fatalf("ui.New: %v", err) - } - ctx := outfmt.WithMode(ui.WithUI(context.Background(), u), outfmt.Mode{JSON: true}) + ctx := newCmdJSONOutputContext(t, os.Stdout, os.Stderr) cmd := AuthKeepCmd{Email: "a@b.com", Key: keyPath} out := captureStdout(t, func() { @@ -72,11 +66,7 @@ func TestAuthManageCmd(t *testing.T) { } func TestAuthServicesCmd_Markdown(t *testing.T) { - u, err := ui.New(ui.Options{Stdout: os.Stdout, Stderr: os.Stderr, Color: "never"}) - if err != nil { - t.Fatalf("ui.New: %v", err) - } - ctx := ui.WithUI(context.Background(), u) + ctx := newCmdOutputContext(t, os.Stdout, os.Stderr) cmd := AuthServicesCmd{Markdown: true} out := captureStdout(t, func() { @@ -90,11 +80,7 @@ func TestAuthServicesCmd_Markdown(t *testing.T) { } func TestAuthServicesCmd_JSON(t *testing.T) { - u, err := ui.New(ui.Options{Stdout: os.Stdout, Stderr: os.Stderr, Color: "never"}) - if err != nil { - t.Fatalf("ui.New: %v", err) - } - ctx := outfmt.WithMode(ui.WithUI(context.Background(), u), outfmt.Mode{JSON: true}) + ctx := newCmdJSONOutputContext(t, os.Stdout, os.Stderr) cmd := AuthServicesCmd{} out := captureStdout(t, func() { @@ -108,11 +94,7 @@ func TestAuthServicesCmd_JSON(t *testing.T) { } func TestAuthServicesCmd_Table(t *testing.T) { - u, err := ui.New(ui.Options{Stdout: os.Stdout, Stderr: os.Stderr, Color: "never"}) - if err != nil { - t.Fatalf("ui.New: %v", err) - } - ctx := ui.WithUI(context.Background(), u) + ctx := newCmdOutputContext(t, os.Stdout, os.Stderr) cmd := AuthServicesCmd{} out := captureStdout(t, func() { @@ -136,11 +118,7 @@ func TestAuthKeepCmd_Text(t *testing.T) { } out := captureStdout(t, func() { - u, err := ui.New(ui.Options{Stdout: os.Stdout, Stderr: os.Stderr, Color: "never"}) - if err != nil { - t.Fatalf("ui.New: %v", err) - } - ctx := ui.WithUI(context.Background(), u) + ctx := newCmdOutputContext(t, os.Stdout, os.Stderr) cmd := AuthKeepCmd{Email: "a@b.com", Key: keyPath} if err := cmd.Run(ctx, &RootFlags{}); err != nil { @@ -157,11 +135,7 @@ func TestAuthStatusCmd_JSON(t *testing.T) { t.Setenv("HOME", home) t.Setenv("XDG_CONFIG_HOME", filepath.Join(home, "xdg")) - u, err := ui.New(ui.Options{Stdout: os.Stdout, Stderr: os.Stderr, Color: "never"}) - if err != nil { - t.Fatalf("ui.New: %v", err) - } - ctx := outfmt.WithMode(ui.WithUI(context.Background(), u), outfmt.Mode{JSON: true}) + ctx := newCmdJSONOutputContext(t, os.Stdout, os.Stderr) if _, err := config.ConfigPath(); err != nil { t.Fatalf("ConfigPath: %v", err) diff --git a/internal/cmd/auth_tokens_more_test.go b/internal/cmd/auth_tokens_more_test.go index 6784bf7..4845f15 100644 --- a/internal/cmd/auth_tokens_more_test.go +++ b/internal/cmd/auth_tokens_more_test.go @@ -11,9 +11,7 @@ import ( "time" "github.com/steipete/gogcli/internal/config" - "github.com/steipete/gogcli/internal/outfmt" "github.com/steipete/gogcli/internal/secrets" - "github.com/steipete/gogcli/internal/ui" ) func TestAuthTokensExportImport_JSON(t *testing.T) { @@ -40,11 +38,7 @@ func TestAuthTokensExportImport_JSON(t *testing.T) { } outPath := filepath.Join(t.TempDir(), "token.json") - u, uiErr := ui.New(ui.Options{Stdout: os.Stdout, Stderr: os.Stderr, Color: "never"}) - if uiErr != nil { - t.Fatalf("ui.New: %v", uiErr) - } - ctx := outfmt.WithMode(ui.WithUI(context.Background(), u), outfmt.Mode{JSON: true}) + ctx := newCmdJSONOutputContext(t, os.Stdout, os.Stderr) var err error exportCmd := AuthTokensExportCmd{ @@ -106,11 +100,7 @@ func TestAuthList_CheckJSON(t *testing.T) { t.Fatalf("SetToken: %v", err) } - u, uiErr := ui.New(ui.Options{Stdout: os.Stdout, Stderr: os.Stderr, Color: "never"}) - if uiErr != nil { - t.Fatalf("ui.New: %v", uiErr) - } - ctx := outfmt.WithMode(ui.WithUI(context.Background(), u), outfmt.Mode{JSON: true}) + ctx := newCmdJSONOutputContext(t, os.Stdout, os.Stderr) var err error listCmd := AuthListCmd{Check: true} diff --git a/internal/cmd/cmd_testutil_test.go b/internal/cmd/cmd_testutil_test.go new file mode 100644 index 0000000..82ff5a6 --- /dev/null +++ b/internal/cmd/cmd_testutil_test.go @@ -0,0 +1,30 @@ +package cmd + +import ( + "context" + "io" + "testing" + + "github.com/steipete/gogcli/internal/outfmt" + "github.com/steipete/gogcli/internal/ui" +) + +func newCmdOutputContext(t *testing.T, stdout, stderr io.Writer) context.Context { + t.Helper() + + u, err := ui.New(ui.Options{Stdout: stdout, Stderr: stderr, Color: "never"}) + if err != nil { + t.Fatalf("ui.New: %v", err) + } + return ui.WithUI(context.Background(), u) +} + +func newCmdJSONContext(t *testing.T) context.Context { + t.Helper() + return newCmdJSONOutputContext(t, io.Discard, io.Discard) +} + +func newCmdJSONOutputContext(t *testing.T, stdout, stderr io.Writer) context.Context { + t.Helper() + return outfmt.WithMode(newCmdOutputContext(t, stdout, stderr), outfmt.Mode{JSON: true}) +}