tg.sh: handle help -h
[topgit/pro.git] / t / t0001-sanity.sh
blob4bfce3cdc315ca7a6a3bb450bececd413fd4eebb
1 #!/bin/sh
3 test_description='various sanity checks
5 Many of these are "tolerate_failure" checks
6 as there are workarounds in place for them.
9 TEST_NO_CREATE_REPO=1
11 . ./test-lib.sh
13 test_plan 19
15 # required working
17 test_tolerate_failure 'POSIX tr to NUL processing' '
18 printf "1x2x3x" | tr "x" "\\000" >lines3z &&
19 val="$(xargs -0 <lines3z printf "%s\n" | wc -l)" &&
20 test $val -eq 3
23 test_expect_success 'POSIX tr from NUL processing' '
24 val="$(echo "x@@@y" | tr "x@y" "1\\0005" | tr "\\000" 2)" &&
25 test z"$val" = z"12225"
28 # tolerated breakage
30 test_tolerate_failure 'POSIX unset behavior' '
31 test z"$(unset it && unset it && echo good || echo bad)" = z"good"
34 test_tolerate_failure 'POSIX exec behavior' '
35 test z"$(false() { :; }; (exec false) && echo "bad" || echo "good")" = z"good"
38 test_tolerate_failure 'POSIX eval behavior' '
39 setec() { ! : || eval "ec=\$?"; } &&
40 setec && test z"$ec" = z"1"
43 test_tolerate_failure 'POSIX trap EXIT behavior' '
44 nomsg() { trap "echo bad" EXIT; } &&
45 result="$(nomsg && trap - EXIT)" &&
46 test z"${result:-good}" = z"good"
49 test_tolerate_failure 'POSIX alias' '
50 alias some=alias
53 test_tolerate_failure LASTOK 'POSIX unalias -a (no subshell)' '
54 alias some=alias &&
55 unalias -a
58 test_tolerate_failure LASTOK 'POSIX unalias -a (subshell w/o aliases)' '
59 unalias -a
62 test_tolerate_failure 'POSIX function redir ops' '
63 redir() {
64 echo bad stderr >&2
65 echo bad stdout
66 } >/dev/null 2>&1 &&
67 test z"$(redir && echo good)" = z"good"
70 test_tolerate_failure 'unsettable LINENO' '
71 { unset LINENO || :; }
74 test_tolerate_failure 'working awk implementation' '
75 # mawk will have a segmentation fault with this
76 awk "
77 function f1(a1) {}
78 function f2(a2) {
79 f1(a2);
80 for (;;) break;
82 function f3() {
83 f2(a3);
84 a3[1];
86 BEGIN { exit; }
89 test_tolerate_failure 'POSIX awk pattern brace quantifiers' '
90 # mawk stupidly does not support these
91 # can you hear us mocking you mawk?
92 result="$(echo not-mawk | awk "/^[a-z-]{5,}\$/")" &&
93 test z"$result" = z"not-mawk"
96 test_expect_success 'POSIX awk ENVIRON array' '
97 EVAR="This is some test here" &&
98 export EVAR &&
99 val="$(awk "BEGIN{exit}END{print ENVIRON[\"EVAR\"]}")" &&
100 test z"$val" = z"$EVAR"
103 test_expect_success 'POSIX awk ENVIRON array detects unset' '
104 EVAR=1 &&
105 unset EVAR &&
106 val="$(awk "BEGIN{exit}END{print ((\"EVAR\" in ENVIRON)?1:0)}")" &&
107 test z"$val" = z"0" &&
108 EVAR= &&
109 export EVAR &&
110 val="$(awk "BEGIN{exit}END{print ((\"EVAR\" in ENVIRON)?1:0)}")" &&
111 test z"$val" = z"1"
114 test_expect_success 'POSIX awk backslash continues line' - <<'EOT'
115 val="$(awk '
116 BEGIN {exit}
117 END {
118 x = "\
119 One \
120 Line \
121 Now \
123 n = split(x, a, " ")
124 ans = ""
125 for (i = 1; i <= n; ++i) {
126 if (i > 1) ans = ans " "
127 ans = ans a[i]
129 print ans
131 ')" &&
132 test z"$val" = z"One Line Now"
135 test_tolerate_failure 'POSIX export unset var exports it' '
136 say_color info "# POSIX is, regrettably, quite explicit about this" &&
137 say_color info "# POSIX requires EVERY exported variable to be in child environment" &&
138 unset NO_SUCH_VAR &&
139 export NO_SUCH_VAR &&
140 printenv NO_SUCH_VAR >/dev/null
143 v_count_words() { eval "${1:-scratch_}=\"$(( $# - 1 ))\""; }
145 test_expect_success 'count word function works' '
146 sane_unset scratch_ &&
147 v_count_words && test "$scratch_" = "-1" &&
148 sane_unset scratch_ &&
149 test "${scratch_+yes}" != "yes" &&
150 v_count_words zero && test "$zero" = "0" &&
151 v_count_words one 1 && test "$one" = "1" &&
152 v_count_words one "a b c" && test "$one" = "1" &&
153 v_count_words two a b && test "$two" = "2" &&
154 v_count_words three three 2 1 && test "$three" = "3" &&
155 line="\"a b c d\" e f g" &&
156 eval v_count_words four "$line" && test "$four" = "4" &&
157 line="\"a b c d\" e" &&
158 v_count_words five $line && test "$five" = "5" &&
159 test "${scratch_+yes}" != "yes"
162 test_expect_success 'cat -' '
163 echo one >one &&
164 echo two >two &&
165 echo three >three &&
166 cat <<-EOT >four &&
167 four
169 echo four >expected &&
170 test_cmp four expected &&
171 cat - <<-EOT >five &&
172 five
174 echo five >expected &&
175 test_cmp five expected &&
176 cat - three <<-EOT >xthree &&
179 echo x >expected &&
180 echo three >>expected &&
181 test_cmp xthree expected &&
182 cat four - <<-EOT >foury &&
185 echo four >expected &&
186 echo y >>expected &&
187 test_cmp foury expected &&
188 cat one - two <<-EOT >one_two &&
191 echo one >expected &&
192 echo 1.5 >>expected &&
193 echo two >>expected &&
194 test_cmp one_two expected &&
195 cat one - two - three <<-EOT >one_two_three &&
198 echo one >expected &&
199 echo 1.9 >>expected &&
200 echo two >>expected &&
201 echo three >>expected &&
202 test_cmp one_two_three expected
205 test_done