Skip to content

Commit

Permalink
Added tests for task/parms
Browse files Browse the repository at this point in the history
  • Loading branch information
kcmerrill committed Mar 4, 2017
1 parent 9a53ac5 commit 01a8165
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 51 deletions.
81 changes: 42 additions & 39 deletions alfred/alfred.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type Alfred struct {
// Variables
Vars map[string]string `yaml:"alfred.vars"`
// All of the tasks parsed from the yaml file
Tasks map[string]*Task `yaml:",inline"`
Tasks map[string]Task `yaml:",inline"`
// Alfred remotes(private/public repos)
remote *remote.Remote
// Originating directory
Expand All @@ -55,8 +55,6 @@ func New(args []string) {
// Grab the current directory and save if off
a.dir, _ = os.Getwd()

fmt.Println(args)

// Set our Arguments
a.args = args

Expand All @@ -70,7 +68,7 @@ func New(args []string) {
if !a.findTask() {
a.args = append(a.args[:1], append([]string{"default"}, a.args[1:]...)...)
if !a.findTask() {
say("ERROR", "Invalid taskkkkkkk.")
say("ERROR", "Invalid task.")
os.Exit(1)
}
}
Expand Down Expand Up @@ -103,7 +101,8 @@ func (a *Alfred) findTask() bool {
break
// Called a local task
case len(a.args) >= 2 && !a.isRemote():
if a.isValidTask(a.args[1]) && !a.Tasks[a.args[1]].IsPrivate() {
task := a.Tasks[a.args[1]]
if a.isValidTask(a.args[1]) && !task.IsPrivate() {
if !a.runTask(a.args[1], a.args[2:], false) {
return false
}
Expand All @@ -113,12 +112,13 @@ func (a *Alfred) findTask() bool {
break
// Called a remote task
case len(a.args) >= 3 && a.isRemote():
if a.isValidTask(a.args[2]) && !a.Tasks[a.args[2]].IsPrivate() {
task := a.Tasks[a.args[2]]
if a.isValidTask(a.args[2]) && !task.IsPrivate() {
if !a.runTask(a.args[2], a.args[3:], false) {
return false
}
} else {
say(a.args[2], "Invalid taskk.")
say(a.args[2], "Invalid task")
return false
}
break
Expand All @@ -130,14 +130,16 @@ func (a *Alfred) findTask() bool {
func (a *Alfred) runTask(task string, args []string, formatted bool) bool {
// Verify again it's a valid task
if !a.isValidTask(task) {
say(task, "Invalid task."+task+"--")
say(task, "Invalid task.")
return false
}

copyOfTask := a.Tasks[task]

// Infinite loop Used for the every command
for {
// Run our setup tasks
for _, taskDefinition := range a.Tasks[task].TaskGroup(a.Tasks[task].Setup, args) {
for _, taskDefinition := range copyOfTask.TaskGroup(copyOfTask.Setup, args) {
if !a.runTask(taskDefinition.Name, taskDefinition.Params, formatted) {
break
}
Expand All @@ -152,30 +154,31 @@ func (a *Alfred) runTask(task string, args []string, formatted bool) bool {
}

// Lets prep it, and if it's bunk, lets see if we can pump out it's usage
if !a.Tasks[task].Prepare(args, a.Vars) {
if !copyOfTask.Prepare(args, a.Vars) {
say(task+":error", "Missing argument(s).")
return false
// No need in going on, programmer error
os.Exit(1)
}

// Lets change the directory if set
if a.Tasks[task].Dir != "" {
if err := os.Chdir(a.Tasks[task].Dir); err != nil {
if err := os.MkdirAll(a.Tasks[task].Dir, 0755); err != nil {
if copyOfTask.Dir != "" {
if err := os.Chdir(copyOfTask.Dir); err != nil {
if err := os.MkdirAll(copyOfTask.Dir, 0755); err != nil {
say(task+":dir", "Invalid directory")
return false
}
os.Chdir(a.Tasks[task].Dir)
os.Chdir(copyOfTask.Dir)
}
}

// We watching for files?
if a.Tasks[task].Watch != "" {
if copyOfTask.Watch != "" {
// Regardless of what's going on, lets set every to 1s
a.Tasks[task].Every = "1s"
copyOfTask.Every = "1s"
for {
matched := filepath.Walk(".", func(path string, f os.FileInfo, err error) error {
if f.ModTime().After(time.Now().Add(-2 * time.Second)) {
m, _ := regexp.Match(a.Tasks[task].Watch, []byte(path))
m, _ := regexp.Match(copyOfTask.Watch, []byte(path))
if m {
// If not a match ...
return errors.New("no matches")
Expand All @@ -193,25 +196,25 @@ func (a *Alfred) runTask(task string, args []string, formatted bool) bool {

// Go through each of the modules ...
// before command, docker stop for example
for module, cmd := range a.Tasks[task].Modules {
if !a.Tasks[task].RunCommand(args[0]+" "+a.remote.ModulePath(module)+" "+cmd, task, formatted) {
for module, cmd := range copyOfTask.Modules {
if !copyOfTask.RunCommand(args[0]+" "+a.remote.ModulePath(module)+" "+cmd, task, formatted) {
// It failed :(
taskok = false
break
}
}

// First, lets show the summary
if a.Tasks[task].Summary != "" {
if copyOfTask.Summary != "" {
fmt.Println("")
say(task, a.Tasks[task].Summary)
say(task, copyOfTask.Summary)
}

// Test ...
if a.Tasks[task].TestF(a.Tasks[task].Test) {
if copyOfTask.TestF(copyOfTask.Test) {
// Lets execute the command if it has one, and add retry logic
for x := 0; x < a.Tasks[task].Retry || x == 0; x++ {
taskok = a.Tasks[task].RunCommand(a.Tasks[task].Command, task, formatted)
for x := 0; x < copyOfTask.Retry || x == 0; x++ {
taskok = copyOfTask.RunCommand(copyOfTask.Command, task, formatted)
if taskok {
break
}
Expand All @@ -223,22 +226,22 @@ func (a *Alfred) runTask(task string, args []string, formatted bool) bool {

// Commands, not to be misaken for command
if taskok {
cmds := strings.Split(a.Tasks[task].Commands, "\n")
cmds := strings.Split(copyOfTask.Commands, "\n")
for _, c := range cmds {
taskok = a.Tasks[task].RunCommand(c, task, formatted)
taskok = copyOfTask.RunCommand(c, task, formatted)
if !taskok {
break
}
}
}

// Handle Serve ...
if taskok && a.Tasks[task].Serve != "" {
go Serve(".", a.Tasks[task].Serve)
if taskok && copyOfTask.Serve != "" {
go Serve(".", copyOfTask.Serve)
}

// Wait ...
if waitDuration, waitError := time.ParseDuration(a.Tasks[task].Wait); waitError == nil {
if waitDuration, waitError := time.ParseDuration(copyOfTask.Wait); waitError == nil {
<-time.After(waitDuration)
}

Expand All @@ -249,7 +252,7 @@ func (a *Alfred) runTask(task string, args []string, formatted bool) bool {
fmt.Println(red("✘"), task)

// Failed? Lets run the failed tasks
for _, taskDefinition := range a.Tasks[task].TaskGroup(a.Tasks[task].Fail, args) {
for _, taskDefinition := range copyOfTask.TaskGroup(copyOfTask.Fail, args) {
if !a.runTask(taskDefinition.Name, taskDefinition.Params, formatted) {
break
}
Expand All @@ -261,21 +264,21 @@ func (a *Alfred) runTask(task string, args []string, formatted bool) bool {
}

// Handle skips ...
if !taskok && a.Tasks[task].Skip {
if !taskok && copyOfTask.Skip {
return false
}

// Handle exits ...
if !taskok && a.Tasks[task].Exit != "" {
if exitCode, err := strconv.Atoi(a.Tasks[task].Exit); err == nil {
if !taskok && copyOfTask.Exit != "" {
if exitCode, err := strconv.Atoi(copyOfTask.Exit); err == nil {
os.Exit(exitCode)
}
return false
}

var wg sync.WaitGroup
// Do we have any tasks we need to run in parallel?
for _, taskDefinition := range a.Tasks[task].TaskGroup(a.Tasks[task].Multitask, args) {
for _, taskDefinition := range copyOfTask.TaskGroup(copyOfTask.Multitask, args) {
wg.Add(1)
go func(t string, args []string) {
defer wg.Done()
Expand All @@ -285,15 +288,15 @@ func (a *Alfred) runTask(task string, args []string, formatted bool) bool {
wg.Wait()

// Ok, we made it here ... Is this task a task group?
for _, taskDefinition := range a.Tasks[task].TaskGroup(a.Tasks[task].Tasks, args) {
for _, taskDefinition := range copyOfTask.TaskGroup(copyOfTask.Tasks, args) {
if !a.runTask(taskDefinition.Name, taskDefinition.Params, formatted) {
break
}
}

// Woot! Lets run the ok tasks
if taskok {
for _, taskDefinition := range a.Tasks[task].TaskGroup(a.Tasks[task].Ok, args) {
for _, taskDefinition := range copyOfTask.TaskGroup(copyOfTask.Ok, args) {
if !a.runTask(taskDefinition.Name, taskDefinition.Params, formatted) {
break
}
Expand All @@ -302,8 +305,8 @@ func (a *Alfred) runTask(task string, args []string, formatted bool) bool {
}

// Do we need to break or should we keep going?
if a.Tasks[task].Every != "" {
if everyDuration, everyErr := time.ParseDuration(a.Tasks[task].Every); everyErr == nil {
if copyOfTask.Every != "" {
if everyDuration, everyErr := time.ParseDuration(copyOfTask.Every); everyErr == nil {
<-time.After(everyDuration)
} else {
break
Expand Down
38 changes: 26 additions & 12 deletions alfred_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,8 @@ func TestAlias(t *testing.T) {
}

/* Make sure the ls command ran */
if !strings.Contains(sut, "alfred.yml") {
t.Logf("ls should contain alfred.yml")
if !strings.Contains(sut, "emptydir") {
t.Logf("ls should contain emptydir")
t.FailNow()
}

Expand Down Expand Up @@ -255,7 +255,7 @@ func TestArgumentsOkAndDefaults(t *testing.T) {
t.FailNow()
}
/* Verfify our command had run succesfully */
if !strings.Contains(sut, ".alfred") {
if !strings.Contains(sut, "emptydir") {
t.Logf("Listing of the current directory should show an alfred.yml file")
t.FailNow()
}
Expand All @@ -281,11 +281,9 @@ func TestRetryLogic(t *testing.T) {
sut, _ := run("alfred twentyseven", t)

/* Verfify our retry logic via error message */
if !strings.Contains(sut, "[twentyseven] Retry logic. Try X times before continuing on\nls: /step27-idonotexist: No such file or directory\nls: /step27-idonotexist: No such file or directory\nls: /step27-idonotexist: No such file or directory") {
if !strings.Contains(sut, "[twentyseven] Retry logic. Try X times before continuing on\nls: cannot access /step27-idonotexist: No such file or directory\nls: cannot access /step27-idonotexist: No such file or directory\nls: cannot access /step27-idonotexist: No such file or directory") {
t.Logf("Expected Retry logic 3 times")
t.FailNow()
}
if strings.Count(sut, "/step27-idonotexist") != 3 {
t.Logf("Expected Retry logic 3 times")
t.FailNow()
}
}

Expand All @@ -311,13 +309,29 @@ func TestCleanedArgs(t *testing.T) {
}
}

func TestExample(t *testing.T) {
sut, _ := run("alfred", t)
if len(sut) >= 0 {
func TestTaskMultiArguments(t *testing.T) {
sut, _ := run("alfred thirty.eight", t)

if !strings.Contains(sut, "-kc-merrill-") {
t.Logf("Expecting -kc-merrill- in command output")
t.FailNow()
}

if !strings.Contains(sut, "-bruce-wayne-") {
t.Logf("Expecting -bruce-wayne- in command output")
t.FailNow()
}

if len(sut) >= 0 {
if !strings.Contains(sut, "-clark-kent-") {
t.Logf("Expecting -clark-kent- in command output")
t.FailNow()
}
}

func TestExample(t *testing.T) {
sut, _ := run("alfred", t)
if len(sut) <= 0 {
t.Logf("Test example failed ... meaning no data :(")
t.FailNow()
}
}

0 comments on commit 01a8165

Please sign in to comment.