From fdfa6dbb0e69742029d53f0e163b8e7e7e6860f5 Mon Sep 17 00:00:00 2001 From: William Hubbs Date: Sat, 16 Apr 2022 15:13:08 -0500 Subject: rewrite tests (#515) * rewrite tests to work with meson This ports our tests to meson and makes them able to be run in parallel. * add tests to ci * rewrite test/check-trailing-newlines in bash This test was using a GNU sed command which does not work on Alpine Linux. --- test/check-obsolete-functions.sh | 11 +++++ test/check-spacing-style.sh | 18 ++++++++ test/check-trailing-newlines.sh | 19 ++++++++ test/check-trailing-whitespace.sh | 12 +++++ test/check-xfunc-usage.sh | 15 +++++++ test/meson.build | 26 +++++++++++ test/runtests.sh | 92 --------------------------------------- test/setup_env.sh | 24 +++++----- test/units/check-is-older-than.sh | 90 ++++++++++++++++++++++++++++++++++++++ test/units/check-sh-yesno.sh | 33 ++++++++++++++ test/units/is_older_than | 83 ----------------------------------- test/units/meson.build | 5 +++ test/units/sh_yesno | 33 -------------- 13 files changed, 240 insertions(+), 221 deletions(-) create mode 100755 test/check-obsolete-functions.sh create mode 100644 test/check-spacing-style.sh create mode 100755 test/check-trailing-newlines.sh create mode 100755 test/check-trailing-whitespace.sh create mode 100755 test/check-xfunc-usage.sh create mode 100644 test/meson.build delete mode 100755 test/runtests.sh create mode 100755 test/units/check-is-older-than.sh create mode 100755 test/units/check-sh-yesno.sh delete mode 100755 test/units/is_older_than create mode 100644 test/units/meson.build delete mode 100755 test/units/sh_yesno (limited to 'test') diff --git a/test/check-obsolete-functions.sh b/test/check-obsolete-functions.sh new file mode 100755 index 00000000..c9a200f1 --- /dev/null +++ b/test/check-obsolete-functions.sh @@ -0,0 +1,11 @@ +#!/bin/sh + +top_srcdir=${SOURCE_ROOT:-..} +. ${top_srcdir}/test/setup_env.sh + +ebegin "Checking for obsolete functions" +out=$(cd ${top_srcdir}; find src -name '*.[ch]' \ + ! -name queue.h \ + -exec grep -n -E '\<(malloc|memory|sys/(errno|fcntl|signal|stropts|termios|unistd))\.h\>' {} +) +[ -z "${out}" ] +eend $? "Avoid these obsolete functions:"$'\n'"${out}" diff --git a/test/check-spacing-style.sh b/test/check-spacing-style.sh new file mode 100644 index 00000000..b8c1cd2a --- /dev/null +++ b/test/check-spacing-style.sh @@ -0,0 +1,18 @@ +#!/bin/sh + +top_srcdir=${SOURCE_ROOT:-..} +. ${top_srcdir}/test/setup_env.sh + +ebegin "Checking spacing style" +out=$(cd ${top_srcdir}; find src -name '*.[ch]' \ + ! -name queue.h \ + -exec grep -n -E \ + -e '\<(for|if|switch|while)\(' \ + -e '\<(for|if|switch|while) \( ' \ + -e ' ;' \ + -e '[[:space:]]$' \ + -e '\){' \ + -e '(^|[^:])//' \ + {} +) +[ -z "${out}" ] +eend $? "These lines violate style rules:"$'\n'"${out}" diff --git a/test/check-trailing-newlines.sh b/test/check-trailing-newlines.sh new file mode 100755 index 00000000..4c99e761 --- /dev/null +++ b/test/check-trailing-newlines.sh @@ -0,0 +1,19 @@ +#!/bin/sh + +top_srcdir=${SOURCE_ROOT:-..} +. ${top_srcdir}/test/setup_env.sh + +ebegin "Checking trailing newlines in code" +out=$(cd ${top_srcdir}; + for f in $(find */ -name '*.[ch]') ; do + while read -r line; do + if [ -n "${line}" ]; then + blankline= + else + blankline=1 + fi + done < "${f}" + [ -n "${blankline}" ] && printf "%s\n" "${f}" + done) +[ -z "${out}" ] +eend $? "Trailing newlines need to be deleted:"$'\n'"${out}" diff --git a/test/check-trailing-whitespace.sh b/test/check-trailing-whitespace.sh new file mode 100755 index 00000000..4aa4af9a --- /dev/null +++ b/test/check-trailing-whitespace.sh @@ -0,0 +1,12 @@ +#!/bin/sh + +top_srcdir=${SOURCE_ROOT:-..} +. ${top_srcdir}/test/setup_env.sh + +ebegin "Checking trailing whitespace in code" +# XXX: Should we check man pages too ? +out=$(cd ${top_srcdir}; find */ \ + '(' -name '*.[ch]' -o -name '*.in' -o -name '*.sh' ')' \ + -exec grep -n -E '[[:space:]]+$' {} +) +[ -z "${out}" ] +eend $? "Trailing whitespace needs to be deleted:"$'\n'"${out}" diff --git a/test/check-xfunc-usage.sh b/test/check-xfunc-usage.sh new file mode 100755 index 00000000..1e35002a --- /dev/null +++ b/test/check-xfunc-usage.sh @@ -0,0 +1,15 @@ +#!/bin/sh + +top_srcdir=${SOURCE_ROOT:-..} +. ${top_srcdir}/test/setup_env.sh + +ebegin "Checking for x* func usage" +out=$(cd ${top_srcdir}; find src -name '*.[ch]' \ + ! -name queue.h \ + -exec grep -n -E '\<(malloc|strdup)[[:space:]]*\(' {} + \ + | grep -v \ + -e src/shared/helpers.h \ + -e src/libeinfo/libeinfo.c) + +[ -z "${out}" ] +eend $? "These need to be using the x* variant:"$'\n'"${out}" diff --git a/test/meson.build b/test/meson.build new file mode 100644 index 00000000..5f1274df --- /dev/null +++ b/test/meson.build @@ -0,0 +1,26 @@ +if meson.version().version_compare('>=0.56.0') + build_root = meson.project_build_root() + source_root = meson.project_source_root() +else + build_root = meson.build_root() + source_root = meson.source_root() +endif + +test_env = [ + 'BUILD_ROOT=' + build_root, + 'SOURCE_ROOT=' + source_root + ] + +check_obsolete_functions = find_program('check-obsolete-functions.sh') +check_spacing_style = find_program('check-spacing-style.sh') +check_trailing_newlines = find_program('check-trailing-newlines.sh') +check_trailing_whitespace = find_program('check-trailing-whitespace.sh') +check_xfunc_usage = find_program('check-xfunc-usage.sh') + +test('check for obsolete functions', check_obsolete_functions, env : test_env) +test('check spacing style', check_spacing_style, env : test_env) +test('check trailing newlines', check_trailing_newlines, env : test_env) +test('check trailing whitespace', check_trailing_whitespace, env : test_env) +test('check xfunc usage', check_xfunc_usage, env : test_env) + +subdir('units') diff --git a/test/runtests.sh b/test/runtests.sh deleted file mode 100755 index 5e21d2ab..00000000 --- a/test/runtests.sh +++ /dev/null @@ -1,92 +0,0 @@ -#!/bin/sh - -top_srcdir=${top_srcdir:-..} -. ${top_srcdir}/test/setup_env.sh - -libeinfo_srcdir="${srcdir}/../libeinfo" -libeinfo_builddir="${builddir}/../libeinfo" -librc_srcdir="${srcdir}/../librc" -librc_builddir="${builddir}/../librc" -rc_srcdir="${srcdir}/../rc" -rc_builddir="${builddir}/../rc" - -checkit() { - local base=$1; shift - echo "$@" | tr ' ' '\n' > ${base}.out - diff -u ${base}.list ${base}.out - eend $? - : $(( ret += $? )) -} - -ret=0 - -fail_on_out() { - if [ -n "${out}" ]; then - eerror "Last command failed; failing" - exit 1 - fi -} - -ebegin "Checking trailing whitespace in code" -# XXX: Should we check man pages too ? -out=$(cd ${top_srcdir}; find */ \ - '(' -name '*.[ch]' -o -name '*.in' -o -name '*.sh' ')' \ - -exec grep -n -E '[[:space:]]+$' {} +) -[ -z "${out}" ] -eend $? "Trailing whitespace needs to be deleted:"$'\n'"${out}" -fail_on_out - -ebegin "Checking trailing newlines in code" -out=$(cd ${top_srcdir}; - for f in `find */ -name '*.[ch]'` ; do - sed -n -e :a -e '/^\n*$/{$q1;N;ba' -e '}' $f || echo $f - done) -[ -z "${out}" ] -eend $? "Trailing newlines need to be deleted:"$'\n'"${out}" -fail_on_out - -ebegin "Checking for obsolete functions" -out=$(cd ${top_srcdir}; find src -name '*.[ch]' \ - ! -name queue.h \ - -exec grep -n -E '\<(malloc|memory|sys/(errno|fcntl|signal|stropts|termios|unistd))\.h\>' {} +) -[ -z "${out}" ] -eend $? "Avoid these obsolete functions:"$'\n'"${out}" -fail_on_out - -ebegin "Checking for x* func usage" -out=$(cd ${top_srcdir}; find src -name '*.[ch]' \ - ! -name queue.h \ - -exec grep -n -E '\<(malloc|strdup)[[:space:]]*\(' {} + \ - | grep -v \ - -e src/includes/helpers.h \ - -e src/libeinfo/libeinfo.c) -[ -z "${out}" ] -eend $? "These need to be using the x* variant:"$'\n'"${out}" -fail_on_out - -ebegin "Checking spacing style" -out=$(cd ${top_srcdir}; find src -name '*.[ch]' \ - ! -name queue.h \ - -exec grep -n -E \ - -e '\<(for|if|switch|while)\(' \ - -e '\<(for|if|switch|while) \( ' \ - -e ' ;' \ - -e '[[:space:]]$' \ - -e '\){' \ - -e '(^|[^:])//' \ - {} +) -[ -z "${out}" ] -eend $? "These lines violate style rules:"$'\n'"${out}" -fail_on_out - -einfo "Running unit tests" -eindent -for u in units/*; do - [ -x "${u}" -a -f "${u}" ] || continue - ebegin "$(basename "${u}")" - ./"${u}" - eend $? - : $(( ret += $? )) -done - -exit ${ret} diff --git a/test/setup_env.sh b/test/setup_env.sh index 881984f9..1dcd12d2 100755 --- a/test/setup_env.sh +++ b/test/setup_env.sh @@ -1,23 +1,21 @@ #!/bin/sh -if [ -z "${top_srcdir}" ] ; then - echo "You must set top_srcdir before sourcing this file" 1>&2 +if [ -z "${BUILD_ROOT}" ] ; then + printf "%s\n" "You must export BUILD_ROOT before sourcing this file" >&2 exit 1 fi -srcdir=${srcdir:-.} -top_builddir=${top_builddir:-${top_srcdir}} -builddir=${builddir:-${srcdir}} - -LD_LIBRARY_PATH=${top_builddir}/src/libeinfo:${top_builddir}/src/librc:${LD_LIBRARY_PATH} -PATH=${top_builddir}/src/rc:${PATH} -export LD_LIBRARY_PATH PATH +if [ -z "${SOURCE_ROOT}" ] ; then + printf "%s\n" "You must export SOURCE_ROOT before sourcing this file" >&2 + exit 1 +fi -if [ ! -f ${top_srcdir}/sh/functions.sh ] ; then - echo "functions.sh not yet created !?" 1>&2 +if [ ! -f ${BUILD_ROOT}/sh/functions.sh ] ; then + printf "%s\n" "functions.sh not yet created !?" >&2 exit 1 -elif ! . ${top_srcdir}/sh/functions.sh; then - echo "Sourcing functions.sh failed !?" 1>&2 +elif ! . ${BUILD_ROOT}/sh/functions.sh; then + printf "%s\n" "Sourcing functions.sh failed !?" >&2 exit 1 fi +PATH="${BUILD_ROOT}"/src/einfo:${PATH} diff --git a/test/units/check-is-older-than.sh b/test/units/check-is-older-than.sh new file mode 100755 index 00000000..8987fcd2 --- /dev/null +++ b/test/units/check-is-older-than.sh @@ -0,0 +1,90 @@ +#!/bin/sh +# unit test for is_older_than code of baselayout (2008/06/19) +# Author: Matthias Schwarzott + +if [ -z "${BUILD_ROOT}" ]; then + printf "%s\n" "BUILD_ROOT must be defined" >&2 + exit 1 +fi +PATH="${BUILD_ROOT}"/src/is_older_than:${PATH} + +TMPDIR="${BUILD_ROOT}"/tmp-"$(basename "$0")" + +# Please note that we added this unit test because the function +# should really be called is_newer_than as it's what it's really testing. +# Or more perversly, returning 0 on failure and 1 and success. + +# bool is_older_than(reference, files/dirs to check) +# +# return 0 if any of the files/dirs are newer than +# the reference file +# +# EXAMPLE: if is_older_than a.out *.o ; then ... +ref_is_older_than() +{ + local x= ref="$1" + shift + + for x; do + [ "${x}" -nt "${ref}" ] && return 0 + if [ -d "${x}" ]; then + ref_is_older_than "${ref}" "${x}"/* && return 0 + fi + done + return 1 +} + +do_test() +{ + local r1= r2= + + ref_is_older_than "$@" + r1=$? + is_older_than "$@" + r2=$? + + [ -n "${VERBOSE}" ] && + printf "reference = %s | OpenRC = %s\n" "$r1" "$r2" + [ $r1 = $r2 ] +} + +echo_cmd() +{ + [ -n "${VERBOSE}" ] && printf "%s\n" "$@" + "$@" +} + +test_it() +{ + do_test "${TMPDIR}"/ref "${TMPDIR}"/dir1 "${TMPDIR}"/dir2 +} + +run_test() +{ + echo_cmd mkdir -p "${TMPDIR}"/dir1 "${TMPDIR}"/dir2 + echo_cmd touch "${TMPDIR}"/dir1/f1 "${TMPDIR}"/dir1/f2 \ + "${TMPDIR}"/dir1/f3 "${TMPDIR}"/dir2/f1 \ + "${TMPDIR}"/dir2/f2 "${TMPDIR}"/dir2/f3 + echo_cmd sleep 1 + echo_cmd touch "${TMPDIR}"/ref + test_it || return 1 + + echo_cmd sleep 1 + echo_cmd touch "${TMPDIR}"/dir1/f2 + test_it || return 1 + + echo_cmd sleep 1 + echo_cmd touch "${TMPDIR}"/ref + test_it || return 1 + + echo_cmd sleep 1 + echo_cmd touch "${TMPDIR}"/dir2/f2 + test_it || return 1 +} + +rm -rf "${TMPDIR}" +mkdir "${TMPDIR}" +run_test +retval=$? +rm -rf "${TMPDIR}" +exit ${retval} diff --git a/test/units/check-sh-yesno.sh b/test/units/check-sh-yesno.sh new file mode 100755 index 00000000..ecc2f6dd --- /dev/null +++ b/test/units/check-sh-yesno.sh @@ -0,0 +1,33 @@ +#!/bin/sh +# Copyright (c) 2008-2015 The OpenRC Authors. +# See the Authors file at the top-level directory of this distribution and +# https://github.com/OpenRC/openrc/blob/HEAD/AUTHORS +# +# This file is part of OpenRC. It is subject to the license terms in +# the LICENSE file found in the top-level directory of this +# distribution and at https://github.com/OpenRC/openrc/blob/HEAD/LICENSE +# This file may not be copied, modified, propagated, or distributed +# except according to the terms contained in the LICENSE file. + +top_srcdir=${SOURCE_ROOT:-..} +. $top_srcdir/test/setup_env.sh + +ret=0 + +tret=0 +for f in yes YES Yes true TRUE True 1 ; do + if ! yesno $f; then + : $(( tret += 1 )) + echo "!$f!" + fi +done +for f in no NO No false FALSE False 0 ; do + if yesno $f; then + : $(( tret += 1 )) + echo "!$f!" + fi +done +: $(( ret += $tret )) + +eend $ret +exit $ret diff --git a/test/units/is_older_than b/test/units/is_older_than deleted file mode 100755 index 47a62d78..00000000 --- a/test/units/is_older_than +++ /dev/null @@ -1,83 +0,0 @@ -#!/bin/sh -# unit test for is_older_than code of baselayout (2008/06/19) -# Author: Matthias Schwarzott - -TMPDIR=tmp-"$(basename "$0")" - -# Please note that we added this unit test because the function -# should really be called is_newer_than as it's what it's really testing. -# Or more perversly, returning 0 on failure and 1 and success. - -# bool is_older_than(reference, files/dirs to check) -# -# return 0 if any of the files/dirs are newer than -# the reference file -# -# EXAMPLE: if is_older_than a.out *.o ; then ... -ref_is_older_than() -{ - local x= ref="$1" - shift - - for x; do - [ "${x}" -nt "${ref}" ] && return 0 - if [ -d "${x}" ]; then - ref_is_older_than "${ref}" "${x}"/* && return 0 - fi - done - return 1 -} - -do_test() -{ - local r1= r2= - - ref_is_older_than "$@" - r1=$? - is_older_than "$@" - r2=$? - - [ -n "${VERBOSE}" ] && echo "reference = $r1 | OpenRC = $r2" - [ $r1 = $r2 ] -} - -echo_cmd() -{ - [ -n "${VERBOSE}" ] && echo "$@" - "$@" -} - -test_it() -{ - do_test "${TMPDIR}"/ref "${TMPDIR}"/dir1 "${TMPDIR}"/dir2 -} - -run_test() -{ - echo_cmd mkdir -p "${TMPDIR}"/dir1 "${TMPDIR}"/dir2 - echo_cmd touch "${TMPDIR}"/dir1/f1 "${TMPDIR}"/dir1/f2 \ - "${TMPDIR}"/dir1/f3 "${TMPDIR}"/dir2/f1 \ - "${TMPDIR}"/dir2/f2 "${TMPDIR}"/dir2/f3 - echo_cmd sleep 1 - echo_cmd touch "${TMPDIR}"/ref - test_it || return 1 - - echo_cmd sleep 1 - echo_cmd touch "${TMPDIR}"/dir1/f2 - test_it || return 1 - - echo_cmd sleep 1 - echo_cmd touch "${TMPDIR}"/ref - test_it || return 1 - - echo_cmd sleep 1 - echo_cmd touch "${TMPDIR}"/dir2/f2 - test_it || return 1 -} - -rm -rf "${TMPDIR}" -mkdir "${TMPDIR}" -run_test -retval=$? -rm -rf "${TMPDIR}" -exit ${retval} diff --git a/test/units/meson.build b/test/units/meson.build new file mode 100644 index 00000000..23c2758a --- /dev/null +++ b/test/units/meson.build @@ -0,0 +1,5 @@ +is_older_than = find_program('check-is-older-than.sh') +sh_yesno = find_program('check-sh-yesno.sh') + +test('is_older_than', is_older_than, env : test_env) +test('sh_yesno', sh_yesno, env : test_env) diff --git a/test/units/sh_yesno b/test/units/sh_yesno deleted file mode 100755 index 380864ee..00000000 --- a/test/units/sh_yesno +++ /dev/null @@ -1,33 +0,0 @@ -#!/bin/sh -# Copyright (c) 2008-2015 The OpenRC Authors. -# See the Authors file at the top-level directory of this distribution and -# https://github.com/OpenRC/openrc/blob/HEAD/AUTHORS -# -# This file is part of OpenRC. It is subject to the license terms in -# the LICENSE file found in the top-level directory of this -# distribution and at https://github.com/OpenRC/openrc/blob/HEAD/LICENSE -# This file may not be copied, modified, propagated, or distributed -# except according to the terms contained in the LICENSE file. - -: ${top_srcdir:=..} -. $top_srcdir/test/setup_env.sh - -ret=0 - -tret=0 -for f in yes YES Yes true TRUE True 1 ; do - if ! yesno $f; then - : $(( tret += 1 )) - echo "!$f!" - fi -done -for f in no NO No false FALSE False 0 ; do - if yesno $f; then - : $(( tret += 1 )) - echo "!$f!" - fi -done -: $(( ret += $tret )) - -eend $ret -exit $ret -- cgit v1.2.3