refactor cmd interface for better testing

This change allows for E2E blackbox testing of pomodoro commands
by adding a New(*pomo.Config) *Cli.cmd function to the cmd module.
In a subsequent PR we should refactor pomo.Config into a public
module.
This commit is contained in:
Kevin Schoon 2020-09-21 09:20:44 -05:00
parent dd872159f1
commit cbb4165766
2 changed files with 58 additions and 4 deletions

View File

@ -232,15 +232,17 @@ func _config(config *pomo.Config) func(*cli.Cmd) {
} }
} }
func Run() { func New(config *pomo.Config) *cli.Cli {
app := cli.App("pomo", "Pomodoro CLI") app := cli.App("pomo", "Pomodoro CLI")
app.LongDesc = "Pomo helps you track what you did, how long it took you to do it, and how much effort you expect it to take." app.LongDesc = "Pomo helps you track what you did, how long it took you to do it, and how much effort you expect it to take."
app.Spec = "[OPTIONS]" app.Spec = "[OPTIONS]"
var ( var (
config = &pomo.Config{} path = app.StringOpt("p path", defaultConfigPath(), "path to the pomo config directory")
path = app.StringOpt("p path", defaultConfigPath(), "path to the pomo config directory")
) )
app.Before = func() { app.Before = func() {
if config == nil {
config = &pomo.Config{}
}
maybe(pomo.LoadConfig(*path, config)) maybe(pomo.LoadConfig(*path, config))
} }
app.Version("v version", pomo.Version) app.Version("v version", pomo.Version)
@ -252,5 +254,7 @@ func Run() {
app.Command("list l", "list historical tasks", list(config)) app.Command("list l", "list historical tasks", list(config))
app.Command("delete d", "delete a stored task", _delete(config)) app.Command("delete d", "delete a stored task", _delete(config))
app.Command("status st", "output the current status", _status(config)) app.Command("status st", "output the current status", _status(config))
app.Run(os.Args) return app
} }
func Run() { New(nil).Run(os.Args) }

50
pkg/cmd/cmd_test.go Normal file
View File

@ -0,0 +1,50 @@
package cmd
import (
"database/sql"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"
pomo "github.com/kevinschoon/pomo/pkg/internal"
)
func checkErr(t *testing.T, err error) {
if err != nil {
t.Helper()
t.Fatal(err)
}
}
func initTestConfig(t *testing.T) (*pomo.Store, *pomo.Config) {
tmpPath, err := ioutil.TempDir(os.TempDir(), "pomo-test")
checkErr(t, err)
config := &pomo.Config{
DateTimeFmt: "2006-01-02 15:04",
BasePath: tmpPath,
DBPath: filepath.Join(tmpPath, "pomo.db"),
SocketPath: filepath.Join(tmpPath, "pomo.sock"),
IconPath: filepath.Join(tmpPath, "icon.png"),
}
store, err := pomo.NewStore(config.DBPath)
checkErr(t, err)
checkErr(t, pomo.InitDB(store))
return store, config
}
func TestPomoCreate(t *testing.T) {
store, config := initTestConfig(t)
cmd := New(config)
checkErr(t, cmd.Run([]string{"pomo", "create", "fuu"}))
// verify the task was created
store.With(func(tx *sql.Tx) error {
task, err := store.ReadTask(tx, 1)
checkErr(t, err)
if task.Message != "fuu" {
checkErr(t, fmt.Errorf("task should have message fuu, got %s", task.Message))
}
return nil
})
}