From cbb4165766a8b373536f1855c5edfdc09d4ee287 Mon Sep 17 00:00:00 2001 From: Kevin Schoon Date: Mon, 21 Sep 2020 09:20:44 -0500 Subject: [PATCH] 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. --- pkg/cmd/cmd.go | 12 +++++++---- pkg/cmd/cmd_test.go | 50 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 4 deletions(-) create mode 100644 pkg/cmd/cmd_test.go diff --git a/pkg/cmd/cmd.go b/pkg/cmd/cmd.go index 292e428..17bcfb9 100644 --- a/pkg/cmd/cmd.go +++ b/pkg/cmd/cmd.go @@ -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.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]" 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() { + if config == nil { + config = &pomo.Config{} + } maybe(pomo.LoadConfig(*path, config)) } app.Version("v version", pomo.Version) @@ -252,5 +254,7 @@ func Run() { app.Command("list l", "list historical tasks", list(config)) app.Command("delete d", "delete a stored task", _delete(config)) app.Command("status st", "output the current status", _status(config)) - app.Run(os.Args) + return app } + +func Run() { New(nil).Run(os.Args) } diff --git a/pkg/cmd/cmd_test.go b/pkg/cmd/cmd_test.go new file mode 100644 index 0000000..b7b7436 --- /dev/null +++ b/pkg/cmd/cmd_test.go @@ -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 + }) +}