Gentoo Archives: gentoo-dev

From: Ionen Wolkens <ionen@g.o>
To: gentoo-dev@l.g.o
Subject: [gentoo-dev] [PATCH v3 2/2] eclass/tests/esed.sh: tests for esed.eclass
Date: Sat, 04 Jun 2022 20:47:21
Message-Id: 20220604204634.27567-3-ionen@gentoo.org
In Reply to: [gentoo-dev] [PATCH v3 0/2] Add esed.eclass for modifying with checks for no-op by Ionen Wolkens
1 Bit sloppy, but should cover most of it.
2
3 Signed-off-by: Ionen Wolkens <ionen@g.o>
4 ---
5 eclass/tests/esed.sh | 263 +++++++++++++++++++++++++++++++++++++++++++
6 1 file changed, 263 insertions(+)
7 create mode 100755 eclass/tests/esed.sh
8
9 diff --git a/eclass/tests/esed.sh b/eclass/tests/esed.sh
10 new file mode 100755
11 index 00000000000..d9dfe699e42
12 --- /dev/null
13 +++ b/eclass/tests/esed.sh
14 @@ -0,0 +1,263 @@
15 +#!/usr/bin/env bash
16 +# Copyright 2022 Gentoo Authors
17 +# Distributed under the terms of the GNU General Public License v2
18 +
19 +EAPI=8
20 +source tests-common.sh || exit
21 +
22 +inherit esed
23 +
24 +cd "${WORKDIR:-/dev/null}" || exit
25 +
26 +tsddied=n
27 +tsddie() {
28 + tsddied=y
29 + tsddiemsg=${*}
30 + echo "would die: ${tsddiemsg}" >&2
31 + # silence some further errors given didn't actually die
32 + sed() { :; }
33 + die() { :; }
34 +}
35 +
36 +tsdbegin() {
37 + tbegin "${1}"
38 + tsddied=n
39 + unset -f sed
40 + die() { tsddie "${@}"; }
41 +}
42 +
43 +tsdend() {
44 + if [[ ${1} == fatal* && ${tsddied} == n ]]; then
45 + tend 127 "should have died"
46 + elif [[ ${1} == fatal:* && ${tsddied} == y && ${tsddiemsg} != *"${1#fatal:}"* ]]; then
47 + tend 128 "died as expected but die message does not match '*${1#fatal:}*'"
48 + elif [[ ${1} == nonfatal && ${tsddied} == y ]]; then
49 + tend 129 "should not have died"
50 + else
51 + tend ${2:-0} "something went wrong(tm)"
52 + fi
53 +}
54 +
55 +tsdfile() {
56 + local file
57 + for file in "${@}"; do
58 + if [[ ${file%:*} ]]; then
59 + echo "${file%:*}" > "${file#*:}" || exit
60 + elif [[ -e ${file#*:} ]]; then
61 + rm -- "${file#*:}" || exit
62 + fi
63 + done
64 +}
65 +
66 +tsdcmp() {
67 + local contents
68 + contents=$(<"${1}") || exit
69 + if [[ ${contents} != "${2}" ]]; then
70 + echo "${FUNCNAME[0]}: '${contents}' != '${2}'"
71 + return 1
72 + fi
73 +}
74 +
75 +tsdbegin "esed: change on single file"
76 +tsdfile replace:file
77 +esed s/replace/new/ file
78 +tsdcmp file new
79 +tsdend nonfatal ${?}
80 +
81 +tsdbegin "esed: die due to no change on a single file"
82 +tsdfile keep:file
83 +esed s/replace/new/ file
84 +tsdcmp file keep
85 +tsdend fatal:no-op ${?}
86 +
87 +tsdbegin "esed: sequential changes"
88 +tsdfile replace1:file
89 +esed -e s/replace1/replace2/ -e s/replace2/new/ file
90 +tsdcmp file new
91 +tsdend nonfatal ${?}
92 +
93 +tsdbegin "esed: change on at least one of two files with ESED_VERBOSE=1"
94 +tsdfile keep:file1 replace:file2
95 +ESED_VERBOSE=1 esed s/replace/new/ file1 file2
96 +tsdcmp file1 keep &&
97 + tsdcmp file2 new
98 +tsdend nonfatal ${?}
99 +
100 +tsdbegin "esed: die due to no change on two files with ESED_VERBOSE=1"
101 +tsdfile keep:file{1..2}
102 +ESED_VERBOSE=1 esed s/replace/new/ file1 file2
103 +tsdcmp file1 keep &&
104 + tsdcmp file2 keep
105 +tsdend fatal:'no-op' ${?}
106 +
107 +tsdbegin "esed: -E/-n/-r arguments"
108 +tsdfile $'hide\nshow\nhide\nthis':file
109 +esed -E -n -r '/(show|this)/p' file
110 +tsdcmp file $'show\nthis'
111 +tsdend nonfatal ${?}
112 +
113 +tsdbegin "esed: die due to passing unrecognized -i"
114 +tsdfile replace:file
115 +esed -i s/replace/new/ file
116 +tsdend fatal:'unrecognized option'
117 +
118 +tsdbegin "esed: change files with one nicely named '-- -e'"
119 +tsdfile replace:"-- -e" replace:file
120 +esed -e s/replace/new/ file -- "-- -e"
121 +tsdcmp file new &&
122 + tsdcmp "-- -e" new
123 +tsdend nonfatal ${?}
124 +
125 +tsdbegin "esed: die due to no files in arguments"
126 +esed -e s/replace/new/ -e s/replace/new/
127 +tsdend fatal:'no files in'
128 +
129 +tsdbegin "esed: die due to missing file"
130 +tsdfile :missing
131 +esed s/replace/new/ missing
132 +tsdend fatal:'not a file'
133 +
134 +tsdbegin "enewsed: change on a new file"
135 +tsdfile replace:file :newfile
136 +enewsed s/replace/new/ file newfile
137 +tsdcmp file replace &&
138 + tsdcmp newfile new
139 +tsdend nonfatal ${?}
140 +
141 +tsdbegin "enewsed: die due to too many files"
142 +tsdfile replace:file1 replace:file2 :newfile
143 +enewsed s/replace/new/ file1 file2 newfile
144 +tsdend fatal:'exactly one input'
145 +
146 +tsdbegin "enewsed: die due to missing output file"
147 +tsdfile keep:file
148 +enewsed -e s/replace/new/ -e s/replace/new/ file
149 +tsdend fatal:'no files in'
150 +
151 +tsdbegin "enewsed: die due too few arguments beside files"
152 +tsdfile keep:file
153 +enewsed file newfile
154 +tsdend fatal:'too few arguments'
155 +
156 +tsdbegin "erepl: change on a single file"
157 +tsdfile 0000:file
158 +erepl 0 1 file
159 +tsdcmp file 1111
160 +tsdend nonfatal ${?}
161 +
162 +tsdbegin "erepl: die due to no change on a single file"
163 +tsdfile keep:file
164 +erepl missing new file
165 +tsdcmp file keep
166 +tsdend fatal:'no-op' ${?}
167 +
168 +tsdbegin "erepl: change on at least one of two files with ESED_VERBOSE=1"
169 +tsdfile keep:file1 replace:file2
170 +ESED_VERBOSE=1 erepl replace new file1 file2
171 +tsdcmp file1 keep &&
172 + tsdcmp file2 new
173 +tsdend nonfatal ${?}
174 +
175 +tsdbegin "erepl: die due to no change on two files with ESED_VERBOSE=1"
176 +tsdfile keep:file{1..2}
177 +ESED_VERBOSE=1 erepl replace new file1 file2
178 +tsdcmp file1 keep &&
179 + tsdcmp file2 keep
180 +tsdend fatal:'no-op' ${?}
181 +
182 +tsdbegin "erepl: change containing globs that should be ignored"
183 +tsdfile "*[0-9]{1,2}()":file
184 +erepl "*[0-9]{1,2}()" new file
185 +tsdcmp file new
186 +tsdend nonfatal ${?}
187 +
188 +tsdbegin "enewrepl: change on a new file"
189 +tsdfile replace:file :newfile
190 +enewrepl replace new file newfile
191 +tsdcmp file replace &&
192 + tsdcmp newfile new
193 +tsdend nonfatal ${?}
194 +
195 +tsdbegin "enewrepl: die due to too many files"
196 +tsdfile replace:file1 replace:file2 :newfile
197 +enewrepl replace new file1 file2 newfile
198 +tsdend fatal:'exactly one input'
199 +
200 +tsdbegin "enewrepl: die due to missing output file"
201 +tsdfile keep:file
202 +enewrepl replace new file
203 +tsdend fatal:'too few arguments'
204 +
205 +tsdbegin "erepld: delete matching lines"
206 +tsdfile $'match\nkeep\nmatch':file
207 +erepld ^match file
208 +tsdcmp file keep
209 +tsdend nonfatal ${?}
210 +
211 +tsdbegin "enewrepld: delete matching lines"
212 +tsdfile $'match\nkeep\nmatch':file :newfile
213 +enewrepld ^match file newfile
214 +tsdcmp file $'match\nkeep\nmatch' &&
215 + tsdcmp newfile keep
216 +tsdend nonfatal ${?}
217 +
218 +tsdbegin "ereplp: change matching lines"
219 +tsdfile $'match=0000\nkeep=0000\nmatch=0000':file
220 +ereplp ^match 0 1 file
221 +tsdcmp file $'match=1111\nkeep=0000\nmatch=1111'
222 +tsdend nonfatal ${?}
223 +
224 +tsdbegin "enewreplp: change matching lines"
225 +tsdfile $'match=0000\nkeep=0000\nmatch=0000':file :newfile
226 +enewreplp ^match 0 1 file newfile
227 +tsdcmp file $'match=0000\nkeep=0000\nmatch=0000' &&
228 + tsdcmp newfile $'match=1111\nkeep=0000\nmatch=1111'
229 +tsdend nonfatal ${?}
230 +
231 +tsdbegin "efind+esed: change found files"
232 +tsdfile keep:file1.find1 replace:file2.find1
233 +efind . -type f -name '*.find1' -erun esed s/replace/new/
234 +tsdcmp file1.find1 keep &&
235 + tsdcmp file2.find1 new
236 +tsdend nonfatal ${?}
237 +
238 +tsdbegin "efind+esed: die due no changes to found files"
239 +tsdfile keep:file1.find2 keep:file2.find2
240 +efind . -type f -name '*.find2' -erun esed s/replace/new/
241 +tsdcmp file1.find2 keep &&
242 + tsdcmp file2.find2 keep
243 +tsdend fatal:'no-op' ${?}
244 +
245 +tsdbegin "efind: die due to bad command"
246 +tsdfile keep:file.find3
247 +efind . -type f -name '*.find3' -erun ./no-such-file
248 +tsdend fatal:'failed: ./no-such-file' ${?}
249 +
250 +tsdbegin "efind: don't die for shell functions"
251 +tsdfile keep:*.find4
252 +tsd-test-func() {
253 + # would die in the function if there was an issue
254 + echo "${FUNCNAME[*]} running for files: ${*@Q}"
255 + local i=1
256 + (( --i )) # uh-oh, this leaves "unintended" failure return value
257 +}
258 +efind . -type f -name '*.find4' -erun tsd-test-func
259 +tsdend nonfatal
260 +
261 +tsdbegin "efind: die due to missing -erun"
262 +tsdfile keep:ignore-perm.find5
263 +efind . -type f -name '*.find5'
264 +tsdend fatal:'missing -erun'
265 +
266 +tsdbegin "efind: die due to no files found"
267 +efind . -type f -name '*.missing' -erun echo -n
268 +tsdend fatal:'no files from'
269 +
270 +echo
271 +echo "note: any error messages after 'would die:' can be ignored"
272 +if [[ ${tret} == 0 ]]; then
273 + echo "${0##*/} finished successfully"
274 +else
275 + echo "${0##*/} failed (status: ${tret})"
276 +fi
277 +texit
278 --
279 2.35.1