github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/tests/includes/check.sh (about)

     1  check_dependencies() {
     2  	local dep missing
     3  	missing=""
     4  
     5  	for dep in "$@"; do
     6  		if ! which "$dep" >/dev/null 2>&1; then
     7  			[[ "$missing" ]] && missing="$missing, $dep" || missing="$dep"
     8  		fi
     9  	done
    10  
    11  	if [[ "$missing" ]]; then
    12  		echo "Missing dependencies: $missing" >&2
    13  		echo ""
    14  		exit 1
    15  	fi
    16  }
    17  
    18  check_juju_dependencies() {
    19  	local dep missing
    20  	missing=""
    21  
    22  	for dep in "$@"; do
    23  		if ! juju "$dep" >/dev/null 2>&1; then
    24  			[[ "$missing" ]] && missing="$missing, juju $dep" || missing="juju $dep"
    25  		fi
    26  	done
    27  
    28  	if [[ "$missing" ]]; then
    29  		echo "Missing juju commands: $missing" >&2
    30  		echo ""
    31  		exit 1
    32  	fi
    33  }
    34  
    35  check_not_contains() {
    36  	local input value chk
    37  
    38  	input=${1}
    39  	shift
    40  
    41  	value=${1}
    42  	shift
    43  
    44  	chk=$(echo "${input}" | grep "${value}" || true)
    45  	if [[ -n ${chk} ]]; then
    46  		printf "Unexpected \"${value}\" found\n\n%s\n" "${input}" >&2
    47  		exit 1
    48  	else
    49  		echo "Success: \"${value}\" not found" >&2
    50  	fi
    51  }
    52  
    53  check_contains() {
    54  	local input value chk
    55  
    56  	input=${1}
    57  	shift
    58  
    59  	value=${1}
    60  	shift
    61  
    62  	chk=$(echo "${input}" | grep "${value}" || true)
    63  	if [[ -z ${chk} ]]; then
    64  		printf "Expected \"%s\" not found\n\n%s\n" "${value}" "${input}" >&2
    65  		exit 1
    66  	else
    67  		echo "Success: \"${value}\" found" >&2
    68  	fi
    69  }
    70  
    71  check_gt() {
    72  	local input value chk
    73  
    74  	input=${1}
    75  	shift
    76  
    77  	value=${1}
    78  	shift
    79  
    80  	if [[ ${input} > ${value} ]]; then
    81  		printf "Success: \"%s\" > \"%s\"\n" "${input}" "${value}" >&2
    82  	else
    83  		printf "Expected \"%s\" > \"%s\"\n" "${input}" "${value}" >&2
    84  		exit 1
    85  	fi
    86  }
    87  
    88  check_ge() {
    89  	local input value chk
    90  
    91  	input=${1}
    92  	shift
    93  
    94  	value=${1}
    95  	shift
    96  
    97  	if [[ ${input} > ${value} ]] || [ "${input}" == "${value}" ]; then
    98  		printf "Success: \"%s\" >= \"%s\"\n" "${input}" "${value}" >&2
    99  	else
   100  		printf "Expected \"%s\" >= \"%s\"\n" "${input}" "${value}" >&2
   101  		exit 1
   102  	fi
   103  }
   104  
   105  check() {
   106  	local want got
   107  
   108  	want=${1}
   109  
   110  	got=
   111  	while read -r d; do
   112  		if [ -z "${got}" ]; then
   113  			got="${d}"
   114  		else
   115  			got="${got}\n${d}"
   116  		fi
   117  	done
   118  
   119  	OUT=$(echo "${got}" | grep -E "${want}" || echo "(NOT FOUND)")
   120  	if [[ ${OUT} == "(NOT FOUND)" ]]; then
   121  		echo "" >&2
   122  		# shellcheck disable=SC2059
   123  		printf "$(red \"Expected\"): ${want}\n" >&2
   124  		# shellcheck disable=SC2059
   125  		printf "$(red \"Received\"): ${got}\n" >&2
   126  		echo "" >&2
   127  		exit 1
   128  	fi
   129  }