summaryrefslogtreecommitdiff
path: root/regress/infra-unittests/check-portability.sh
blob: 617cacde80a7026e13c6431ccf3ec9eca8a7d946 (plain)
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
#! /bin/sh
# $NetBSD: check-portability.sh,v 1.3 2020/05/05 05:55:25 rillig Exp $
#
# Test cases for mk/check/check-portability.*.
#

set -eu

. "./test.subr"

# Runs the shell program for the given file.
check_portability_sh() {
	env	PATCHDIR='patches' \
		PREFIX='/nonexistent' \
		"$@" \
		sh "$pkgsrcdir/mk/check/check-portability.sh" \
		1>"$tmpdir/out" 2>&1 \
	&& exitcode=0 || exitcode=$?
}

# Runs the AWK program in standalone mode for the given file.
check_portability_awk() {
	env	CK_FNAME="$1" \
		CK_PROGNAME='check-portability.awk' \
		awk	-f "$pkgsrcdir/mk/check/check-subr.awk" \
			-f "$pkgsrcdir/mk/check/check-portability.awk" \
			"$1" \
		1>"$tmpdir/out" 2>&1 \
	&& exitcode=0 || exitcode=$?
}

test_case_set_up() {
	rm -rf "$tmpdir/work"
	mkdir "$tmpdir/work"
	cd "$tmpdir/work"
}

# TODO: remove the "work/" from the tests.
# The $tmpdir must be a bit structured:
# $tmpdir/pkgsrc	these files override the actual pkgsrc files
# $tmpdir/work		current working directory


if test_case_begin "test ... = ..."; then

	create_file_lines 'work/file' \
		'if [ "$var" = value ]; then' \
		'  ...' \
		'elif test "$var" = value ]; then' \
		'  ...' \
		'fi'

	check_portability_awk 'file'

	assert_that 'out' --file-is-empty
	assert_that $exitcode --equals 0

	test_case_end
fi


if test_case_begin 'test ... == ...'; then

	create_file_lines 'work/file' \
		'if [ "$var" == value ]; then' \
		'  ...' \
		'elif test "$var" == value ]; then' \
		'  ...' \
		'fi'

	check_portability_awk 'file'

	create_file 'expected' <<'EOF'
ERROR: [check-portability.awk] => Found test ... == ...:
ERROR: [check-portability.awk] file:1: if [ "$var" == value ]; then
ERROR: [check-portability.awk] file:3: elif test "$var" == value ]; then

Explanation:
===========================================================================
The "test" command, as well as the "[" command, are not required to know
the "==" operator. Only a few implementations like bash and some
versions of ksh support it.

When you run "test foo == foo" on a platform that does not support the
"==" operator, the result will be "false" instead of "true". This can
lead to unexpected behavior.

There are two ways to fix this error message. If the file that contains
the "test ==" is needed for building the package, you should create a
patch for it, replacing the "==" operator with "=". If the file is not
needed, add its name to the CHECK_PORTABILITY_SKIP variable in the
package Makefile.
===========================================================================

EOF
	assert_that 'out' --file-equals 'expected'
	assert_that $exitcode --equals 1

	test_case_end
fi


if test_case_begin 'configure patched, configure.in bad'; then

	create_file_lines 'work/patches/patch-aa' \
		'+++ configure 2020-05-04'
	create_file_lines 'work/configure' \
		'#! /bin/sh' \
		'good'
	create_file_lines 'work/configure.in' \
		'test a == b'

	check_portability_sh \
		'CHECK_PORTABILITY_EXPERIMENTAL=yes'

	assert_that "out" --file-is-empty
	assert_that $exitcode --equals 0

	test_case_end
fi


if test_case_begin 'configure patched and still bad'; then

	create_file_lines 'work/patches/patch-aa' \
		'+++ configure 2020-05-04'
	create_file_lines 'work/configure' \
		'#! /bin/sh' \
		'test a == b'

	check_portability_sh \
		'CHECK_PORTABILITY_EXPERIMENTAL=yes'

	create_file 'expected' <<'EOF'
ERROR: [check-portability.awk] => Found test ... == ...:
ERROR: [check-portability.awk] configure:2: test a == b

Explanation:
===========================================================================
The "test" command, as well as the "[" command, are not required to know
the "==" operator. Only a few implementations like bash and some
versions of ksh support it.

When you run "test foo == foo" on a platform that does not support the
"==" operator, the result will be "false" instead of "true". This can
lead to unexpected behavior.

There are two ways to fix this error message. If the file that contains
the "test ==" is needed for building the package, you should create a
patch for it, replacing the "==" operator with "=". If the file is not
needed, add its name to the CHECK_PORTABILITY_SKIP variable in the
package Makefile.
===========================================================================

EOF
	assert_that 'out' --file-equals 'expected'
	assert_that $exitcode --equals 1

	test_case_end
fi


if test_case_begin 'special characters in filenames'; then

	# Ensure that the filename matching for patched files
	# does not treat special characters as shell metacharacters.

	create_file_lines 'work/patches/patch-aa' \
		'+++ [[[[(`" 2020-05-04'
	create_file_lines 'work/+++ [[[[(`"' \
		'#! /bin/sh' \
		'test a = b'

	check_portability_sh \
		'CHECK_PORTABILITY_EXPERIMENTAL=yes'

	assert_that 'out' --file-is-empty
	assert_that $exitcode --equals 0

	test_case_end
fi


if test_case_begin 'no patches'; then

	# Ensure that no error message is printed when there are no
	# patch files.

	create_file_lines 'file' \
		'#! /bin/sh' \
		'test a = b'

	check_portability_sh

	assert_that 'out' --file-is-empty
	assert_that $exitcode --equals 0

	test_case_end
fi


if test_case_begin 'no experimental by default'; then

	create_file_lines 'configure.in' \
		'test a == b'

	check_portability_sh \
		'CHECK_PORTABILITY_EXPERIMENTAL=no'

	assert_that 'out' --file-is-empty
	assert_that $exitcode --equals 0

	test_case_end
fi