File: test-lib.sh

package info (click to toggle)
patch 2.8-2
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 7,888 kB
  • sloc: ansic: 41,776; sh: 7,300; yacc: 1,858; makefile: 143; sed: 54
file content (205 lines) | stat: -rw-r--r-- 4,205 bytes parent folder | download
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
# Library for simple test scripts
# Copyright 2009-2025 Free Software Foundation, Inc.
#
# Copying and distribution of this file, with or without modification,
# in any medium, are permitted without royalty provided the copyright
# notice and this notice are preserved.

# FIXME: Requires a version of diff that understands "-u".

_require_gnu_diff() {
    case "`diff --version 2> /dev/null`" in
    *GNU*)
	;;
    *)
	echo "This test requires GNU diff" >&2
	exit 77
    esac
}

_try_chmod() {
    chmod "$1" "$2" && test "`ls -l "$2" | cut -b2-10`" = "$3"
}

_require_chmod() {
    tmp=`mktemp working-chmod.XXXXXXXX`
    if ! _try_chmod 644 "$tmp" "rw-r--r--" || \
       ! _try_chmod 600 "$tmp" "rw-------"; then
	rm -f "$tmp"
	echo "This test requires chmod support" >&2
	exit 77
    fi
    rm -f "$tmp"
}

_require_hardlinks() {
    tmpdir=`mktemp -d hardlinks.XXXXXXXX`
    if ! touch "$tmpdir/f" ||
       ! ln "$tmpdir/f" "$tmpdir/g"; then
	rm -rf "$tmpdir"
	echo "This test requires hardlink support" >&2
	exit 77
    fi
    rm -rf "$tmpdir"
}

_require_symlinks() {
    tmpdir=`mktemp -d hardlinks.XXXXXXXX`
    if ! touch "$tmpdir/f" ||
       ! ln -s "f" "$tmpdir/g"; then
	rm -rf "$tmpdir"
	echo "This test requires symlink support" >&2
	exit 77
    fi
    rm -rf "$tmpdir"
}

_require_special_characters() {
    if ! tmp=`mktemp '	 '.XXXXXXXX`; then
	echo "This test requires special character support in filenames" >&2
	exit 77
    fi
    rm -f "$tmp"
}

require() {
    utility="$1"
    if type _require_${utility} > /dev/null 2> /dev/null; then
	_require_${utility}
    elif ! type "${utility}" > /dev/null 2> /dev/null; then
	echo "This test requires the ${utility} utility" >&2
	exit 77
    fi
}

have_ed() {
    type ed >/dev/null 2>/dev/null
}

use_tmpdir() {
    tmpdir=$abs_top_builddir/tests/tmp.$$
    mkdir "$tmpdir" && cd "$tmpdir" || exit 2
}

use_local_patch() {
    test -n "$PATCH" || PATCH=$abs_top_builddir/src/patch

    eval 'patch() {
	if test -n "$GDB" ; then
	  printf "\n\n" >&3
	  gdbserver localhost:53153 $PATCH "$@" 2>&3
	else
          $PATCH "$@"
	fi
    }'
}

clean_env() {
    unset PATCH_GET POSIXLY_CORRECT QUOTING_STYLE SIMPLE_BACKUP_SUFFIX \
	  VERSION_CONTROL PATCH_VERSION_CONTROL GDB
}

if diff -u -L expected -L got /dev/null /dev/null 2> /dev/null; then
    eval '_compare() {
	diff -u -L expected -L got "$1" "$2"
    }'
else
    eval '_compare() {
	echo "expected:"
	cat "$1"
	echo "got:"
	cat "$2"
    }'
fi

_check() {
    _start_test "$@"
    expected=`cat`
    if got=`set +x; eval "$*" 3>&2 </dev/null 2>&1` && \
            test "$expected" = "$got" ; then
	echo "ok"
	checks_succeeded="$checks_succeeded + 1"
    else
	echo "FAILED"
	if test "$expected" != "$got" ; then
	    echo "$expected" > expected~
	    echo "$got" > got~
	    _compare expected~ got~
	    rm -f expected~ got~
	fi
	checks_failed="$checks_failed + 1"
    fi
}

check() {
    _check "$@"
}

ncheck() {
    _check "$@" < /dev/null
}

cleanup() {
    status=$?
    checks_succeeded=`expr $checks_succeeded`
    checks_failed=`expr $checks_failed`
    checks_total=`expr $checks_succeeded + $checks_failed`
    if test $checks_total -gt 0 ; then
	if test $checks_failed -gt 0 && test $status -eq 0 ; then
	    status=1
	fi
	echo "$checks_total tests ($checks_succeeded passed," \
	     "$checks_failed failed)"
    fi
    if test -n "$tmpdir" ; then
	chmod -R u+rwx "$tmpdir" 2>/dev/null
	cd / && rm -rf "$tmpdir"
    fi
    exit $status
}

if ( eval 'test -n "${BASH_LINENO[0]}"' 2>/dev/null ); then
    eval '
	_start_test() {
	    printf "[${BASH_LINENO[2]}] %s -- " "$*"
	}'
else
    eval '
	_start_test() {
	    printf "* %s -- " "$*"
	}'
fi

# The seq utility is not universally available -- provide a replacement.
if ! type seq > /dev/null 2> /dev/null; then
    seq() {(
	case $# in
	0)	echo "seq: missing operand" >&2
	    return 1 ;;
	1)	set -- 1 1 $1 ;;
	2)  set -- $1 1 $2 ;;
	3)	;;
	*)	echo "seq: extra operands" >&2
	    return 1 ;;
	esac

	i=$1
	if test $2 -gt 0; then
	    op=-le
	else
	    op=-ge
	fi

	while test $i $op $3; do
	    echo $i
	    i=`expr $i + $2`
	done
    )}
fi

require cat
clean_env

checks_succeeded=0
checks_failed=0
trap cleanup 0