Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: Test

on:
push:
branches: [ main ]
pull_request:
branches: [ main ]

jobs:
test:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest, macos-latest]
go-version: [1.21]

steps:
- uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: ${{ matrix.go-version }}

- name: Run tests
run: make test
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
dist/
.bats/
pomodoro
23 changes: 23 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
BATS_VERSION := v1.11.0
BATS_DIR := .bats
BATS := $(BATS_DIR)/bin/bats
BINARY := pomodoro

GO_SOURCES := $(shell find . -name '*.go' -not -path './vendor/*')

.PHONY: test
test: $(BINARY) $(BATS)
$(BATS) test/

$(BINARY): $(GO_SOURCES) go.mod go.sum
go build -o $(BINARY) .

$(BATS):
@mkdir -p $(BATS_DIR)
@echo "Downloading bats-core $(BATS_VERSION)..."
@curl -sSL https://github.com/bats-core/bats-core/archive/$(BATS_VERSION).tar.gz | tar xz -C $(BATS_DIR) --strip-components=1
@chmod +x $(BATS_DIR)/bin/bats

.PHONY: clean
clean:
rm -rf $(BATS_DIR) $(BINARY)
18 changes: 10 additions & 8 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,18 @@ func init() {

viper.AutomaticEnv()

var err error
RootCmd.PersistentPreRun = func(cmd *cobra.Command, args []string) {
var err error

client, err = openpomodoro.NewClient(directoryFlag)
if err != nil {
log.Fatalf("Could not create client: %v", err)
}
client, err = openpomodoro.NewClient(directoryFlag)
if err != nil {
log.Fatalf("Could not create client: %v", err)
}

settings, err = client.Settings()
if err != nil {
log.Fatalf("Could not retrieve settings: %v", err)
settings, err = client.Settings()
if err != nil {
log.Fatalf("Could not retrieve settings: %v", err)
}
}
}

Expand Down
9 changes: 6 additions & 3 deletions cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ func init() {
"time ago this Pomodoro started")

command.Flags().IntVarP(
&durationFlag, "duration", "d",
int(settings.DefaultPomodoroDuration.Minutes()),
&durationFlag, "duration", "d", 0,
"duration for this Pomodoro")

command.Flags().StringArrayVarP(
Expand All @@ -43,7 +42,11 @@ func startCmd(cmd *cobra.Command, args []string) error {

p := openpomodoro.NewPomodoro()
p.Description = description
p.Duration = time.Duration(durationFlag) * time.Minute
if durationFlag == 0 {
p.Duration = settings.DefaultPomodoroDuration
} else {
p.Duration = time.Duration(durationFlag) * time.Minute
}
p.StartTime = time.Now().Add(-agoFlag)
p.Tags = tagsFlag

Expand Down
47 changes: 47 additions & 0 deletions test/amend.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#!/usr/bin/env bats

load test_helper

@test "amend changes description of current pomodoro" {
pomodoro start "Original task"
run pomodoro amend "Amended task"
[ "$status" -eq 0 ]
assert_file_contains "current" "Amended task"
}

@test "amend adds tags to current pomodoro" {
pomodoro start "Task"
run pomodoro amend -t "work,urgent"
[ "$status" -eq 0 ]
assert_file_contains "current" "tags=work,urgent"
}

@test "amend changes duration of current pomodoro" {
pomodoro start "Task"
run pomodoro amend -d 45
[ "$status" -eq 0 ]
assert_file_contains "current" "duration=45"
}


@test "amend creates new current when no current exists" {
pomodoro start "Task" --ago 5m
pomodoro finish
run pomodoro amend "New task"
[ "$status" -eq 0 ]
assert_file_contains "current" "New task"
}

@test "amend outputs current pomodoro status" {
pomodoro start "Task"
run pomodoro amend "Amended task"
[ "$status" -eq 0 ]
[[ "$output" =~ "Amended task" ]]
}

@test "amend with no arguments succeeds" {
pomodoro start "Task"
run pomodoro amend
[ "$status" -eq 0 ]
assert_file_contains "current" "Task"
}
26 changes: 26 additions & 0 deletions test/break.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/usr/bin/env bats

load test_helper

@test "break executes break hook before starting timer" {
create_hook "break" 'echo "BREAK_HOOK" >> "$TEST_DIR/hook_log"; exit 1'

run pomodoro break
[ "$status" -ne 0 ]

assert_hook_contains "BREAK_HOOK"
}

@test "break with custom duration parses correctly" {
create_hook "break" 'echo "BREAK_STARTED" >> "$TEST_DIR/hook_log"; exit 1'

run pomodoro break "10"
[ "$status" -ne 0 ]

assert_hook_contains "BREAK_STARTED"
}

@test "break with invalid duration fails" {
run pomodoro break "invalid"
[ "$status" -ne 0 ]
}
35 changes: 35 additions & 0 deletions test/cancel.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/usr/bin/env bats

load test_helper

@test "cancel empties current file and removes from history" {
pomodoro start "Task to cancel"
run pomodoro cancel
[ "$status" -eq 0 ]
assert_file_empty "current"
assert_file_empty "history"
}

@test "cancel preserves existing history but removes current" {
pomodoro start "First task" --ago 5m
pomodoro finish
pomodoro start "Task to cancel"
run pomodoro cancel
[ "$status" -eq 0 ]

assert_file_contains "history" "First task"
assert_file_empty "current"
}

@test "cancel with no current pomodoro succeeds" {
run pomodoro cancel
[ "$status" -eq 0 ]
assert_file_empty "current"
}

@test "cancel produces no output" {
pomodoro start "Test task"
run pomodoro cancel
[ "$status" -eq 0 ]
[ -z "$output" ]
}
27 changes: 27 additions & 0 deletions test/clear.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/usr/bin/env bats

load test_helper

@test "clear empties current file and does not affect history" {
pomodoro start "First task" --ago 5m
pomodoro finish
pomodoro start "Second task"
run pomodoro clear
[ "$status" -eq 0 ]

assert_file_contains "history" "First task"
assert_file_empty "current"
}

@test "clear with no current pomodoro succeeds" {
run pomodoro clear
[ "$status" -eq 0 ]
assert_file_empty "current"
}

@test "clear produces no output" {
pomodoro start "Test task"
run pomodoro clear
[ "$status" -eq 0 ]
[ -z "$output" ]
}
45 changes: 45 additions & 0 deletions test/finish.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/usr/bin/env bats

load test_helper

@test "finish moves current pomodoro to history" {
pomodoro start "Work session"
run pomodoro finish
[ "$status" -eq 0 ]

assert_file_empty "current"
assert_file_contains "history" "Work session"
}

@test "finish records actual elapsed time in history" {
pomodoro start "Work session" -d 30 --ago 10m
run pomodoro finish
[ "$status" -eq 0 ]

assert_file_contains "history" "Work session"
assert_file_contains "history" "duration=10"
}

@test "finish appends to existing history" {
pomodoro start "First task" --ago 5m
pomodoro finish
pomodoro start "Second task" --ago 3m
run pomodoro finish
[ "$status" -eq 0 ]

assert_file_contains "history" "First task"
assert_file_contains "history" "Second task"
}

@test "finish outputs elapsed time" {
pomodoro start "Test task" --ago 5m
run pomodoro finish
[ "$status" -eq 0 ]
[[ "$output" =~ "5:" ]]
}

@test "finish with no current pomodoro succeeds" {
run pomodoro finish
[ "$status" -eq 0 ]
assert_file_empty "current"
}
59 changes: 59 additions & 0 deletions test/history.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#!/usr/bin/env bats

load test_helper

@test "history shows nothing when no history exists" {
run pomodoro history
[ "$status" -eq 0 ]
[ -z "$output" ]
}

@test "history shows completed pomodoros" {
pomodoro start "First task" --ago 10m
pomodoro finish
pomodoro start "Second task" --ago 5m
pomodoro finish

run pomodoro history
[ "$status" -eq 0 ]
[[ "$output" =~ "First task" ]]
[[ "$output" =~ "Second task" ]]
}

@test "history limit flag restricts output" {
pomodoro start "Task 1" --ago 15m
pomodoro finish
pomodoro start "Task 2" --ago 10m
pomodoro finish
pomodoro start "Task 3" --ago 5m
pomodoro finish

run pomodoro history --limit 2
[ "$status" -eq 0 ]
[[ "$output" =~ "Task 2" ]]
[[ "$output" =~ "Task 3" ]]
[[ ! "$output" =~ "Task 1" ]]
}

@test "history shows timestamps and durations" {
pomodoro start "Test task" --ago 10m
pomodoro finish

run pomodoro history
[ "$status" -eq 0 ]
[[ "$output" =~ "Test task" ]]
[[ "$output" =~ "$(date '+%Y-%m-%d')" ]]
[[ "$output" =~ "duration=10" ]]
}

@test "history with zero limit shows all entries" {
pomodoro start "Task 1" --ago 10m
pomodoro finish
pomodoro start "Task 2" --ago 5m
pomodoro finish

run pomodoro history --limit 0
[ "$status" -eq 0 ]
[[ "$output" =~ "Task 1" ]]
[[ "$output" =~ "Task 2" ]]
}
Loading