-
-
Notifications
You must be signed in to change notification settings - Fork 188
/
index.bats
532 lines (387 loc) · 12.8 KB
/
index.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
#!/usr/bin/env bats
load test_helper
# #############################################################################
@test "'index' builds an index if one doesn't exist." {
{
"${_NB}" init
rm "${NB_DIR}/home/.index"
[[ ! -e "${NB_DIR}/home/.index" ]]
}
run "${_NB}" index
printf "\${status}: '%s'\\n" "${status}"
printf "\${output}: '%s'\\n" "${output}"
[[ -e "${NB_DIR}/home/.index" ]]
}
# add #########################################################################
@test "'index add <filename>' adds an item to the index." {
{
"${_NB}" init
"${_NB}" add "first.md" --title "one"
echo "" > "${NB_DIR}/home/.index"
[[ ! "$(cat "${NB_DIR}/home/.index")" =~ ^first.md$ ]]
}
run "${_NB}" index add "$(ls "${NB_DIR}/home")"
printf "\${status}: '%s'\\n" "${status}"
printf "\${output}: '%s'\\n" "${output}"
printf "\$(cat \${NB_DIR}/home/.index): '%s'\\n" \
"$(cat "${NB_DIR}/home/.index")"
printf "\$(ls ${NB_DIR}/home): '%s'\\n" "$(ls "${NB_DIR}/home")"
[[ "$(cat "${NB_DIR}/home/.index")" =~ first.md$ ]]
}
@test "'index add' with no argument returns 1 and prints help." {
{
"${_NB}" init
"${_NB}" add "first.md" --title "one"
echo "" > "${NB_DIR}/home/.index"
[[ ! "$(cat "${NB_DIR}/home/.index")" =~ ^first.md$ ]]
}
run "${_NB}" index add
printf "\${status}: '%s'\\n" "${status}"
printf "\${output}: '%s'\\n" "${output}"
[[ "${status}" -eq 1 ]]
[[ "${lines[0]}" =~ Usage.*\: ]]
[[ "${lines[1]}" == " nb index add <filename>" ]]
}
@test "'index add <filename>' with non-file returns 1 and prints message." {
{
"${_NB}" init
"${_NB}" add "first.md" --title "one"
echo "" > "${NB_DIR}/home/.index"
[[ ! "$(cat "${NB_DIR}/home/.index")" =~ ^first.md$ ]]
}
run "${_NB}" index add 'not-a-file'
printf "\${status}: '%s'\\n" "${status}"
printf "\${output}: '%s'\\n" "${output}"
[[ "${status}" -eq 1 ]]
[[ "${lines[0]}" =~ File\ not\ found ]]
}
@test "'index add <filename>' with existing entry does nothing." {
{
"${_NB}" init
"${_NB}" add "first.md" --title "one"
[[ "$(cat "${NB_DIR}/home/.index")" =~ ^first.md$ ]]
}
run "${_NB}" index add "$(ls "${NB_DIR}/home")"
printf "\${status}: '%s'\\n" "${status}"
printf "\${output}: '%s'\\n" "${output}"
[[ "$(cat "${NB_DIR}/home/.index")" =~ ^first.md$ ]]
[[ -z "${lines[1]}" ]]
}
@test "'index add <filename>' skips temporary files." {
{
"${_NB}" init
declare _temp_filenames=(
"Example Temp One.md~"
"#Example Temp Two.md#"
"Example Temp Three.md.swp"
"Example Temp Four.md.swap"
".#Example Temp Five.md"
)
declare __filename=
for __filename in "${_temp_filenames[@]:-}"
do
"${_NB}" run touch "${__filename:-}"
done
}
declare __filename=
for __filename in "${_temp_filenames[@]:-}"
do
run "${_NB}" index add "${__filename:-}"
printf "\${status}: '%s'\\n" "${status}"
printf "\${output}: '%s'\\n" "${output}"
[[ "${status}" -eq 0 ]]
[[ -z "${output}" ]]
[[ -z "$(cat "${NB_DIR/home/.index}")" ]]
done
}
# get_basename ################################################################
@test "'index get_basename' with valid index prints the filename for an id." {
{
"${_NB}" init
"${_NB}" add "# one"
}
run "${_NB}" index get_basename 1
printf "\${status}: '%s'\\n" "${status}"
printf "\${output}: '%s'\\n" "${output}"
[[ "${status}" -eq 0 ]]
[[ "${lines[0]}" =~ 20[0-9]+\.md$ ]]
}
@test "'index get_basename' with invalid index prints nothing." {
{
"${_NB}" init
"${_NB}" add "first.md" --title "one"
}
run "${_NB}" index get_basename 12345
printf "\${status}: '%s'\\n" "${status}"
printf "\${output}: '%s'\\n" "${output}"
[[ "${status}" -eq 1 ]]
[[ -z "${output}" ]]
}
@test "'index get_basename' with no argument returns 1 and prints help." {
{
"${_NB}" init
"${_NB}" add "first.md" --title "one"
}
run "${_NB}" index get_basename
printf "\${status}: '%s'\\n" "${status}"
printf "\${output}: '%s'\\n" "${output}"
[[ "${status}" -eq 1 ]]
[[ "${lines[0]}" =~ Usage.*\: ]]
}
# get_id #########################################################################
@test "'index get_id <filename>' returns the id for <filename>." {
{
"${_NB}" init
"${_NB}" add "first.md" --title "one"
}
run "${_NB}" index get_id "$(ls "${NB_DIR}/home")"
printf "\${status}: '%s'\\n" "${status}"
printf "\${output}: '%s'\\n" "${output}"
[[ "${status}" -eq 0 ]]
[[ "${output}" == "1" ]]
}
@test "'index get_id' with no argument returns 1 and prints help." {
{
"${_NB}" init
"${_NB}" add "first.md" --title "one"
}
run "${_NB}" index get_id
printf "\${status}: '%s'\\n" "${status}"
printf "\${output}: '%s'\\n" "${output}"
[[ "${status}" -eq 1 ]]
[[ "${lines[0]}" =~ Usage.*\: ]]
}
@test "'index get_id <filename>' with non-entry returns 1." {
{
"${_NB}" init
"${_NB}" add "first.md" --title "one"
}
run "${_NB}" index get_id 'not-an-entry'
printf "\${status}: '%s'\\n" "${status}"
printf "\${output}: '%s'\\n" "${output}"
[[ "${status}" -eq 1 ]]
[[ -z "${output}" ]]
}
# get_max_id ##################################################################
@test "'index get_max_id' returns the max id number." {
{
"${_NB}" init
"${_NB}" add "first.md" --title "one"
"${_NB}" add "second.md" --title "two"
"${_NB}" add "third.md" --title "three"
}
run "${_NB}" index get_max_id
printf "\${status}: '%s'\\n" "${status}"
printf "\${output}: '%s'\\n" "${output}"
[[ "${status}" -eq 0 ]]
[[ "${lines[0]}" == "3" ]]
}
@test "'index get_max_id' with empty notebook returns 0." {
{
"${_NB}" init
}
run "${_NB}" index get_max_id
printf "\${status}: '%s'\\n" "${status}"
printf "\${output}: '%s'\\n" "${output}"
[[ "${status}" -eq 0 ]]
[[ "${lines[0]}" == "0" ]]
}
# delete ######################################################################
@test "'index delete <filename>' deletes an item from the index." {
{
"${_NB}" init
"${_NB}" add "first.md" --title "one"
}
run "${_NB}" index delete "$(ls "${NB_DIR}/home")"
printf "\${status}: '%s'\\n" "${status}"
printf "\${output}: '%s'\\n" "${output}"
[[ "${status}" -eq 0 ]]
[[ "$(cat "${NB_DIR}/home/.index")" == "" ]]
}
@test "'index delete <filename>' with brackets and spaces in filename deletes an item from the index." {
{
"${_NB}" init
"${_NB}" add "[ ] first.md" --title "one"
}
run "${_NB}" index delete "$(ls "${NB_DIR}/home")"
printf "\${status}: '%s'\\n" "${status}"
printf "\${output}: '%s'\\n" "${output}"
[[ "${status}" -eq 0 ]]
[[ "$(cat "${NB_DIR}/home/.index")" == "" ]]
}
@test "'index delete' with no argument returns 1 and prints help." {
{
"${_NB}" init
"${_NB}" add "first.md" --title "one"
}
run "${_NB}" index delete
printf "\${status}: '%s'\\n" "${status}"
printf "\${output}: '%s'\\n" "${output}"
[[ "${status}" -eq 1 ]]
[[ "${lines[0]}" =~ Usage.*\: ]]
}
@test "'index delete <filename>' with non-file returns 1 and prints nothing." {
{
"${_NB}" init
"${_NB}" add "first.md" --title "one"
}
run "${_NB}" index delete 'not-a-file'
printf "\${status}: '%s'\\n" "${status}"
printf "\${output}: '%s'\\n" "${output}"
[[ "${status}" -eq 1 ]]
[[ -z "${output}" ]]
}
# show ########################################################################
@test "'index show' prints index." {
{
"${_NB}" init
"${_NB}" add "# one"
}
run "${_NB}" index show
printf "\${status}: '%s'\\n" "${status}"
printf "\${output}: '%s'\\n" "${output}"
[[ "${status}" -eq 0 ]]
[[ "${output}" == "$(cat "${NB_DIR}/home/.index")" ]]
}
# update ######################################################################
@test "'index update <old> <new>' updates the index." {
{
"${_NB}" init
"${_NB}" add "# one"
}
run "${_NB}" index update "$(ls "${NB_DIR}/home")" "example.md"
printf "\${status}: '%s'\\n" "${status}"
printf "\${output}: '%s'\\n" "${output}"
printf \
"$(cat \"\$\{NB_DIR\}/home/.index\"): '%s'\\n" \
"$(cat "${NB_DIR}/home/.index")"
[[ "${status}" -eq 0 ]]
[[ "$(cat "${NB_DIR}/home/.index")" == "example.md" ]]
}
@test "'index update <old> <new>' with brackets and spaces in <old> filename updates the index." {
{
"${_NB}" init
"${_NB}" add "[ ] first.md" --title "# one"
}
run "${_NB}" index update "$(ls "${NB_DIR}/home")" "example.md"
printf "\${status}: '%s'\\n" "${status}"
printf "\${output}: '%s'\\n" "${output}"
printf \
"$(cat \"\$\{NB_DIR\}/home/.index\"): '%s'\\n" \
"$(cat "${NB_DIR}/home/.index")"
[[ "${status}" -eq 0 ]]
[[ "$(cat "${NB_DIR}/home/.index")" == "example.md" ]]
}
@test "'index update' with no arguments returns 1 and prints help." {
{
"${_NB}" init
"${_NB}" add "# one"
}
run "${_NB}" index update
printf "\${status}: '%s'\\n" "${status}"
printf "\${output}: '%s'\\n" "${output}"
[[ "${status}" -eq 1 ]]
[[ "${lines[0]}" =~ Usage.*\: ]]
}
@test "'index update' with first argument returns 1 and prints help." {
{
"${_NB}" init
"${_NB}" add "# one"
}
run "${_NB}" index update "$(ls "${NB_DIR}/home")"
printf "\${status}: '%s'\\n" "${status}"
printf "\${output}: '%s'\\n" "${output}"
[[ "${status}" -eq 1 ]]
[[ "${lines[0]}" =~ Usage.*\: ]]
}
# verify ######################################################################
@test "'index verify' verifies a valid index." {
{
"${_NB}" init
"${_NB}" add "first.md" --title "one"
"${_NB}" add "second.md" --title "two"
printf \
"\"\$(cat \"\${NB_DIR}/home/.index\")\": '%s'\\n" \
"$(cat "${NB_DIR}/home/.index")"
printf "\$(ls \${NB_DIR}/home): '%s'\\n" "$(ls "${NB_DIR}/home")"
[[ "$(cat "${NB_DIR}/home/.index")" == "$(ls "${NB_DIR}/home")" ]]
}
run "${_NB}" index verify
printf "\${status}: '%s'\\n" "${status}"
printf "\${output}: '%s'\\n" "${output}"
[[ "${status}" -eq 0 ]]
[[ -z "${output}" ]]
}
@test "'index verify' returns 1 with an invalid index." {
{
"${_NB}" init
"${_NB}" add "first.md" --title "one"
"${_NB}" add "second.md" --title "two"
printf "not-a-file\\n" >> "${NB_DIR}/home/.index"
[[ "$(cat "${NB_DIR}/home/.index")" != "$(ls "${NB_DIR}/home")" ]]
}
run "${_NB}" index verify
printf "\${status}: '%s'\\n" "${status}"
printf "\${output}: '%s'\\n" "${output}"
[[ "${status}" -eq 1 ]]
[[ "${lines[0]}" =~ !.*\ Index\ corrupted\.\ To\ fix,\ run: ]]
[[ "${lines[1]}" =~ \ \ nb\ index\ reconcile ]]
}
@test "'index verify' returns 1 with a duplicates." {
{
"${_NB}" init
"${_NB}" add "first.md" --title "one"
"${_NB}" add "second.md" --title "two"
printf "second.md\\n" >> "${NB_DIR}/home/.index"
[[ "$(cat "${NB_DIR}/home/.index")" != "$(ls "${NB_DIR}/home")" ]]
}
run "${_NB}" index verify
printf "\${status}: '%s'\\n" "${status}"
printf "\${output}: '%s'\\n" "${output}"
[[ "${status}" -eq 1 ]]
[[ "${lines[0]}" =~ !.*\ Index\ corrupted\.\ To\ fix,\ run: ]]
[[ "${lines[1]}" =~ \ \ nb\ index\ reconcile ]]
}
@test "'index verify' skips common temporary files." {
{
"${_NB}" init
"${_NB}" add "File One.md" --content "Example content one."
"${_NB}" add "File Two.md" --content "Example content two."
declare _temp_filenames=(
"Example Temp One.md~"
"#Example Temp Two.md#"
"Example Temp Three.md.swp"
"Example Temp Four.md.swap"
".#Example Temp Five.md"
)
declare __filename=
for __filename in "${_temp_filenames[@]:-}"
do
touch "${NB_DIR}/home/${__filename:-}"
done
diff \
<(cat "${NB_DIR}/home/.index") \
<(cat <<HEREDOC
File One.md
File Two.md
HEREDOC
)
}
run "${_NB}" index verify
printf "\${status}: '%s'\\n" "${status}"
printf "\${output}: '%s'\\n" "${output}"
[[ "${status}" -eq 0 ]]
[[ -z "${output}" ]]
}
# help ########################################################################
@test "'help index' exits with status 0." {
run "${_NB}" help index
[[ ${status} -eq 0 ]]
}
@test "'help index' prints help information." {
run "${_NB}" help index
printf "\${status}: '%s'\\n" "${status}"
printf "\${output}: '%s'\\n" "${output}"
[[ "${lines[0]}" =~ Usage.*\: ]]
[[ "${lines[1]}" == " nb index add <filename>" ]]
}