From 95df3720e18afbc2f908c899bfa4ec21d75f84ed Mon Sep 17 00:00:00 2001 From: Jan Kiszka Date: Fri, 8 Oct 2021 17:24:46 +0200 Subject: [PATCH] tests: Add basic tests for menu plugin This checks basic features of kas menu, some option selection, yaml including, rebuilding and target selection. The UI is operated by mocking smack's GridFormHelp.runOnce and some result evaluation functions. Injecting keys from the keyboard was not easily possibly (if at all), thus this approach. The menu rendering is not shown due to pytest. Signed-off-by: Jan Kiszka --- .github/workflows/next.yml | 20 +++++++- tests/test_menu.py | 84 +++++++++++++++++++++++++++++++ tests/test_menu/Kconfig | 32 ++++++++++++ tests/test_menu/bitbake | 3 ++ tests/test_menu/oe-init-build-env | 3 ++ tests/test_menu/opt1.yaml | 7 +++ tests/test_menu/test.yaml | 5 ++ 7 files changed, 153 insertions(+), 1 deletion(-) create mode 100644 tests/test_menu.py create mode 100644 tests/test_menu/Kconfig create mode 100755 tests/test_menu/bitbake create mode 100755 tests/test_menu/oe-init-build-env create mode 100644 tests/test_menu/opt1.yaml create mode 100644 tests/test_menu/test.yaml diff --git a/.github/workflows/next.yml b/.github/workflows/next.yml index 13839a3..9ad284a 100644 --- a/.github/workflows/next.yml +++ b/.github/workflows/next.yml @@ -8,6 +8,8 @@ on: env: SHELLCHECK_VERSION: v0.7.1 SHELLCHECK_SHA256: 64f17152d96d7ec261ad3086ed42d18232fcb65148b44571b564d688269d36c8 + NEWT_VERSION: 0.52.21 + NEWT_SHA256: 265eb46b55d7eaeb887fca7a1d51fe115658882dfe148164b6c49fccac5abb31 jobs: perform_tests: @@ -38,10 +40,26 @@ jobs: echo "$SHELLCHECK_SHA256 shellcheck-$SHELLCHECK_VERSION.linux.x86_64.tar.xz" | sha256sum -c tar -xJf shellcheck-$SHELLCHECK_VERSION.linux.x86_64.tar.xz sudo cp shellcheck-$SHELLCHECK_VERSION/shellcheck /usr/local/bin/ + - name: Install python-newt + run: | + sudo apt-get update + sudo apt-get install libpopt-dev libslang2-dev + wget -q https://releases.pagure.org/newt/newt-$NEWT_VERSION.tar.gz + echo "$NEWT_SHA256 newt-$NEWT_VERSION.tar.gz" | sha256sum -c + tar -C /tmp -xzf newt-$NEWT_VERSION.tar.gz + cd /tmp/newt-$NEWT_VERSION + autoconf + ./configure --with-python=python${{ matrix.python-version }} + make -j $(nproc) + sudo make install + ln -s /usr/local/lib/python${{ matrix.python-version }}/site-packages/_snack.so \ + $(python3 -c 'import site; print(site.getsitepackages()[0])')/ + ln -s /usr/local/lib/python${{ matrix.python-version }}/site-packages/snack.py \ + $(python3 -c 'import site; print(site.getsitepackages()[0])')/ - name: Run tests run: | scripts/checkcode.sh . - pytest + TERM=xterm pytest deploy_containers: name: Build and deploy container images diff --git a/tests/test_menu.py b/tests/test_menu.py new file mode 100644 index 0000000..e67aed2 --- /dev/null +++ b/tests/test_menu.py @@ -0,0 +1,84 @@ +# kas - setup tool for bitbake based projects +# +# Copyright (c) Siemens AG, 2021 +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be +# included in all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + +import os +import shutil +from kas import kas + +INPUTS = iter([' ', None, ' ', None]) +ACTIONS = iter([None, 'build', None, 'build']) +SELECTIONS = iter([0, 3]) + + +def mock_runOnce(unused1): + return next(INPUTS) + + +def mock_buttonPressed(unused1, unused2): + return next(ACTIONS) + + +def mock_current(unused1): + return next(SELECTIONS) + + +def file_contains(filename, expected): + with open(filename) as file: + for line in file.readlines(): + if line == expected: + return True + return False + + +def check_bitbake_options(expected): + with open('build/bitbake.options') as file: + return file.readline() == expected + + +def test_menu(monkeypatch, tmpdir): + tdir = str(tmpdir.mkdir('test_menu')) + shutil.rmtree(tdir, ignore_errors=True) + shutil.copytree('tests/test_menu', tdir) + cwd = os.getcwd() + os.chdir(tdir) + + monkeypatch.setattr('snack.GridFormHelp.runOnce', mock_runOnce) + monkeypatch.setattr('snack.ButtonBar.buttonPressed', mock_buttonPressed) + monkeypatch.setattr('snack.Listbox.current', mock_current) + + # select opt1 & build + kas.kas(['menu']) + assert file_contains('build/conf/local.conf', 'OPT1 = "1"\n') + assert file_contains('.config.yaml', 'build_system: openembedded\n') + assert check_bitbake_options('-c build target1\n') + + # rebuild test + kas.kas(['build']) + assert file_contains('build/conf/local.conf', 'OPT1 = "1"\n') + assert check_bitbake_options('-c build target1\n') + + # select alternative target & build + kas.kas(['menu']) + assert file_contains('build/conf/local.conf', 'OPT1 = "1"\n') + assert check_bitbake_options('-c build target2\n') + + os.chdir(cwd) diff --git a/tests/test_menu/Kconfig b/tests/test_menu/Kconfig new file mode 100644 index 0000000..29ae401 --- /dev/null +++ b/tests/test_menu/Kconfig @@ -0,0 +1,32 @@ +config KAS_INCLUDE_MAIN + string + default "test.yaml" + +config KAS_BUILD_SYSTEM + string + default "openembedded" + +config BOOL_OPT1 + bool "ppt1" + +config KAS_INCLUDE_OPT1 + string + default "opt1.yaml" + depends on BOOL_OPT1 + +choice + prompt "choice" + default CHOICE1 + +config CHOICE1 + bool "choice1" + +config CHOICE2 + bool "choice2" + +endchoice + +config KAS_TARGET_CHOICE + string + default "target1" if CHOICE1 + default "target2" if CHOICE2 diff --git a/tests/test_menu/bitbake b/tests/test_menu/bitbake new file mode 100755 index 0000000..9698cde --- /dev/null +++ b/tests/test_menu/bitbake @@ -0,0 +1,3 @@ +#!/bin/sh + +echo "$@" > bitbake.options diff --git a/tests/test_menu/oe-init-build-env b/tests/test_menu/oe-init-build-env new file mode 100755 index 0000000..8a0e21f --- /dev/null +++ b/tests/test_menu/oe-init-build-env @@ -0,0 +1,3 @@ +#!/bin/sh + +export PATH=$(pwd):$PATH diff --git a/tests/test_menu/opt1.yaml b/tests/test_menu/opt1.yaml new file mode 100644 index 0000000..b50aeb8 --- /dev/null +++ b/tests/test_menu/opt1.yaml @@ -0,0 +1,7 @@ +header: + version: 11 + +local_conf_header: + opt1: | + OPT1 = "1" + diff --git a/tests/test_menu/test.yaml b/tests/test_menu/test.yaml new file mode 100644 index 0000000..5958697 --- /dev/null +++ b/tests/test_menu/test.yaml @@ -0,0 +1,5 @@ +header: + version: 11 + +repos: + this: