From 1408f3f96ea483039e45aa217bb51385d1b5d74d Mon Sep 17 00:00:00 2001 From: Leandro Lanzieri Date: Mon, 12 Oct 2020 14:19:40 +0200 Subject: [PATCH] dist/tools/kconfiglib: add tests for genconfig.py Also add the test to 'python-tests' GitHub actions. --- .github/workflows/tools-test.yml | 3 + dist/tools/kconfiglib/tests/Kconfig.test | 29 ++++ dist/tools/kconfiglib/tests/bar_y.config | 1 + dist/tools/kconfiglib/tests/baz_y.config | 1 + dist/tools/kconfiglib/tests/foo_y.config | 1 + dist/tools/kconfiglib/tests/opt_2_y.config | 1 + dist/tools/kconfiglib/tests/test.sh | 186 +++++++++++++++++++++ 7 files changed, 222 insertions(+) create mode 100644 dist/tools/kconfiglib/tests/Kconfig.test create mode 100644 dist/tools/kconfiglib/tests/bar_y.config create mode 100644 dist/tools/kconfiglib/tests/baz_y.config create mode 100644 dist/tools/kconfiglib/tests/foo_y.config create mode 100644 dist/tools/kconfiglib/tests/opt_2_y.config create mode 100755 dist/tools/kconfiglib/tests/test.sh diff --git a/.github/workflows/tools-test.yml b/.github/workflows/tools-test.yml index 1d8fa2ae8c..56fdc079e1 100644 --- a/.github/workflows/tools-test.yml +++ b/.github/workflows/tools-test.yml @@ -23,9 +23,12 @@ jobs: run: | python -m pip install --upgrade pip python -m pip install tox + python -m pip install kconfiglib - name: Test backport_pr run: cd dist/tools/backport_pr && tox - name: Test compile_and_test_for_board run: cd dist/tools/compile_and_test_for_board && tox - name: Test riotctrl_shell run: cd dist/pythonlibs/riotctrl_shell && tox + - name: Test kconfig script + run: cd dist/tools/kconfiglib/tests && ./test.sh diff --git a/dist/tools/kconfiglib/tests/Kconfig.test b/dist/tools/kconfiglib/tests/Kconfig.test new file mode 100644 index 0000000000..2c23a0fd71 --- /dev/null +++ b/dist/tools/kconfiglib/tests/Kconfig.test @@ -0,0 +1,29 @@ +config APPLICATION + bool + default y + depends on MODULE_FOO + +config MODULE_FOO + bool "The Foo" + +config MODULE_BAR + bool "The Bar" + depends on MODULE_BAZ + +config MODULE_BAZ + bool "The Baz" + +choice + bool "Options" + +config OPT_1 + bool "1" + +config OPT_2 + bool "2" + depends on MODULE_BAZ + +config OPT_3 + bool "3" + +endchoice diff --git a/dist/tools/kconfiglib/tests/bar_y.config b/dist/tools/kconfiglib/tests/bar_y.config new file mode 100644 index 0000000000..4749633fef --- /dev/null +++ b/dist/tools/kconfiglib/tests/bar_y.config @@ -0,0 +1 @@ +CONFIG_MODULE_BAR=y diff --git a/dist/tools/kconfiglib/tests/baz_y.config b/dist/tools/kconfiglib/tests/baz_y.config new file mode 100644 index 0000000000..1d944a40ed --- /dev/null +++ b/dist/tools/kconfiglib/tests/baz_y.config @@ -0,0 +1 @@ +CONFIG_MODULE_BAZ=y diff --git a/dist/tools/kconfiglib/tests/foo_y.config b/dist/tools/kconfiglib/tests/foo_y.config new file mode 100644 index 0000000000..2dc6adf99b --- /dev/null +++ b/dist/tools/kconfiglib/tests/foo_y.config @@ -0,0 +1 @@ +CONFIG_MODULE_FOO=y diff --git a/dist/tools/kconfiglib/tests/opt_2_y.config b/dist/tools/kconfiglib/tests/opt_2_y.config new file mode 100644 index 0000000000..9905513cbd --- /dev/null +++ b/dist/tools/kconfiglib/tests/opt_2_y.config @@ -0,0 +1 @@ +CONFIG_OPT_2=y diff --git a/dist/tools/kconfiglib/tests/test.sh b/dist/tools/kconfiglib/tests/test.sh new file mode 100755 index 0000000000..8b98354841 --- /dev/null +++ b/dist/tools/kconfiglib/tests/test.sh @@ -0,0 +1,186 @@ +#!/usr/bin/env bash +# +# Copyright (C) 2020 HAW Hamburg +# 2020 Kaspar Schleiser +# 2020 Inria +# 2020 Freie Universität Berlin +# +# This file is subject to the terms and conditions of the GNU Lesser +# General Public License v2.1. See the file LICENSE in the top level +# directory for more details. + +# This script performs some tests on the genconfig.py script. It uses as main +# Kconfig tree file 'Kconfig.test'. It runs the script under different +# conditions by injecting different configuration files and checking the return +# values. + +KCONFIGLIB_DIR="$(readlink -f "$(dirname "$0")/..")" +TESTS_DIR=${KCONFIGLIB_DIR}/tests +GENCONFIG=${KCONFIGLIB_DIR}/genconfig.py + +KCONFIG_FILE="${TESTS_DIR}/Kconfig.test" +CONFIG_FOO_Y="${TESTS_DIR}/foo_y.config" +CONFIG_BAR_Y="${TESTS_DIR}/bar_y.config" +CONFIG_BAZ_Y="${TESTS_DIR}/baz_y.config" +CONFIG_OPT_2_Y="${TESTS_DIR}/opt_2_y.config" + +# prints a character representing the result: +# - a green tick when $1 is 0 +# - a red cross when $1 is 1 +function print_result { + local RED="\033[0;31m" + local GREEN="\033[0;32m" + local NO_COLOUR="\033[0m" + + if (( "$1" == 0 )); then + echo -e "${GREEN}✓$NO_COLOUR" + else + echo -e "${RED}x$NO_COLOUR" + fi +} + +# sets the global accumulative RESULT variable to keep record of errors during +# tests. +function set_result { + NEW_RESULT=$1 + + if (( NEW_RESULT != 0)) + then + RESULT=$NEW_RESULT + fi +} + +# runs a test passed in $1. It will print the corresponding name and result and +# update the global state of the tests. +function run_test { + echo -n "Running test: \"$*\" " + $1 + res=$? + + print_result $res + set_result $res +} + +# TEST +# checks that the used files are present. +function check_files_exist { + if ! test -f "${KCONFIG_FILE}" + then + 1>&2 echo -n "Missing Kconfig file ${KCONFIG_FILE}" + return 1 + fi + if ! test -f "${CONFIG_FOO_Y}" + then + 1>&2 echo -n "Missing configuration file ${CONFIG_FOO_Y}" + return 1 + fi + if ! test -f "${CONFIG_BAR_Y}" + then + 1>&2 echo -n "Missing configuration file ${CONFIG_BAR_Y}" + return 1 + fi + if ! test -f "${CONFIG_BAZ_Y}" + then + 1>&2 echo -n "Missing configuration file ${CONFIG_BAZ_Y}" + return 1 + fi + if ! test -f "${CONFIG_OPT_2_Y}" + then + 1>&2 echo -n "Missing configuration file ${CONFIG_BAZ_Y}" + return 1 + fi +} + +# TEST +# verifies that when the APPLICATION symbol is present, the script fails when +# its value is not 'y' because of missing dependencies (e.g. missing features). +function application_missing_deps_should_fail { + if OUT=$(${GENCONFIG} --kconfig-filename "${KCONFIG_FILE}" 2>&1) + then + 1>&2 echo -n "$OUT" + return 1 + else + return 0 + fi +} + +# TEST +# verifies that when the dependencies for the APPLICATION symbol are present +# the symbol gets the 'y' value and the script succeeds. +function application_with_deps_should_succeed { + if OUT=$(${GENCONFIG} --kconfig-filename "${KCONFIG_FILE}" \ + --config-sources "${CONFIG_FOO_Y}" 2>&1) + then + return 0 + else + 1>&2 echo -n "$OUT" + return 1 + fi +} + +# TEST +# verifies that when a module that the user tried to enable can't be enabled +# (i.e. has missing dependencies) the script fails. +function missing_module_should_fail { + if OUT=$(${GENCONFIG} --kconfig-filename "${KCONFIG_FILE}" \ + --config-sources "${CONFIG_BAR_Y}" "${CONFIG_FOO_Y}" 2>&1) + then + 1>&2 echo -n "$OUT" + return 1 + else + return 0 + fi +} + +# TEST +# verifies that when a module that the user tried to enable has its dependencies +# active, the script succeeds. +function module_with_deps_should_succeed { + if OUT=$(${GENCONFIG} --kconfig-filename "${KCONFIG_FILE}" \ + --config-sources "${CONFIG_BAZ_Y}" "${CONFIG_BAR_Y}" "${CONFIG_FOO_Y}" 2>&1) + then + return 0 + else + 1>&2 echo -n "$OUT" + return 1 + fi +} + +# TEST +# verifies that when a choice option that the user tried to enable can't be +# enabled (i.e. has missing dependencies) the script fails. +function missing_choice_should_fail { + if OUT=$(${GENCONFIG} --kconfig-filename "${KCONFIG_FILE}" \ + --config-sources "${CONFIG_FOO_Y}" "${CONFIG_OPT_2_Y}" 2>&1) + then + 1>&2 echo -n "$OUT" + return 1 + else + return 0 + fi +} + +# TEST +# verifies that when a choice option that the user tried to enable has its +# dependencies active, the script succeeds. +function choice_with_deps_should_succeed { + if OUT=$(${GENCONFIG} --kconfig-filename "${KCONFIG_FILE}" \ + --config-sources "${CONFIG_FOO_Y}" "${CONFIG_BAZ_Y}" \ + "${CONFIG_OPT_2_Y}" 2>&1) + then + return 0 + else + 1>&2 echo -n "$OUT" + return 1 + fi +} + +run_test check_files_exist +run_test application_missing_deps_should_fail +run_test application_with_deps_should_succeed +run_test missing_module_should_fail +run_test module_with_deps_should_succeed +run_test missing_choice_should_fail +run_test choice_with_deps_should_succeed + +exit $(( RESULT ))