-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #320 from IBM-Cloud/dev
Promote dev to master
- Loading branch information
Showing
8 changed files
with
376 additions
and
21 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
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,117 @@ | ||
package plugin | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"os" | ||
"testing" | ||
|
||
"github.com/IBM-Cloud/ibm-cloud-cli-sdk/testhelpers" | ||
"github.com/spf13/cobra" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
type cobraTestPlugin struct { | ||
cmd *cobra.Command | ||
metadata string | ||
} | ||
|
||
type urfaveTestPlugin struct { | ||
metadata string | ||
} | ||
|
||
func marshalMetadata(meta PluginMetadata) string { | ||
json, err := json.Marshal(meta) | ||
if err != nil { | ||
panic(fmt.Errorf("could not marshal metadata: %v", err.Error())) | ||
} | ||
|
||
return string(json) | ||
} | ||
|
||
var pluginMetadata = PluginMetadata{ | ||
Name: "test", | ||
Commands: []Command{ | ||
{ | ||
Name: "list", | ||
Description: "List your apps, containers and services in the target space.", | ||
Usage: "ibmcloud list", | ||
}, | ||
}, | ||
} | ||
|
||
func (p *cobraTestPlugin) GetMetadata() PluginMetadata { | ||
p.cmd = testhelpers.GenerateCobraCommand() | ||
pluginMetadata.Commands[0].Flags = ConvertCobraFlagsToPluginFlags(p.cmd) | ||
p.metadata = marshalMetadata(fillMetadata(pluginMetadata)) | ||
|
||
return pluginMetadata | ||
} | ||
|
||
func (p *urfaveTestPlugin) GetMetadata() PluginMetadata { | ||
pluginMetadata.Commands[0].Flags = []Flag{ | ||
{ | ||
Name: "output", | ||
Description: "Specify output format, only 'JSON' is supported.", | ||
Hidden: false, | ||
HasValue: true, | ||
}, | ||
} | ||
p.metadata = marshalMetadata(fillMetadata(pluginMetadata)) | ||
|
||
return pluginMetadata | ||
} | ||
|
||
func (p *cobraTestPlugin) Run(context PluginContext, args []string) {} | ||
func (p *urfaveTestPlugin) Run(context PluginContext, args []string) {} | ||
|
||
func TestStartWithArgsWithCobraCommand(t *testing.T) { | ||
orgStdout := os.Stdout | ||
stdoutMock := testhelpers.CreateMockStdout() | ||
stdoutFile := stdoutMock.File | ||
|
||
// cleanup mock | ||
defer func() { | ||
os.Stdout = orgStdout | ||
os.RemoveAll(stdoutFile.Name()) | ||
stdoutFile.Close() | ||
}() | ||
|
||
// mock stdout with empty file | ||
os.Stdout = stdoutFile | ||
|
||
cmd := []string{"SendMetadata"} | ||
pl := &cobraTestPlugin{} | ||
|
||
StartWithArgs(pl, cmd) | ||
|
||
stdoutMockOut := stdoutMock.Read() | ||
|
||
assert.Equal(t, pl.metadata, string(stdoutMockOut)) | ||
} | ||
|
||
func TestStartWithArgsWithUrfaveCommand(t *testing.T) { | ||
orgStdout := os.Stdout | ||
stdoutMock := testhelpers.CreateMockStdout() | ||
stdoutFile := stdoutMock.File | ||
|
||
// cleanup mock | ||
defer func() { | ||
os.Stdout = orgStdout | ||
os.RemoveAll(stdoutFile.Name()) | ||
stdoutFile.Close() | ||
}() | ||
|
||
// mock stdout with empty file | ||
os.Stdout = stdoutFile | ||
|
||
cmd := []string{"SendMetadata"} | ||
pl := &urfaveTestPlugin{} | ||
|
||
StartWithArgs(pl, cmd) | ||
|
||
stdoutMockOut := stdoutMock.Read() | ||
|
||
assert.Equal(t, pl.metadata, string(stdoutMockOut)) | ||
|
||
} |
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,37 @@ | ||
package plugin | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/IBM-Cloud/ibm-cloud-cli-sdk/testhelpers" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestConvertCobraFlagsToPluginFlags(t *testing.T) { | ||
assert := assert.New(t) | ||
cmd := testhelpers.GenerateCobraCommand() | ||
outputFlag := cmd.Flag("output") | ||
quietFlag := cmd.Flag("quiet") | ||
deprecateFlag := cmd.Flag("outputJSON") | ||
|
||
flags := ConvertCobraFlagsToPluginFlags(cmd) | ||
|
||
assert.Equal(3, len(flags)) | ||
|
||
// NOTE: flags are sorted in lexicographical order | ||
assert.Equal(outputFlag.Usage, flags[0].Description) | ||
assert.True(flags[0].HasValue) | ||
assert.Equal(outputFlag.Hidden, flags[0].Hidden) | ||
assert.Equal(outputFlag.Name, flags[0].Name) | ||
|
||
assert.Equal(deprecateFlag.Usage, flags[1].Description) | ||
assert.False(flags[1].HasValue) | ||
assert.Equal(deprecateFlag.Hidden, flags[1].Hidden) | ||
assert.Equal(deprecateFlag.Name, flags[1].Name) | ||
|
||
assert.Equal(quietFlag.Usage, flags[2].Description) | ||
assert.False(flags[2].HasValue) | ||
assert.Equal(quietFlag.Hidden, flags[2].Hidden) | ||
assert.Equal(fmt.Sprintf("%s,%s", quietFlag.Shorthand, quietFlag.Name), flags[2].Name) | ||
} |
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,48 @@ | ||
package testhelpers | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"os" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
// GenerateCobraCommand will create a cobra command with basic flags used for testing | ||
func GenerateCobraCommand() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "example", | ||
} | ||
|
||
cmd.Flags().StringP("output", "", "", "Specify output format, only 'JSON' is supported.") | ||
cmd.Flags().BoolP("quiet", "q", false, "Suppress verbose output") | ||
cmd.Flags().BoolP("outputJSON", "", false, "Output data into JSON format") | ||
|
||
// NOTE: Added #nosec tag since flag is not attached to a real command | ||
cmd.Flags().MarkDeprecated("outputJSON", "outputJSON deprecated use --output instead") // #nosec | ||
return cmd | ||
} | ||
|
||
type mockStdoutFile struct { | ||
File *os.File | ||
} | ||
|
||
// CreateMockStdout will create a temp file used for mocking stdout for testing | ||
func CreateMockStdout() *mockStdoutFile { | ||
f, err := os.CreateTemp("", "cli_sdk_mock_stdout") | ||
if err != nil { | ||
panic(fmt.Errorf("failed to create tmp file for mocking stdout: %v", err.Error())) | ||
} | ||
return &mockStdoutFile{ | ||
File: f, | ||
} | ||
} | ||
|
||
// Read will open the temp mock stdout file and return contents as a string | ||
func (m *mockStdoutFile) Read() string { | ||
out, err := ioutil.ReadFile(m.File.Name()) | ||
if err != nil { | ||
panic(fmt.Errorf("failed to read stdout file: %v", err.Error())) | ||
} | ||
return string(out) | ||
} |