xref: /aosp_15_r20/external/coreboot/util/gitconfig/test/helpers.sh (revision b9411a12aaaa7e1e6a6fb7c5e057f44ee179a49c)
1#!/usr/bin/env bash
2##
3## This program is free software; you can redistribute it and/or modify
4## it under the terms of the GNU General Public License as published by
5## the Free Software Foundation; version 3 or later of the License.
6##
7## This program is distributed in the hope that it will be useful,
8## but WITHOUT ANY WARRANTY; without even the implied warranty of
9## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10## GNU General Public License for more details.
11##
12## SPDX-License-Identifier: GPL-3.0-or-later
13## <https://spdx.org/licenses/GPL-3.0-or-later.html>
14##
15
16set -o errexit
17set -o nounset
18
19# dependency check
20dependencies=(cut git readlink)
21for dependency in "${dependencies[@]}"; do
22	if ! command -v "${dependency}" 1>/dev/null; then
23		echo "missing ${dependency}, test skipped" >&2
24		exit 0
25	fi
26done
27
28# helper functions
29function clone_submodules() {
30	clone_dir="${1}"
31	log_dir="${2}"
32
33	modules_dir="$(readlink --canonicalize-missing \
34		"$(git rev-parse --git-dir)/modules")"
35	cd "${clone_dir}"
36	git submodule init 1>>"${log_dir}/clone.log" 2>&1
37	for submodule in $(git config --get-regexp "submodule\..*\.url" \
38		| cut --delimiter=. --fields=2); do
39		git config "submodule.${submodule}.url" \
40			"${modules_dir}/${submodule}"
41	done
42	git submodule update 1>>"${log_dir}/clone.log" 2>&1
43}
44
45function check_exit_code() {
46	declare -i err=${?}
47
48	# either "positive" or "negative"
49	polarity="${1}"
50	log_file="${2}"
51
52	# exit code 124 is special as per `timeout` manpage
53	if [ "${polarity}" == "positive" ] && [ ${err} -eq 124 ]; then
54		echo >&2 "timed out"
55	fi
56
57	if [ "${polarity}" == "positive" ] && [ ${err} -ne 0 ]; then
58		echo "bad exit code: expected 0, actually ${err}"
59		echo "for details, refer to log file \"${log_file}\""
60		exit ${err}
61	elif [ "${polarity}" == "negative" ] && [ ${err} -eq 0 ]; then
62		echo "bad exit code: expected non-zero, actually 0"
63		echo "for details, refer to log file \"${log_file}\""
64		exit 1
65	fi
66}
67