generated from kurtosis-tech/package-template-repo
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: update command builder and add unit test
- Loading branch information
1 parent
cd7b56c
commit cb62315
Showing
3 changed files
with
120 additions
and
36 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package common | ||
|
||
import ( | ||
"bytes" | ||
"testing" | ||
|
||
"github.com/spf13/cobra" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestDiveCommandBuilder(t *testing.T) { | ||
// Initialize a new DiveCommandBuilder | ||
builder := NewDiveCommandBuilder() | ||
|
||
// Set command attributes | ||
builder.SetUse("testCommand"). | ||
SetShort("Short description"). | ||
SetLong("Long description"). | ||
SetRun(func(cmd *cobra.Command, args []string) { | ||
cmd.Println("test function") | ||
}) | ||
|
||
// Add flags | ||
boolFlag := false | ||
builder.AddBoolFlag(&boolFlag, "boolFlag", false, "Bool flag usage") | ||
|
||
stringFlag := "" | ||
builder.AddStringFlag(&stringFlag, "stringFlag", "default", "String flag usage") | ||
|
||
// Build the command | ||
cmd := builder.Build() | ||
|
||
// Run tests | ||
assert.Equal(t, "testCommand", cmd.Use) | ||
assert.Equal(t, "Short description", cmd.Short) | ||
assert.Equal(t, "Long description", cmd.Long) | ||
|
||
// Test flag addition | ||
assert.NotNil(t, cmd.Flags().Lookup("boolFlag")) | ||
assert.NotNil(t, cmd.Flags().Lookup("stringFlag")) | ||
|
||
// Test flag default values | ||
assert.False(t, boolFlag) | ||
assert.Equal(t, "default", stringFlag) | ||
|
||
// Test the Run function | ||
|
||
var outputBuffer bytes.Buffer | ||
cmd.SetOut(&outputBuffer) | ||
|
||
// Run the command | ||
cmd.Execute() | ||
|
||
// Check the output | ||
expectedOutput := "test function\n" | ||
actualOutput := outputBuffer.String() | ||
|
||
assert.Equal(t, expectedOutput, actualOutput) | ||
} |
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