-
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.
* Add unit tests for CommandLineApplication * Adds test cases for command execution and introduces the `CaptureStdout` function to capture standard output for a given callback during tests. * Added test cases for commands of the example apps to validate their execution (to gain coverage through integration tests).
- Loading branch information
1 parent
30fc209
commit 23825a2
Showing
12 changed files
with
256 additions
and
4 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
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
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
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 | ||
} |
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
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,87 @@ | ||
package charmer | ||
|
||
import ( | ||
"github.com/matzefriedrich/cobra-extensions/pkg/commands" | ||
"github.com/matzefriedrich/cobra-extensions/pkg/types" | ||
"github.com/spf13/cobra" | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
func Test_CommandLineApplication_Execute_without_any_command_does_not_return_error(t *testing.T) { | ||
|
||
// Arrange | ||
sut := NewCommandLineApplication("test-app", "") | ||
|
||
// Act | ||
err := sut.Execute() | ||
|
||
// Assert | ||
assert.NoError(t, err) | ||
} | ||
|
||
func Test_CommandLineApplication_Execute_smoke_test(t *testing.T) { | ||
|
||
// Arrange | ||
application := NewCommandLineApplication("test-app", "") | ||
|
||
command := newTestCommand(noop()) | ||
sut := command.AsTypedCommand() | ||
application.AddGroupCommand(newTestGroupCommand(), func(setup types.CommandSetup) { | ||
setup.AddCommand(sut) | ||
}) | ||
|
||
application.root.SetArgs([]string{"group", "test"}) | ||
|
||
// Act | ||
err := application.Execute() | ||
|
||
// Assert | ||
assert.NoError(t, err) | ||
assert.True(t, command.Executed()) | ||
} | ||
|
||
type executeFunc func() | ||
|
||
func noop() executeFunc { | ||
return func() {} | ||
} | ||
|
||
type testCommand struct { | ||
use types.CommandName `flag:"test"` | ||
typedCommand types.TypedCommand | ||
executeFunc executeFunc | ||
executed bool | ||
} | ||
|
||
func (t *testCommand) Executed() bool { | ||
return t.executed | ||
} | ||
|
||
func (t *testCommand) Execute() { | ||
t.executed = true | ||
t.executeFunc() | ||
} | ||
|
||
var _ types.TypedCommand = (*testCommand)(nil) | ||
|
||
func newTestCommand(execute executeFunc) *testCommand { | ||
return &testCommand{ | ||
executeFunc: execute, | ||
} | ||
} | ||
|
||
func (t *testCommand) AsTypedCommand() *cobra.Command { | ||
typedCommand := commands.CreateTypedCommand(t) | ||
return typedCommand | ||
} | ||
|
||
type testGroupCommand struct { | ||
types.BaseCommand | ||
use types.CommandName `flag:"group"` | ||
} | ||
|
||
func newTestGroupCommand() *cobra.Command { | ||
command := &testGroupCommand{BaseCommand: types.BaseCommand{}} | ||
return commands.CreateTypedCommand(command) | ||
} |
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
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,29 @@ | ||
package charmer | ||
|
||
import ( | ||
"github.com/matzefriedrich/cobra-extensions/pkg/types" | ||
"github.com/spf13/cobra" | ||
"github.com/stretchr/testify/assert" | ||
"slices" | ||
"testing" | ||
) | ||
|
||
func Test_CommandSetup_AddGroupCommand_adds_subcommand_via_setup_func(t *testing.T) { | ||
|
||
// Arrange | ||
root := &cobra.Command{} | ||
sut := newCommandSetup(root) | ||
|
||
// Act | ||
group := &cobra.Command{Use: "group"} | ||
c := &cobra.Command{Use: "command"} | ||
|
||
sut.AddGroupCommand(group, func(setup types.CommandSetup) { | ||
setup.AddCommand(c) | ||
}) | ||
|
||
actual := slices.Contains(group.Commands(), c) | ||
|
||
// Assert | ||
assert.True(t, actual) | ||
} |
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