FRE-4694: Add CLI command end-to-end tests
Add comprehensive e2e tests for all CLI commands with mocked API responses. Fix test infrastructure to handle global state (os.Stdout capture, HOME env var) and broken test parallelism in stdout-capturing tests. - Add testutil_test.go with runFreshCommand, setupE2E, mockAPIServer - Add e2e_full_test.go with ~40 tests covering auth, mail, contacts, attachments, folders, labels, drafts, help output - Add newRootCmdBase() for fresh command trees per test - Remove t.Parallel() from stdout-capturing and HOME-dependent tests - Fix SessionWithMockSession to use runFreshCommand (stdout capture) Co-Authored-By: Paperclip <noreply@paperclip.ing>
This commit is contained in:
190
cmd/e2e_test.go
Normal file
190
cmd/e2e_test.go
Normal file
@@ -0,0 +1,190 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// TestMailCommand tests the mail CLI command structure
|
||||
func TestMailCommand(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
rootCmd := newRootCmdBase()
|
||||
rootCmd.SetArgs([]string{"mail", "--help"})
|
||||
|
||||
var buf bytes.Buffer
|
||||
rootCmd.SetOut(&buf)
|
||||
|
||||
err := rootCmd.Execute()
|
||||
if err != nil {
|
||||
t.Fatalf("Mail help command failed: %v", err)
|
||||
}
|
||||
|
||||
output, _ := io.ReadAll(&buf)
|
||||
helpText := string(output)
|
||||
|
||||
// Verify mail subcommands are present
|
||||
expectedSubcommands := []string{"list", "read", "send", "delete", "trash", "draft", "search"}
|
||||
for _, subcmd := range expectedSubcommands {
|
||||
if !contains(helpText, subcmd) {
|
||||
t.Errorf("Mail help missing subcommand: %s", subcmd)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestMailListCommand tests the mail list subcommand
|
||||
func TestMailListCommand(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
rootCmd := newRootCmdBase()
|
||||
rootCmd.SetArgs([]string{"mail", "list", "--help"})
|
||||
|
||||
var buf bytes.Buffer
|
||||
rootCmd.SetOut(&buf)
|
||||
|
||||
err := rootCmd.Execute()
|
||||
if err != nil {
|
||||
t.Fatalf("Mail list help command failed: %v", err)
|
||||
}
|
||||
|
||||
output, _ := io.ReadAll(&buf)
|
||||
if len(output) == 0 {
|
||||
t.Error("Mail list help output is empty")
|
||||
}
|
||||
}
|
||||
|
||||
// TestContactCommand tests the contact CLI command structure
|
||||
func TestContactCommand(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
rootCmd := newRootCmdBase()
|
||||
rootCmd.SetArgs([]string{"contact", "--help"})
|
||||
|
||||
var buf bytes.Buffer
|
||||
rootCmd.SetOut(&buf)
|
||||
|
||||
err := rootCmd.Execute()
|
||||
if err != nil {
|
||||
t.Fatalf("Contact help command failed: %v", err)
|
||||
}
|
||||
|
||||
output, _ := io.ReadAll(&buf)
|
||||
helpText := string(output)
|
||||
|
||||
// Verify contact subcommands are present
|
||||
expectedSubcommands := []string{"list", "add", "edit", "delete"}
|
||||
for _, subcmd := range expectedSubcommands {
|
||||
if !contains(helpText, subcmd) {
|
||||
t.Errorf("Contact help missing subcommand: %s", subcmd)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestAttachmentCommand tests the attachment CLI command structure
|
||||
func TestAttachmentCommand(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
rootCmd := newRootCmdBase()
|
||||
rootCmd.SetArgs([]string{"attachment", "--help"})
|
||||
|
||||
var buf bytes.Buffer
|
||||
rootCmd.SetOut(&buf)
|
||||
|
||||
err := rootCmd.Execute()
|
||||
if err != nil {
|
||||
t.Fatalf("Attachment help command failed: %v", err)
|
||||
}
|
||||
|
||||
output, _ := io.ReadAll(&buf)
|
||||
helpText := string(output)
|
||||
|
||||
// Verify attachment subcommands are present
|
||||
expectedSubcommands := []string{"upload", "download", "list"}
|
||||
for _, subcmd := range expectedSubcommands {
|
||||
if !contains(helpText, subcmd) {
|
||||
t.Errorf("Attachment help missing subcommand: %s", subcmd)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestFolderCommand tests the folder CLI command structure
|
||||
func TestFolderCommand(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
rootCmd := newRootCmdBase()
|
||||
rootCmd.SetArgs([]string{"folder", "--help"})
|
||||
|
||||
var buf bytes.Buffer
|
||||
rootCmd.SetOut(&buf)
|
||||
|
||||
err := rootCmd.Execute()
|
||||
if err != nil {
|
||||
t.Fatalf("Folder help command failed: %v", err)
|
||||
}
|
||||
|
||||
output, _ := io.ReadAll(&buf)
|
||||
helpText := string(output)
|
||||
|
||||
// Verify folder subcommands are present
|
||||
expectedSubcommands := []string{"list", "create", "update", "delete"}
|
||||
for _, subcmd := range expectedSubcommands {
|
||||
if !contains(helpText, subcmd) {
|
||||
t.Errorf("Folder help missing subcommand: %s", subcmd)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestLabelCommand tests the label CLI command structure
|
||||
func TestLabelCommand(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
rootCmd := newRootCmdBase()
|
||||
rootCmd.SetArgs([]string{"label", "--help"})
|
||||
|
||||
var buf bytes.Buffer
|
||||
rootCmd.SetOut(&buf)
|
||||
|
||||
err := rootCmd.Execute()
|
||||
if err != nil {
|
||||
t.Fatalf("Label help command failed: %v", err)
|
||||
}
|
||||
|
||||
output, _ := io.ReadAll(&buf)
|
||||
helpText := string(output)
|
||||
|
||||
// Verify label subcommands are present
|
||||
expectedSubcommands := []string{"list", "create", "update", "delete", "apply", "remove"}
|
||||
for _, subcmd := range expectedSubcommands {
|
||||
if !contains(helpText, subcmd) {
|
||||
t.Errorf("Label help missing subcommand: %s", subcmd)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestDraftCommand tests the draft CLI command structure
|
||||
func TestDraftCommand(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
rootCmd := newRootCmdBase()
|
||||
rootCmd.SetArgs([]string{"draft", "--help"})
|
||||
|
||||
var buf bytes.Buffer
|
||||
rootCmd.SetOut(&buf)
|
||||
|
||||
err := rootCmd.Execute()
|
||||
if err != nil {
|
||||
t.Fatalf("Draft help command failed: %v", err)
|
||||
}
|
||||
|
||||
output, _ := io.ReadAll(&buf)
|
||||
helpText := string(output)
|
||||
|
||||
// Verify draft subcommands are present
|
||||
expectedSubcommands := []string{"list", "save", "edit", "send"}
|
||||
for _, subcmd := range expectedSubcommands {
|
||||
if !contains(helpText, subcmd) {
|
||||
t.Errorf("Draft help missing subcommand: %s", subcmd)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user