Blame tests/roles/custom-test-fonts/files/run-lang-coverage-test

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