From 71083c13f0a0bce339fbf32e2456afc06d4380d7 Mon Sep 17 00:00:00 2001 From: Matthias Friedrich <1573457+matzefriedrich@users.noreply.github.com> Date: Thu, 26 Sep 2024 21:00:55 +0200 Subject: [PATCH] Adds test cases for command execution. 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. --- example/commands/decrypt_test.go | 28 +++++++++++++++ example/commands/encrypt_test.go | 18 ++++++++++ example/commands/hello_test.go | 18 ++++++++++ internal/utils/capture_stdout.go | 58 ++++++++++++++++++++++++++++++++ 4 files changed, 122 insertions(+) create mode 100644 example/commands/decrypt_test.go create mode 100644 example/commands/encrypt_test.go create mode 100644 example/commands/hello_test.go create mode 100644 internal/utils/capture_stdout.go diff --git a/example/commands/decrypt_test.go b/example/commands/decrypt_test.go new file mode 100644 index 0000000..70de653 --- /dev/null +++ b/example/commands/decrypt_test.go @@ -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) +} diff --git a/example/commands/encrypt_test.go b/example/commands/encrypt_test.go new file mode 100644 index 0000000..c1a1782 --- /dev/null +++ b/example/commands/encrypt_test.go @@ -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) +} diff --git a/example/commands/hello_test.go b/example/commands/hello_test.go new file mode 100644 index 0000000..42c768d --- /dev/null +++ b/example/commands/hello_test.go @@ -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) +} diff --git a/internal/utils/capture_stdout.go b/internal/utils/capture_stdout.go new file mode 100644 index 0000000..b43e326 --- /dev/null +++ b/internal/utils/capture_stdout.go @@ -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 +}