summaryrefslogtreecommitdiff
path: root/src/tests/check_all_args.in
blob: 3a2f1216509ecaed70c674949c139bd5abe938f2 (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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
#!/bin/sh -e
#---------------------------------------------------------------------
# run procenv with every argument
#---------------------------------------------------------------------

script_name=${0##*/}

# Use full path to build binary
procenv="@builddir@/procenv"
man_path="@man_path@"
driver="@procenv_platform@"

template="/tmp/${script_name}.XXXXXXXXXX"

json_tests=0
xml_tests=0

xml_checker=xmlwf
json_checker=json_pp

# XML disallows reliable encoding of control characters. Since the only
# anticipated possible source of such literal control characters is
# environment variables, such tests will be skipped if this variable has
# the value 1.
control_chars_in_env=0

# with random values
non_display_options="
--crumb-separator=.
--file=/tmp/foo
--format=crumb
--indent=7
--indent-char=X
--separator=X
"

lang_list=

die ()
{
	local str="$*"
	echo "ERROR: $str" >&2
	[ -n "$cmd" ] && echo "ERROR: (cmd='$cmd')" >&2
	[ -e "$stdout" ] && echo "ERROR: stdout:\n$(cat $stdout)" >&2
	[ -e "$stderr" ] && echo "ERROR: stderr:\n$(cat $stderr)" >&2
	exit 1
}

warn ()
{
	local str="$*"
	echo "WARNING: $str"
}

debug ()
{
	local str="$*"
	[ -n "$PROCENV_TEST_DEBUG" ] && echo "DEBUG: $str" || :
}

msg ()
{
	local str="$*"
	echo "INFO:LANG=$LANG:LC_ALL=$LC_ALL: $str"
}

run_command()
{
	_run_command success "$1" "$2" "$3"
}

run_command_expect_fail()
{
	_run_command failure "$1" "$2" "$3"
}

is_utf8()
{
	local locale="$1"

	echo "$locale"|grep -qi "utf.*8" && echo yes || echo no
}

_run_command()
{
	local expected_outcome="$1"
	local cmdline="$2"
	local stdout="$3"
	local stderr="$4"

	local failed=0

	[ -z "$cmdline" ] && die "need command"

	debug "Running: '$cmdline'"

	# interestingly, all environment except OpenBSD allow the code below
	# to run even if cmdline fail.s
	set +e
	{ eval "$cmdline"; ret=$?; } || :
	set -e

	if [ "$expected_outcome" = success ]
	then
		[ "$ret" -ne 0 ] && failed=1
	else 
		[ "$ret" -eq 0 ] && failed=1
	fi

	[ "$failed" -eq 1 ] && \
		die "Command '$cmdline' returned $ret but expected $expected_outcome"

	return 0
}

get_procenv_options()
{
	$procenv --help | grep -- '-[a-zA-Z0-9], --[a-zA-Z][a-zA-Z0-9]*' |\
		cut -d: -f1 | tr ',' '\n' | tr -d ' ' | egrep -v -- '(-h|--help)'
}

get_man_options()
{
	groff -a -man -Tascii "$man_path" |\
		grep -- '^ *<->[a-zA-Z0-9], <-><->[a-zA-Z0-9][a-zA-Z0-9]' |\
		tr -d '[<>]' |\
		sed 's/^ *//g' |\
		tr ',' '\n' |\
		tr -d ' ' |\
		egrep -v -- '(-h|--help)'
}

init()
{
	[ -n "$PROCENV_TEST_DISABLE" ] && msg "Tests disabled" && exit 0
    
	[ -f "$procenv" ] || die "cannot find binary: $procenv"

	command -v "$json_checker" >/dev/null 2>&1 && \
		json_tests=1 || warn "no $json_checker - JSON tests disabled"

	command -v "$xml_checker" >/dev/null 2>&1 && \
		xml_tests=1 || warn "no $xml_checker - XML tests disabled"
    
	env | grep -q "[[:cntrl:]]" && control_chars_in_env=1

	# Always test the default locales
	lang_list="C C.UTF-8 POSIX"

	if [ -n "$LANG" ] && [ "$LANG" != "C" ] && [ "$LANG" != "C.UTF-8" ]
	then
		lang_list="$lang_list $LANG"
	fi

	msg "language list: '$lang_list'"
}

run_tests()
{
	utf8=$(is_utf8 "$LANG")

	procenv_version=$($procenv --version|grep "version: [0-9]"|awk '{print $2}')
	[ -n "$procenv_version" ] || die "failed to query procenv version"

	# list of all output sections seen when running with an individual
	# argument.
	arg_sections=$(mktemp "$template")

	#---------------------------------------------------------------------
	msg "Running procenv version $procenv_version tests for language '$LANG'"

	arg="--help"
	msg "Ensure '$arg' works"

	stdout=$(mktemp "$template")
	stderr=$(mktemp "$template")
	cmd="$procenv $arg 1>\"$stdout\" 2>\"$stderr\""
	run_command "$cmd" "$stdout" "$stderr"

	[ -s "$stdout" ] || die "Expected stdout output in file $stdout"
	[ -s "$stderr" ] && die "Unexpected stderr output in file $stdout"

	rm -f "$stdout" "$stderr"

	#---------------------------------------------------------------------
	# XXX: note that we run with each argument in turn *BEFORE*
	# running with no arguments to avoid the situation where a
	# particular type of outputcauses procenv to hang/spin but it's
	# unclear which argument that output relates to if procenv is
	# run without args.

	msg "Ensure all documented options produce output"

	procenv_options=$(get_procenv_options)
	echo "$procenv_options" | while read arg
	do
		stdout=$(mktemp "$template")
		stderr=$(mktemp "$template")

		# No format option
		cmd="$procenv $arg 1>\"$stdout\" 2>\"$stderr\""
		msg "Running '$cmd'"
		run_command "$cmd" "$stdout" "$stderr"

		[ -z "$stdout" ] && die "No stdout output running '$cmd'"
		[ -s "$stderr" ] && die "Unexpected stderr output running '$cmd' ($stderr)"

		# Establish the name of the section
		section=$(head -1 "$stdout"|grep "^[^ ].*:$" || :)
		[ -z "$section" ] && die "No section title found running '$cmd'"

		# record the section we've seen
		echo "$section" >> "$arg_sections"

		rm -f "$stdout" "$stderr"

		# Text format
		cmd="$procenv --format=text $arg 1>\"$stdout\" 2>\"$stderr\""
		msg "Running '$cmd'"
		run_command "$cmd" "$stdout" "$stderr"

		[ -z "$stdout" ] && die "No stdout output running '$cmd'"
		[ -s "$stderr" ] && die "Unexpected stderr output running '$cmd' ($stderr)"

		if [ "$section" = "capabilities:" ]
		then
			tmp=$(grep 'CAP_LAST_CAP+1' "$stdout"|cut -d: -f2|tr -d ' ' || :)
			# take care to only perform this test on
			# platforms the _have_ capabilities :)
			if [ -n "$tmp" ]
			then
				last=$(expr "$tmp" - 1)

				[ -n "$last" ] || die "unable to establish last capability"
				{ grep -q "number: $last" "$stdout"; ret=$?; } || :
				if [ $ret -ne 0 ]
				then
					warn ""
					warn ""
					warn "Found extra capability procenv is not aware of (number $last)"
					warn ""
					warn "Note that this warning will appear if procenv is built on a"
					warn "system which is running a newer kernel than the"
					warn "development environment assumes (for example if built in a"
					warn "chroot for a back-level release of ubuntu)"
					warn ""
					warn "Please check for a newer version of procenv, "
					warn "build on a newer system, or raise a bug at:"
					warn ""
					warn "    @package_url@"
					warn ""
					warn ""
				fi
			fi
		fi

		rm -f "$stdout" "$stderr"

		# JSON format
		if [ "$json_tests" -eq 1 ]
		then
			cmd="$procenv --format=json $arg 1>\"$stdout\" 2>\"$stderr\""
			msg "Running '$cmd'"
			run_command "$cmd" "$stdout" "$stderr"

			[ -z "$stdout" ] && die "No stdout output running '$cmd'"
			[ -s "$stderr" ] && die "Unexpected stderr output running '$cmd' ($stderr)"

			run_command "$json_checker < \"$stdout\" >/dev/null" "" ""

			rm -f "$stdout" "$stderr"
		fi

		# XML format
		if [ "$xml_tests" -eq 1 ]
		then
			cmd="$procenv --format=xml $arg"
			if [ "$section" = "environment:" -a "$control_chars_in_env" -eq 1 ]
			then
				warn "Skipping '$cmd' as control characters found in environment"
			else
				msg "Running '$cmd'"
				cmd="$cmd 1>\"$stdout\" 2>\"$stderr\""
				run_command "$cmd" "$stdout" "$stderr"
				[ -z "$stdout" ] && die "No stdout output running '$cmd'"
				[ -s "$stderr" ] && die "Unexpected stderr output running '$cmd' ($stderr)"
				[ -z "$($xml_checker "$stdout")" ] || die "XML output not well-formed"
			fi

			rm -f "$stdout" "$stderr"
		fi
	done

	#---------------------------------------------------------------------
	msg "Ensure $procenv runs with no errors"

	stdout=$(mktemp "$template")
	stderr=$(mktemp "$template")
	$procenv 1>"$stdout" 2>"$stderr"
	rm -f "$stdout" "$stderr"

	# Save all section names to compare to the total set of section
	# names generated by running procenv once for each output
	# argument.
	sections=$($procenv|grep "^[a-z0-9][a-z0-9 ]*:$")

	msg "Ensuring output is sorted by section"

	main_sections=$(mktemp "$template")
	sorted_sections=$(mktemp "$template")
	unique_sections=$(mktemp "$template")
	unique_arg_sections=$(mktemp "$template")

	echo "$sections"|grep -q ^meta: || die "no meta section in main sections"
	grep -q ^meta: "$arg_sections"  || die "no meta section in arg sections"

	# ignore meta which always comes first
	echo "$sections"   |grep -v meta:         > "$main_sections"
	echo "$sections"   |grep -v meta:|sort    > "$sorted_sections"
	echo "$sections"   |grep -v meta:|sort -u > "$unique_sections"

	cat "$arg_sections"|egrep -v "^(meta|version|pathconf):"|\
		sort -u > "$unique_arg_sections"

	diff=$(diff "$main_sections" "$sorted_sections" || :)
	[ -n "$diff" ] && die "sections are not sorted - diff: $diff"

	diff=$(diff "$sorted_sections" "$unique_sections" || :)
	[ -n "$diff" ] && die "sections are not unique - diff: $diff"

	# Note that we don't compare the sorted and unique arg_sections
	# since there are 2 ways to produce the same section output
	# (short and long option).

	diff=$(diff "$unique_sections" "$unique_arg_sections" || :)
	[ -n "$diff" ] && die "default output sections do not match arg-collected sections - diff: $diff"

	rm -f "$main_sections" \
	      "$sorted_sections" \
	      "$unique_sections" \
	      "$arg_sections" \
	      "$unique_arg_sections"

	# pathconf cannot be handled like other sections since it is
	# indented under the mount output, so just check we can find
	# some evidence of it being displayed.
	#
	# Don't do this for the unknown driver since that doesn't produce
	# mount output.

	if [ "$driver" != "unknown" ]
	then
		$procenv|egrep -q "\<pathconf\>:" || die "Failed to find pathconf"
	fi

	#---------------------------------------------------------------------
	msg "Ensure --file=<file> works"

	out=$(mktemp "$template")
	stdout=$(mktemp "$template")
	stderr=$(mktemp "$template")
	cmd="$procenv --file=\"$out\" 1>\"$stdout\" 2>\"$stderr\""
	run_command "$cmd" "$stdout" "$stderr"

	[ -s "$stdout" ] && die "Unexpected stdout output: $stdout"
	[ -s "$stderr" ] && die "Unexpected stderr output: $stderr"
	[ -s "$out" ] || die "Expected output in file $out"

	rm "$out"
	rm -f "$stdout" "$stderr"

	#---------------------------------------------------------------------
	msg "Ensure running multiple '--file=<file> --version' works"

	out=$(mktemp "$template")
	stdout=$(mktemp "$template")
	stderr=$(mktemp "$template")
	cmd="$procenv --file=\"$out\" --version 1>\"$stdout\" 2>\"$stderr\""

	# run twice - we expect the output file to be overwritten
	for expected in 1 1
	do
		run_command "$cmd" "$stdout" "$stderr"
		[ -s "$stdout" ] && die "Unexpected stdout output: $stdout"
		[ -s "$stderr" ] && die "Unexpected stderr output: $stderr"
		[ -s "$out" ] || die "Expected output in file $out"

		count=$(grep -c ^version: "$out")
		[ "$count" -eq "$expected" ] || die "expected $expected match in file $out - got $count"
	done

	rm "$out"
	rm -f "$stdout" "$stderr"

	#---------------------------------------------------------------------
	msg "Ensure '--file-append --file=<file> --version' works"

	out=$(mktemp "$template")
	stdout=$(mktemp "$template")
	stderr=$(mktemp "$template")
	for options in "--file=\"$out\" --file-append" "--file-append --file=\"$out\""
	do
		cmd="$procenv $options --version 1>\"$stdout\" 2>\"$stderr\""

		run_command "$cmd" "$stdout" "$stderr"
		[ -s "$stdout" ] && die "Unexpected stdout output: $stdout"
		[ -s "$stderr" ] && die "Unexpected stderr output: $stderr"
		[ -s "$out" ] || die "Expected output in file $out"

		expected=1
		count=$(grep -c ^version: "$out")
		[ "$count" -eq "$expected" ] || die "expected $expected match in file $out - got $count"

		run_command "$cmd" "$stdout" "$stderr"
		[ -s "$stdout" ] && die "Unexpected stdout output: $stdout"
		[ -s "$stderr" ] && die "Unexpected stderr output: $stderr"
		[ -s "$out" ] || die "Expected output in file $out"

		# the output file should be appended
		expected=$((expected+1))
		count=$(grep -c ^version: "$out")
		[ "$count" -eq "$expected" ] || die "expected $expected match in file $out - got $count"

		rm "$out"
		rm -f "$stdout" "$stderr"
	done

	#---------------------------------------------------------------------
	msg "Ensure --output=stdout works"

	stdout=$(mktemp "$template")
	stderr=$(mktemp "$template")

	cmd="$procenv --output=stdout 1>\"$stdout\" 2>\"$stderr\""
	run_command "$cmd" "$stdout" "$stderr"

	[ -s "$stdout" ] || die "Expected stdout output in file $stdout"
	[ -s "$stderr" ] && die "Unexpected stderr output: $stderr"

	# ensure internal compression is working
	grep -q '^$' "$stdout" && die "Found blank lines in output"

	rm -f "$stdout" "$stderr"

	#---------------------------------------------------------------------
	msg "Ensure --output=stderr works"

	stdout=$(mktemp "$template")
	stderr=$(mktemp "$template")
	cmd="$procenv --output=stderr 1>\"$stdout\" 2>\"$stderr\""
	run_command "$cmd" "$stdout" "$stderr"

	[ -s "$stderr" ] || die "Expected stderr output in file $stderr"
	[ -s "$stdout" ] && die "Unexpected stdout output: $stdout"
	rm -f "$stdout" "$stderr"

	#---------------------------------------------------------------------
	msg "Ensure --exec works (with no previous arguments)"

	str="hello world"
	out=$(mktemp "$template")
	stdout=$(mktemp "$template")
	stderr=$(mktemp "$template")
	cmd="$procenv --output=file --file=\"$out\" \
		--exec echo \"$str\" 1>\"$stdout\" 2>\"$stderr\""
	run_command "$cmd" "$stdout" "$stderr"

	[ -s "$out" ] || die "Expected $procenv output in file $out"
	[ -s "$stderr" ] && die "Unexpected stderr output in file $stderr"
	[ -s "$stdout" ] || die "Expected stdout output in file $stdout"
	result=$(cat "$stdout")

	[ "$result" != "$str" ] && die \
		"Expected file $stdout to contain '$str', found '$result'"

	rm -f "$stdout" "$stderr" "$out"

	#---------------------------------------------------------------------
	msg "Ensure '--version --exec' works"

	str="hello world"
	out=$(mktemp "$template")
	stdout=$(mktemp "$template")
	stderr=$(mktemp "$template")
	cmd="$procenv --output=file --file=\"$out\" --version \
		--exec echo \"$str\" 1>\"$stdout\" 2>\"$stderr\""
	run_command "$cmd" "$stdout" "$stderr"

	[ -s "$out" ] || die "Expected $procenv output in file $out"
	grep -q ^version: "$out" || die "Expected $procenv --version output in file $out"
	[ -s "$stderr" ] && die "Unexpected stderr output in file $stderr"
	[ -s "$stdout" ] || die "Expected stdout output in file $stdout"
	result=$(cat "$stdout")

	[ "$result" != "$str" ] && die \
		"Expected file $stdout to contain '$str', found '$result'"

	rm -f "$stdout" "$stderr" "$out"

	#---------------------------------------------------------------------
	msg "Ensure '--exec' can exec $procenv"

	out=$(mktemp "$template")
	out2=$(mktemp "$template")

	stdout=$(mktemp "$template")
	stderr=$(mktemp "$template")
	cmd="$procenv --output=file --file=\"$out\" \
		--exec -- $procenv --file=\"$out2\" \
		1>\"$stdout\" 2>\"$stderr\""
	run_command "$cmd" "$stdout" "$stderr"

	[ -s "$out" ] || die "Expected $procenv output in file $out"
	[ -s "$out2" ] || die "Expected $procenv output in file $out2"
	[ -s "$stdout" ] && die "Unexpected stdout output in file $stdout"
	[ -s "$stderr" ] && die "Unexpected stderr output in file $stderr"

	rm -f "$stdout" "$stderr" "$out" "$out2"

	#---------------------------------------------------------------------
	msg "Ensure 'PROCENV_EXEC' and 'PROCENV_FILE' variables work"

	out=$(mktemp "$template")
	stdout=$(mktemp "$template")
	stderr=$(mktemp "$template")

    test_script=$(mktemp "$template")
    test_log=$(mktemp "$template")
    expected="foo"
    cat <<EOT > "$test_script"
    #!/bin/sh

    echo "$expected" > "$test_log"
EOT
    chmod +x "$test_script"

    (PROCENV_FILE="$out" PROCENV_EXEC="$test_script" \
        "$procenv" 1>"$stdout" 2>"$stderr")

	[ -s "$out" ] || die "Expected $procenv output in file $out"
	[ -s "$test_log" ] || die "Expected output in test log file $test_log"
    output=$(cat "$test_log")

    [ "$output" = "$expected" ] || \
        die "incorect output from script (expected '$expected', got '$output')"

	[ -s "$stdout" ] && die "Unexpected stdout output in file $stdout"
	[ -s "$stderr" ] && die "Unexpected stderr output in file $stderr"

	rm -f "$out" "$stdout" "$stderr" "$test_script" "$test_log"

	#---------------------------------------------------------------------
	arg='--format=text'
	msg "Ensure '$arg' works"

	stdout=$(mktemp "$template")
	stderr=$(mktemp "$template")
	cmd="$procenv $arg 1>\"$stdout\" 2>\"$stderr\""
	run_command "$cmd" "$stdout" "$stderr"

	[ -s "$stdout" ] || die "Expected stdout output in file $stdout"
	[ -s "$stderr" ] && die "Unexpected stderr output in file $stdout"

	rm -f "$stdout" "$stderr"

	#---------------------------------------------------------------------
	arg='--format=crumb'
	msg "Ensure '$arg' works"

	stdout=$(mktemp "$template")
	stderr=$(mktemp "$template")
	cmd="$procenv $arg 1>\"$stdout\" 2>\"$stderr\""
	run_command "$cmd" "$stdout" "$stderr"

	[ -s "$stdout" ] || die "Expected stdout output in file $stdout"
	[ -s "$stderr" ] && die "Unexpected stderr output in file $stdout"

	rm -f "$stdout" "$stderr"

	#---------------------------------------------------------------------
	arg='--indent-char'

	seps=". ;"
	[ "$utf8" = yes ] && seps="$seps → ❤"

	for sep in $seps
	do
		opt="${arg}=${sep}"
		msg "Ensure '$opt' works"

		stdout=$(mktemp "$template")
		stderr=$(mktemp "$template")

		cmd="$procenv \"$opt\" \
			--version 1>\"$stdout\" 2>\"$stderr\""
		run_command "$cmd" "$stdout" "$stderr"

		[ -s "$stdout" ] || die "Expected stdout output in file $stdout"
		[ -s "$stderr" ] && die "Unexpected stderr output in file $stdout"

		grep -q "^${sep}${sep}version: ${procenv_version}$" "$stdout" ||\
			die "Did not find expected output for arg '$arg'"

		rm -f "$stdout" "$stderr"
	done

	# --indent-char *must* be a single character, so ensure multi-char
	# values are flagged as incorrect.
	seps="12 123 1234"
	[ "$utf8" = yes ] && seps="$seps →❤ →❤→ ❤→❤❤"

	for sep in $seps
	do
		opt="${arg}=${sep}"
		msg "Ensure '$opt' works"

		stdout=$(mktemp "$template")
		stderr=$(mktemp "$template")

		cmd="$procenv \"$opt\" \
			--version 1>\"$stdout\" 2>\"$stderr\""
		run_command_expect_fail "$cmd"

		[ -s "$stdout" ] && die "Unexpected stdout output in file $stdout"
		[ -s "$stderr" ] || die "Expected stderr output in file $stdout"

		rm -f "$stdout" "$stderr"
	done

	#---------------------------------------------------------------------
	arg='--separator'

	# Choose a few common ones, a few UTF-8 ones and a few
	# multi-character ones.
	seps=". ; 12 123 1234"
	[ "$utf8" = yes ] && sep="$seps → ❤ →❤ →❤→ ❤→❤❤"

	for sep in $seps
	do
		opt="${arg}=${sep}"
		msg "Ensure '$opt' works"

		stdout=$(mktemp "$template")
		stderr=$(mktemp "$template")

		cmd="$procenv \"$opt\" \
			--version 1>\"$stdout\" 2>\"$stderr\""
		run_command "$cmd" "$stdout" "$stderr"

		[ -s "$stdout" ] || die "Expected stdout output in file $stdout"
		[ -s "$stderr" ] && die "Unexpected stderr output in file $stdout"

		grep -q "^  version${sep}${procenv_version}$" "$stdout" ||\
			die "Did not find expected output for arg '$arg'"

		rm -f "$stdout" "$stderr"
	done

	#---------------------------------------------------------------------
	arg='--crumb-separator'

	# Choose a few common ones, a few UTF-8 ones and a few
	# multi-character ones.
	seps=". ; 12 123 1234"
	[ "$utf8" = yes ] && seps="$seps → ❤ →❤ →❤→ ❤→❤❤"

	for sep in $seps
	do
		opt="${arg}=${sep}"
		msg "Ensure '$opt' works"

		stdout=$(mktemp "$template")
		stderr=$(mktemp "$template")

		cmd="$procenv --format=crumb \"$opt\" \
			--separator=\"$sep\" \
			--version 1>\"$stdout\" 2>\"$stderr\""
		run_command "$cmd" "$stdout" "$stderr"

		[ -s "$stdout" ] || die "Expected stdout output in file $stdout"
		[ -s "$stderr" ] && die "Unexpected stderr output in file $stdout"

		grep -q "^version${sep}version${sep}${procenv_version}$" "$stdout" ||\
			die "Did not find expected output for arg '$arg'"

		rm -f "$stdout" "$stderr"
	done

	#---------------------------------------------------------------------

	if [ "$xml_tests" -eq 1 ]
	then
		arg='--format=xml'
		if [ "$control_chars_in_env" -eq 1 ]
		then
			warn "Skipping '$arg' test as control characters found in environment"
		else
			msg "Ensure '$arg' works"

			stdout=$(mktemp "$template")
			stderr=$(mktemp "$template")
			cmd="$procenv $arg 1>\"$stdout\" 2>\"$stderr\""
			run_command "$cmd" "$stdout" "$stderr"

			[ -s "$stdout" ] || die "Expected stdout output in file $stdout"
			[ -s "$stderr" ] && die "Unexpected stderr output in file $stdout"

			[ -z "$($xml_checker "$stdout")" ] || die "XML output not well-formed"

			rm -f "$stdout" "$stderr"
		fi
	fi

	#---------------------------------------------------------------------

	if [ "$json_tests" -eq 1 ]
	then
		arg='--format=json'
		msg "Ensure '$arg' works"

		stdout=$(mktemp "$template")
		stderr=$(mktemp "$template")
		cmd="$procenv $arg 1>\"$stdout\" 2>\"$stderr\""
		run_command "$cmd" "$stdout" "$stderr"

		[ -s "$stdout" ] || die "Expected stdout output in file $stdout"
		[ -s "$stderr" ] && die "Unexpected stderr output in file $stdout"

		$json_checker < "$stdout" >/dev/null

		rm -f "$stdout" "$stderr"
	fi

	for arg in $non_display_options
	do
		# XXX: specifying a non-display option _after_
		# specifing a display option used to be a NOP, but
		# is now disallowed as it is non-sensical and
		# the result could be surprising to the user. 
		#
		# We select '--meta' as a random display option.
		msg "Ensure non-display option ('$arg') disallowed after display option ('--meta')"

		stdout=$(mktemp "$template")
		stderr=$(mktemp "$template")
		cmd="$procenv --meta $arg 1>'$stdout' 2>'$stderr'"
		run_command_expect_fail "$cmd"

		[ -s "$stdout" ] && die "Unexpected stdout output in file $stdout"
		[ -s "$stderr" ] || die "Expected stderr output in file $stdout"

		rm -f "$stdout" "$stderr"
	done

	msg "Ensure an empty environment is tolerated"
	if command -v env >/dev/null 2>&1
	then
		cmd="env -i $procenv --environment >/dev/null 2>&1"
		run_command "$cmd" "" ""
	fi
}

run_final_tests()
{
	# check options specified in man page exactly correlate with
	# those documented by procenv (and vice versa)
	procenv_options=$(get_procenv_options)
	man_options=$(get_man_options)

	for opt in $procenv_options
	do
		echo "$man_options"|grep -q "^${opt}$" ||\
			die "failed to find option '$opt' in man page: $man_path"
	done

	for opt in $man_options
	do
		echo "$procenv_options"|grep -q "^${opt}$" ||\
			die "failed to find option '$opt' in procenv help output"
	done
}

#---------------------------------------------------------------------
# main
#---------------------------------------------------------------------

while getopts "o" opt
do
	case "$opt" in
		o)
			# allow this useful function to be called independent of the tests.
			get_procenv_options
			exit 0
			;;
	esac
done

init

saved_lang=$LANG

for lang in $lang_list
do
	if ! locale -a|grep -q "^${lang}$"
	then
		msg "Not testing with locale language $lang"
		continue
	fi
	export LANG="$lang" LC_ALL="$lang"
	run_tests
done

run_final_tests

[ -n "$saved_lang" ] && export LANG="$saved_lang" LC_ALL="$saved_lang"

exit 0

#---------------------------------------------------------------------