/ maint / check-cli-help
check-cli-help
 1  #!/usr/bin/env bash
 2  set -euo pipefail
 3  
 4  : "${CARGO:=cargo}"
 5  
 6  # --------------------------------------------------------------------------------
 7  # subcommands that just go and do a thing, by default, need an entry in this list
 8  # this causes us to run, eg `arti proxy --help` rather than just `arti proxy`.
 9  help_arg () {
10      case "$subcommand" in
11          'proxy' | 'hss onion-name' | 'relay' | 'hsc prepare-service-discovery-key' )
12  	        help_arg='--help' ;;
13          *) ;;
14      esac
15  }
16  # --------------------------------------------------------------------------------
17  
18  fail () {
19      echo >&2 "ERROR: $*"
20      exit 16
21  }
22  
23  exec 4>&2
24  
25  x () {
26      echo >&4 "    $*"
27      "$@"
28  }
29  
30  trap '
31  	echo "last output from cargo run:"
32  	cat "$help_file"
33  	exit 1
34  ' 0
35  
36  echo 'checking help output'
37  
38  check_recurse () {
39      mkdir -p target/test/check-cli-help
40      help_file="target/test/check-cli-help/help-output,$*.txt"
41      help_file="${help_file// /,}"
42      local help_arg=''
43  
44      subcommand="$*"
45      subcommand="${subcommand#* -- }"
46      help_arg
47  
48      case "$*" in
49  	*' help') return ;;
50      esac
51  
52      exec 3>"$help_file"
53      set +e
54      # shellcheck disable=SC2086
55      x "$CARGO" run "$@" $help_arg >&3 2>&3
56      rc=$?
57      set -e
58      test $rc = 0 || test $rc = 2 || fail "help message unexpected status $rc"
59      for exp in 'Usage' 'Options'; do
60  	grep "^$exp:" "$help_file" >/dev/null || fail "expected to find $exp"
61      done
62      commands=$(perl -ne '
63  	next unless m{^Commands:}...m{^\S};
64  	next unless m{^  \S};
65  	s{^  ([a-z][0-9a-z-]*) .*}{$1} or die "$_ ?";
66  	print;
67      ' "$help_file")
68      for command in $commands; do
69  	check_recurse "$@" "$command"
70      done
71  }
72  
73  check_recurse                --bin arti --
74  check_recurse --all-features --bin arti --
75  
76  trap '' 0
77  echo ok.