Skip to content

Commit 54855f3

Browse files
Add tests for finish --break and utility functions
- Add bats tests for --break flag with and without duration - Add Go unit tests for parseDurationMinutes using testify - Fix missing trailing newline in cmd/util.go - Add short flag -b for --break option - Use TrimSpace to properly handle NoOptDefVal 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 1e0ae65 commit 54855f3

File tree

4 files changed

+89
-4
lines changed

4 files changed

+89
-4
lines changed

cmd/finish.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package cmd
22

33
import (
44
"fmt"
5+
"strings"
56
"time"
67

78
"github.com/open-pomodoro/openpomodoro-cli/format"
@@ -18,7 +19,8 @@ func init() {
1819
RunE: finishCmd,
1920
}
2021

21-
command.Flags().StringVar(&breakFlag, "break", "", "take a break after finishing (duration in minutes)")
22+
command.Flags().StringVarP(&breakFlag, "break", "b", "", "take a break after finishing (duration in minutes)")
23+
command.Flags().Lookup("break").NoOptDefVal = " "
2224

2325
RootCmd.AddCommand(command)
2426
}
@@ -43,9 +45,10 @@ func finishCmd(cmd *cobra.Command, args []string) error {
4345
if cmd.Flags().Changed("break") {
4446
breakDuration := settings.DefaultBreakDuration
4547

46-
if breakFlag != "" {
48+
trimmedFlag := strings.TrimSpace(breakFlag)
49+
if trimmedFlag != "" {
4750
var err error
48-
breakDuration, err = parseDurationMinutes(breakFlag)
51+
breakDuration, err = parseDurationMinutes(trimmedFlag)
4952
if err != nil {
5053
return err
5154
}

cmd/util.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,4 @@ func parseDurationMinutes(s string) (time.Duration, error) {
2727
s = fmt.Sprintf("%sm", s)
2828
}
2929
return time.ParseDuration(s)
30-
}
30+
}

cmd/util_test.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package cmd
2+
3+
import (
4+
"testing"
5+
"time"
6+
7+
"github.com/stretchr/testify/assert"
8+
"github.com/stretchr/testify/require"
9+
)
10+
11+
func TestParseDurationMinutes(t *testing.T) {
12+
tests := []struct {
13+
name string
14+
input string
15+
expected time.Duration
16+
wantErr bool
17+
}{
18+
{
19+
name: "integer defaults to minutes",
20+
input: "5",
21+
expected: 5 * time.Minute,
22+
},
23+
{
24+
name: "explicit minutes",
25+
input: "10m",
26+
expected: 10 * time.Minute,
27+
},
28+
{
29+
name: "seconds",
30+
input: "30s",
31+
expected: 30 * time.Second,
32+
},
33+
{
34+
name: "hours",
35+
input: "1h",
36+
expected: 1 * time.Hour,
37+
},
38+
{
39+
name: "complex duration",
40+
input: "1h30m",
41+
expected: 90 * time.Minute,
42+
},
43+
{
44+
name: "invalid duration",
45+
input: "invalid",
46+
wantErr: true,
47+
},
48+
{
49+
name: "zero",
50+
input: "0",
51+
expected: 0,
52+
},
53+
}
54+
55+
for _, tt := range tests {
56+
t.Run(tt.name, func(t *testing.T) {
57+
got, err := parseDurationMinutes(tt.input)
58+
if tt.wantErr {
59+
require.Error(t, err)
60+
} else {
61+
require.NoError(t, err)
62+
assert.Equal(t, tt.expected, got)
63+
}
64+
})
65+
}
66+
}

test/finish.bats

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,19 @@ load test_helper
4343
[ "$status" -eq 0 ]
4444
assert_file_empty "current"
4545
}
46+
47+
@test "finish --break flag is accepted" {
48+
create_hook "break" 'exit 1'
49+
50+
pomodoro start "Task" --ago 1m
51+
run pomodoro finish --break
52+
[ "$status" -ne 0 ]
53+
}
54+
55+
@test "finish --break with custom duration is accepted" {
56+
create_hook "break" 'exit 1'
57+
58+
pomodoro start "Task" --ago 1m
59+
run pomodoro finish --break 5
60+
[ "$status" -ne 0 ]
61+
}

0 commit comments

Comments
 (0)