diff --git a/commands/api/api_test.go b/commands/api/api_test.go index 85bad997..3922ff03 100644 --- a/commands/api/api_test.go +++ b/commands/api/api_test.go @@ -7,6 +7,7 @@ import ( "io" "net/http" "os" + "os/exec" "testing" "gitlab.com/gitlab-org/cli/pkg/iostreams" @@ -188,11 +189,6 @@ func Test_NewCmdApi(t *testing.T) { }, wantsErr: false, }, - { - name: "no arguments", - cli: "", - wantsErr: true, - }, { name: "with hostname", cli: "graphql --hostname tom.petty", @@ -243,6 +239,29 @@ func Test_NewCmdApi(t *testing.T) { } } +func Test_NewCmdApiWithNoArguments(t *testing.T) { + f := cmdtest.StubFactory("") + f.IO, _, _, _ = iostreams.Test() + + if os.Getenv("BE_CRASHER") == "1" { + cmd := NewCmdApi(f, func(o *ApiOptions) error { + return nil + }) + cmd.Execute() + } + + // Clever trick from: + // https://stackoverflow.com/questions/26225513/how-to-test-os-exit-scenarios-in-go + testCmd := exec.Command(os.Args[0], "-test.run=Test_NewCmdApiWithNoArguments") + testCmd.Env = append(os.Environ(), "BE_CRASHER=1") + err := testCmd.Run() + + if e, ok := err.(*exec.ExitError); ok && !e.Success() { + return + } + t.Fatalf("process ran with err %v, want exit status 1", err) +} + func Test_apiRun(t *testing.T) { tests := []struct { name string diff --git a/commands/cmdutils/cmdutils.go b/commands/cmdutils/cmdutils.go index 0eccbe09..4ad34f5b 100644 --- a/commands/cmdutils/cmdutils.go +++ b/commands/cmdutils/cmdutils.go @@ -66,7 +66,7 @@ func LoadGitLabTemplate(tmplType, tmplName string) (string, error) { func ReturnHelpWhenNoArgs(cmd *cobra.Command, args []string) { if len(args) == 0 { cmd.Help() - os.Exit(0) + os.Exit(1) } }