Fixing test complaining about exit code

This commit is contained in:
Gary Holtz 2024-04-22 22:18:17 -05:00
parent a1e6e5a761
commit e0c4a27553
No known key found for this signature in database
GPG Key ID: 0C3B92677CFEE92F
2 changed files with 25 additions and 6 deletions

View File

@ -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

View File

@ -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)
}
}