Skip to content

Commit

Permalink
Adds test cases for command execution.
Browse files Browse the repository at this point in the history
Introduced `CaptureStdout` function to capture standard output for a given callback in tests. Added test cases for `HelloCommand`, `CryptCommand`, and `DecryptCommand` to validate their execution using captured stdout.
  • Loading branch information
matzefriedrich committed Sep 26, 2024
1 parent 2483955 commit 71083c1
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 0 deletions.
28 changes: 28 additions & 0 deletions example/commands/decrypt_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package commands

import (
"github.com/matzefriedrich/cobra-extensions/internal/utils"
"github.com/stretchr/testify/assert"
"testing"
)

func Test_DecryptCommand_Execute(t *testing.T) {

// Arrange
const expectedMessage = "Hello World"

encryptedMessage, _ := utils.CaptureStdout(t, "", func() error {
encryptCommand := CreateEncryptMessageCommand()
encryptCommand.SetArgs([]string{"--message", expectedMessage})
return encryptCommand.Execute()
})

decryptedMessage, err := utils.CaptureStdout(t, encryptedMessage, func() error {
decryptCommand := CreateDecryptMessageCommand()
return decryptCommand.Execute()
})

// Assert
assert.NoError(t, err)
assert.Equal(t, expectedMessage, decryptedMessage)
}
18 changes: 18 additions & 0 deletions example/commands/encrypt_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package commands

import (
"github.com/stretchr/testify/assert"
"testing"
)

func Test_CryptCommand_Execute(t *testing.T) {

// Arrange
sut := CreateEncryptMessageCommand()
sut.SetArgs([]string{"--message", "Hello World"})
// Act
err := sut.Execute()

// Assert
assert.NoError(t, err)
}
18 changes: 18 additions & 0 deletions example/commands/hello_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package commands

import (
"github.com/stretchr/testify/assert"
"testing"
)

func Test_HelloCommand_Execute(t *testing.T) {
// Arrange
sut := CreateHelloCommand()
sut.SetArgs([]string{"John Doe"})

// Act
err := sut.Execute()

// Assert
assert.NoError(t, err)
}
58 changes: 58 additions & 0 deletions internal/utils/capture_stdout.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package utils

import (
"bytes"
"os"
"testing"
)

// CaptureStdout captures the standard output of the provided callback function.
//
// Parameters:
// - t: the testing framework instance.
// - in: the input string to be written to stdin.
// - cb: the callback function whose stdout needs to be captured.
//
// Returns:
// - The captured stdout as a string.
func CaptureStdout(t *testing.T, in string, cb func() error) (string, error) {

originalStdin := os.Stdin
defer func() { os.Stdin = originalStdin }()

originalStdOut := os.Stdout
defer func() { os.Stdout = originalStdOut }()

stdinReader, stdinWriter, stdinPipeErr := os.Pipe()
if stdinPipeErr != nil {
t.Fatalf("failed to create pipe: %v", stdinPipeErr)
}

stdoutReader, stdoutWriter, stdoutPipeErr := os.Pipe()
if stdoutPipeErr != nil {
t.Fatalf("failed to create pipe: %v", stdoutPipeErr)
}

os.Stdin = stdinReader
os.Stdout = stdoutWriter

_, _ = stdinWriter.Write([]byte(in))
_ = stdinWriter.Close()

output := make(chan string)
go func() {
var buf bytes.Buffer
_, _ = buf.ReadFrom(stdoutReader)
output <- buf.String()
}()

err := cb()
if err != nil {
return "", err
}

_ = stdoutWriter.Close()

data := <-output
return data, nil
}

0 comments on commit 71083c1

Please sign in to comment.