ba1083c
#! /bin/bash -efu
ba1083c
ba1083c
debug() {
ba1083c
	if [ -n "$DEBUG" ]; then
ba1083c
		echo "$*" >&2
ba1083c
	fi
ba1083c
}
ba1083c
ba1083c
msg_usage() {
ba1083c
	cat <<_EOF_
ba1083c
Run language coverage test.
ba1083c
ba1083c
Usage:
ba1083c
$PROG <options>
ba1083c
ba1083c
Options:
ba1083c
-h, --help		Display this help and exit
ba1083c
-v, --verbose		Turn on debug
ba1083c
-l, --lang=LANG		Test LANG language coverage (default: en)
ba1083c
-p, --path=PATH		Test fonts on PATH
ba1083c
-a, --artifactsdir=DIR	test environment dir to store artifacts
ba1083c
-i, --ignore=FILE	Ignore FILE to check.
ba1083c
_EOF_
ba1083c
}
ba1083c
ba1083c
PROG="${PROG:-${0##*/}}"
ba1083c
DEBUG="${DEBUG:-}"
ba1083c
OPT_LANG="${OPT_LANG:-en}"
ba1083c
OPT_PATH="${OPT_PATH:-}"
ba1083c
OPT_ARTIFACTS_DIR="${OPT_ARTIFACTS_DIR:-}"
ba1083c
OPT_IGNORE=()
ba1083c
ba1083c
opt=$(getopt -n "$0" --options "hvl:p:t:a:i:" --longoptions "help,verbose,lang:,path:,test:,artifactsdir:,ignore:" -- "$@")
ba1083c
eval set -- "$opt"
ba1083c
while [[ $# -gt 0 ]]; do
ba1083c
	case "$1" in
ba1083c
	-i|--ignore)
ba1083c
		OPT_IGNORE+=("$2")
ba1083c
		shift 2
ba1083c
		;;
ba1083c
	-a|--artifactsdir)
ba1083c
		OPT_ARTIFACTS_DIR="$2"
ba1083c
		shift 2
ba1083c
		;;
ba1083c
	-p|--path)
ba1083c
		OPT_PATH="$2"
ba1083c
		shift 2
ba1083c
		;;
ba1083c
	-l|--lang)
ba1083c
		OPT_LANG="$2"
ba1083c
		shift 2
ba1083c
		;;
ba1083c
	-v|--verbose)
ba1083c
		DEBUG="-v"
ba1083c
		shift
ba1083c
		;;
ba1083c
	-h|--help)
ba1083c
		msg_usage
ba1083c
		exit 0
ba1083c
		;;
ba1083c
	--)
ba1083c
		shift
ba1083c
		;;
ba1083c
	*)
ba1083c
		msg_usage
ba1083c
		exit 1
ba1083c
	esac
ba1083c
done
ba1083c
ba1083c
if [ -z "$OPT_ARTIFACTS_DIR" ] || [ -z "$OPT_LANG" ] || [ -z "$OPT_PATH" ]; then
ba1083c
	echo "Use: $PROG -h for help."
ba1083c
	exit 0
ba1083c
fi
ba1083c
ba1083c
debug "Path: $OPT_PATH"
ba1083c
debug "Lang: $OPT_LANG"
ba1083c
debug "Artifacts dir: $OPT_ARTIFACTS_DIR"
ba1083c
debug "Ignore: ${OPT_IGNORE[@]}"
ba1083c
STR_TEST_DASHED=$(echo "$OPT_PATH" | sed -e 's/\//-/g')
ba1083c
ba1083c
clean_exit() {
ba1083c
	rc=$?;
ba1083c
	trap - SIGINT SIGTERM SIGABRT EXIT
ba1083c
	echo "Run test $OPT_PATH: done."
ba1083c
	for pid in $(ps -o pid --no-headers --ppid $$); do
ba1083c
		if [ -n "$(ps -p $pid -o pid=)" ]; then
ba1083c
			kill -s HUP $pid
ba1083c
		fi
ba1083c
	done
ba1083c
	local log_file_name="$STR_TEST_DASHED.log"
ba1083c
	local log_file_path="$OPT_ARTIFACTS_DIR/$log_file_name"
ba1083c
	local status
ba1083c
	if [[ $rc -eq 127 ]]; then
ba1083c
		status="ERROR"
ba1083c
	elif grep -q "RESULT: WARN" "$log_file_path"; then
ba1083c
		status="ERROR"
ba1083c
	elif grep -q "RESULT: FAIL" "$log_file_path"; then
ba1083c
		status="FAIL"
ba1083c
	elif grep -q "RESULT: PASS" "$log_file_path"; then
ba1083c
		status="PASS"
ba1083c
	elif grep -q "FAIL" "$log_file_path"; then
ba1083c
		status="FAIL"
ba1083c
	elif grep -q "PASS" "$log_file_path"; then
ba1083c
		status="PASS"
ba1083c
	else
ba1083c
		status="ERROR"
ba1083c
	fi
ba1083c
	echo "$status $OPT_PATH" >> "$OPT_ARTIFACTS_DIR/test.log"
ba1083c
	mv "$log_file_path" "$OPT_ARTIFACTS_DIR/${status}_${log_file_name}"
ba1083c
	local results="$OPT_ARTIFACTS_DIR/results.yml"
ba1083c
	local result=$(echo $status | tr '[:upper:]' '[:lower:]')
ba1083c
	test -f "$results" || echo 'results:' > "$results"
ba1083c
	echo "- {result: $result, test: $OPT_PATH}" >> "$results"
ba1083c
	exit 0
ba1083c
}
ba1083c
trap clean_exit SIGINT SIGTERM SIGABRT EXIT
ba1083c
ba1083c
mkdir -p "$OPT_ARTIFACTS_DIR"
ba1083c
export OUTPUTFILE="$(realpath "$OPT_ARTIFACTS_DIR")/$STR_TEST_DASHED-out.log"
ba1083c
logfile_stdout="$OPT_ARTIFACTS_DIR/$STR_TEST_DASHED.log"
ba1083c
logfile_stderr="$OPT_ARTIFACTS_DIR/$STR_TEST_DASHED-err.log"
ba1083c
exec 3>&1 4>&2 1> >(tee -a "$logfile_stdout" >&3) 2> >(tee -a "$logfile_stderr" >&4)
ba1083c
ba1083c
contains() {
ba1083c
	local e match="$1"
ba1083c
	shift
ba1083c
	for e; do [[ "$e" == "$match" ]] && return 1; done
ba1083c
	return 0
ba1083c
}
ba1083c
ba1083c
debug "Check language coverage"
ba1083c
set +f
d5459ce
for i in `find $OPT_PATH -regex '.*/*\.\(t1\)?\(ttf\)?\(otf\)?\(ttc\)?\(pcf.*\)?'`; do
d5459ce
set -f
d5459ce
	debug "$i"
ba1083c
	if test -f $i; then
ba1083c
		n=`basename $i`
ba1083c
		set +e
ba1083c
		contains "$n" "${OPT_IGNORE[@]}"
ba1083c
		ret=$?
ba1083c
		set -e
ba1083c
		if [ $ret -eq 1 ]; then
ba1083c
			debug "ignoring $i"
ba1083c
			continue
ba1083c
		fi
ba1083c
		debug "  $i"
ba1083c
		res=`fc-validate -l $OPT_LANG $i`
ba1083c
		if echo $res | grep -q Missing; then
ba1083c
			echo "RESULT: FAIL: $i doesn't satisfy $OPT_LANG language coverage."
ba1083c
		else
ba1083c
			echo "RESULT: PASS: $i satisfy $OPT_LANG language coverage."
ba1083c
		fi
ba1083c
	fi
ba1083c
done
ba1083c