From b2f73adcefa29aa17781eab417488782263234b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20B=C3=A4umer?= Date: Tue, 26 Mar 2019 15:30:56 +0100 Subject: [PATCH] Add command tests --- cmd/commander/commander.go | 8 +++--- cmd/commander/commander_linux_test.go | 35 +++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 3 deletions(-) diff --git a/cmd/commander/commander.go b/cmd/commander/commander.go index 8d5e8dc5..5fe6cabf 100644 --- a/cmd/commander/commander.go +++ b/cmd/commander/commander.go @@ -33,14 +33,16 @@ func NewContextFromCli(c *cli.Context) CommanderContext { } func main() { - log.SetOutput(ioutil.Discard) + run(os.Args) +} +func run(args []string) bool { app := createCliApp() - - if err := app.Run(os.Args); err != nil { + if err := app.Run(args); err != nil { fmt.Println(err.Error()) os.Exit(1) } + return true } func createCliApp() *cli.App { diff --git a/cmd/commander/commander_linux_test.go b/cmd/commander/commander_linux_test.go index 316ebadc..7268d0ab 100644 --- a/cmd/commander/commander_linux_test.go +++ b/cmd/commander/commander_linux_test.go @@ -3,6 +3,7 @@ package main import ( "github.com/stretchr/testify/assert" "io/ioutil" + "log" "testing" ) @@ -36,3 +37,37 @@ tests: assert.Equal(t, "Test suite failed", got.Error()) } + +func Test_WithTitle(t *testing.T) { + tests := []byte(` +tests: + my title: + command: echo hello + exit-code: 0 + another: + command: echo another + exit-code: 1 +`) + err := ioutil.WriteFile(TestSuiteFile, tests, 0755) + + assert.Nil(t, err) + + got := testCommand(TestSuiteFile, "my title", CommanderContext{}) + assert.Nil(t, got) +} + +func TestRunApp(t *testing.T) { + tests := []byte(` +tests: + my title: + command: echo hello + exit-code: 0 +`) + err := ioutil.WriteFile(TestSuiteFile, tests, 0755) + if err != nil { + log.Fatal(err) + } + + got := run([]string{"", "test", TestSuiteFile}) + assert.True(t, got) +}