package cli import ( "fmt" "github.com/spf13/cobra" "testing" ) 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.") } } 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() } 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() }) } }