Blame tests/roles/custom-test-fonts/files/run-family-test

Akira TAGOH 0fac32a
#! /bin/bash -efu
Akira TAGOH 0fac32a
Akira TAGOH 0fac32a
debug() {
Akira TAGOH 0fac32a
	if [ -n "$DEBUG" ]; then
Akira TAGOH 0fac32a
		echo "$*" >&2
Akira TAGOH 0fac32a
	fi
Akira TAGOH 0fac32a
}
Akira TAGOH 0fac32a
Akira TAGOH 0fac32a
msg_usage() {
Akira TAGOH 0fac32a
	cat <<_EOF_
Akira TAGOH 0fac32a
Run family test.
Akira TAGOH 0fac32a
Akira TAGOH 0fac32a
Usage:
Akira TAGOH 0fac32a
$PROG <options>
Akira TAGOH 0fac32a
Akira TAGOH 0fac32a
Options:
Akira TAGOH 0fac32a
-h, --help		Display this help and exit
Akira TAGOH 0fac32a
-v, --verbose		Turn on debug
Akira TAGOH 0fac32a
-l, --lang=LANG		Test LANG language coverage (default: en)
Akira TAGOH 0fac32a
-f, --family=FILE	Set a family name supposed to be assigned for alias.
Akira TAGOH 0fac32a
-g, --alias=STR		Set an alias name. (default: sans-serif)
Akira TAGOH 0fac32a
-a, --artifactsdir=DIR	Set environment dir to store artifacts
Akira TAGOH 0fac32a
-k, --package=NAME	Set a package name for fonts.
Akira TAGOH 0fac32a
_EOF_
Akira TAGOH 0fac32a
}
Akira TAGOH 0fac32a
Akira TAGOH 0fac32a
PROG="${PROG:-${0##*/}}"
Akira TAGOH 0fac32a
DEBUG="${DEBUG:-}"
Akira TAGOH 0fac32a
OPT_LANG="${OPT_LANG:-en}"
Akira TAGOH 0fac32a
OPT_FAMILY="${OPT_FAMILY:-}"
Akira TAGOH 0fac32a
OPT_ARTIFACTS_DIR="${OPT_ARTIFACTS_DIR:-}"
Akira TAGOH 0fac32a
OPT_ALIAS="${OPT_ALIAS:-sans-serif}"
Akira TAGOH 0fac32a
OPT_PACKAGE="${OPT_PACKAGE:-}"
Akira TAGOH 0fac32a
Akira TAGOH 0fac32a
opt=$(getopt -n "$0" --options "hvl:f:t:a:g:k:" --longoptions "help,verbose,lang:,family:,test:,artifactsdir:,alias:,package:" -- "$@")
Akira TAGOH 0fac32a
eval set -- "$opt"
Akira TAGOH 0fac32a
while [[ $# -gt 0 ]]; do
Akira TAGOH 0fac32a
	case "$1" in
Akira TAGOH 0fac32a
	-k|--package)
Akira TAGOH 0fac32a
		OPT_PACKAGE="$2"
Akira TAGOH 0fac32a
		shift 2
Akira TAGOH 0fac32a
		;;
Akira TAGOH 0fac32a
	-g|--alias)
Akira TAGOH 0fac32a
		OPT_ALIAS="$2"
Akira TAGOH 0fac32a
		shift 2
Akira TAGOH 0fac32a
		;;
Akira TAGOH 0fac32a
	-a|--artifactsdir)
Akira TAGOH 0fac32a
		OPT_ARTIFACTS_DIR="$2"
Akira TAGOH 0fac32a
		shift 2
Akira TAGOH 0fac32a
		;;
Akira TAGOH 0fac32a
	-f|--family)
Akira TAGOH 0fac32a
		OPT_FAMILY="$2"
Akira TAGOH 0fac32a
		shift 2
Akira TAGOH 0fac32a
		;;
Akira TAGOH 0fac32a
	-l|--lang)
Akira TAGOH 0fac32a
		OPT_LANG="$2"
Akira TAGOH 0fac32a
		shift 2
Akira TAGOH 0fac32a
		;;
Akira TAGOH 0fac32a
	-v|--verbose)
Akira TAGOH 0fac32a
		DEBUG="-v"
Akira TAGOH 0fac32a
		shift
Akira TAGOH 0fac32a
		;;
Akira TAGOH 0fac32a
	-h|--help)
Akira TAGOH 0fac32a
		msg_usage
Akira TAGOH 0fac32a
		exit 0
Akira TAGOH 0fac32a
		;;
Akira TAGOH 0fac32a
	--)
Akira TAGOH 0fac32a
		shift
Akira TAGOH 0fac32a
		;;
Akira TAGOH 0fac32a
	*)
Akira TAGOH 0fac32a
		msg_usage
Akira TAGOH 0fac32a
		exit 1
Akira TAGOH 0fac32a
	esac
Akira TAGOH 0fac32a
done
Akira TAGOH 0fac32a
Akira TAGOH 0fac32a
if [ -z "$OPT_ARTIFACTS_DIR" ] || [ -z "$OPT_LANG" ] || [ -z "$OPT_FAMILY" ]; then
Akira TAGOH 0fac32a
	echo "Use: $PROG -h for help."
Akira TAGOH 0fac32a
	exit 0
Akira TAGOH 0fac32a
fi
Akira TAGOH 0fac32a
Akira TAGOH 0fac32a
debug "Alias: $OPT_ALIAS"
Akira TAGOH 0fac32a
debug "Family: $OPT_FAMILY"
Akira TAGOH 0fac32a
debug "Lang: $OPT_LANG"
Akira TAGOH 0fac32a
debug "Artifacts dir: $OPT_ARTIFACTS_DIR"
Akira TAGOH 0fac32a
debug "Package name: $OPT_PACKAGE"
Akira TAGOH 0fac32a
STR_TEST_DASHED=$(echo "$OPT_ALIAS" | sed -e 's/\//-/g' -e 's/ /-/g')
Akira TAGOH 0fac32a
Akira TAGOH 0fac32a
clean_exit() {
Akira TAGOH 0fac32a
	rc=$?;
Akira TAGOH 0fac32a
	trap - SIGINT SIGTERM SIGABRT EXIT
Akira TAGOH 0fac32a
	echo "Run test $OPT_ALIAS: done."
Akira TAGOH 0fac32a
	for pid in $(ps -o pid --no-headers --ppid $$); do
Akira TAGOH 0fac32a
		if [ -n "$(ps -p $pid -o pid=)" ]; then
Akira TAGOH 0fac32a
			kill -s HUP $pid
Akira TAGOH 0fac32a
		fi
Akira TAGOH 0fac32a
	done
Akira TAGOH 0fac32a
	local log_file_name="$STR_TEST_DASHED.log"
Akira TAGOH 0fac32a
	local log_file_path="$OPT_ARTIFACTS_DIR/$log_file_name"
Akira TAGOH 0fac32a
	local status
Akira TAGOH 0fac32a
	if [[ $rc -eq 127 ]]; then
Akira TAGOH 0fac32a
		status="ERROR"
Akira TAGOH 0fac32a
	elif grep -q "RESULT: WARN" "$log_file_path"; then
Akira TAGOH 0fac32a
		status="ERROR"
Akira TAGOH 0fac32a
	elif grep -q "RESULT: FAIL" "$log_file_path"; then
Akira TAGOH 0fac32a
		status="FAIL"
Akira TAGOH 0fac32a
	elif grep -q "RESULT: PASS" "$log_file_path"; then
Akira TAGOH 0fac32a
		status="PASS"
Akira TAGOH 0fac32a
	elif grep -q "FAIL" "$log_file_path"; then
Akira TAGOH 0fac32a
		status="FAIL"
Akira TAGOH 0fac32a
	elif grep -q "PASS" "$log_file_path"; then
Akira TAGOH 0fac32a
		status="PASS"
Akira TAGOH 0fac32a
	else
Akira TAGOH 0fac32a
		status="ERROR"
Akira TAGOH 0fac32a
	fi
Akira TAGOH 0fac32a
	echo "$status $OPT_ALIAS" >> "$OPT_ARTIFACTS_DIR/test.log"
Akira TAGOH 0fac32a
	mv "$log_file_path" "$OPT_ARTIFACTS_DIR/${status}_${log_file_name}"
Akira TAGOH 0fac32a
	local results="$OPT_ARTIFACTS_DIR/results.yml"
Akira TAGOH 0fac32a
	local result=$(echo $status | tr '[:upper:]' '[:lower:]')
Akira TAGOH 0fac32a
	test -f "$results" || echo 'results:' > "$results"
Akira TAGOH 0fac32a
	echo "- {result: $result, test: $OPT_ALIAS}" >> "$results"
Akira TAGOH 0fac32a
	exit 0
Akira TAGOH 0fac32a
}
Akira TAGOH 0fac32a
trap clean_exit SIGINT SIGTERM SIGABRT EXIT
Akira TAGOH 0fac32a
Akira TAGOH 0fac32a
cachedir=`pkg-config --variable cachedir fontconfig`
Akira TAGOH 0fac32a
tmpconfd=`mktemp --tmpdir -d fontsci.XXXXXXXX`
Akira TAGOH 0fac32a
conf=$(for i in `rpm -ql $OPT_PACKAGE | grep conf.d`; do
Akira TAGOH 0fac32a
	echo "<include>$i</include>"
Akira TAGOH 0fac32a
done)
Akira TAGOH 0fac32a
cat <<_EOF_> $tmpconfd/fonts.conf
Akira TAGOH 0fac32a
<fontconfig>
Akira TAGOH 0fac32a
	<dir>/usr/share/fonts</dir>
Akira TAGOH 0fac32a
	$conf
Akira TAGOH 0fac32a
	<cachedir>$cachedir</cachedir>
Akira TAGOH 0fac32a
</fontconfig>
Akira TAGOH 0fac32a
_EOF_
Akira TAGOH 0fac32a
debug "Config: `cat $tmpconfd/fonts.conf`"
Akira TAGOH 0fac32a
Akira TAGOH 0fac32a
mkdir -p "$OPT_ARTIFACTS_DIR"
Akira TAGOH 0fac32a
export OUTPUTFILE="$(realpath "$OPT_ARTIFACTS_DIR")/$STR_TEST_DASHED-out.log"
Akira TAGOH 0fac32a
logfile_stdout="$OPT_ARTIFACTS_DIR/$STR_TEST_DASHED.log"
Akira TAGOH 0fac32a
logfile_stderr="$OPT_ARTIFACTS_DIR/$STR_TEST_DASHED-err.log"
Akira TAGOH 0fac32a
exec 3>&1 4>&2 1> >(tee -a "$logfile_stdout" >&3) 2> >(tee -a "$logfile_stderr" >&4)
Akira TAGOH 0fac32a
Akira TAGOH 0fac32a
debug "Check family assignment"
Akira TAGOH 0fac32a
res=`FONTCONFIG_FILE=$tmpconfd/fonts.conf fc-match -f "%{family[0]}" :family=$OPT_ALIAS:lang=$OPT_LANG`
Akira TAGOH 0fac32a
if [ "x$res" = "x$OPT_FAMILY" ]; then
Akira TAGOH 0fac32a
	echo "RESULT: PASS: $OPT_FAMILY was assigned to $OPT_ALIAS as expected"
Akira TAGOH 0fac32a
else
Akira TAGOH 0fac32a
	echo "RESULT: FAIL: $OPT_FAMILY wasn't assigned to $OPT_ALIAS (actual result: $res)"
Akira TAGOH 0fac32a
fi
Akira TAGOH 0fac32a
rm -rf $tmpconfd