Skip to content

Commit

Permalink
Merge pull request #51 from SimonBaeumer/add-flags
Browse files Browse the repository at this point in the history
Add flags and default concurrency
  • Loading branch information
SimonBaeumer authored Mar 26, 2019
2 parents 4f79ab7 + b2f73ad commit 46c39b4
Show file tree
Hide file tree
Showing 9 changed files with 109 additions and 31 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# v0.4.0
- Add flags `test` command
- `--verbose` will print more detailed output
- `--no-color` will discard all colors
- `--concurrent [int value]` sets the maximum concurrently executed tests in `go routines`
- Add default test concurrency of `runtime.NumCPU() * 5`

# v0.3.0

- Add `windows` release
Expand Down
62 changes: 45 additions & 17 deletions cmd/commander/commander.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,31 @@ const (

var version string

func main() {
log.SetOutput(ioutil.Discard)
type CommanderContext struct {
Verbose bool
NoColor bool
Concurrent int
}

log.Println("Starting commander")
func NewContextFromCli(c *cli.Context) CommanderContext {
return CommanderContext{
Verbose: c.Bool("verbose"),
NoColor: c.Bool("no-color"),
Concurrent: c.Int("concurrent"),
}
}

app := createCliApp()
func main() {
run(os.Args)
}

if err := app.Run(os.Args); err != nil {
func run(args []string) bool {
app := createCliApp()
if err := app.Run(args); err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
return true
}

func createCliApp() *cli.App {
Expand All @@ -37,28 +51,42 @@ func createCliApp() *cli.App {
app.Usage = "CLI app testing"
app.Version = version

app.Flags = []cli.Flag{
cli.BoolFlag{
Name: "verbose",
Usage: "More output for debugging",
EnvVar: "COMMANDER_VERBOSE",
},
}

app.Commands = []cli.Command{
{
Name: "test",
Usage: "Execute the test suite",
ArgsUsage: "[file] [test]",
Flags: []cli.Flag{
cli.IntFlag{
Name: "concurrent",
EnvVar: "COMMANDER_CONCURRENT",
Usage: "Set the max amount of tests which should run concurrently",
},
cli.BoolFlag{
Name: "no-color",
EnvVar: "COMMANDER_NO_COLOR",
Usage: "Activate or deactivate colored output",
},
cli.BoolFlag{
Name: "verbose",
Usage: "More output for debugging",
EnvVar: "COMMANDER_VERBOSE",
},
},
Action: func(c *cli.Context) error {
return testCommand(c.Args().First(), c.Args().Get(1))
return testCommand(c.Args().First(), c.Args().Get(1), NewContextFromCli(c))
},
},
}
return app
}

func testCommand(file string, title string) error {
func testCommand(file string, title string, ctx CommanderContext) error {
log.SetOutput(ioutil.Discard)
if ctx.Verbose == true {
log.SetOutput(os.Stdout)
}

if file == "" {
file = commanderFile
}
Expand All @@ -82,8 +110,8 @@ func testCommand(file string, title string) error {
tests = []runtime.TestCase{test}
}

results := runtime.Start(tests)
out := output.NewCliOutput()
results := runtime.Start(tests, ctx.Concurrent)
out := output.NewCliOutput(!ctx.NoColor)
if !out.Start(results) {
return fmt.Errorf("Test suite failed")
}
Expand Down
39 changes: 37 additions & 2 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 All @@ -18,7 +19,7 @@ tests:

assert.Nil(t, err)

got := testCommand(TestSuiteFile, "")
got := testCommand(TestSuiteFile, "", CommanderContext{})
assert.Nil(t, got)
}

Expand All @@ -32,7 +33,41 @@ tests:

assert.Nil(t, err)

got := testCommand(TestSuiteFile, "")
got := testCommand(TestSuiteFile, "", CommanderContext{})
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)
}
4 changes: 2 additions & 2 deletions cmd/commander/commander_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func Test_CreateCliApp(t *testing.T) {
}

func Test_TestCommand(t *testing.T) {
err := testCommand("commander.yaml", "")
err := testCommand("commander.yaml", "", CommanderContext{})

if runtime.GOOS == "windows" {
assert.Equal(t, "Error open commander.yaml: The system cannot find the file specified.", err.Error())
Expand All @@ -24,7 +24,7 @@ func Test_TestCommand(t *testing.T) {
}

func Test_TestCommand_ShouldUseCustomFile(t *testing.T) {
err := testCommand("my-test.yaml", "")
err := testCommand("my-test.yaml", "", CommanderContext{})

if runtime.GOOS == "windows" {
assert.Equal(t, "Error open my-test.yaml: The system cannot find the file specified.", err.Error())
Expand Down
2 changes: 1 addition & 1 deletion commander_unix.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@ tests:
contains:
- ✓ should print global env value
- ✓ should print local env value
exit-code: 0
exit-code: 0
10 changes: 6 additions & 4 deletions pkg/output/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,21 @@ var au aurora.Aurora

// OutputWriter represents the output
type OutputWriter struct {
out io.Writer
out io.Writer
color bool
}

// NewCliOutput creates a new OutputWriter with a stdout writer
func NewCliOutput() OutputWriter {
func NewCliOutput(color bool) OutputWriter {
return OutputWriter{
out: os.Stdout,
out: os.Stdout,
color: color,
}
}

// Start starts the writing sequence
func (w *OutputWriter) Start(results <-chan runtime.TestResult) bool {
au = aurora.NewAurora(true)
au = aurora.NewAurora(w.color)
if run.GOOS == "windows" {
au = aurora.NewAurora(false)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/output/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

func Test_NewCliOutput(t *testing.T) {
got := NewCliOutput()
got := NewCliOutput(true)
assert.IsType(t, OutputWriter{}, got)
}

Expand Down
10 changes: 7 additions & 3 deletions pkg/runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package runtime
import (
"github.com/SimonBaeumer/commander/pkg/cmd"
"log"
"runtime"
"strings"
"sync"
)
Expand Down Expand Up @@ -81,7 +82,7 @@ type TestResult struct {
}

// Start starts the given test suite
func Start(tests []TestCase) <-chan TestResult {
func Start(tests []TestCase, maxConcurrent int) <-chan TestResult {
in := make(chan TestCase)
out := make(chan TestResult)

Expand All @@ -92,8 +93,11 @@ func Start(tests []TestCase) <-chan TestResult {
}
}(tests)

//TODO: Add more concurrency
workerCount := 1
workerCount := maxConcurrent
if maxConcurrent == 0 {
workerCount = runtime.NumCPU() * 5
}

var wg sync.WaitGroup
for i := 0; i < workerCount; i++ {
wg.Add(1)
Expand Down
4 changes: 3 additions & 1 deletion pkg/runtime/runtime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ import (
"testing"
)

const SingleConcurrent = 1

func TestRuntime_Start(t *testing.T) {
s := getExampleTestSuite()
got := Start(s)
got := Start(s, SingleConcurrent)

assert.IsType(t, make(<-chan TestResult), got)

Expand Down

0 comments on commit 46c39b4

Please sign in to comment.