spud/cli/start_service_test.go

72 lines
1.5 KiB
Go
Raw Permalink Normal View History

package cli
import (
2024-10-04 04:03:12 +00:00
"fmt"
"github.com/spf13/cobra"
"testing"
)
2024-10-04 04:03:12 +00:00
func Test_StartServiceCli_StartServiceDefinitionPathMustExist(t *testing.T) {
cli := GetCli()
cli.SetArgs([]string{"start", "-d", "./not-a-file.yml"})
outcome := cli.Execute()
if outcome == nil {
t.Errorf("Expected error, got nil.")
}
}
2024-10-04 04:03:12 +00:00
func Test_StartServiceCli_StartDefaultsServiceDefinitionPath(t *testing.T) {
cli := GetCli()
cli.SetArgs([]string{"start"})
startCommand, _, _ := cli.Find([]string{"start"})
startCommand.RunE = func(cmd *cobra.Command, args []string) error {
actual, _ := cmd.PersistentFlags().GetString("definition")
if actual != "./service.yml" {
t.Errorf("Unexpected default value for 'definition' / 'd' arg: %s", actual)
}
return nil
}
cli.Execute()
}
2024-10-04 04:03:12 +00:00
func Test_StartServiceCli_ErrorIfHostAndPortNotProvidedTogether(t *testing.T) {
inputs := [][]string{
{"start", "-H", "host"},
{"start", "-p", "9999"},
}
for _, input := range inputs {
t.Run(fmt.Sprintf("%+v", input), func(t *testing.T) {
cli := GetCli()
cli.SetArgs(input)
startCommand, _, _ := cli.Find([]string{"start"})
previousPreRun := startCommand.PersistentPreRunE
startCommand.PersistentPreRunE = func(cmd *cobra.Command, args []string) error {
if err := previousPreRun(cmd, args); err == nil {
t.Errorf("Expected error, got nil.")
}
return nil
}
startCommand.RunE = func(cmd *cobra.Command, args []string) error {
return nil
}
cli.Execute()
})
}
}