From e4f0e05af32f9dedd9bda16a0b75670a4eed8518 Mon Sep 17 00:00:00 2001 From: Bastien Nocera Date: Mon, 4 Apr 2022 17:46:57 +0200 Subject: [PATCH] tests: Make tests a build option And don't skip tests if any test dependencies are missing. --- .gitlab-ci.yml | 2 +- README.md | 4 ++++ meson.build | 17 ++++++++++++++++- meson_options.txt | 4 ++++ tests/integration-test.py | 19 +++++++------------ 5 files changed, 32 insertions(+), 14 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index cd6ffc5..773d26d 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -25,7 +25,7 @@ build_stable: - dnf update -y && dnf install -y $DEPENDENCIES - dnf reinstall -y glib2 script: - - meson -Dgtk_doc=true -Dgtk-tests=true _build + - meson -Dtests=true -Dgtk_doc=true -Dgtk-tests=true _build - ninja -v -C _build - ninja -v -C _build install - ninja -v -C _build uninstall diff --git a/README.md b/README.md index 7da59ea..2506569 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,10 @@ $ ninja -v -C _build install ``` It requires libgudev, systemd (>= 233 for the accelerometer quirks) and polkit-gobject. +Running the tests will require gobject-introspection support for Python, +[python-dbusmock](https://github.com/martinpitt/python-dbusmock/), +[umockdev](https://github.com/martinpitt/umockdev) and [psutil](https://github.com/giampaolo/psutil). + Usage ----- diff --git a/meson.build b/meson.build index eb6ca15..b96ffc1 100644 --- a/meson.build +++ b/meson.build @@ -66,4 +66,19 @@ if get_option('gtk_doc') subdir('docs') endif -subdir('tests') +if get_option('tests') + # Python 3 required modules + python3_required_modules = ['psutil', 'dbusmock', 'gi'] + + python = import('python') + python3 = python.find_installation('python3') + foreach p : python3_required_modules + # Source: https://docs.python.org/3/library/importlib.html#checking-if-a-module-can-be-imported + script = 'import importlib.util; import sys; exit(1) if importlib.util.find_spec(\''+ p +'\') is None else exit(0)' + if run_command(python3, '-c', script, check: false).returncode() != 0 + error('Python3 module \'' + p + '\' required for running tests but not found') + endif + endforeach + + subdir('tests') +endif diff --git a/meson_options.txt b/meson_options.txt index 758427d..de5fbf4 100644 --- a/meson_options.txt +++ b/meson_options.txt @@ -6,6 +6,10 @@ option('systemdsystemunitdir', description: 'systemd unit directory', type: 'string', value: 'auto') +option('tests', + description: 'Whether to run tests', + type: 'boolean', + value: false) option('gtk-tests', description: 'Whether to build GTK tests', type: 'boolean', diff --git a/tests/integration-test.py b/tests/integration-test.py index 4adeb86..8504626 100755 --- a/tests/integration-test.py +++ b/tests/integration-test.py @@ -21,32 +21,27 @@ import os import sys import dbus +import dbusmock +import gi import tempfile -import subprocess import psutil +import subprocess import unittest import time try: - import gi from gi.repository import GLib from gi.repository import Gio except ImportError as e: - sys.stderr.write('Skipping tests, PyGobject not available for Python 3, or missing GI typelibs: %s\n' % str(e)) - sys.exit(0) + sys.stderr.write('PyGobject not available for Python 3, or missing GI typelibs: %s\n' % str(e)) + sys.exit(1) try: gi.require_version('UMockdev', '1.0') from gi.repository import UMockdev except ImportError: - sys.stderr.write('Skipping tests, umockdev not available (https://github.com/martinpitt/umockdev)\n') - sys.exit(0) - -try: - import dbusmock -except ImportError: - sys.stderr.write('Skipping tests, python-dbusmock not available (http://pypi.python.org/pypi/python-dbusmock).\n') - sys.exit(0) + sys.stderr.write('umockdev not available (https://github.com/martinpitt/umockdev)\n') + sys.exit(1) SP = 'net.hadess.SensorProxy'