info.sh
1 #! /usr/bin/env atf-sh 2 3 . $(atf_get_srcdir)/test_environment.sh 4 5 tests_init \ 6 raw_json_single \ 7 raw_json_multiple \ 8 raw_json_compact_multiple \ 9 raw_json_all \ 10 info_all \ 11 info_full \ 12 info_name_only \ 13 info_comment \ 14 info_deps \ 15 info_rdeps \ 16 info_files \ 17 info_size \ 18 info_origin \ 19 info_prefix \ 20 info_quiet \ 21 info_exists \ 22 info_not_found \ 23 info_no_args \ 24 info_glob \ 25 info_regex \ 26 info_locked \ 27 info_options \ 28 info_pkg_file 29 30 # Helper: register a rich test package with deps, files, options 31 setup_pkg() { 32 touch file1 file2 33 mkdir -p mydir 34 35 atf_check -s exit:0 sh ${RESOURCEDIR}/test_subr.sh new_pkg "dep" "dep" "1.0" "${TMPDIR}" 36 atf_check -o ignore -s exit:0 pkg register -M dep.ucl 37 38 atf_check -s exit:0 sh ${RESOURCEDIR}/test_subr.sh new_pkg "test" "test" "2.5" "${TMPDIR}" 39 cat << EOF >> test.ucl 40 deps: { 41 dep: { 42 origin: dep, 43 version: "1.0" 44 } 45 } 46 files: { 47 ${TMPDIR}/file1: "", 48 ${TMPDIR}/file2: "", 49 } 50 directories: { 51 ${TMPDIR}/mydir: "", 52 } 53 options: { 54 "OPT1": "on", 55 "OPT2": "off", 56 } 57 EOF 58 atf_check -o ignore -s exit:0 pkg register -M test.ucl 59 } 60 61 info_all_body() { 62 setup_pkg 63 64 # pkg info -a lists all installed packages 65 atf_check \ 66 -o match:"dep-1.0" \ 67 -o match:"test-2.5" \ 68 -s exit:0 \ 69 pkg info -a 70 71 # quiet mode 72 atf_check \ 73 -o match:"dep-1.0" \ 74 -o match:"test-2.5" \ 75 -s exit:0 \ 76 pkg info -qa 77 } 78 79 info_full_body() { 80 setup_pkg 81 82 # Single package with no flags → full info 83 atf_check \ 84 -o match:"Name" \ 85 -o match:"Version" \ 86 -o match:"Comment" \ 87 -o match:"Prefix" \ 88 -s exit:0 \ 89 pkg info test 90 } 91 92 info_name_only_body() { 93 setup_pkg 94 95 # -E: show name only (exit 0 if exists) 96 atf_check \ 97 -o match:"test" \ 98 -s exit:0 \ 99 pkg info -E test 100 } 101 102 info_comment_body() { 103 setup_pkg 104 105 # -I: show comment 106 atf_check \ 107 -o match:"a test" \ 108 -s exit:0 \ 109 pkg info -I test 110 } 111 112 info_deps_body() { 113 setup_pkg 114 115 # -d: show deps 116 atf_check \ 117 -o match:"dep-1.0" \ 118 -s exit:0 \ 119 pkg info -d test 120 } 121 122 info_rdeps_body() { 123 setup_pkg 124 125 # -r: show reverse deps 126 atf_check \ 127 -o match:"test-2.5" \ 128 -s exit:0 \ 129 pkg info -r dep 130 } 131 132 info_files_body() { 133 setup_pkg 134 135 # -l: list files 136 atf_check \ 137 -o match:"${TMPDIR}/file1" \ 138 -o match:"${TMPDIR}/file2" \ 139 -s exit:0 \ 140 pkg info -l test 141 142 # quiet mode 143 atf_check \ 144 -o match:"${TMPDIR}/file1" \ 145 -s exit:0 \ 146 pkg info -ql test 147 } 148 149 info_size_body() { 150 setup_pkg 151 152 # -s: show flat size 153 atf_check \ 154 -o match:"0.00B" \ 155 -s exit:0 \ 156 pkg info -s test 157 } 158 159 info_origin_body() { 160 setup_pkg 161 162 # -o: show origin 163 atf_check \ 164 -o match:"test" \ 165 -s exit:0 \ 166 pkg info -o test 167 } 168 169 info_prefix_body() { 170 setup_pkg 171 172 # -p: show prefix 173 atf_check \ 174 -o match:"${TMPDIR}" \ 175 -s exit:0 \ 176 pkg info -p test 177 } 178 179 info_quiet_body() { 180 setup_pkg 181 182 # -q with -a: just name-version 183 OUTPUT=$(pkg info -qa) 184 case "$OUTPUT" in 185 *"Name"*|*"Comment"*|*"Prefix"*) 186 atf_fail "Quiet mode should not show field labels" ;; 187 esac 188 189 # Should still list packages 190 echo "$OUTPUT" | grep -q "test-2.5" || atf_fail "test-2.5 not listed" 191 echo "$OUTPUT" | grep -q "dep-1.0" || atf_fail "dep-1.0 not listed" 192 } 193 194 info_exists_body() { 195 setup_pkg 196 197 # -e: exit 0 if package exists 198 atf_check \ 199 -s exit:0 \ 200 pkg info -e test 201 202 # exit 1 if not 203 atf_check \ 204 -s exit:1 \ 205 pkg info -e nonexistent 206 } 207 208 info_not_found_body() { 209 setup_pkg 210 211 # Non-existent package 212 atf_check \ 213 -e match:"No package.*matching" \ 214 -s exit:1 \ 215 pkg info nosuchpkg 216 217 # Quiet: no stderr output 218 atf_check \ 219 -e empty \ 220 -s exit:1 \ 221 pkg info -q nosuchpkg 222 } 223 224 info_no_args_body() { 225 # No packages installed, no args → lists nothing 226 atf_check \ 227 -o empty \ 228 -s exit:0 \ 229 pkg info 230 } 231 232 info_glob_body() { 233 setup_pkg 234 235 # -g: glob matching 236 atf_check \ 237 -o match:"test-2.5" \ 238 -o not-match:"dep-1.0" \ 239 -s exit:0 \ 240 pkg info -g 'tes*' 241 242 # Match both 243 atf_check \ 244 -o match:"test-2.5" \ 245 -o match:"dep-1.0" \ 246 -s exit:0 \ 247 pkg info -g '*' 248 249 # No match 250 atf_check \ 251 -e match:"No package" \ 252 -s exit:1 \ 253 pkg info -g 'zzz*' 254 } 255 256 info_regex_body() { 257 setup_pkg 258 259 # -x: regex matching 260 atf_check \ 261 -o match:"test-2.5" \ 262 -o not-match:"dep-1.0" \ 263 -s exit:0 \ 264 pkg info -x '^tes' 265 266 # Match both 267 atf_check \ 268 -o match:"test-2.5" \ 269 -o match:"dep-1.0" \ 270 -s exit:0 \ 271 pkg info -x '.*' 272 } 273 274 info_locked_body() { 275 setup_pkg 276 277 # Lock a package, then info -k should show lock status 278 atf_check -o ignore -s exit:0 pkg lock -y test 279 280 atf_check \ 281 -o match:"yes" \ 282 -s exit:0 \ 283 pkg info -k test 284 285 atf_check -o ignore -s exit:0 pkg unlock -y test 286 } 287 288 info_options_body() { 289 setup_pkg 290 291 # -O: show options (registered via manifest) 292 atf_check \ 293 -o match:"OPT1" \ 294 -o match:"OPT2" \ 295 -s exit:0 \ 296 pkg info -f test 297 } 298 299 info_pkg_file_body() { 300 # -F: info from a .pkg file directly 301 atf_check -s exit:0 sh ${RESOURCEDIR}/test_subr.sh new_pkg "filepkg" "filepkg" "3.0" "/" 302 touch a 303 echo ${TMPDIR}/a > plist 304 atf_check -o ignore pkg create -M filepkg.ucl -p plist 305 306 atf_check \ 307 -o match:"filepkg" \ 308 -o match:"3.0" \ 309 -s exit:0 \ 310 pkg info -F filepkg-3.0.pkg 311 312 # -l on a file 313 atf_check \ 314 -o match:"${TMPDIR}/a" \ 315 -s exit:0 \ 316 pkg info -lF filepkg-3.0.pkg 317 } 318 319 raw_json_single_body() { 320 atf_require python3 "Requires python3 to run this test" 321 atf_check -s exit:0 sh ${RESOURCEDIR}/test_subr.sh new_pkg "pkgA" "pkgA" "1.0" "/" 322 touch a 323 echo ${TMPDIR}/a > plist 324 atf_check pkg create -M pkgA.ucl -p plist 325 atf_check -o ignore pkg add pkgA-1.0.pkg 326 # Single package raw JSON should be valid JSON (array with one element) 327 atf_check -o save:out.json pkg info --raw --raw-format json pkgA 328 atf_check -o ignore -e empty python3 -m json.tool out.json 329 } 330 331 raw_json_multiple_body() { 332 atf_require python3 "Requires python3 to run this test" 333 atf_check -s exit:0 sh ${RESOURCEDIR}/test_subr.sh new_pkg "pkgA" "pkgA" "1.0" "/" 334 touch a 335 echo ${TMPDIR}/a > plist 336 atf_check pkg create -M pkgA.ucl -p plist 337 atf_check -o ignore pkg add pkgA-1.0.pkg 338 339 atf_check -s exit:0 sh ${RESOURCEDIR}/test_subr.sh new_pkg "pkgB" "pkgB" "2.0" "/" 340 touch b 341 echo ${TMPDIR}/b > plist 342 atf_check pkg create -M pkgB.ucl -p plist 343 atf_check -o ignore pkg add pkgB-2.0.pkg 344 345 # Two packages raw JSON must be a valid JSON array 346 atf_check -o save:out.json pkg info --raw --raw-format json pkgA pkgB 347 atf_check -o ignore -e empty python3 -m json.tool out.json 348 # Verify it is a JSON array with 2 elements 349 atf_check -o inline:"2\n" python3 -c "import json; print(len(json.load(open('out.json'))))" 350 } 351 352 raw_json_compact_multiple_body() { 353 atf_require python3 "Requires python3 to run this test" 354 atf_check -s exit:0 sh ${RESOURCEDIR}/test_subr.sh new_pkg "pkgA" "pkgA" "1.0" "/" 355 touch a 356 echo ${TMPDIR}/a > plist 357 atf_check pkg create -M pkgA.ucl -p plist 358 atf_check -o ignore pkg add pkgA-1.0.pkg 359 360 atf_check -s exit:0 sh ${RESOURCEDIR}/test_subr.sh new_pkg "pkgB" "pkgB" "2.0" "/" 361 touch b 362 echo ${TMPDIR}/b > plist 363 atf_check pkg create -M pkgB.ucl -p plist 364 atf_check -o ignore pkg add pkgB-2.0.pkg 365 366 # json-compact with multiple packages must also be valid JSON 367 atf_check -o save:out.json pkg info --raw --raw-format json-compact pkgA pkgB 368 atf_check -o ignore -e empty python3 -m json.tool out.json 369 atf_check -o inline:"2\n" python3 -c "import json; print(len(json.load(open('out.json'))))" 370 } 371 372 raw_json_all_body() { 373 atf_require python3 "Requires python3 to run this test" 374 atf_check -s exit:0 sh ${RESOURCEDIR}/test_subr.sh new_pkg "pkgA" "pkgA" "1.0" "/" 375 touch a 376 echo ${TMPDIR}/a > plist 377 atf_check pkg create -M pkgA.ucl -p plist 378 atf_check -o ignore pkg add pkgA-1.0.pkg 379 380 atf_check -s exit:0 sh ${RESOURCEDIR}/test_subr.sh new_pkg "pkgB" "pkgB" "2.0" "/" 381 touch b 382 echo ${TMPDIR}/b > plist 383 atf_check pkg create -M pkgB.ucl -p plist 384 atf_check -o ignore pkg add pkgB-2.0.pkg 385 386 # -a with raw JSON must be a valid JSON array 387 atf_check -o save:out.json pkg info -a --raw --raw-format json 388 atf_check -o ignore -e empty python3 -m json.tool out.json 389 atf_check -o inline:"2\n" python3 -c "import json; print(len(json.load(open('out.json'))))" 390 }