test(cmd): share command UI test contexts
This commit is contained in:
parent
aaafcca8e2
commit
2fcd86d1d7
@ -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{
|
||||
|
||||
@ -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() {
|
||||
|
||||
@ -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)
|
||||
|
||||
@ -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}
|
||||
|
||||
30
internal/cmd/cmd_testutil_test.go
Normal file
30
internal/cmd/cmd_testutil_test.go
Normal file
@ -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})
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user