Skip to content

Commit

Permalink
Clean up tests (#82)
Browse files Browse the repository at this point in the history
* Clean up shell tests

* Clean up shell tests

* Cleaning up config handler tests

* Clean up context

* Clena up  shell tests

* Clean up context tests

* Clean up base helper tests

* Clean up awss_helper_test.go

* Reorganize colima helper test

* Add BDD comments throughout colima helper test

* Clean up colima helper

* Clean kube helper tests

* Clean up mock helper test

* Clean up omni tests

* Calling constructor

* Clean up sops helper

* Clean up talos helper test

* Organize terraform helper test

* Use constructors

* Remove setupTestEnv

* remove setupTestEnv

* Add BDD style comments

* Reorg

* Use the correct mock constructors

* Add BDD comments

* Organize root teset

* Clean up config mock interface

* Clena up NewMockContext
  • Loading branch information
rmvangun authored Oct 6, 2024
1 parent 51cc18a commit 613a307
Show file tree
Hide file tree
Showing 23 changed files with 5,867 additions and 5,939 deletions.
522 changes: 235 additions & 287 deletions cmd/context_test.go

Large diffs are not rendered by default.

87 changes: 41 additions & 46 deletions cmd/env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package cmd
import (
"bytes"
"errors"
"fmt"
"strings"
"testing"

Expand All @@ -15,11 +14,7 @@ import (

func TestEnvCmd(t *testing.T) {
originalExitFunc := exitFunc
var exitCode int
exitFunc = func(code int) {
exitCode = code
panic(fmt.Sprintf("exit code: %d", code)) // Simulate exit
}
exitFunc = mockExit
t.Cleanup(func() {
exitFunc = originalExitFunc
})
Expand All @@ -28,9 +23,9 @@ func TestEnvCmd(t *testing.T) {
defer resetRootCmd()
defer recoverPanic(t)

// Given: a valid config handler, shell, and helper
mockCliConfigHandler := config.NewMockConfigHandler(nil, nil, nil, nil, nil, nil)
mockProjectConfigHandler := config.NewMockConfigHandler(nil, nil, nil, nil, nil, nil)
// Given a valid config handler, shell, and helper
mockCliConfigHandler := config.NewMockConfigHandler()
mockProjectConfigHandler := config.NewMockConfigHandler()
mockShell, err := shell.NewMockShell("cmd")
if err != nil {
t.Fatalf("NewMockShell() error = %v", err)
Expand All @@ -48,7 +43,7 @@ func TestEnvCmd(t *testing.T) {
})
setupContainer(mockCliConfigHandler, mockProjectConfigHandler, mockShell, mockHelper, nil, nil, nil)

// When: the env command is executed
// When the env command is executed
output := captureStdout(func() {
rootCmd.SetArgs([]string{"env"})
err := rootCmd.Execute()
Expand All @@ -57,7 +52,7 @@ func TestEnvCmd(t *testing.T) {
}
})

// Then: the output should contain the environment variables
// Then the output should contain the environment variables
expectedOutput := "VAR1=value1\nVAR2=value2\n"
if output != expectedOutput {
t.Errorf("Expected output %q, got %q", expectedOutput, output)
Expand All @@ -68,9 +63,9 @@ func TestEnvCmd(t *testing.T) {
defer resetRootCmd()
defer recoverPanic(t)

// Given: a container that returns an error when resolving helpers
mockCliConfigHandler := config.NewMockConfigHandler(nil, nil, nil, nil, nil, nil)
mockProjectConfigHandler := config.NewMockConfigHandler(nil, nil, nil, nil, nil, nil)
// Given a container that returns an error when resolving helpers
mockCliConfigHandler := config.NewMockConfigHandler()
mockProjectConfigHandler := config.NewMockConfigHandler()
mockShell, err := shell.NewMockShell("cmd")
if err != nil {
t.Fatalf("NewMockShell() error = %v", err)
Expand All @@ -89,7 +84,7 @@ func TestEnvCmd(t *testing.T) {
mockContainer.Register("dockerHelper", mockHelper)
Initialize(mockContainer)

// When: the env command is executed with verbose flag
// When the env command is executed with verbose flag
output := captureStderr(func() {
rootCmd.SetArgs([]string{"env", "--verbose"})
err := rootCmd.Execute()
Expand All @@ -98,7 +93,7 @@ func TestEnvCmd(t *testing.T) {
}
})

// Then: the output should indicate the error
// Then the output should indicate the error
expectedOutput := "Error resolving helpers: resolve helpers error"
if !strings.Contains(output, expectedOutput) {
t.Errorf("Expected output to contain %q, got %q", expectedOutput, output)
Expand All @@ -109,9 +104,9 @@ func TestEnvCmd(t *testing.T) {
defer resetRootCmd()
defer recoverPanic(t)

// Given: a container that returns an error when resolving helpers
mockCliConfigHandler := config.NewMockConfigHandler(nil, nil, nil, nil, nil, nil)
mockProjectConfigHandler := config.NewMockConfigHandler(nil, nil, nil, nil, nil, nil)
// Given a container that returns an error when resolving helpers
mockCliConfigHandler := config.NewMockConfigHandler()
mockProjectConfigHandler := config.NewMockConfigHandler()
mockShell, err := shell.NewMockShell("cmd")
if err != nil {
t.Fatalf("NewMockShell() error = %v", err)
Expand All @@ -135,10 +130,10 @@ func TestEnvCmd(t *testing.T) {
rootCmd.SetOut(&buf)
rootCmd.SetErr(&buf)

// When: the env command is executed without verbose flag
// When the env command is executed without verbose flag
rootCmd.SetArgs([]string{"env"})
err = rootCmd.Execute()
// Then: the error should be nil and no output should be produced
// Then the error should be nil and no output should be produced
if err != nil {
t.Fatalf("Expected error nil, got %v", err)
}
Expand All @@ -151,16 +146,16 @@ func TestEnvCmd(t *testing.T) {
defer resetRootCmd()
defer recoverPanic(t)

// Given: a container that returns an error when resolving the shell
mockCliConfigHandler := config.NewMockConfigHandler(nil, nil, nil, nil, nil, nil)
mockProjectConfigHandler := config.NewMockConfigHandler(nil, nil, nil, nil, nil, nil)
// Given a container that returns an error when resolving the shell
mockCliConfigHandler := config.NewMockConfigHandler()
mockProjectConfigHandler := config.NewMockConfigHandler()
mockContainer := di.NewMockContainer()
mockContainer.SetResolveError("shell", errors.New("resolve shell error")) // Simulate error
mockContainer.Register("cliConfigHandler", mockCliConfigHandler)
mockContainer.Register("projectConfigHandler", mockProjectConfigHandler)
Initialize(mockContainer)

// When: the env command is executed with verbose flag
// When the env command is executed with verbose flag
output := captureStderr(func() {
rootCmd.SetArgs([]string{"env", "--verbose"})
err := rootCmd.Execute()
Expand All @@ -169,7 +164,7 @@ func TestEnvCmd(t *testing.T) {
}
})

// Then: the output should indicate the error
// Then the output should indicate the error
expectedOutput := "Error resolving shell: resolve shell error"
if !strings.Contains(output, expectedOutput) {
t.Errorf("Expected output to contain %q, got %q", expectedOutput, output)
Expand All @@ -188,9 +183,9 @@ func TestEnvCmd(t *testing.T) {
defer resetRootCmd()
defer recoverPanic(t)

// Given: a helper that returns an error when getting environment variables
mockCliConfigHandler := config.NewMockConfigHandler(nil, nil, nil, nil, nil, nil)
mockProjectConfigHandler := config.NewMockConfigHandler(nil, nil, nil, nil, nil, nil)
// Given a helper that returns an error when getting environment variables
mockCliConfigHandler := config.NewMockConfigHandler()
mockProjectConfigHandler := config.NewMockConfigHandler()
mockShell, err := shell.NewMockShell("cmd")
if err != nil {
t.Fatalf("NewMockShell() error = %v", err)
Expand All @@ -205,7 +200,7 @@ func TestEnvCmd(t *testing.T) {
})
setupContainer(mockCliConfigHandler, mockProjectConfigHandler, mockShell, mockHelper, nil, nil, nil)

// When: the env command is executed with verbose flag
// When the env command is executed with verbose flag
output := captureStderr(func() {
rootCmd.SetArgs([]string{"env", "--verbose"})
err := rootCmd.Execute()
Expand All @@ -214,7 +209,7 @@ func TestEnvCmd(t *testing.T) {
}
})

// Then: the output should indicate the error
// Then the output should indicate the error
expectedOutput := "Error getting environment variables: get env vars error"
if !strings.Contains(output, expectedOutput) {
t.Errorf("Expected output to contain %q, got %q", expectedOutput, output)
Expand All @@ -225,9 +220,9 @@ func TestEnvCmd(t *testing.T) {
defer resetRootCmd()
defer recoverPanic(t)

// Given: a helper that returns an error when getting environment variables
mockCliConfigHandler := config.NewMockConfigHandler(nil, nil, nil, nil, nil, nil)
mockProjectConfigHandler := config.NewMockConfigHandler(nil, nil, nil, nil, nil, nil)
// Given a helper that returns an error when getting environment variables
mockCliConfigHandler := config.NewMockConfigHandler()
mockProjectConfigHandler := config.NewMockConfigHandler()
mockShell, err := shell.NewMockShell("cmd")
if err != nil {
t.Fatalf("NewMockShell() error = %v", err)
Expand All @@ -247,11 +242,11 @@ func TestEnvCmd(t *testing.T) {
rootCmd.SetOut(&buf)
rootCmd.SetErr(&buf)

// When: the env command is executed without verbose flag
// When the env command is executed without verbose flag
rootCmd.SetArgs([]string{"env"})
err = rootCmd.Execute()

// Then: the error should be nil and no output should be produced
// Then the error should be nil and no output should be produced
if err != nil {
t.Fatalf("Expected error nil, got %v", err)
}
Expand All @@ -264,9 +259,9 @@ func TestEnvCmd(t *testing.T) {
defer resetRootCmd()
defer recoverPanic(t)

// Given: a helper that returns an error when executing PostEnvExec
mockCliConfigHandler := config.NewMockConfigHandler(nil, nil, nil, nil, nil, nil)
mockProjectConfigHandler := config.NewMockConfigHandler(nil, nil, nil, nil, nil, nil)
// Given a helper that returns an error when executing PostEnvExec
mockCliConfigHandler := config.NewMockConfigHandler()
mockProjectConfigHandler := config.NewMockConfigHandler()
mockShell, err := shell.NewMockShell("cmd")
if err != nil {
t.Fatalf("NewMockShell() error = %v", err)
Expand All @@ -284,7 +279,7 @@ func TestEnvCmd(t *testing.T) {
})
setupContainer(mockCliConfigHandler, mockProjectConfigHandler, mockShell, mockHelper, nil, nil, nil)

// When: the env command is executed with verbose flag
// When the env command is executed with verbose flag
output := captureStderr(func() {
rootCmd.SetArgs([]string{"env", "--verbose"})
err := rootCmd.Execute()
Expand All @@ -293,7 +288,7 @@ func TestEnvCmd(t *testing.T) {
}
})

// Then: the output should indicate the error
// Then the output should indicate the error
expectedOutput := "Error executing PostEnvExec: post env exec error"
if !strings.Contains(output, expectedOutput) {
t.Errorf("Expected output to contain %q, got %q", expectedOutput, output)
Expand All @@ -304,9 +299,9 @@ func TestEnvCmd(t *testing.T) {
defer resetRootCmd()
defer recoverPanic(t)

// Given: a helper that returns an error when executing PostEnvExec
mockCliConfigHandler := config.NewMockConfigHandler(nil, nil, nil, nil, nil, nil)
mockProjectConfigHandler := config.NewMockConfigHandler(nil, nil, nil, nil, nil, nil)
// Given a helper that returns an error when executing PostEnvExec
mockCliConfigHandler := config.NewMockConfigHandler()
mockProjectConfigHandler := config.NewMockConfigHandler()
mockShell, err := shell.NewMockShell("cmd")
if err != nil {
t.Fatalf("NewMockShell() error = %v", err)
Expand All @@ -329,11 +324,11 @@ func TestEnvCmd(t *testing.T) {
rootCmd.SetOut(&buf)
rootCmd.SetErr(&buf)

// When: the env command is executed without verbose flag
// When the env command is executed without verbose flag
rootCmd.SetArgs([]string{"env"})
err = rootCmd.Execute()

// Then: the error should be nil and no output should be produced
// Then the error should be nil and no output should be produced
if err != nil {
t.Fatalf("Expected error nil, got %v", err)
}
Expand Down
Loading

0 comments on commit 613a307

Please sign in to comment.