• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env sh
2#
3#
4# SPDX-License-Identifier: GPL-2.0-only
5
6#set -x # uncomment for debug
7
8JUNIT=0
9INVERT=0
10
11usage () {
12	printf "Usage: %s <sub-command> [Options]\n" "$0"
13	printf " Sub-commands:\n"
14	printf "    lint-stable        : Run standard lint tests - should pass\n"
15	printf "    lint-extended      : Run extended lint tests - should pass\n"
16	printf "    lint               : Run full lint tests - Not expected to pass\n\n"
17
18	printf "  Options:\n"
19	printf "    -h | --help       : Show this help message\n"
20	printf "    -I | --invert     : Invert results - used for testing linters\n"
21	printf "    -J | --junit      : Send test output to a JUnit file\n"
22	printf "\n"
23}
24
25#write to the junit xml file if --junit was specified
26junit_write () {
27	if [ "$JUNIT" -eq 1 ]; then
28		echo "$1" >> "$XMLFILE"
29	fi
30}
31
32# Look if we have getopt. If not, build it.
33if [ "$(uname)" = "Darwin" ]; then
34  GETOPT="getopt hIJ"
35else
36  GETOPT="getopt -l help,junit,invert -o hIJ"
37fi
38
39if ! cmd_args="$($GETOPT -- "$@")"; then
40	usage
41	exit 0
42fi
43eval set -- "${cmd_args}"
44
45while true; do
46	case "$1" in
47	-h | --help)
48		usage
49		exit 0
50		;;
51	-I | --invert)
52		INVERT=1
53		;;
54	-J | --junit)
55		echo "selected junit"
56		JUNIT=1
57		;;
58	--) shift; break ;;
59	*) break ;;
60	esac
61	shift
62done
63
64#verify the first command line parameter
65if [ -z "$1" ]; then
66	echo "Error: A sub-command is needed."
67	usage
68	exit 1
69elif [ "$1" != "lint" ] && [ "$1" != "lint-stable" ] &&
70		[ "$1" != "lint-extended" ]; then
71	echo "Error: $1 is not a valid sub-command."
72	usage
73	exit 1
74fi
75
76LINTLOG=$(mktemp .tmpconfig.lintXXXXXX);
77XMLFILE="$(dirname "$0")/junit.xml"
78if [ "$1" = "lint-extended" ]; then
79	XMLFILE="$(dirname "$0")/extended-junit.xml"
80fi
81FAILED=0;
82
83if [ "${JUNIT}" -eq 1 ]; then
84	echo '<?xml version="1.0" encoding="utf-8"?>' > "$XMLFILE"
85	junit_write '<testsuite>'
86fi
87
88#run all scripts of the requested type
89for script in "$(dirname "$0")/${1}-"*; do
90	printf "%s " "$(grep '^# DESCR:' "$script" | sed 's,.*DESCR: *,,')"
91	printf "(%s): " "$(basename "$script")"
92	junit_write "	<testcase classname='lint' name='$(basename "$script")'>"
93	$script | tee "$LINTLOG"
94
95	#if the lint script gives any output, that's a failure
96	if [ "${INVERT}" -eq 1 ] && [ "$(wc -l < "$LINTLOG")" -ne 0 ]; then
97		echo "success"
98		junit_write "		<system-out><![CDATA[success]]></system-out>"
99	elif [ "${INVERT}" -eq 0 ] && [ "$(wc -l < "$LINTLOG")" -eq 0 ]; then
100		echo "success"
101		junit_write "		<system-out><![CDATA[success]]></system-out>"
102	else
103		echo "test failed"
104		junit_write "		<failure type='testFailed'><![CDATA["
105		junit_write "$(cat "$LINTLOG")"
106		junit_write "]]></failure>"
107		rm -f "$LINTLOG"
108		FAILED=$(( FAILED + 1 ))
109	fi
110	junit_write '	</testcase>'
111done
112
113rm -f "$LINTLOG"
114junit_write '</testsuite>'
115test $FAILED -eq 0 || { echo "ERROR: $FAILED test(s) failed."; exit 1; };
116