func (c *TodoCommand) runStart() int { tsk, index := c.promptSelectTask() if index < 0 { return failure } // task.Start(tsk) is idempotent, and simply won't // do anything if the task is in progress, but we // want to indicate to the user if they are not // actually starting the task if task.InProgress(tsk) { c.UI.Warn("Task is already in progress") return success } task.Start(tsk) if err := c.DB.Save(tsk); err != nil { c.errorf("(subcommand start) Error: %s", err) return failure } c.UI.Info(fmt.Sprintf("Started '%s'", tsk.Name)) return success }
// TestTodoSuggest tests the `suggest` subcommand func TestTodoSuggest(t *testing.T) { ui, db, user, c := newMockTodoCommand(t) // load a task into the db tsk := newTestTask(t, db, user) tsk.Name = "SUGGESTED" if err := db.Save(tsk); err != nil { t.Fatal(err) } tagName := "random tag" tag.Task(tsk, tagName) ui.InputReader = bytes.NewBufferString("y\n") // yes, start the task t.Log("running: `elos todo suggest`") code := c.Run([]string{"suggest"}) t.Log("command 'start' terminated") errput := ui.ErrorWriter.String() output := ui.OutputWriter.String() t.Logf("Error output:\n %s", errput) t.Logf("Output:\n %s", output) // verify there were no errors if errput != "" { t.Fatalf("Expected no error output, got: %s", errput) } // verify success if code != success { t.Fatalf("Expected successful exit code along with empty error output.") } // verify output if !strings.Contains(output, "SUGGESTED") { t.Fatal("Expected output to containe 'SUGGESTED', the name of the only task") } if !strings.Contains(output, tagName) { t.Fatal("Expected output to contain the task's tag's name") } if !strings.Contains(strings.ToLower(output), "start") { t.Fatal("Should ask if we want to start the task") } t.Log("Reloading the task") if err := db.PopulateByID(tsk); err != nil { t.Fatal(err) } t.Logf("Task loaded:\n%+v", tsk) if !task.InProgress(tsk) { t.Fatal("The task should be in progress now, cause we indicated we wanted to start it") } }
// runStop runs the 'stop' command, which stops a task specified // by the user. func (c *TodoCommand) runStop() int { anyInProgress := false for _, t := range c.tasks { if task.InProgress(t) { anyInProgress = true break } } if !anyInProgress { c.UI.Output("No tasks in progress") return success } tsk, index := c.promptSelectTask(func(t *models.Task) bool { return task.InProgress(t) }) if index < 0 { return failure } // task.Stop(tsk) is idempotent, meaning it won't stop the task // if it is not in progress, but we want to indicate this condition // to the user. if !task.InProgress(tsk) { c.UI.Warn("Task is not in progress") return success } task.Stop(tsk) if err := c.DB.Save(tsk); err != nil { c.errorf("(subcommand stop) Error: %s", err) return failure } // Info, i.e., "You worked for 20m that time" c.UI.Info(fmt.Sprintf("You worked for %s that time", tsk.Stages[len(tsk.Stages)-1].Time().Sub(tsk.Stages[len(tsk.Stages)-2].Time()))) return success }
// runCurrent executes the "elos todo current" command. // // Current prints the tasks that are currently in progress func (c *TodoCommand) runCurrent() int { printedTask := false c.printTaskList(func(t *models.Task) bool { ok := task.InProgress(t) if ok { printedTask = true } return ok }) if !printedTask { c.UI.Output("You have no tasks in progress") } return success }
// TestTodoStartStop tests the `start` and `stop` subcommands func TestTodoStartStop(t *testing.T) { ui, db, user, c := newMockTodoCommand(t) // load a task into the db tsk := newTestTask(t, db, user) // load the input ui.InputReader = bytes.NewBuffer([]byte("0\n")) t.Log("running: `elos todo start`") code := c.Run([]string{"start"}) t.Log("command 'start' terminated") errput := ui.ErrorWriter.String() output := ui.OutputWriter.String() t.Logf("Error output:\n %s", errput) t.Logf("Output:\n %s", output) // verify there were no errors if errput != "" { t.Fatalf("Expected no error output, got: %s", errput) } // verify success if code != success { t.Fatalf("Expected successful exit code along with empty error output.") } // verify some of the output if !strings.Contains(output, "0)") { t.Fatalf("Output should have contained a 0) for listing tasks") } if !strings.Contains(output, "Which number?") { t.Fatalf("Output should have asked for a task number") } t.Log("Checking that the task was started") if err := db.PopulateByID(tsk); err != nil { t.Fatal(err) } t.Logf("Here's the task:\n%+v", tsk) if !task.InProgress(tsk) { t.Fatalf("Expected the task to in progress") } // get a fresh ui ui = new(cli.MockUi) c.UI = ui // load the input ui.InputReader = bytes.NewBuffer([]byte("0\n")) t.Log("running: `elos todo stop`") code = c.Run([]string{"stop"}) t.Log("command run terminated") errput = ui.ErrorWriter.String() output = ui.OutputWriter.String() t.Logf("Error output:\n %s", errput) t.Logf("Output:\n %s", output) // verify there were no errors if errput != "" { t.Fatalf("Expected no error output, got: %s", errput) } // verify success if code != success { t.Fatalf("Expected successful exit code along with empty error output.") } // verify some of the output if !strings.Contains(output, "0)") { t.Fatalf("Output should have contained a 0) for listing tasks") } if !strings.Contains(output, "Which number?") { t.Fatalf("Output should have asked for a task number") } t.Log("Checking that the task was stopped") if err := db.PopulateByID(tsk); err != nil { t.Fatal(err) } t.Logf("Here's the task:\n%+v", tsk) if task.InProgress(tsk) { t.Fatalf("Expected the task to _not_ in progress") } }