forked from meadowface/bash-prompt-vcs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-bash-prompt-vcs
executable file
·698 lines (574 loc) · 23.8 KB
/
test-bash-prompt-vcs
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
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
#!/usr/bin/env bash
# Tests for bash-prompt-vcs.bash
#
# Copyright (c) 2016 Marc Meadows
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to
# deal in the Software without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
# sell copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.
#
# Make sure this code works if it's called when both -e and -u are set.
set -e
set -u
# Include traceback printing code.
source debug-util.bash
# A set of functions to act as scaffolding for testing the prompt code.
# Tests will run until the first failure or error, at which point the
# error handler is called, the failure message is reported, and the process
# is ended.
#
# To use, write a test function f() which returns 0 on success and 1 on
# failure. On failure, it may also set a message using the failmsg()
# function.
#
# Then call that function with a short description via check() like:
# check "Test the f-ing function" f arg1 arg2
#
# Test functions can call check() again inside of them for grouping
# purposes. check() will indent the output of sub-calls.
#
# If you need to call out to another program, use the check_run() function
# as it will handle grabbing the output for the failmsg and checking for
# failure.
# Open a file descriptor that will spew to stderr but that is independent so
# we can direct test information to it in the ERR trap instead of stderr which
# might pollute the captured output of some code under test, especially in
# captured subprocesses.
exec 3>&2
# To be called from an ERR trap. This will detect whether we're within a
# check() call and report the failmsg. If there is no failmsg, then assume
# this is an unexpected exit during a test and report it differently to help
# suss out errors in the test code v. actual failures. If we're NOT within a
# check() call, then report it as a totally unexpected exit.
check_handler() {
cmd="${BASH_COMMAND}"
if [[ "${_pending_check_funcs}" ]]; then
printf "FAILED\n%s\n" "${_failmsg:-}"
if [[ -n ${_failmsg:-} ]]; then
printf "Aborting due to check failure: %s\n" \
"${_pending_check_funcs}"
else
printf "Aborting due to unexpected exit of '%s' during check %s.\n" \
"${cmd}" "${_pending_check_funcs}"
fi
else
printf "Unexpected exit from '%s' outside of any check.\n" "${cmd}"
fi
}
# Set up an ERR trap which will use the above to report detailed failure
# errors. Make sure output from this is directed to FD 3 and not stderr.
err_handler() {
check_handler >&3
traceback 1 >&3
# set -e doesn't descend into subshells, but trap ERR does
# (with set -o errtrace), so make sure to exit after an error.
exit 1
}
trap err_handler ERR
set -o errtrace
# _failmsg is cleared every time check() is called and is appended to every
# time failmsg() is called. So it can be used to build up a single message in
# multiple calls.
failmsg() {
local moremsg
printf -v moremsg "$@"
_failmsg="${_failmsg:-}${moremsg}"
}
# The workhorse function of the test scaffolding. Calls the function
# passed in after reporting the passed-in description. On success, will
# then output 'OK' or 'ALL...OK' when there are nested groups of check calls.
#
# Maintains the global variables:
# _pending_check_funcs - The stack of all check functions pending. Used
# in check_handler() to see if we're in a check.
# _pending_check_result - Flag denoting whether we are waiting for a
# check to pass/fail. Used to format output nicely.
# _failmsg - Cleared before calling the function and set
# inside the test functions by calling failmsg().
check() {
local -r description=$1
shift
local -r func=$1
# If we're here and still pending a check result, it means that a test
# function has called check() inside of it, so output a newline and indent
# the new check. This allows grouping of checks inside functions and a
# nice hierarchal output.
if [[ "${_pending_check_result:-}" ]]; then
printf "\n"
fi
# Report the function name we're about to call at the appropriate
# indentation.
# Indentation level is the number of check funcs we have in the stack
# already, which we can get by counting colons in $_pending_check_funcs.
local colons="${_pending_check_funcs:-}"
colons="${colons//[^:]}" # remove everything but colons
local -r indent=$((${#colons} * 2))
printf "%*s%s: " ${indent} "" "${description}"
# Push the function name we're about to call, preceded by a '/'
# and set the global state for the call.
_pending_check_funcs="${_pending_check_funcs:-}${_pending_check_funcs:+:}${func}"
_pending_check_result="yes"
unset _failmsg
"$@"
# Report the results, formatting group results differently.
if [[ "${_pending_check_result:-}" ]]; then
printf "OK\n"
else
printf "%*sALL %s: OK\n" ${indent} "" "${description}"
fi
# Pop the function name just called off the stack and clear the globals.
unset _failmsg
unset _pending_check_result
_pending_check_funcs="${_pending_check_funcs%:*}"
}
# Run the command represented by the args, redirecting output into a variable.
# If the command succeeds, just return 0.
# If the command fails, set a fail message with its returncode and
# output, then return 1 to trigger the error trap.
check_run() {
local output
local returncode=0
output=$("$@" 2>&1) || returncode=$?
if [[ ${returncode} -ne 0 ]]; then
failmsg "\nRUN FAIL:\n'%s' failed.\nreturncode: %d\noutput: [%s]\n" \
"$*" ${returncode} "${output}"
return 1
fi
}
# Test that the prompt outputs what is expected, failing if not.
# Will make sure the prefix/suffix variables are part of the output.
expect_prompt() {
local -r expected="$1"
local prompt
# Run once to gather output to compare and then again in the local symbol
# space so we can check for symbol leakage after all tests have been run.
prompt=$(bpvcs_bash_prompt)
bpvcs_bash_prompt >/dev/null
if [[ "${prompt}" = "${prefix:-}${expected}${suffix:-}" ]]; then
return 0
else
failmsg "Expected: %q\n got: %q\n" "${prefix:-}${expected}${suffix:-}" "${prompt}"
failmsg "Working trees in ${tmpdir}\n"
return 1
fi
}
# Some handy constants
readonly FIRST_FILENAME="first.txt"
readonly SECOND_FILENAME="second.txt"
readonly ALT_SECOND_FILENAME="2nd.txt"
readonly THIRD_FILENAME="third.txt"
readonly MISSING_FILENAME="missing.txt"
readonly UNICODE_FILENAME=$'\U0001F4A9'
readonly SPACES_FILENAME="filename with spaces.txt"
# Tests for non-working tree directories.
not_sandbox() {
# Need to save/restore suffix so we clobber it for later tests.
local save_suffix="${suffix}"
prefix=""
suffix=""
cd "${tmpdir}"
check "not a sandbox" expect_prompt ""
suffix="${save_suffix}"
}
# Tests for git working trees.
run_git() {
check_run git -c user.name="Test BPVCS" \
-c user.email="[email protected]" \
"$@"
}
git_sandbox() {
prefix="${git_prefix}"
local gitdir
gitdir=$(mktemp -d --tmpdir="${tmpdir}" test_git.XXX)
cd "${gitdir}"
run_git init
check "git init" expect_prompt "(Initial commit on master|✔)"
echo "First post!" >"${FIRST_FILENAME}"
run_git add "${FIRST_FILENAME}"
check "git add" expect_prompt "(Initial commit on master|●1)"
run_git commit -m first
check "git added" expect_prompt "(master|✔)"
echo "No tracked" >"${gitdir}/notme"
check "git untracked" expect_prompt "(master|…1)"
echo "Changed!" >"${FIRST_FILENAME}"
check "git change" expect_prompt "(master|…1△1)"
echo "toooooo" >"${SECOND_FILENAME}"
check "git various" expect_prompt "(master|…2△1)"
run_git add "${SECOND_FILENAME}"
check "git staged" expect_prompt "(master|…1△1●1)"
run_git commit -m "moar files"
check "git commit part" expect_prompt "(master|…1△1)"
echo "spacey" >"${SPACES_FILENAME}"
check "git spaces" expect_prompt "(master|…2△1)"
rm "${SPACES_FILENAME}"
echo poo >"${UNICODE_FILENAME}"
check "git unicode" expect_prompt "(master|…2△1)"
rm "${UNICODE_FILENAME}"
# git tries to track removes so a non-git remove still looks like
# a delete but the "${MISSING_FILENAME}" is now an untracked file.
mv "${SECOND_FILENAME}" "${MISSING_FILENAME}"
check "git missing" expect_prompt "(master|…2△1●1)"
mv "${MISSING_FILENAME}" "${SECOND_FILENAME}"
run_git mv "${SECOND_FILENAME}" "${ALT_SECOND_FILENAME}"
check "git rename" expect_prompt "(master|…1△1●1)"
run_git commit -a -m "save it all"
check "git almost clean" expect_prompt "(master|…1)"
echo "short lived third" >"${THIRD_FILENAME}"
run_git add "${THIRD_FILENAME}"
run_git commit -m "add third"
run_git rm "${THIRD_FILENAME}"
check "git rm" expect_prompt "(master|…1●1)"
run_git commit -m "we did not want third"
echo "notme" >"${gitdir}/.gitignore"
check "git ignore" expect_prompt "(master|…1)"
run_git add ".gitignore"
run_git commit ".gitignore" -m "nothing to see here"
check "git faux clean" expect_prompt "(master|✔)"
rm "${gitdir}/notme"
check "git clean" expect_prompt "(master|✔)"
run_git checkout -b riskychange
check "git branch" expect_prompt "(riskychange|✔)"
run_git checkout master
check "git master" expect_prompt "(master|✔)"
# Replace git with a that outputs nonsense to test parsing errors.
git() {
echo "Nonesense"
}
prefix="${err_prefix}"
check "git bad status" expect_prompt "(unexpected git status output)"
prefix="${git_prefix}"
unset git
check "git back to good" expect_prompt "(master|✔)"
# Select a home for the clone but remove it as git clone will create it.
local gitclonedir
gitclonedir=$(mktemp -d --tmpdir="${tmpdir}" test_gitclone.XXX)
rmdir "${gitclonedir}"
cd "${tmpdir}"
run_git clone "${gitdir}" "${gitclonedir}"
cd "${gitclonedir}"
check "git clone clean" expect_prompt "(master|✔)"
# Put clone ahead of remote (original)
echo "oneoneone" >>"${FIRST_FILENAME}"
run_git commit -a -m "one eleven"
check "git clone ahead" expect_prompt "(master⇡|✔)"
cd "${gitdir}"
echo "too" >>"${ALT_SECOND_FILENAME}"
run_git commit -a -m "also"
cd "${gitclonedir}"
check "git clone ahead/behind prefetch" expect_prompt "(master⇡|✔)"
run_git fetch
check "git clone ahead/behind postfetch" expect_prompt "(master⇡⇣|✔)"
}
# Tests for hg working trees.
run_hg() {
check_run hg --config ui.username=test-bpvcs "$@"
}
hg_sandbox() {
prefix="${hg_prefix}"
local hgdir
hgdir=$(mktemp -d --tmpdir="${tmpdir}" test_hg.XXX)
cd "${hgdir}"
run_hg init
check "hg init" expect_prompt "(default|✔)"
echo "First post!" >"${FIRST_FILENAME}"
run_hg add "${FIRST_FILENAME}"
check "hg add" expect_prompt "(default|△1)"
run_hg commit -m first
check "hg added" expect_prompt "(default|✔)"
echo "No tracked" >"${hgdir}/notme"
check "hg untracked" expect_prompt "(default|…1)"
echo "Changed!" >"${FIRST_FILENAME}"
check "hg change" expect_prompt "(default|…1△1)"
echo "toooooo" >"${SECOND_FILENAME}"
check "hg various" expect_prompt "(default|…2△1)"
run_hg add "${SECOND_FILENAME}"
check "hg add second" expect_prompt "(default|…1△2)"
run_hg commit -m "moar files"
check "hg commit" expect_prompt "(default|…1)"
echo "spacey" >"${SPACES_FILENAME}"
check "hg spaces" expect_prompt "(default|…2)"
rm "${SPACES_FILENAME}"
echo poo >"${UNICODE_FILENAME}"
check "hg unicode" expect_prompt "(default|…2)"
rm "${UNICODE_FILENAME}"
# hg tracks deleted but not removed files, but does not
# auto-stage them or commit them. So treat it as an untracked.
mv "${SECOND_FILENAME}" "${MISSING_FILENAME}"
check "hg missing" expect_prompt "(default|…3)"
mv "${MISSING_FILENAME}" "${SECOND_FILENAME}"
run_hg rename "${SECOND_FILENAME}" "${ALT_SECOND_FILENAME}"
check "hg rename" expect_prompt "(default|…1△1)"
run_hg commit -m "save it all"
check "hg almost clean" expect_prompt "(default|…1)"
echo "short lived third" >"${THIRD_FILENAME}"
run_hg add "${THIRD_FILENAME}"
run_hg commit -m "add third"
run_hg rm "${THIRD_FILENAME}"
check "hg rm" expect_prompt "(default|…1△1)"
run_hg commit -m "we did not want third"
echo "notme" >"${hgdir}/.hgignore"
check "hg ignore" expect_prompt "(default|…1)"
run_hg add ".hgignore"
run_hg commit ".hgignore" -m "nothing to see here"
check "hg faux clean" expect_prompt "(default|✔)"
rm "${hgdir}/notme"
check "hg clean" expect_prompt "(default|✔)"
run_hg branch riskychange
check "hg branch" expect_prompt "(riskychange|✔)"
run_hg branch --clean
check "hg default again" expect_prompt "(default|✔)"
# Replace hg with a that outputs nonsense to test parsing errors.
hg() {
echo "branch: fooled you"
}
prefix="${err_prefix}"
check "hg bad status" expect_prompt "(unexpected hg summary output)"
hg() {
echo "commit: fooledyou"
}
check "hg bad status" expect_prompt "(unexpected hg summary output)"
prefix="${hg_prefix}"
unset hg
check "hg back to good" expect_prompt "(default|✔)"
}
# Tests for svn sandboxes.
run_svnadmin() {
check_run svnadmin "$@"
}
run_svn() {
check_run svn "$@"
}
svn_sandbox() {
prefix="${svn_prefix}"
local svndir
svndir=$(mktemp -d --tmpdir="${tmpdir}" test_svn.XXX)
cd "${svndir}"
run_svnadmin create SVNROOT
run_svn checkout "file://${svndir}/SVNROOT" trunk
cd "trunk"
check "svn init" expect_prompt "(✔)"
echo "First post!" >"${FIRST_FILENAME}"
run_svn add "${FIRST_FILENAME}"
check "svn add" expect_prompt "(△1)"
run_svn commit -m first
check "svn added" expect_prompt "(✔)"
echo "No tracked" >"${svndir}/trunk/notme"
check "svn untracked" expect_prompt "(…1)"
echo "Changed!" >"${FIRST_FILENAME}"
check "svn change" expect_prompt "(…1△1)"
echo "toooooo" >"${SECOND_FILENAME}"
check "svn various" expect_prompt "(…2△1)"
run_svn add "${SECOND_FILENAME}"
check "svn add second" expect_prompt "(…1△2)"
run_svn commit -m "moar files"
check "svn commit" expect_prompt "(…1)"
echo "spacey" >"${SPACES_FILENAME}"
check "svn spaces" expect_prompt "(…2)"
rm "${SPACES_FILENAME}"
echo poo >"${UNICODE_FILENAME}"
check "svn unicode" expect_prompt "(…2)"
rm "${UNICODE_FILENAME}"
# Like hg, svn tracks deleted but not removed files, but does not
# auto-stage them or commit them. So treat it as an untracked.
mv "${SECOND_FILENAME}" "${MISSING_FILENAME}"
check "svn missing" expect_prompt "(…3)"
mv "${MISSING_FILENAME}" "${SECOND_FILENAME}"
# A rename counts as two changes, an add and a delete
run_svn rename "${SECOND_FILENAME}" "${ALT_SECOND_FILENAME}"
check "svn rename" expect_prompt "(…1△2)"
run_svn commit -m "save it all"
check "svn almost clean" expect_prompt "(…1)"
echo "short lived third" >"${THIRD_FILENAME}"
run_svn add "${THIRD_FILENAME}"
run_svn commit -m "add third"
run_svn rm "${THIRD_FILENAME}"
check "svn rm" expect_prompt "(…1△1)"
run_svn commit -m "we did not want third"
run_svn propset svn:ignore "notme" .
check "svn ignore" expect_prompt "(△1)"
# NOTE: The update is needed!
# See http://subversion.apache.org/faq.html#wc-out-of-date
run_svn update
run_svn commit -m "nothing to see here"
check "svn faux clean" expect_prompt "(✔)"
rm "${svndir}/trunk/notme"
check "svn clean" expect_prompt "(✔)"
# Replace svn with a that outputs nonsense to test parsing errors.
svn() {
echo "Nonesense"
}
prefix="${err_prefix}"
check "svn bad status" expect_prompt "(unexpected svn status output)"
unset svn
# Expected "FAIL": Because errtrace will enable the ERR trap to propagate
# to subshells, this test will spew noise since svn
# actually will return 1 in the case of upgrade-needed.
# The "return 1" aspect propagates to the subshell but
# the "part of ||" doesn't since the || is in the parent
# and that is what prevents set -e from killing the code.
#
# Note that the prompt returned *is* in fact the one
# expected so the code under test does everything OK
# unless someone sets a noisy ERR trap and even then it
# will most likely work.
#
# So for now work-around this by temporarily disabling
# errtrace for this test. The product code will be
# changed post-1.0.0 release to avoid the need for this.
#TODO: remove, see above
set +o errtrace
# version numbers below work as of svn 1.8.13
# http://stackoverflow.com/questions/1364618/how-do-i-determine-the-svn-working-copy-layout-version
sqlite3 "${svndir}/trunk/.svn/wc.db" "PRAGMA user_version=27"
check "svn upgrade needed" expect_prompt "('svn upgrade' needed)"
#TODO: remove, see above
set -o errtrace
prefix="${svn_prefix}"
sqlite3 "${svndir}/trunk/.svn/wc.db" "PRAGMA user_version=31"
check "svn back to good" expect_prompt "(✔)"
}
all_sandboxes() {
check "Not sandbox" not_sandbox
check "git" git_sandbox
check "hg" hg_sandbox
check "svn" svn_sandbox
}
prompt_output() {
# Make a home to work in.
local tmpdir
tmpdir=$(mktemp -d --tmpdir bpvcs-test-XXX)
# Run through all of the tests with color making sure the output is
# wrapped correctly with color prefixes and suffixes and that those
# have the \x01 and \x02 characters that readline requires to recognize
# non-printing characters.
local -r NP_ON=$'\x01'
local -r NP_OFF=$'\x02'
local -r ESC=$'\E'
local prefix
local suffix="${NP_ON}${ESC}[0m${NP_OFF}"
local git_prefix="${NP_ON}${ESC}[0;32m${NP_OFF} "
local hg_prefix="${NP_ON}${ESC}[0;36m${NP_OFF} "
local svn_prefix="${NP_ON}${ESC}[0;35m${NP_OFF} "
local err_prefix="${NP_ON}${ESC}[0;31m${NP_OFF} "
check "chromatic" all_sandboxes
# Run all tests again, this time with colors off and making sure that the
# non-color prefixes are used.
suffix=""
git_prefix=" git:"
hg_prefix=" hg:"
svn_prefix=" svn:"
err_prefix=" ERR:"
unset BPVCS_COLORS
check "monochrome" all_sandboxes
# Clean up the temp dir.
if [[ -n "${tmpdir}" ]]; then
rm -rf "${tmpdir}"
fi
}
# Output a list of shell symbols (variables and functions) in a stable order.
# Exclude common "working variables" and this script's specific symbols.
shell_state() {
(compgen -A variable ; compgen -A function) \
| grep -v '^PIPESTATUS$' \
| grep -v '^OLDPWD$' \
| grep -v '^BASH_REMATCH$' \
| grep -v '^test_bpvcs_' \
| sort
}
load_and_symbols() {
# NOTE: Since this test script is run as a separate script, we don't
# inherit any bash-prompt-vcs symbols even if it's being used on the test
# host.
test_bpvcs_before=$(shell_state)
source bash-prompt-vcs.bash
test_bpvcs_loaded=$(shell_state)
test_bpvcs_new_symbols=($(comm -31 <(printf "%s" "${test_bpvcs_before}") \
<(printf "%s" "${test_bpvcs_loaded}")))
local -i unexpected=0
local new
for new in "${test_bpvcs_new_symbols[@]}"; do
case "${new}" in
BPVCS_AHEAD_INDICATOR \
| bpvcs_bash_prompt \
| BPVCS_BEHIND_INDICATOR \
| BPVCS_CHANGED_INDICATOR \
| BPVCS_CLEAN_INDICATOR \
| BPVCS_COLORS \
| BPVCS_ERR_COLOR \
| BPVCS_GIT_COLOR \
| BPVCS_HG_COLOR \
| BPVCS_RESET_COLOR \
| BPVCS_STAGED_INDICATOR \
| BPVCS_SVN_COLOR \
| BPVCS_UNTRACKED_INDICATOR \
| BPVCS_VERSION) ;; # these are all expected, ignore them.
*) failmsg "UNEXPECTED: ${new}\n"
((++unexpected))
;;
esac
done
if [[ ${unexpected} -ne 0 ]]; then
failmsg "${unexpected} unexpected symbols when loading"
return 1
fi
}
# Make sure the version being tested matches what we expect and is read-only.
version() {
local expected_version="1.0.0"
if [[ "${BPVCS_VERSION}" != "${expected_version}" ]]; then
failmsg "BPVCS_VERSION of '${BPVCS_VERSION}' isn't the expected '${expected_version}'"
return 1
fi
if [[ ! $(declare -p BPVCS_VERSION) =~ [[:space:]]-r[[:space:]] ]]; then
failmsg "BPVCS_VERSION is not read-only."
return 1
fi
}
# Test that the defaults are as expected.
defaults() {
# Confirm these rather than using the variables up above so the tests remain
# somewhat readable and useful for looking at to see what the code will output.
if [[ "${BPVCS_UNTRACKED_INDICATOR}" != "…" ]]; then failmsg "Wrong default untracked indicator"; return 1; fi
if [[ "${BPVCS_CHANGED_INDICATOR}" != "△" ]]; then failmsg "Wrong default changed indicator"; return 1; fi
if [[ "${BPVCS_STAGED_INDICATOR}" != "●" ]]; then failmsg "Wrong default staged indicator"; return 1; fi
if [[ "${BPVCS_CLEAN_INDICATOR}" != "✔" ]]; then failmsg "Wrong default clean indicator"; return 1; fi
if [[ "${BPVCS_AHEAD_INDICATOR}" != "⇡" ]]; then failmsg "Wrong default ahead indicator"; return 1; fi
if [[ "${BPVCS_BEHIND_INDICATOR}" != "⇣" ]]; then failmsg "Wrong default behind indicator"; return 1; fi
# Make sure we have default colors defined and that we default to using color.
if [[ -z "${BPVCS_GIT_COLOR:-}" ]]; then failmsg "No GIT default color!"; return 1; fi
if [[ -z "${BPVCS_HG_COLOR:-}" ]]; then failmsg "No HG default color!"; return 1; fi
if [[ -z "${BPVCS_SVN_COLOR:-}" ]]; then failmsg "No SVN default color!"; return 1; fi
if [[ -z "${BPVCS_RESET_COLOR:-}" ]]; then failmsg "No reset colors default!"; return 1; fi
if [[ -z "${BPVCS_COLORS:-}" ]]; then failmsg "No default color!"; return 1; fi
}
symbol_leakage() {
test_bpvcs_after=$(shell_state)
test_bpvcs_leaked_symbols=$(comm -31 <(printf "%s" "${test_bpvcs_loaded}") \
<(printf "%s" "${test_bpvcs_after}"))
if [[ -n "${test_bpvcs_leaked_symbols}" ]]; then
failmsg "Symbols leaked:\n%s\n" "${test_bpvcs_leaked_symbols}"
fi
}
main() {
check "Sourcing and symbols" load_and_symbols
check "Version" version
check "Defaults" defaults
check "Prompt Output" prompt_output
check "Symbol Leakage" symbol_leakage
echo "ALL tests: OK"
}
main