Skip to content

Commit f7bddd8

Browse files
committed
removed os exit code control from tests
1 parent a2bb752 commit f7bddd8

File tree

2 files changed

+2
-14
lines changed

2 files changed

+2
-14
lines changed

pkg/cmd/api/api.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"errors"
77
"io"
88
"net/http"
9-
"os"
109

1110
"github.com/MakeNowJust/heredoc/v2"
1211
"github.com/OctopusDeploy/cli/pkg/apiclient"
@@ -16,9 +15,6 @@ import (
1615
"github.com/spf13/cobra"
1716
)
1817

19-
// OsExit is a variable so tests can stub it to avoid terminating the process.
20-
var OsExit = os.Exit
21-
2218
func NewCmdAPI(f factory.Factory) *cobra.Command {
2319
cmd := &cobra.Command{
2420
Use: "api <url>",

pkg/cmd/api/api_test.go

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"net/http"
77
"testing"
88

9-
apiPkg "github.com/OctopusDeploy/cli/pkg/cmd/api"
109
cmdRoot "github.com/OctopusDeploy/cli/pkg/cmd/root"
1110
"github.com/OctopusDeploy/cli/test/testutil"
1211
"github.com/spf13/cobra"
@@ -51,12 +50,6 @@ func TestApiCommand(t *testing.T) {
5150
}},
5251

5352
{"prints error response body on non-2xx status", func(t *testing.T, api *testutil.MockHttpServer, rootCmd *cobra.Command, stdOut *bytes.Buffer, stdErr *bytes.Buffer) {
54-
// Stub os.Exit so the test doesn't terminate the process
55-
origExit := apiPkg.OsExit
56-
var exitCode int
57-
apiPkg.OsExit = func(code int) { exitCode = code }
58-
defer func() { apiPkg.OsExit = origExit }()
59-
6053
cmdReceiver := testutil.GoBegin2(func() (*cobra.Command, error) {
6154
defer api.Close()
6255
rootCmd.SetArgs([]string{"api", "/api/nonexistent"})
@@ -70,9 +63,8 @@ func TestApiCommand(t *testing.T) {
7063
})
7164

7265
_, err := testutil.ReceivePair(cmdReceiver)
73-
assert.Nil(t, err)
74-
assert.Equal(t, http.StatusNotFound, exitCode)
75-
assert.Contains(t, stdOut.String(), `"ErrorMessage": "Not found"`)
66+
assert.NotNil(t, err)
67+
assert.Contains(t, err.Error(), "Not found")
7668
}},
7769

7870
{"outputs raw body when response is not valid JSON", func(t *testing.T, api *testutil.MockHttpServer, rootCmd *cobra.Command, stdOut *bytes.Buffer, stdErr *bytes.Buffer) {

0 commit comments

Comments
 (0)