From f52e12eafd17eba51fadd12b33e00fea31d31d83 Mon Sep 17 00:00:00 2001 From: Bartosz Golaszewski Date: Tue, 6 Aug 2019 17:01:53 +0200 Subject: [PATCH] tools: tests: run the bats script indirectly from a shell script Bats makes it very difficult to run some initial code once, perform checks for requirements and optionally exit if they are not satisfied. Instead of working around it in the .bats script, just create a separate shell script that does all the initial checks and then execs the actual bats test-suite. Signed-off-by: Bartosz Golaszewski --- tools/Makefile.am | 4 +-- tools/gpio-tools-test.bats | 1 - tools/gpio-tools-test.sh | 58 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+), 3 deletions(-) mode change 100755 => 100644 tools/gpio-tools-test.bats create mode 100755 tools/gpio-tools-test.sh diff --git a/tools/Makefile.am b/tools/Makefile.am index d9615f9..da25283 100644 --- a/tools/Makefile.am +++ b/tools/Makefile.am @@ -28,10 +28,10 @@ gpiomon_SOURCES = gpiomon.c gpiofind_SOURCES = gpiofind.c -EXTRA_DIST = gpio-tools-test.bats +EXTRA_DIST = gpio-tools-test.bats gpio-tools-test.sh if WITH_TESTS -bin_SCRIPTS = gpio-tools-test.bats +bin_SCRIPTS = gpio-tools-test.sh gpio-tools-test.bats endif diff --git a/tools/gpio-tools-test.bats b/tools/gpio-tools-test.bats old mode 100755 new mode 100644 index df0533c..f36d4d4 --- a/tools/gpio-tools-test.bats +++ b/tools/gpio-tools-test.bats @@ -1,4 +1,3 @@ -#!/usr/bin/env bats # SPDX-License-Identifier: LGPL-2.1-or-later # diff --git a/tools/gpio-tools-test.sh b/tools/gpio-tools-test.sh new file mode 100755 index 0000000..be4f492 --- /dev/null +++ b/tools/gpio-tools-test.sh @@ -0,0 +1,58 @@ +#!/usr/bin/env bash +# SPDX-License-Identifier: LGPL-2.1-or-later + +# +# This file is part of libgpiod. +# +# Copyright (C) 2019 Bartosz Golaszewski +# + +MIN_KERNEL_VERSION="5.1.0" +BATS_SCRIPT="gpio-tools-test.bats" +SOURCE_DIR="$(dirname ${BASH_SOURCE[0]})" + +die() { + echo "$@" 1>&2 + exit 1 +} + +check_kernel() { + local REQUIRED=$1 + local CURRENT=$(uname -r) + + SORTED=$(printf "$REQUIRED\n$CURRENT" | sort -V | head -n 1) + + if [ "$SORTED" != "$REQUIRED" ] + then + die "linux kernel version must be at least: v$REQUIRED - got: v$CURRENT" + fi +} + +check_prog() { + local PROG=$1 + + which "$PROG" > /dev/null + if [ "$?" -ne "0" ] + then + die "$PROG not found - needed to run the tests" + fi +} + +# Check all required non-coreutils tools +check_prog bats +check_prog modprobe +check_prog rmmod +check_prog udevadm + +# Check if we're running a kernel at the required version or later +check_kernel $MIN_KERNEL_VERSION + +# The bats script must be in the same directory. +if [ ! -e "$SOURCE_DIR/$BATS_SCRIPT" ] +then + die "testing script not found" +fi + +BATS_PATH=$(which bats) + +exec $BATS_PATH $SOURCE_DIR/$BATS_SCRIPT -- 2.30.2