Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cli cmd tests #182

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions pkg/cmd/local_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/projectCreate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
21 changes: 21 additions & 0 deletions pkg/cmd/project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
98 changes: 97 additions & 1 deletion pkg/cmd/propertiesDelete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
70 changes: 68 additions & 2 deletions pkg/cmd/propertiesGet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -49,6 +54,67 @@ func TestPropertiesGetHelpFlagSetCorrectly(t *testing.T) {
finalWordHandler := factory.GetFinalWordHandler().(*MockFinalWordHandler)
o := finalWordHandler.ReportedObject
assert.Nil(t, o)

assert.Nil(t, err)
}
}

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)
// }
21 changes: 21 additions & 0 deletions pkg/cmd/properties_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
45 changes: 43 additions & 2 deletions pkg/cmd/runsSubmitLocal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
39 changes: 39 additions & 0 deletions pkg/cmd/runsSubmit_tests.go
Original file line number Diff line number Diff line change
@@ -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")
}