Author: Kevin Schoon [kevinschoon@gmail.com]
Hash: cbb4165766a8b373536f1855c5edfdc09d4ee287
Timestamp: Mon, 21 Sep 2020 14:20:44 +0000 (4 years ago)

+58 -4 +/-2 browse
refactor cmd interface for better testing
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.
1diff --git a/pkg/cmd/cmd.go b/pkg/cmd/cmd.go
2index 292e428..17bcfb9 100644
3--- a/pkg/cmd/cmd.go
4+++ b/pkg/cmd/cmd.go
5 @@ -232,15 +232,17 @@ func _config(config *pomo.Config) func(*cli.Cmd) {
6 }
7 }
8
9- func Run() {
10+ func New(config *pomo.Config) *cli.Cli {
11 app := cli.App("pomo", "Pomodoro CLI")
12 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."
13 app.Spec = "[OPTIONS]"
14 var (
15- config = &pomo.Config{}
16- path = app.StringOpt("p path", defaultConfigPath(), "path to the pomo config directory")
17+ path = app.StringOpt("p path", defaultConfigPath(), "path to the pomo config directory")
18 )
19 app.Before = func() {
20+ if config == nil {
21+ config = &pomo.Config{}
22+ }
23 maybe(pomo.LoadConfig(*path, config))
24 }
25 app.Version("v version", pomo.Version)
26 @@ -252,5 +254,7 @@ func Run() {
27 app.Command("list l", "list historical tasks", list(config))
28 app.Command("delete d", "delete a stored task", _delete(config))
29 app.Command("status st", "output the current status", _status(config))
30- app.Run(os.Args)
31+ return app
32 }
33+
34+ func Run() { New(nil).Run(os.Args) }
35 diff --git a/pkg/cmd/cmd_test.go b/pkg/cmd/cmd_test.go
36new file mode 100644
37index 0000000..b7b7436
38--- /dev/null
39+++ b/pkg/cmd/cmd_test.go
40 @@ -0,0 +1,50 @@
41+ package cmd
42+
43+ import (
44+ "database/sql"
45+ "fmt"
46+ "io/ioutil"
47+ "os"
48+ "path/filepath"
49+ "testing"
50+
51+ pomo "github.com/kevinschoon/pomo/pkg/internal"
52+ )
53+
54+ func checkErr(t *testing.T, err error) {
55+ if err != nil {
56+ t.Helper()
57+ t.Fatal(err)
58+ }
59+ }
60+
61+ func initTestConfig(t *testing.T) (*pomo.Store, *pomo.Config) {
62+ tmpPath, err := ioutil.TempDir(os.TempDir(), "pomo-test")
63+ checkErr(t, err)
64+ config := &pomo.Config{
65+ DateTimeFmt: "2006-01-02 15:04",
66+ BasePath: tmpPath,
67+ DBPath: filepath.Join(tmpPath, "pomo.db"),
68+ SocketPath: filepath.Join(tmpPath, "pomo.sock"),
69+ IconPath: filepath.Join(tmpPath, "icon.png"),
70+ }
71+ store, err := pomo.NewStore(config.DBPath)
72+ checkErr(t, err)
73+ checkErr(t, pomo.InitDB(store))
74+ return store, config
75+ }
76+
77+ func TestPomoCreate(t *testing.T) {
78+ store, config := initTestConfig(t)
79+ cmd := New(config)
80+ checkErr(t, cmd.Run([]string{"pomo", "create", "fuu"}))
81+ // verify the task was created
82+ store.With(func(tx *sql.Tx) error {
83+ task, err := store.ReadTask(tx, 1)
84+ checkErr(t, err)
85+ if task.Message != "fuu" {
86+ checkErr(t, fmt.Errorf("task should have message fuu, got %s", task.Message))
87+ }
88+ return nil
89+ })
90+ }