-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
1 parent
2483955
commit 71083c1
Showing
4 changed files
with
122 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |