Skip to content

Commit

Permalink
Merge pull request #22 from joerdav/tests
Browse files Browse the repository at this point in the history
chore: more runner tests
  • Loading branch information
joerdav authored Jan 20, 2023
2 parents 0387429 + 06db839 commit da7aad7
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 20 deletions.
3 changes: 2 additions & 1 deletion cmd/xc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"log"
"os"
"runtime"
"runtime/debug"
"strings"

Expand Down Expand Up @@ -141,7 +142,7 @@ func runMain() error {
}
// xc task1 task2
for _, tav := range tav {
runner, err := run.NewRunner(t)
runner, err := run.NewRunner(t, runtime.GOOS)
if err != nil {
return err
}
Expand Down
5 changes: 2 additions & 3 deletions run/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"os"
"os/exec"
"runtime"
"strings"

"github.com/joerdav/xc/models"
Expand All @@ -32,15 +31,15 @@ type Runner struct {
//
// NewRunner will return an error in the case that Dependent tasks are cyclical,
// invalid or at a larger depth than 50.
func NewRunner(ts models.Tasks) (runner Runner, err error) {
func NewRunner(ts models.Tasks, runtime string) (runner Runner, err error) {
runner = Runner{
sep: ";",
cmdRunner: "bash",
flag: "-c",
runner: runCmd,
tasks: ts,
}
if runtime.GOOS == "windows" {
if runtime == "windows" {
runner.sep = "&&"
runner.cmdRunner = "cmd"
runner.flag = "/C"
Expand Down
80 changes: 64 additions & 16 deletions run/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package run

import (
"context"
"fmt"
"os/exec"
"strings"
"testing"
Expand All @@ -12,25 +13,29 @@ import (

func TestRun(t *testing.T) {
tests := []struct {
name string
tasks models.Tasks
taskName string
expectedCmds []string
expectedError bool
name string
runtime string
tasks models.Tasks
taskName string
expectedCmds []string
expectedRunError bool
expectedParseError bool
}{
{
name: "given an invalid task should not run command",
name: "given an invalid task should not run command",
runtime: "darwin",
tasks: []models.Task{
{
Name: "mytask",
},
},
taskName: "fake",
expectedCmds: []string{},
expectedError: true,
taskName: "fake",
expectedCmds: []string{},
expectedRunError: true,
},
{
name: "given a valid command should run",
name: "given a valid command should run",
runtime: "darwin",
tasks: []models.Task{
{
Name: "mytask",
Expand All @@ -43,7 +48,25 @@ func TestRun(t *testing.T) {
expectedCmds: []string{`bash -c echo "somecmd";somecmd`},
},
{
name: "given a valid command with dep should run",
name: "given a circular task should not run",
runtime: "darwin",
tasks: []models.Task{
{
Name: "mytask",
DependsOn: []string{"mytask2"},
},
{
Name: "mytask2",
DependsOn: []string{"mytask"},
},
},
taskName: "mytask",
expectedCmds: []string{},
expectedParseError: true,
},
{
name: "given a valid command with dep should run",
runtime: "darwin",
tasks: []models.Task{
{
Name: "mytask",
Expand All @@ -62,25 +85,50 @@ func TestRun(t *testing.T) {
taskName: "mytask2",
expectedCmds: []string{`bash -c echo "somecmd";somecmd`, `bash -c echo "somecmd2";somecmd2`},
},
{
name: "given a valid command with dep should run on windows",
runtime: "windows",
tasks: []models.Task{
{
Name: "mytask",
Commands: []string{
"somecmd",
},
},
{
Name: "mytask2",
Commands: []string{
"somecmd2",
},
DependsOn: []string{"mytask"},
},
},
taskName: "mytask2",
expectedCmds: []string{`cmd /C echo "somecmd"&&somecmd`, `cmd /C echo "somecmd2"&&somecmd2`},
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
cmds := []string{}
runner, err := NewRunner(tt.tasks)
runner, err := NewRunner(tt.tasks, tt.runtime)
fmt.Println("results", err, tt.expectedParseError)
if (err != nil) != tt.expectedParseError {
t.Fatalf("expected error %v, got %v", tt.expectedParseError, err)
}
if err != nil {
t.Fatal(err)
return
}
runner.runner = func(c *exec.Cmd) error {
cmds = append(cmds, strings.Join(c.Args, " "))
return nil
}
err = runner.Run(context.Background(), tt.taskName)
if (err != nil) != tt.expectedError {
t.Errorf("expected error %v, got %v", tt.expectedError, err)
if (err != nil) != tt.expectedRunError {
t.Fatalf("expected error %v, got %v", tt.expectedRunError, err)
}
if diff := cmp.Diff(tt.expectedCmds, cmds); diff != "" {
t.Errorf("invalid commands received, %s", diff)
t.Fatalf("invalid commands received, %s", diff)
}
})
}
Expand Down

0 comments on commit da7aad7

Please sign in to comment.