/ tests / frontend / rquery.sh
rquery.sh
  1  #! /usr/bin/env atf-sh
  2  
  3  . $(atf_get_srcdir)/test_environment.sh
  4  
  5  tests_init \
  6  	rquery_name_version \
  7  	rquery_all \
  8  	rquery_comment \
  9  	rquery_origin \
 10  	rquery_prefix \
 11  	rquery_deps \
 12  	rquery_rdeps \
 13  	rquery_options \
 14  	rquery_categories \
 15  	rquery_size \
 16  	rquery_eval \
 17  	rquery_eval_complex \
 18  	rquery_glob \
 19  	rquery_regex \
 20  	rquery_no_repo \
 21  	rquery_not_found \
 22  	rquery_multiple_pkgs \
 23  	rquery_multi_repo
 24  
 25  # Helper: set up a local file:// repo with rich packages
 26  setup_repo() {
 27  	atf_check -s exit:0 sh ${RESOURCEDIR}/test_subr.sh new_pkg "dep" "dep" "1.0" "/usr/local"
 28  	cat << EOF >> dep.ucl
 29  categories: [net]
 30  EOF
 31  	atf_check pkg create -o ${TMPDIR}/repo -M dep.ucl
 32  
 33  	cat << EOF > test.ucl
 34  name: test
 35  origin: test
 36  version: "2.5"
 37  maintainer: test
 38  categories: [devel, test]
 39  comment: a test
 40  www: http://test
 41  prefix: /usr/local
 42  abi: "*"
 43  desc: <<EOD
 44  This is a test
 45  EOD
 46  deps: {
 47      dep: {
 48          origin: dep,
 49          version: "1.0"
 50      }
 51  }
 52  options: {
 53      "OPT1": "on",
 54      "OPT2": "off",
 55  }
 56  EOF
 57  	atf_check pkg create -o ${TMPDIR}/repo -M test.ucl
 58  
 59  	atf_check -o ignore pkg repo ${TMPDIR}/repo
 60  
 61  	mkdir -p ${TMPDIR}/reposconf
 62  	cat << EOF > ${TMPDIR}/reposconf/test.conf
 63  test: {
 64      url: file://${TMPDIR}/repo,
 65      enabled: true
 66  }
 67  EOF
 68  
 69  	atf_check -o ignore \
 70  		pkg -o REPOS_DIR="${TMPDIR}/reposconf" \
 71  		update
 72  
 73  	RDIR="${TMPDIR}/reposconf"
 74  }
 75  
 76  rquery_name_version_body() {
 77  	setup_repo
 78  
 79  	# %n %v: name and version
 80  	atf_check \
 81  		-o inline:"test-2.5\n" \
 82  		-s exit:0 \
 83  		pkg -o REPOS_DIR="${RDIR}" rquery -U "%n-%v" test
 84  
 85  	atf_check \
 86  		-o inline:"dep-1.0\n" \
 87  		-s exit:0 \
 88  		pkg -o REPOS_DIR="${RDIR}" rquery -U "%n-%v" dep
 89  }
 90  
 91  rquery_all_body() {
 92  	setup_repo
 93  
 94  	# -a: all packages
 95  	atf_check \
 96  		-o match:"dep" \
 97  		-o match:"test" \
 98  		-s exit:0 \
 99  		pkg -o REPOS_DIR="${RDIR}" rquery -Ua "%n"
100  }
101  
102  rquery_comment_body() {
103  	setup_repo
104  
105  	# %c: comment
106  	atf_check \
107  		-o inline:"a test\n" \
108  		-s exit:0 \
109  		pkg -o REPOS_DIR="${RDIR}" rquery -U "%c" test
110  }
111  
112  rquery_origin_body() {
113  	setup_repo
114  
115  	# %o: origin
116  	atf_check \
117  		-o inline:"test\n" \
118  		-s exit:0 \
119  		pkg -o REPOS_DIR="${RDIR}" rquery -U "%o" test
120  }
121  
122  rquery_prefix_body() {
123  	setup_repo
124  
125  	# %p: prefix
126  	atf_check \
127  		-o inline:"/usr/local\n" \
128  		-s exit:0 \
129  		pkg -o REPOS_DIR="${RDIR}" rquery -U "%p" test
130  }
131  
132  rquery_deps_body() {
133  	setup_repo
134  
135  	# %dn %dv: dependency name and version
136  	atf_check \
137  		-o inline:"dep 1.0\n" \
138  		-s exit:0 \
139  		pkg -o REPOS_DIR="${RDIR}" rquery -U "%dn %dv" test
140  
141  	# %#d: dep count
142  	atf_check \
143  		-o inline:"1\n" \
144  		-s exit:0 \
145  		pkg -o REPOS_DIR="${RDIR}" rquery -U "%#d" test
146  
147  	# dep has no deps
148  	atf_check \
149  		-o inline:"0\n" \
150  		-s exit:0 \
151  		pkg -o REPOS_DIR="${RDIR}" rquery -U "%#d" dep
152  }
153  
154  rquery_rdeps_body() {
155  	setup_repo
156  
157  	# %rn: reverse dep name
158  	atf_check \
159  		-o inline:"test\n" \
160  		-s exit:0 \
161  		pkg -o REPOS_DIR="${RDIR}" rquery -U "%rn" dep
162  
163  	# %#r: reverse dep count
164  	atf_check \
165  		-o inline:"1\n" \
166  		-s exit:0 \
167  		pkg -o REPOS_DIR="${RDIR}" rquery -U "%#r" dep
168  
169  	# test has no rdeps
170  	atf_check \
171  		-o inline:"0\n" \
172  		-s exit:0 \
173  		pkg -o REPOS_DIR="${RDIR}" rquery -U "%#r" test
174  }
175  
176  rquery_options_body() {
177  	setup_repo
178  
179  	# %Ok %Ov: option key and value
180  	atf_check \
181  		-o match:"OPT1" \
182  		-o match:"OPT2" \
183  		-s exit:0 \
184  		pkg -o REPOS_DIR="${RDIR}" rquery -U "%Ok %Ov" test
185  
186  	# %#O: option count
187  	atf_check \
188  		-o inline:"2\n" \
189  		-s exit:0 \
190  		pkg -o REPOS_DIR="${RDIR}" rquery -U "%#O" test
191  
192  	# dep has no options
193  	atf_check \
194  		-o inline:"0\n" \
195  		-s exit:0 \
196  		pkg -o REPOS_DIR="${RDIR}" rquery -U "%#O" dep
197  }
198  
199  rquery_categories_body() {
200  	setup_repo
201  
202  	# %Cn: category names
203  	atf_check \
204  		-o match:"devel" \
205  		-o match:"test" \
206  		-s exit:0 \
207  		pkg -o REPOS_DIR="${RDIR}" rquery -U "%Cn" test
208  
209  	# %#C: category count
210  	atf_check \
211  		-o inline:"2\n" \
212  		-s exit:0 \
213  		pkg -o REPOS_DIR="${RDIR}" rquery -U "%#C" test
214  
215  	atf_check \
216  		-o inline:"1\n" \
217  		-s exit:0 \
218  		pkg -o REPOS_DIR="${RDIR}" rquery -U "%#C" dep
219  }
220  
221  rquery_size_body() {
222  	setup_repo
223  
224  	# %sh: human-readable size
225  	atf_check \
226  		-o match:"0" \
227  		-s exit:0 \
228  		pkg -o REPOS_DIR="${RDIR}" rquery -U "%sh" test
229  
230  	# %sb: raw bytes
231  	atf_check \
232  		-o inline:"0\n" \
233  		-s exit:0 \
234  		pkg -o REPOS_DIR="${RDIR}" rquery -U "%sb" test
235  }
236  
237  rquery_eval_body() {
238  	setup_repo
239  
240  	# -e: evaluation - match by name
241  	atf_check \
242  		-o inline:"test\n" \
243  		-s exit:0 \
244  		pkg -o REPOS_DIR="${RDIR}" rquery -U -e '%n == "test"' "%n"
245  
246  	# No match
247  	atf_check \
248  		-o empty \
249  		-s exit:0 \
250  		pkg -o REPOS_DIR="${RDIR}" rquery -U -e '%n == "nosuch"' "%n"
251  
252  	# Not equal
253  	atf_check \
254  		-o inline:"dep\n" \
255  		-s exit:0 \
256  		pkg -o REPOS_DIR="${RDIR}" rquery -U -e '%n != "test"' "%n"
257  
258  	# Has deps
259  	atf_check \
260  		-o inline:"test\n" \
261  		-s exit:0 \
262  		pkg -o REPOS_DIR="${RDIR}" rquery -U -e "%#d > 0" "%n"
263  
264  	# Has no deps
265  	atf_check \
266  		-o inline:"dep\n" \
267  		-s exit:0 \
268  		pkg -o REPOS_DIR="${RDIR}" rquery -U -e "%#d == 0" "%n"
269  }
270  
271  rquery_eval_complex_body() {
272  	setup_repo
273  
274  	# AND: has deps AND has options
275  	atf_check \
276  		-o inline:"test\n" \
277  		-s exit:0 \
278  		pkg -o REPOS_DIR="${RDIR}" rquery -U -e "%#d > 0 && %#O > 0" "%n"
279  
280  	# OR: has deps OR name is dep
281  	atf_check \
282  		-o match:"test" \
283  		-o match:"dep" \
284  		-s exit:0 \
285  		pkg -o REPOS_DIR="${RDIR}" rquery -U -e '%#d > 0 || %n == "dep"' "%n"
286  
287  	# No results with impossible condition
288  	atf_check \
289  		-o empty \
290  		-s exit:0 \
291  		pkg -o REPOS_DIR="${RDIR}" rquery -U -e "%#d > 0 && %#O == 0" "%n"
292  
293  	# Eval with explicit pattern
294  	atf_check \
295  		-o inline:"test\n" \
296  		-s exit:0 \
297  		pkg -o REPOS_DIR="${RDIR}" rquery -U -e '%n == "test"' "%n" test
298  
299  	# Eval with pattern that does not match
300  	atf_check \
301  		-o empty \
302  		-s exit:1 \
303  		pkg -o REPOS_DIR="${RDIR}" rquery -U -e '%n == "test"' "%n" nosuch
304  }
305  
306  rquery_glob_body() {
307  	setup_repo
308  
309  	# -g: glob match
310  	atf_check \
311  		-o inline:"test\n" \
312  		-s exit:0 \
313  		pkg -o REPOS_DIR="${RDIR}" rquery -Ug "%n" 'tes*'
314  
315  	# Glob matching all
316  	atf_check \
317  		-o match:"test" \
318  		-o match:"dep" \
319  		-s exit:0 \
320  		pkg -o REPOS_DIR="${RDIR}" rquery -Ug "%n" '*'
321  
322  	# Glob no match
323  	atf_check \
324  		-o empty \
325  		-s exit:1 \
326  		pkg -o REPOS_DIR="${RDIR}" rquery -Ug "%n" 'zzz*'
327  }
328  
329  rquery_regex_body() {
330  	setup_repo
331  
332  	# -x: regex match
333  	atf_check \
334  		-o inline:"test\n" \
335  		-s exit:0 \
336  		pkg -o REPOS_DIR="${RDIR}" rquery -Ux "%n" '^tes'
337  
338  	# Regex match all
339  	atf_check \
340  		-o match:"test" \
341  		-o match:"dep" \
342  		-s exit:0 \
343  		pkg -o REPOS_DIR="${RDIR}" rquery -Ux "%n" '.'
344  
345  	# Regex no match
346  	atf_check \
347  		-o empty \
348  		-s exit:1 \
349  		pkg -o REPOS_DIR="${RDIR}" rquery -Ux "%n" '^zzz'
350  }
351  
352  rquery_no_repo_body() {
353  	# No repo configured
354  	export REPOS_DIR=/nonexistent
355  	atf_check \
356  		-e match:"No active remote repositories" \
357  		-s exit:3 \
358  		pkg -C '' -R '' rquery -a "%n"
359  }
360  
361  rquery_not_found_body() {
362  	setup_repo
363  
364  	# Package not found
365  	atf_check \
366  		-o empty \
367  		-s exit:1 \
368  		pkg -o REPOS_DIR="${RDIR}" rquery -U "%n" nosuchpkg
369  }
370  
371  rquery_multiple_pkgs_body() {
372  	setup_repo
373  
374  	# Query two packages at once
375  	atf_check \
376  		-o match:"dep" \
377  		-o match:"test" \
378  		-s exit:0 \
379  		pkg -o REPOS_DIR="${RDIR}" rquery -U "%n" dep test
380  
381  	# Mixed format: name-version with comment
382  	atf_check \
383  		-o match:"dep-1.0: a test" \
384  		-o match:"test-2.5: a test" \
385  		-s exit:0 \
386  		pkg -o REPOS_DIR="${RDIR}" rquery -U "%n-%v: %c" dep test
387  }
388  
389  rquery_multi_repo_body() {
390  	# Create two separate repos with different packages
391  	atf_check -s exit:0 sh ${RESOURCEDIR}/test_subr.sh new_pkg "alpha" "alpha" "1.0" "/usr/local"
392  	atf_check pkg create -o ${TMPDIR}/repoA -M alpha.ucl
393  
394  	atf_check -s exit:0 sh ${RESOURCEDIR}/test_subr.sh new_pkg "beta" "beta" "2.0" "/usr/local"
395  	atf_check pkg create -o ${TMPDIR}/repoB -M beta.ucl
396  
397  	atf_check -o ignore pkg repo ${TMPDIR}/repoA
398  	atf_check -o ignore pkg repo ${TMPDIR}/repoB
399  
400  	mkdir -p ${TMPDIR}/reposconf
401  	cat << EOF > ${TMPDIR}/reposconf/multi.conf
402  repoA: {
403      url: file://${TMPDIR}/repoA,
404      enabled: true
405  }
406  repoB: {
407      url: file://${TMPDIR}/repoB,
408      enabled: true
409  }
410  EOF
411  
412  	atf_check -o ignore \
413  		pkg -o REPOS_DIR="${TMPDIR}/reposconf" \
414  		update
415  
416  	# Both repos queried by default
417  	atf_check \
418  		-o match:"alpha" \
419  		-o match:"beta" \
420  		-s exit:0 \
421  		pkg -o REPOS_DIR="${TMPDIR}/reposconf" \
422  		rquery -Ua "%n"
423  
424  	# -r: restrict to repoA
425  	atf_check \
426  		-o inline:"alpha\n" \
427  		-s exit:0 \
428  		pkg -o REPOS_DIR="${TMPDIR}/reposconf" \
429  		rquery -Ur repoA -a "%n"
430  
431  	# -r: restrict to repoB
432  	atf_check \
433  		-o inline:"beta\n" \
434  		-s exit:0 \
435  		pkg -o REPOS_DIR="${TMPDIR}/reposconf" \
436  		rquery -Ur repoB -a "%n"
437  }