diff --git a/pkg/cmd/local_test.go b/pkg/cmd/local_test.go index 9585cf6f..09eb5823 100644 --- a/pkg/cmd/local_test.go +++ b/pkg/cmd/local_test.go @@ -8,9 +8,30 @@ package cmd import ( "testing" + "github.com/galasa-dev/cli/pkg/utils" "github.com/stretchr/testify/assert" ) +func TestLocalNoCommandsProducesUsageReport(t *testing.T) { + // Given... + factory := NewMockFactory() + var args []string = []string{"local"} + + // When... + Execute(factory, args) + + // Then... + stdOutConsole := factory.GetStdOutConsole().(*utils.MockConsole) + outText := stdOutConsole.ReadText() + assert.Contains(t, outText, "Usage:") + assert.Contains(t, outText, "galasactl local [command]") + + // We expect an exit code of 0 for this command. + finalWordHandler := factory.GetFinalWordHandler().(*MockFinalWordHandler) + o := finalWordHandler.ReportedObject + assert.Nil(t, o) +} + func TestCommandListContainsLocalCommand(t *testing.T) { /// Given... factory := NewMockFactory() diff --git a/pkg/cmd/projectCreate_test.go b/pkg/cmd/projectCreate_test.go index 41ded8fa..81954cff 100644 --- a/pkg/cmd/projectCreate_test.go +++ b/pkg/cmd/projectCreate_test.go @@ -621,7 +621,7 @@ func TestCreateProjectUsingCommandLineNoFeaturesSetWorks(t *testing.T) { // Check that the default folder was created. fs := factory.GetFileSystem() var isExists bool - isExists, err = fs.DirExists("my.pkg") + isExists, _ = fs.DirExists("my.pkg") assert.True(t, isExists) } diff --git a/pkg/cmd/project_test.go b/pkg/cmd/project_test.go index 8a40229e..836efeee 100644 --- a/pkg/cmd/project_test.go +++ b/pkg/cmd/project_test.go @@ -8,9 +8,30 @@ package cmd import ( "testing" + "github.com/galasa-dev/cli/pkg/utils" "github.com/stretchr/testify/assert" ) +func TestProjectNoCommandsProducesUsageReport(t *testing.T) { + // Given... + factory := NewMockFactory() + var args []string = []string{"project"} + + // When... + Execute(factory, args) + + // Then... + stdOutConsole := factory.GetStdOutConsole().(*utils.MockConsole) + outText := stdOutConsole.ReadText() + assert.Contains(t, outText, "Usage:") + assert.Contains(t, outText, "galasactl project [command]") + + // We expect an exit code of 0 for this command. + finalWordHandler := factory.GetFinalWordHandler().(*MockFinalWordHandler) + o := finalWordHandler.ReportedObject + assert.Nil(t, o) +} + func TestCommandListContainsProjectCommand(t *testing.T) { /// Given... factory := NewMockFactory() diff --git a/pkg/cmd/propertiesDelete_test.go b/pkg/cmd/propertiesDelete_test.go index 99f57ad5..a69aaa35 100644 --- a/pkg/cmd/propertiesDelete_test.go +++ b/pkg/cmd/propertiesDelete_test.go @@ -7,10 +7,106 @@ package cmd import ( "testing" - + "github.com/galasa-dev/cli/pkg/utils" "github.com/stretchr/testify/assert" ) +func TestPropertiesDeleteNoArgsReturnsError(t *testing.T) { + // Given... + factory := NewMockFactory() + var args []string = []string{"properties", "delete"} + + // When... + err := Execute(factory, args) + + // Then... + stdOutConsole := factory.GetStdOutConsole().(*utils.MockConsole) + outText := stdOutConsole.ReadText() + assert.Contains(t, outText, "Usage:") + assert.Contains(t, outText, "galasactl properties delete [flags]") + + stdErrConsole := factory.GetStdErrConsole().(*utils.MockConsole) + errText := stdErrConsole.ReadText() + assert.Contains(t, errText, "Error: required flag(s) \"name\", \"namespace\" not set") + + // We expect an exit code of 1 for this command. But it seems that syntax errors caught by cobra still return no error. + finalWordHandler := factory.GetFinalWordHandler().(*MockFinalWordHandler) + o := finalWordHandler.ReportedObject + assert.Nil(t, o) + + assert.NotNil(t, err) +} + +func TestPropertiesDeleteWithoutName(t *testing.T) { + // Given... + factory := NewMockFactory() + var args []string = []string{"properties", "delete", "--namespace", "jitters"} + + // When... + err := Execute(factory, args) + + // Then... + stdOutConsole := factory.GetStdOutConsole().(*utils.MockConsole) + outText := stdOutConsole.ReadText() + assert.Contains(t, outText, "Usage:") + + stdErrConsole := factory.GetStdErrConsole().(*utils.MockConsole) + errText := stdErrConsole.ReadText() + assert.Contains(t, errText, "Error: required flag(s) \"name\" not set") + + // We expect an exit code of 1 for this command. But it seems that syntax errors caught by cobra still return no error. + finalWordHandler := factory.GetFinalWordHandler().(*MockFinalWordHandler) + o := finalWordHandler.ReportedObject + assert.Nil(t, o) + + assert.NotNil(t, err) +} + +func TestPropertiesDeleteWithoutNamespace(t *testing.T) { + // Given... + factory := NewMockFactory() + var args []string = []string{"properties", "delete", "--name", "jeepers"} + + // When... + err := Execute(factory, args) + + // Then... + stdOutConsole := factory.GetStdOutConsole().(*utils.MockConsole) + outText := stdOutConsole.ReadText() + assert.Contains(t, outText, "Usage:") + + stdErrConsole := factory.GetStdErrConsole().(*utils.MockConsole) + errText := stdErrConsole.ReadText() + assert.Contains(t, errText, "Error: required flag(s) \"namespace\" not set") + + // We expect an exit code of 1 for this command. But it seems that syntax errors caught by cobra still return no error. + finalWordHandler := factory.GetFinalWordHandler().(*MockFinalWordHandler) + o := finalWordHandler.ReportedObject + assert.Nil(t, o) + + assert.NotNil(t, err) +} + +func TestPropertiesDeleteWithNameAndNamespace(t *testing.T) { + // Given... + factory := NewMockFactory() + fs := factory.GetFileSystem() + var args []string = []string{"properties", "delete", "--namespace", "gyro", "--name", "space.ball"} + homeDir, _ := fs.GetUserHomeDirPath() + galasaDir := homeDir + "/.galasa/" + fs.WriteTextFile(galasaDir+"bootstrap.properties", "") + + // When... + err := Execute(factory, args) + + // Then... + assert.Nil(t, err) + + stdOutConsole := factory.GetStdOutConsole().(*utils.MockConsole) + outText := stdOutConsole.ReadText() + assert.Equal(t, outText, "") +} + func TestPropertiesDeleteCommandInCommandCollectionHasName(t *testing.T) { factory := NewMockFactory() diff --git a/pkg/cmd/propertiesGet_test.go b/pkg/cmd/propertiesGet_test.go index cadc50a5..394585df 100644 --- a/pkg/cmd/propertiesGet_test.go +++ b/pkg/cmd/propertiesGet_test.go @@ -9,9 +9,14 @@ import ( "testing" "github.com/galasa-dev/cli/pkg/utils" + "github.com/spf13/cobra" "github.com/stretchr/testify/assert" ) +func propertiesExectuteTestReplacement(cmd *cobra.Command, args []string) error { + return nil +} + func TestPropertiesGetCommandInCommandCollectionHasName(t *testing.T) { factory := NewMockFactory() @@ -49,6 +54,67 @@ func TestPropertiesGetHelpFlagSetCorrectly(t *testing.T) { finalWordHandler := factory.GetFinalWordHandler().(*MockFinalWordHandler) o := finalWordHandler.ReportedObject assert.Nil(t, o) - assert.Nil(t, err) -} \ No newline at end of file +} + +func TestPropertiesGetNoArgsReturnsError(t *testing.T) { + // Given... + factory := NewMockFactory() + var args []string = []string{"properties", "get"} + // When... + err := Execute(factory, args) + + // Then... + stdOutConsole := factory.GetStdOutConsole().(*utils.MockConsole) + outText := stdOutConsole.ReadText() + assert.Contains(t, outText, "Usage:") + assert.Contains(t, outText, "galasactl properties get [flags]") + + stdErrConsole := factory.GetStdErrConsole().(*utils.MockConsole) + errText := stdErrConsole.ReadText() + assert.Contains(t, errText, "Error: required flag(s) \"namespace\" not set") + + assert.NotNil(t, err) +} + +// func TestPropertiesGetWithNamespaceReturnsTable(t *testing.T) { +// // Given... +// factory := NewMockFactory() +// fs := factory.GetFileSystem() +// homeDir, _ := fs.GetUserHomeDirPath() +// galasaDir := homeDir + "/.galasa/" +// fs.WriteTextFile(galasaDir+"bootstrap.properties", "") +// var args []string = []string{"properties", "get", "--namespace", "framework", "--log", "-"} + +// var err error + +// var commands map[string]*cobra.Command +// var parsedCommandValues map[string]interface{} + +// commands, parsedCommandValues, err = CreateCommandTree(factory) +// rootCmd := commands["root"] +// propsGetCmd := commands["properties get"] + +// // TODO: Over-ride the RunE function... +// propsGetCmd.RunE = propertiesExectuteTestReplacement + +// rootCmd.SetArgs(args) + +// // When... +// // err := Execute(factory, args) +// err = rootCmd.Execute() + +// // Then... +// assert.Nil(t, err) + +// propsGetData := parsedCommandValues["properties get"].(PropertiesGetCmdValues) +// assert.Nil(t, propsGetData.propertiesInfix) +// assert.Nil(t, propsGetData.propertiesPrefix) +// assert.Nil(t, propsGetData.propertiesSuffix) + +// propsData := parsedCommandValues["properties"].(PropertiesCmdValues) +// assert.Equal(t, "framework", propsData.namespace) + +// rootData := parsedCommandValues["root"].(RootCmdValues) +// assert.Equal(t, "-", rootData.logFileName) +// } \ No newline at end of file diff --git a/pkg/cmd/properties_test.go b/pkg/cmd/properties_test.go index 87ba7102..6814980e 100644 --- a/pkg/cmd/properties_test.go +++ b/pkg/cmd/properties_test.go @@ -8,9 +8,30 @@ package cmd import ( "testing" + "github.com/galasa-dev/cli/pkg/utils" "github.com/stretchr/testify/assert" ) +func TestPropertiesNoCommandsProducesUsageReport(t *testing.T) { + // Given... + factory := NewMockFactory() + var args []string = []string{"properties"} + + // When... + Execute(factory, args) + + // Then... + stdOutConsole := factory.GetStdOutConsole().(*utils.MockConsole) + outText := stdOutConsole.ReadText() + assert.Contains(t, outText, "Usage:") + assert.Contains(t, outText, "galasactl properties [command]") + + // We expect an exit code of 0 for this command. + finalWordHandler := factory.GetFinalWordHandler().(*MockFinalWordHandler) + o := finalWordHandler.ReportedObject + assert.Nil(t, o) +} + func TestPropertiesCommandInCommandCollection(t *testing.T) { factory := NewMockFactory() diff --git a/pkg/cmd/runsSubmitLocal_test.go b/pkg/cmd/runsSubmitLocal_test.go index cf32774f..11aecc01 100644 --- a/pkg/cmd/runsSubmitLocal_test.go +++ b/pkg/cmd/runsSubmitLocal_test.go @@ -2,15 +2,56 @@ * Copyright contributors to the Galasa project * * SPDX-License-Identifier: EPL-2.0 - */ +*/ package cmd import ( "testing" - "github.com/stretchr/testify/assert" ) +func TestRunsSubmitLocalWithoutObrWithClassErrors(t *testing.T) { + // Given... + factory := NewMockFactory() + var args []string = []string{"runs", "submit", "local", "--class", "osgi.bundle/class.path"} + + // When... + err := Execute(factory, args) + + // Then... + // Should throw an error asking for flags to be set + assert.NotNil(t, err, "err should have been set!") + assert.Contains(t, err.Error(), "required flag(s) \"obr\" not set") +} + +func TestRunsSubmitLocalWithoutClassWithObrErrors(t *testing.T) { + // Given... + factory := NewMockFactory() + var args []string = []string{"runs", "submit", "local", "--obr", "mvn:second.breakfast/elevenses/0.1.0/brunch"} + + // When... + err := Execute(factory, args) + + // Then... + // Should throw an error asking for flags to be set + assert.NotNil(t, err, "err should have been set!") + assert.Contains(t, err.Error(), "required flag(s) \"class\" not set") +} + +func TestMultipleRequiredFlagsNotSetReturnsListInError(t *testing.T) { + // Given... + factory := NewMockFactory() + var args []string = []string{"runs", "submit", "local"} + + // When... + err := Execute(factory, args) + + // Then... + // Should throw an error asking for flags to be set + assert.NotNil(t, err, "err should have been set!") + assert.Contains(t, err.Error(), "required flag(s) \"class\", \"obr\" not set") +} + func TestRunsSubmitLocalCommandInCommandCollection(t *testing.T) { factory := NewMockFactory() diff --git a/pkg/cmd/runsSubmit_tests.go b/pkg/cmd/runsSubmit_tests.go new file mode 100644 index 00000000..c798f702 --- /dev/null +++ b/pkg/cmd/runsSubmit_tests.go @@ -0,0 +1,39 @@ +/* + * Copyright contributors to the Galasa project + * + * SPDX-License-Identifier: EPL-2.0 +*/ +package cmd + +import ( + "testing" + "github.com/stretchr/testify/assert" +) + +func TestRunsSubmitWithoutFlagsErrors(t *testing.T) { + // Given... + factory := NewMockFactory() + var args []string = []string{"runs", "submit", "local"} + + // When... + err := Execute(factory, args) + + // Then... + // Should throw an error asking for flags to be set + assert.NotNil(t, err, "err should have been set!") + assert.Contains(t, err.Error(), "GAL1009E: The submit command requires either test selection flags (eg: --stream, --class, --bundle, --package, --tag, --regex, --test) or --portfolio flag to be specified. Use the --help flag for more details.") +} + +func TestRunsSubmitExecutesWithPortfolio(t *testing.T) { + // Given... + factory := NewMockFactory() + var args []string = []string{"runs", "submit", "local", "--class", "osgi.bundle/class.path"} + + // When... + err := Execute(factory, args) + + // Then... + // Should throw an error asking for flags to be set + assert.NotNil(t, err, "err should have been set!") + assert.Contains(t, err.Error(), "required flag(s) \"obr\" not set") +} \ No newline at end of file