forked from kawsark/pomo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpomo_test.bats
228 lines (197 loc) · 5.77 KB
/
pomo_test.bats
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#!/usr/bin/env bats
function setup() {
POMO_DIR=$(mktemp --directory --tmpdir="$BATS_RUN_TMPDIR")
export POMO_DIR
export POMO_FILE=$POMO_DIR/pomo
export POMO_CONFIG=$POMO_DIR/pomo.cfg
source pomo.sh
}
# Test helpers
function _pomo_clock_helper() {
# Mock stat so no timing issue around checking the time of POMO_FILE.
started_at=$1
function pomo_stat() { echo "$started_at"; }
export -f pomo_stat
run pomo_clock
}
function _pomo_msg_helper() {
expected_duration=$1
# duplicate first fake stat time as pomo_msg calls pomo_update once at initialisation.
stat_times=( "$2" "${@:2}" )
# Set iteration in a file as pomo_msg is run by BATS in a subshell.
iteration_file=$(mktemp --tmpdir="$BATS_RUN_TMPDIR")
echo 0 > "$iteration_file"
function pomo_stat() {
stat_iteration=$(cat "$iteration_file")
echo "${stat_times[$stat_iteration]}"
echo $((stat_iteration+1)) > "$iteration_file"
}
run pomo_start
SECONDS=0
run pomo_msg
[[ $expected_duration -eq $SECONDS ]]
}
# Mocks
function notify-send() {
# Mock notify-send to echo.
# We set the app-name to notify send using -a, which is not a valid option
# for echo. Remove the flag and pass everything (app name and message) to
# echo.
shift
echo "$@"
}
@test "pomo_start creates file" {
[[ ! -e $POMO_FILE ]]
run pomo_start
[[ -e $POMO_FILE ]]
}
@test "pomo_stop removes file" {
run ./pomo.sh start
[[ -e $POMO_FILE ]]
run pomo_stop
[[ ! -e $POMO_FILE ]]
}
@test "pomo_clock when stopped returns blank" {
run pomo_clock
[[ "$output" == " --:--" ]]
}
@test "pomo_ispaused is true when paused" {
run pomo_start
run pomo_pause
run pomo_ispaused
[[ "$status" -eq 0 ]]
}
@test "pomo_ispaused is false when started" {
run pomo_start
run pomo_ispaused
[[ "$status" -eq 1 ]]
}
@test "pomo_ispaused is false when stopped" {
run pomo_start
run pomo_stop
run pomo_ispaused
[[ "$status" -eq 1 ]]
}
@test "pomo_isstopped is true when stopped" {
run pomo_start
run pomo_stop
run pomo_isstopped
[[ "$status" -eq 0 ]]
}
@test "pomo_isstopped is true before first run of pomo" {
run pomo_isstopped
[[ "$status" -eq 0 ]]
}
@test "pomo_isstopped is false when started" {
run pomo_start
run pomo_isstopped
[[ "$status" -eq 1 ]]
}
@test "pomo_isstopped is false when paused" {
run pomo_start
run pomo_pause
run pomo_isstopped
[[ "$status" -eq 1 ]]
}
@test "pomo_clock when started shows time remaining in running block" {
run pomo_start
_pomo_clock_helper 52
[[ "$output" =~ " W24:08" ]]
}
@test "pomo_clock when started and paused shows time remaining in running block" {
run pomo_start
run pomo_pause
_pomo_clock_helper 52
[[ "$output" =~ "PW24:08" ]]
}
@test "pomo_clock when started shows time remaining in break block" {
run pomo_start
_pomo_clock_helper 1565
[[ "$output" =~ " B03:55" ]]
}
@test "pomo_clock when started and paused shows time remaining in break block" {
run pomo_start
run pomo_pause
_pomo_clock_helper 1565
[[ "$output" =~ "PB03:55" ]]
}
@test "pomo_set creates a timestamp" {
now=$(${DATE_CMD} +%s)
offset=33
run pomo_stamp $offset
run "${STAT_CMD}" -c %Y "$POMO"
[[ "$output" -eq $((now - offset)) ]]
}
@test "pomo_pause stops and starts the clock" {
run pomo_stamp 1200
run pomo_pause
sleep 5
run pomo_stat
[[ "$output" -eq 1200 ]]
run pomo_pause
sleep 5
run pomo_stat
[[ "$output" -eq 1205 ]]
}
@test "pomo_pause creates file if it was stopped before" {
run pomo_start
run pomo_stop
run pomo_pause
[[ -e $POMO_FILE ]]
}
@test "pomo_pause creates file before first run of pomo" {
[[ ! -e $POMO_FILE ]]
run pomo_pause
[[ -e $POMO_FILE ]]
}
@test "pomo_update does not update the POMO file if not required" {
run pomo_stamp 33
run "${STAT_CMD}" -c %Y "$POMO"
t1=$output
run pomo_update
run "${STAT_CMD}" -c %Y "$POMO"
t2=$output
[[ "$t1" -eq "$t2" ]]
}
@test "pomo_update updates the POMO file if required" {
block_time=$(( (WORK_TIME+BREAK_TIME)*60 ))
run pomo_stamp $(( block_time + 50 ))
run "${STAT_CMD}" -c %Y "$POMO"
t1=$output
run pomo_update
run "${STAT_CMD}" -c %Y "$POMO"
t2=$output
[[ "$(( t1 + block_time))" -eq "$t2" ]]
}
@test "message is sent using notify-send if found" {
test_msg="test message"
run send_msg "$test_msg"
[[ "$output" == "Pomodoro $test_msg" ]]
}
@test "message is sent using echo if notify-send not found" {
function command() { return 1; }
export -f command
test_msg="test message"
run send_msg "$test_msg"
[[ "$output" == "$test_msg" ]]
}
@test "pomo_msg sends message about end of work block" {
_pomo_msg_helper 5 $((WORK_TIME*60-5)) $((WORK_TIME*60))
[[ "$output" == "Pomodoro End of a work period. Time for a break!" ]]
}
@test "pomo_msg sends message about end of break block" {
_pomo_msg_helper 5 $(( (WORK_TIME+BREAK_TIME)*60-5)) $(( (WORK_TIME+BREAK_TIME)*60))
[[ "$output" == "Pomodoro End of a break period. Time for work!" ]]
}
@test "pomo_msg handles timestamp update before it can send the end of break message 1" {
_pomo_msg_helper 5 $(( (WORK_TIME+BREAK_TIME)*60-5)) 0
[[ "$output" == "Pomodoro End of a break period. Time for work!" ]]
}
@test "pomo_msg handles timestamp update before it can send the end of break message 2" {
_pomo_msg_helper 5 $(( (WORK_TIME+BREAK_TIME)*60-5)) 1
[[ "$output" == "Pomodoro End of a break period. Time for work!" ]]
}
@test "pomo_msg handles being paused" {
_pomo_msg_helper 7 $((WORK_TIME*60-5)) $((WORK_TIME*60-2)) $((WORK_TIME*60))
[[ "$output" == "Pomodoro End of a work period. Time for a break!" ]]
}