Skip to content

Commit

Permalink
Add command tests
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonBaeumer committed Mar 26, 2019
1 parent 16350e0 commit b2f73ad
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
8 changes: 5 additions & 3 deletions cmd/commander/commander.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
35 changes: 35 additions & 0 deletions cmd/commander/commander_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"github.com/stretchr/testify/assert"
"io/ioutil"
"log"
"testing"
)

Expand Down Expand Up @@ -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)
}

0 comments on commit b2f73ad

Please sign in to comment.