forked from git/git
-
Notifications
You must be signed in to change notification settings - Fork 127
/
test-lib-functions.sh
1904 lines (1735 loc) · 43.4 KB
/
test-lib-functions.sh
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
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Library of functions shared by all tests scripts, included by
# test-lib.sh.
#
# Copyright (c) 2005 Junio C Hamano
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see http://www.gnu.org/licenses/ .
# The semantics of the editor variables are that of invoking
# sh -c "$EDITOR \"$@\"" files ...
#
# If our trash directory contains shell metacharacters, they will be
# interpreted if we just set $EDITOR directly, so do a little dance with
# environment variables to work around this.
#
# In particular, quoting isn't enough, as the path may contain the same quote
# that we're using.
test_set_editor () {
FAKE_EDITOR="$1"
export FAKE_EDITOR
EDITOR='"$FAKE_EDITOR"'
export EDITOR
}
test_decode_color () {
awk '
function name(n) {
if (n == 0) return "RESET";
if (n == 1) return "BOLD";
if (n == 2) return "FAINT";
if (n == 3) return "ITALIC";
if (n == 7) return "REVERSE";
if (n == 30) return "BLACK";
if (n == 31) return "RED";
if (n == 32) return "GREEN";
if (n == 33) return "YELLOW";
if (n == 34) return "BLUE";
if (n == 35) return "MAGENTA";
if (n == 36) return "CYAN";
if (n == 37) return "WHITE";
if (n == 40) return "BLACK";
if (n == 41) return "BRED";
if (n == 42) return "BGREEN";
if (n == 43) return "BYELLOW";
if (n == 44) return "BBLUE";
if (n == 45) return "BMAGENTA";
if (n == 46) return "BCYAN";
if (n == 47) return "BWHITE";
}
{
while (match($0, /\033\[[0-9;]*m/) != 0) {
printf "%s<", substr($0, 1, RSTART-1);
codes = substr($0, RSTART+2, RLENGTH-3);
if (length(codes) == 0)
printf "%s", name(0)
else {
n = split(codes, ary, ";");
sep = "";
for (i = 1; i <= n; i++) {
printf "%s%s", sep, name(ary[i]);
sep = ";"
}
}
printf ">";
$0 = substr($0, RSTART + RLENGTH, length($0) - RSTART - RLENGTH + 1);
}
print
}
'
}
lf_to_nul () {
perl -pe 'y/\012/\000/'
}
nul_to_q () {
perl -pe 'y/\000/Q/'
}
q_to_nul () {
perl -pe 'y/Q/\000/'
}
q_to_cr () {
tr Q '\015'
}
q_to_tab () {
tr Q '\011'
}
qz_to_tab_space () {
tr QZ '\011\040'
}
append_cr () {
sed -e 's/$/Q/' | tr Q '\015'
}
remove_cr () {
tr '\015' Q | sed -e 's/Q$//'
}
# In some bourne shell implementations, the "unset" builtin returns
# nonzero status when a variable to be unset was not set in the first
# place.
#
# Use sane_unset when that should not be considered an error.
sane_unset () {
unset "$@"
return 0
}
test_tick () {
if test -z "${test_tick+set}"
then
test_tick=1112911993
else
test_tick=$(($test_tick + 60))
fi
GIT_COMMITTER_DATE="$test_tick -0700"
GIT_AUTHOR_DATE="$test_tick -0700"
export GIT_COMMITTER_DATE GIT_AUTHOR_DATE
}
# Stop execution and start a shell. This is useful for debugging tests.
#
# Be sure to remove all invocations of this command before submitting.
# WARNING: the shell invoked by this helper does not have the same environment
# as the one running the tests (shell variables and functions are not
# available, and the options below further modify the environment). As such,
# commands copied from a test script might behave differently than when
# running the test.
#
# Usage: test_pause [options]
# -t
# Use your original TERM instead of test-lib.sh's "dumb".
# This usually restores color output in the invoked shell.
# -s
# Invoke $SHELL instead of $TEST_SHELL_PATH.
# -h
# Use your original HOME instead of test-lib.sh's "$TRASH_DIRECTORY".
# This allows you to use your regular shell environment and Git aliases.
# CAUTION: running commands copied from a test script into the paused shell
# might result in files in your HOME being overwritten.
# -a
# Shortcut for -t -s -h
test_pause () {
PAUSE_TERM=$TERM &&
PAUSE_SHELL=$TEST_SHELL_PATH &&
PAUSE_HOME=$HOME &&
while test $# != 0
do
case "$1" in
-t)
PAUSE_TERM="$USER_TERM"
;;
-s)
PAUSE_SHELL="$SHELL"
;;
-h)
PAUSE_HOME="$USER_HOME"
;;
-a)
PAUSE_TERM="$USER_TERM"
PAUSE_SHELL="$SHELL"
PAUSE_HOME="$USER_HOME"
;;
*)
break
;;
esac
shift
done &&
TERM="$PAUSE_TERM" HOME="$PAUSE_HOME" "$PAUSE_SHELL" <&6 >&5 2>&7
}
# Wrap git with a debugger. Adding this to a command can make it easier
# to understand what is going on in a failing test.
#
# Usage: debug [options] <git command>
# -d <debugger>
# --debugger=<debugger>
# Use <debugger> instead of GDB
# -t
# Use your original TERM instead of test-lib.sh's "dumb".
# This usually restores color output in the debugger.
# WARNING: the command being debugged might behave differently than when
# running the test.
#
# Examples:
# debug git checkout master
# debug --debugger=nemiver git $ARGS
# debug -d "valgrind --tool=memcheck --track-origins=yes" git $ARGS
debug () {
GIT_DEBUGGER=1 &&
DEBUG_TERM=$TERM &&
while test $# != 0
do
case "$1" in
-t)
DEBUG_TERM="$USER_TERM"
;;
-d)
GIT_DEBUGGER="$2" &&
shift
;;
--debugger=*)
GIT_DEBUGGER="${1#*=}"
;;
*)
break
;;
esac
shift
done &&
dotfiles=".gdbinit .lldbinit"
for dotfile in $dotfiles
do
dotfile="$USER_HOME/$dotfile" &&
test -f "$dotfile" && cp "$dotfile" "$HOME" || :
done &&
TERM="$DEBUG_TERM" GIT_DEBUGGER="${GIT_DEBUGGER}" "$@" <&6 >&5 2>&7 &&
for dotfile in $dotfiles
do
rm -f "$HOME/$dotfile"
done
}
# Usage: test_commit [options] <message> [<file> [<contents> [<tag>]]]
# -C <dir>:
# Run all git commands in directory <dir>
# --notick
# Do not call test_tick before making a commit
# --append
# Use ">>" instead of ">" when writing "<contents>" to "<file>"
# --printf
# Use "printf" instead of "echo" when writing "<contents>" to
# "<file>", use this to write escape sequences such as "\0", a
# trailing "\n" won't be added automatically. This option
# supports nothing but the FORMAT of printf(1), i.e. no custom
# ARGUMENT(s).
# --signoff
# Invoke "git commit" with --signoff
# --author <author>
# Invoke "git commit" with --author <author>
# --no-tag
# Do not tag the resulting commit
# --annotate
# Create an annotated tag with "--annotate -m <message>". Calls
# test_tick between making the commit and tag, unless --notick
# is given.
#
# This will commit a file with the given contents and the given commit
# message, and tag the resulting commit with the given tag name.
#
# <file>, <contents>, and <tag> all default to <message>.
test_commit () {
notick= &&
echo=echo &&
append= &&
author= &&
signoff= &&
indir= &&
tag=light &&
while test $# != 0
do
case "$1" in
--notick)
notick=yes
;;
--printf)
echo=printf
;;
--append)
append=yes
;;
--author)
author="$2"
shift
;;
--signoff)
signoff="$1"
;;
--date)
notick=yes
GIT_COMMITTER_DATE="$2"
GIT_AUTHOR_DATE="$2"
shift
;;
-C)
indir="$2"
shift
;;
--no-tag)
tag=none
;;
--annotate)
tag=annotate
;;
*)
break
;;
esac
shift
done &&
indir=${indir:+"$indir"/} &&
file=${2:-"$1.t"} &&
if test -n "$append"
then
$echo "${3-$1}" >>"$indir$file"
else
$echo "${3-$1}" >"$indir$file"
fi &&
git ${indir:+ -C "$indir"} add "$file" &&
if test -z "$notick"
then
test_tick
fi &&
git ${indir:+ -C "$indir"} commit \
${author:+ --author "$author"} \
$signoff -m "$1" &&
case "$tag" in
none)
;;
light)
git ${indir:+ -C "$indir"} tag "${4:-$1}"
;;
annotate)
if test -z "$notick"
then
test_tick
fi &&
git ${indir:+ -C "$indir"} tag -a -m "$1" "${4:-$1}"
;;
esac
}
# Call test_merge with the arguments "<message> <commit>", where <commit>
# can be a tag pointing to the commit-to-merge.
test_merge () {
label="$1" &&
shift &&
test_tick &&
git merge -m "$label" "$@" &&
git tag "$label"
}
# Efficiently create <nr> commits, each with a unique number (from 1 to <nr>
# by default) in the commit message.
#
# Usage: test_commit_bulk [options] <nr>
# -C <dir>:
# Run all git commands in directory <dir>
# --ref=<n>:
# ref on which to create commits (default: HEAD)
# --start=<n>:
# number commit messages from <n> (default: 1)
# --message=<msg>:
# use <msg> as the commit mesasge (default: "commit %s")
# --filename=<fn>:
# modify <fn> in each commit (default: %s.t)
# --contents=<string>:
# place <string> in each file (default: "content %s")
# --id=<string>:
# shorthand to use <string> and %s in message, filename, and contents
#
# The message, filename, and contents strings are evaluated by printf, with the
# first "%s" replaced by the current commit number. So you can do:
#
# test_commit_bulk --filename=file --contents="modification %s"
#
# to have every commit touch the same file, but with unique content.
#
test_commit_bulk () {
tmpfile=.bulk-commit.input
indir=.
ref=HEAD
n=1
message='commit %s'
filename='%s.t'
contents='content %s'
while test $# -gt 0
do
case "$1" in
-C)
indir=$2
shift
;;
--ref=*)
ref=${1#--*=}
;;
--start=*)
n=${1#--*=}
;;
--message=*)
message=${1#--*=}
;;
--filename=*)
filename=${1#--*=}
;;
--contents=*)
contents=${1#--*=}
;;
--id=*)
message="${1#--*=} %s"
filename="${1#--*=}-%s.t"
contents="${1#--*=} %s"
;;
-*)
BUG "invalid test_commit_bulk option: $1"
;;
*)
break
;;
esac
shift
done
total=$1
add_from=
if git -C "$indir" rev-parse --quiet --verify "$ref"
then
add_from=t
fi
while test "$total" -gt 0
do
test_tick &&
echo "commit $ref"
printf 'author %s <%s> %s\n' \
"$GIT_AUTHOR_NAME" \
"$GIT_AUTHOR_EMAIL" \
"$GIT_AUTHOR_DATE"
printf 'committer %s <%s> %s\n' \
"$GIT_COMMITTER_NAME" \
"$GIT_COMMITTER_EMAIL" \
"$GIT_COMMITTER_DATE"
echo "data <<EOF"
printf "$message\n" $n
echo "EOF"
if test -n "$add_from"
then
echo "from $ref^0"
add_from=
fi
printf "M 644 inline $filename\n" $n
echo "data <<EOF"
printf "$contents\n" $n
echo "EOF"
echo
n=$((n + 1))
total=$((total - 1))
done >"$tmpfile"
git -C "$indir" \
-c fastimport.unpacklimit=0 \
fast-import <"$tmpfile" || return 1
# This will be left in place on failure, which may aid debugging.
rm -f "$tmpfile"
# If we updated HEAD, then be nice and update the index and working
# tree, too.
if test "$ref" = "HEAD"
then
git -C "$indir" checkout -f HEAD || return 1
fi
}
# This function helps systems where core.filemode=false is set.
# Use it instead of plain 'chmod +x' to set or unset the executable bit
# of a file in the working directory and add it to the index.
test_chmod () {
chmod "$@" &&
git update-index --add "--chmod=$@"
}
# Get the modebits from a file or directory, ignoring the setgid bit (g+s).
# This bit is inherited by subdirectories at their creation. So we remove it
# from the returning string to prevent callers from having to worry about the
# state of the bit in the test directory.
#
test_modebits () {
ls -ld "$1" | sed -e 's|^\(..........\).*|\1|' \
-e 's|^\(......\)S|\1-|' -e 's|^\(......\)s|\1x|'
}
# Unset a configuration variable, but don't fail if it doesn't exist.
test_unconfig () {
config_dir=
if test "$1" = -C
then
shift
config_dir=$1
shift
fi
git ${config_dir:+-C "$config_dir"} config --unset-all "$@"
config_status=$?
case "$config_status" in
5) # ok, nothing to unset
config_status=0
;;
esac
return $config_status
}
# Set git config, automatically unsetting it after the test is over.
test_config () {
config_dir=
if test "$1" = -C
then
shift
config_dir=$1
shift
fi
test_when_finished "test_unconfig ${config_dir:+-C '$config_dir'} '$1'" &&
git ${config_dir:+-C "$config_dir"} config "$@"
}
test_config_global () {
test_when_finished "test_unconfig --global '$1'" &&
git config --global "$@"
}
write_script () {
{
echo "#!${2-"$SHELL_PATH"}" &&
cat
} >"$1" &&
chmod +x "$1"
}
# Use test_set_prereq to tell that a particular prerequisite is available.
# The prerequisite can later be checked for in two ways:
#
# - Explicitly using test_have_prereq.
#
# - Implicitly by specifying the prerequisite tag in the calls to
# test_expect_{success,failure} and test_external{,_without_stderr}.
#
# The single parameter is the prerequisite tag (a simple word, in all
# capital letters by convention).
test_unset_prereq () {
! test_have_prereq "$1" ||
satisfied_prereq="${satisfied_prereq% $1 *} ${satisfied_prereq#* $1 }"
}
test_set_prereq () {
if test -n "$GIT_TEST_FAIL_PREREQS_INTERNAL"
then
case "$1" in
# The "!" case is handled below with
# test_unset_prereq()
!*)
;;
# (Temporary?) whitelist of things we can't easily
# pretend not to support
SYMLINKS)
;;
# Inspecting whether GIT_TEST_FAIL_PREREQS is on
# should be unaffected.
FAIL_PREREQS)
;;
*)
return
esac
fi
case "$1" in
!*)
test_unset_prereq "${1#!}"
;;
*)
satisfied_prereq="$satisfied_prereq$1 "
;;
esac
}
satisfied_prereq=" "
lazily_testable_prereq= lazily_tested_prereq=
# Usage: test_lazy_prereq PREREQ 'script'
test_lazy_prereq () {
lazily_testable_prereq="$lazily_testable_prereq$1 "
eval test_prereq_lazily_$1=\$2
}
test_run_lazy_prereq_ () {
script='
mkdir -p "$TRASH_DIRECTORY/prereq-test-dir-'"$1"'" &&
(
cd "$TRASH_DIRECTORY/prereq-test-dir-'"$1"'" &&'"$2"'
)'
say >&3 "checking prerequisite: $1"
say >&3 "$script"
test_eval_ "$script"
eval_ret=$?
rm -rf "$TRASH_DIRECTORY/prereq-test-dir-$1"
if test "$eval_ret" = 0; then
say >&3 "prerequisite $1 ok"
else
say >&3 "prerequisite $1 not satisfied"
fi
return $eval_ret
}
test_have_prereq () {
# prerequisites can be concatenated with ','
save_IFS=$IFS
IFS=,
set -- $*
IFS=$save_IFS
total_prereq=0
ok_prereq=0
missing_prereq=
for prerequisite
do
case "$prerequisite" in
!*)
negative_prereq=t
prerequisite=${prerequisite#!}
;;
*)
negative_prereq=
esac
case " $lazily_tested_prereq " in
*" $prerequisite "*)
;;
*)
case " $lazily_testable_prereq " in
*" $prerequisite "*)
eval "script=\$test_prereq_lazily_$prerequisite" &&
if test_run_lazy_prereq_ "$prerequisite" "$script"
then
test_set_prereq $prerequisite
fi
lazily_tested_prereq="$lazily_tested_prereq$prerequisite "
esac
;;
esac
total_prereq=$(($total_prereq + 1))
case "$satisfied_prereq" in
*" $prerequisite "*)
satisfied_this_prereq=t
;;
*)
satisfied_this_prereq=
esac
case "$satisfied_this_prereq,$negative_prereq" in
t,|,t)
ok_prereq=$(($ok_prereq + 1))
;;
*)
# Keep a list of missing prerequisites; restore
# the negative marker if necessary.
prerequisite=${negative_prereq:+!}$prerequisite
# Abort if this prereq was marked as required
if test -n "$GIT_TEST_REQUIRE_PREREQ"
then
case " $GIT_TEST_REQUIRE_PREREQ " in
*" $prerequisite "*)
BAIL_OUT "required prereq $prerequisite failed"
;;
esac
fi
if test -z "$missing_prereq"
then
missing_prereq=$prerequisite
else
missing_prereq="$prerequisite,$missing_prereq"
fi
esac
done
test $total_prereq = $ok_prereq
}
test_declared_prereq () {
case ",$test_prereq," in
*,$1,*)
return 0
;;
esac
return 1
}
test_verify_prereq () {
test -z "$test_prereq" ||
expr >/dev/null "$test_prereq" : '[A-Z0-9_,!]*$' ||
BUG "'$test_prereq' does not look like a prereq"
}
test_expect_failure () {
test_start_
test "$#" = 3 && { test_prereq=$1; shift; } || test_prereq=
test "$#" = 2 ||
BUG "not 2 or 3 parameters to test-expect-failure"
test_verify_prereq
export test_prereq
if ! test_skip "$@"
then
say >&3 "checking known breakage of $TEST_NUMBER.$test_count '$1': $2"
if test_run_ "$2" expecting_failure
then
test_known_broken_ok_ "$1"
else
test_known_broken_failure_ "$1"
fi
fi
test_finish_
}
test_expect_success () {
test_start_
test "$#" = 3 && { test_prereq=$1; shift; } || test_prereq=
test "$#" = 2 ||
BUG "not 2 or 3 parameters to test-expect-success"
test_verify_prereq
export test_prereq
if ! test_skip "$@"
then
say >&3 "expecting success of $TEST_NUMBER.$test_count '$1': $2"
if test_run_ "$2"
then
test_ok_ "$1"
else
test_failure_ "$@"
fi
fi
test_finish_
}
# test_external runs external test scripts that provide continuous
# test output about their progress, and succeeds/fails on
# zero/non-zero exit code. It outputs the test output on stdout even
# in non-verbose mode, and announces the external script with "# run
# <n>: ..." before running it. When providing relative paths, keep in
# mind that all scripts run in "trash directory".
# Usage: test_external description command arguments...
# Example: test_external 'Perl API' perl ../path/to/test.pl
test_external () {
test "$#" = 4 && { test_prereq=$1; shift; } || test_prereq=
test "$#" = 3 ||
BUG "not 3 or 4 parameters to test_external"
descr="$1"
shift
test_verify_prereq
export test_prereq
if ! test_skip "$descr" "$@"
then
# Announce the script to reduce confusion about the
# test output that follows.
say_color "" "# run $test_count: $descr ($*)"
# Export TEST_DIRECTORY, TRASH_DIRECTORY and GIT_TEST_LONG
# to be able to use them in script
export TEST_DIRECTORY TRASH_DIRECTORY GIT_TEST_LONG
# Run command; redirect its stderr to &4 as in
# test_run_, but keep its stdout on our stdout even in
# non-verbose mode.
"$@" 2>&4
if test "$?" = 0
then
if test $test_external_has_tap -eq 0; then
test_ok_ "$descr"
else
say_color "" "# test_external test $descr was ok"
test_success=$(($test_success + 1))
fi
else
if test $test_external_has_tap -eq 0; then
test_failure_ "$descr" "$@"
else
say_color error "# test_external test $descr failed: $@"
test_failure=$(($test_failure + 1))
fi
fi
fi
}
# Like test_external, but in addition tests that the command generated
# no output on stderr.
test_external_without_stderr () {
# The temporary file has no (and must have no) security
# implications.
tmp=${TMPDIR:-/tmp}
stderr="$tmp/git-external-stderr.$$.tmp"
test_external "$@" 4> "$stderr"
test -f "$stderr" || error "Internal error: $stderr disappeared."
descr="no stderr: $1"
shift
say >&3 "# expecting no stderr from previous command"
if test ! -s "$stderr"
then
rm "$stderr"
if test $test_external_has_tap -eq 0; then
test_ok_ "$descr"
else
say_color "" "# test_external_without_stderr test $descr was ok"
test_success=$(($test_success + 1))
fi
else
if test "$verbose" = t
then
output=$(echo; echo "# Stderr is:"; cat "$stderr")
else
output=
fi
# rm first in case test_failure exits.
rm "$stderr"
if test $test_external_has_tap -eq 0; then
test_failure_ "$descr" "$@" "$output"
else
say_color error "# test_external_without_stderr test $descr failed: $@: $output"
test_failure=$(($test_failure + 1))
fi
fi
}
# debugging-friendly alternatives to "test [-f|-d|-e]"
# The commands test the existence or non-existence of $1
test_path_is_file () {
test "$#" -ne 1 && BUG "1 param"
if ! test -f "$1"
then
echo "File $1 doesn't exist"
false
fi
}
test_path_is_file_not_symlink () {
test "$#" -ne 1 && BUG "1 param"
test_path_is_file "$1" &&
if test -h "$1"
then
echo "$1 shouldn't be a symbolic link"
false
fi
}
test_path_is_dir () {
test "$#" -ne 1 && BUG "1 param"
if ! test -d "$1"
then
echo "Directory $1 doesn't exist"
false
fi
}
test_path_is_dir_not_symlink () {
test "$#" -ne 1 && BUG "1 param"
test_path_is_dir "$1" &&
if test -h "$1"
then
echo "$1 shouldn't be a symbolic link"
false
fi
}
test_path_exists () {
test "$#" -ne 1 && BUG "1 param"
if ! test -e "$1"
then
echo "Path $1 doesn't exist"
false
fi
}
test_path_is_symlink () {
test "$#" -ne 1 && BUG "1 param"
if ! test -h "$1"
then
echo "Symbolic link $1 doesn't exist"
false
fi
}
# Check if the directory exists and is empty as expected, barf otherwise.
test_dir_is_empty () {
test "$#" -ne 1 && BUG "1 param"
test_path_is_dir "$1" &&
if test -n "$(ls -a1 "$1" | egrep -v '^\.\.?$')"
then
echo "Directory '$1' is not empty, it contains:"
ls -la "$1"
return 1
fi
}
# Check if the file exists and has a size greater than zero
test_file_not_empty () {
test "$#" = 2 && BUG "2 param"
if ! test -s "$1"
then
echo "'$1' is not a non-empty file."
false
fi
}
test_path_is_missing () {
test "$#" -ne 1 && BUG "1 param"
if test -e "$1"
then
echo "Path exists:"
ls -ld "$1"
if test $# -ge 1
then
echo "$*"
fi
false
fi
}
# test_line_count checks that a file has the number of lines it
# ought to. For example:
#
# test_expect_success 'produce exactly one line of output' '
# do something >output &&
# test_line_count = 1 output
# '
#
# is like "test $(wc -l <output) = 1" except that it passes the
# output through when the number of lines is wrong.
test_line_count () {
if test $# != 3
then
BUG "not 3 parameters to test_line_count"
elif ! test $(wc -l <"$3") "$1" "$2"
then
echo "test_line_count: line count for $3 !$1 $2"
cat "$3"
return 1
fi
}
# SYNOPSIS:
# test_stdout_line_count <bin-ops> <value> <cmd> [<args>...]
#
# test_stdout_line_count checks that the output of a command has the number
# of lines it ought to. For example:
#
# test_stdout_line_count = 3 git ls-files -u
# test_stdout_line_count -gt 10 ls
test_stdout_line_count () {
local ops val trashdir &&
if test "$#" -le 3
then
BUG "expect 3 or more arguments"
fi &&
ops="$1" &&
val="$2" &&
shift 2 &&
if ! trashdir="$(git rev-parse --git-dir)/trash"; then
BUG "expect to be run inside a worktree"
fi &&
mkdir -p "$trashdir" &&
"$@" >"$trashdir/output" &&
test_line_count "$ops" "$val" "$trashdir/output"
}
test_file_size () {
test "$#" -ne 1 && BUG "1 param"
test-tool path-utils file-size "$1"
}
# Returns success if a comma separated string of keywords ($1) contains a
# given keyword ($2).
# Examples:
# `list_contains "foo,bar" bar` returns 0
# `list_contains "foo" bar` returns 1