From 4f3ef98bc22d72a96971f3e41560fa1671b0287b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krzysztof=20Suszy=C5=84ski?= Date: Fri, 4 Oct 2019 15:14:45 +0200 Subject: [PATCH] Adding test to main --- cmd/installer/main.go | 6 +++++- cmd/installer/main_test.go | 28 ++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 cmd/installer/main_test.go diff --git a/cmd/installer/main.go b/cmd/installer/main.go index 3b225a4..4d39a7e 100644 --- a/cmd/installer/main.go +++ b/cmd/installer/main.go @@ -6,6 +6,10 @@ import ( "github.com/wavesoftware/serverless-installer/internal/cli" ) +var exit func(int) = func(exitcode int) { + os.Exit(exitcode) +} + func main() { - os.Exit(cli.NewRunner().Run()) + exit(cli.NewRunner().Run()) } diff --git a/cmd/installer/main_test.go b/cmd/installer/main_test.go new file mode 100644 index 0000000..00203fe --- /dev/null +++ b/cmd/installer/main_test.go @@ -0,0 +1,28 @@ +package main + +import ( + "os" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestMain(t *testing.T) { + // given + oldExit := exit + oldArgs := os.Args + defer func() { + exit = oldExit + os.Args = oldArgs + }() + exitcode := ^int(0) + exit = func(received int) { + exitcode = received + } + os.Args = []string{"cmd", "--help"} + + // when + main() + + assert.Equal(t, 0, exitcode) +}