diff options
author | Bastien Nocera <hadess@hadess.net> | 2016-11-02 17:27:44 +0100 |
---|---|---|
committer | Bastien Nocera <hadess@hadess.net> | 2016-11-02 18:32:00 +0100 |
commit | 0a942cc5dd79837cdc0c73ffd748f8ac4d778390 (patch) | |
tree | 2c0afee29d7757e192fb74cf1ce60ffa4349ef34 | |
parent | 90e261f157e1c6898f0aed43b41efa5b79df3f49 (diff) | |
download | libgudev-0a942cc5dd79837cdc0c73ffd748f8ac4d778390.tar.gz |
tests: Add test suite
With the first test for a GUdevEnumerator bug.
https://bugzilla.gnome.org/show_bug.cgi?id=773224
-rw-r--r-- | Makefile.am | 2 | ||||
-rw-r--r-- | Makefile.decl | 53 | ||||
-rw-r--r-- | configure.ac | 12 | ||||
-rw-r--r-- | tests/Makefile.am | 16 | ||||
-rw-r--r-- | tests/test-enumerator-filter.c | 81 |
5 files changed, 164 insertions, 0 deletions
diff --git a/Makefile.am b/Makefile.am index 1f941f6..2df90eb 100644 --- a/Makefile.am +++ b/Makefile.am @@ -174,6 +174,8 @@ if ENABLE_GTK_DOC SUBDIRS += docs endif +SUBDIRS += tests + #docs/html: # $(AM_V_at)$(MKDIR_P) $(dir $@) # $(AM_V_LN)$(LN_S) -f ../gudev/html $@ diff --git a/Makefile.decl b/Makefile.decl new file mode 100644 index 0000000..aab33c3 --- /dev/null +++ b/Makefile.decl @@ -0,0 +1,53 @@ +GTESTER = gtester # in $PATH for non-GLIB packages +GTESTER_REPORT = gtester-report # in $PATH for non-GLIB packages + +# initialize variables for unconditional += appending +TEST_PROGS = + +# test: run all tests in cwd and subdirs +test: ${TEST_PROGS} + @test -z "${TEST_PROGS}" || ${GTESTER} --verbose ${TEST_PROGS} + @ for subdir in $(SUBDIRS) . ; do \ + test "$$subdir" = "." -o "$$subdir" = "po" || \ + ( cd $$subdir && $(MAKE) $(AM_MAKEFLAGS) $@ ) || exit $? ; \ + done + +# test-report: run tests in subdirs and generate report +# perf-report: run tests in subdirs with -m perf and generate report +# full-report: like test-report: with -m perf and -m slow +test-report perf-report full-report: ${TEST_PROGS} + @test -z "${TEST_PROGS}" || { \ + case $@ in \ + test-report) test_options="-k";; \ + perf-report) test_options="-k -m=perf";; \ + full-report) test_options="-k -m=perf -m=slow";; \ + esac ; \ + if test -z "$$GTESTER_LOGDIR" ; then \ + ${GTESTER} --verbose $$test_options -o test-report.xml ${TEST_PROGS} ; \ + elif test -n "${TEST_PROGS}" ; then \ + ${GTESTER} --verbose $$test_options -o `mktemp "$$GTESTER_LOGDIR/log-XXXXXX"` ${TEST_PROGS} ; \ + fi ; \ + } + @ ignore_logdir=true ; \ + if test -z "$$GTESTER_LOGDIR" ; then \ + GTESTER_LOGDIR=`mktemp -d "\`pwd\`/.testlogs-XXXXXX"`; export GTESTER_LOGDIR ; \ + ignore_logdir=false ; \ + fi ; \ + for subdir in $(SUBDIRS) . ; do \ + test "$$subdir" = "." -o "$$subdir" = "po" || \ + ( cd $$subdir && $(MAKE) $(AM_MAKEFLAGS) $@ ) || exit $? ; \ + done ; \ + $$ignore_logdir || { \ + echo '<?xml version="1.0"?>' > $@.xml ; \ + echo '<report-collection>' >> $@.xml ; \ + for lf in `ls -L "$$GTESTER_LOGDIR"/.` ; do \ + sed '1,1s/^<?xml\b[^>?]*?>//' <"$$GTESTER_LOGDIR"/"$$lf" >> $@.xml ; \ + done ; \ + echo >> $@.xml ; \ + echo '</report-collection>' >> $@.xml ; \ + rm -rf "$$GTESTER_LOGDIR"/ ; \ + ${GTESTER_REPORT} --version 2>/dev/null 1>&2 ; test "$$?" != 0 || ${GTESTER_REPORT} $@.xml >$@.html ; \ + } +.PHONY: test test-report perf-report full-report +# run make test as part of make check +check-local: test diff --git a/configure.ac b/configure.ac index 328b311..7b67b3b 100644 --- a/configure.ac +++ b/configure.ac @@ -41,9 +41,19 @@ GOBJECT_INTROSPECTION_CHECK([1.31.1]) PKG_CHECK_MODULES([LIBUDEV], [libudev >= 199]) PKG_CHECK_MODULES([GLIB], [glib-2.0 >= 2.30.0 gobject-2.0 >= 2.30.0 gio-2.0]) +AC_ARG_ENABLE([umockdev], + AS_HELP_STRING([--disable-umockdev], [disable umockdev tests (default: enabled)]),, + [enable_umockdev=yes]) +if test x"$enable_umockdev" != x"no" ; then + PKG_CHECK_MODULES([UMOCKDEV], umockdev-1.0) +fi + +AM_CONDITIONAL(HAVE_UMOCKDEV, [test x"$enable_umockdev" = x"yes"]) + AC_CONFIG_FILES([ Makefile gudev-1.0.pc + tests/Makefile docs/Makefile docs/version.xml ]) @@ -62,4 +72,6 @@ AC_MSG_RESULT([ CFLAGS: ${CFLAGS} CPPFLAGS: ${CPPFLAGS} LDFLAGS: ${LDFLAGS} + + umockdev (tests): ${enable_umockdev} ]) diff --git a/tests/Makefile.am b/tests/Makefile.am new file mode 100644 index 0000000..4352a81 --- /dev/null +++ b/tests/Makefile.am @@ -0,0 +1,16 @@ +include $(top_srcdir)/Makefile.decl + +all-local: check-local + +if HAVE_UMOCKDEV + +noinst_PROGRAMS = test-enumerator-filter +TEST_PROGS += $(noinst_PROGRAMS) + +test_enumerator_filter_SOURCES = test-enumerator-filter.c +test_enumerator_filter_CFLAGS = $(UMOCKDEV_CFLAGS) -I$(top_srcdir) +test_enumerator_filter_LDADD = $(UMOCKDEV_LIBS) $(top_builddir)/libgudev-1.0.la + +endif + +EXTRA_DIST = test-enumerator-filter.c diff --git a/tests/test-enumerator-filter.c b/tests/test-enumerator-filter.c new file mode 100644 index 0000000..e5fc0b6 --- /dev/null +++ b/tests/test-enumerator-filter.c @@ -0,0 +1,81 @@ +/* umockdev example: use libumockdev in C to fake a battery + * Build with: + * gcc battery.c -Wall `pkg-config --cflags --libs umockdev-1.0 gio-2.0` -o /tmp/battery + * Run with: + * umockdev-wrapper /tmp/battery + * + * Copyright (C) 2013 Canonical Ltd. + * Author: Martin Pitt <martin.pitt@ubuntu.com> + * + * umockdev is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * umockdev is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; If not, see <http://www.gnu.org/licenses/>. + */ + +#include <locale.h> +#include <unistd.h> +#include <stdio.h> +#include <sys/wait.h> +#include <glib.h> +#include <gio/gio.h> +#include <umockdev.h> + +#include <gudev/gudev.h> + +static void +test_enumerator_filter (void) +{ + /* create test bed */ + UMockdevTestbed *testbed = umockdev_testbed_new (); + + /* Add 2 devices in the USB subsystem, and one in the DRM subsystem */ + umockdev_testbed_add_device (testbed, "usb", "dev1", NULL, + "idVendor", "0815", "idProduct", "AFFE", NULL, + "ID_MODEL", "KoolGadget", NULL); + + umockdev_testbed_add_device (testbed, "usb", "dev2", NULL, + "idVendor", "0815", "idProduct", "AFFF", NULL, + "ID_MODEL", "KoolGadget 2", NULL); + + umockdev_testbed_add_device (testbed, "drm", "dev3", NULL, + "ID_FOR_SEAT", "drm-pci-0000_00_02_0", NULL); + + /* Check the number of items in GUdevClient */ + const gchar *subsystems[] = { "drm", NULL}; + GUdevClient *client = g_udev_client_new (subsystems); + + GList *devices = g_udev_client_query_by_subsystem (client, NULL); + g_assert_cmpint (g_list_length (devices), ==, 3); + g_list_free_full (devices, g_object_unref); + + devices = g_udev_client_query_by_subsystem (client, "usb"); + g_assert_cmpint (g_list_length (devices), ==, 2); + g_list_free_full (devices, g_object_unref); + + /* Check the number of items in GUdevEnumerator */ + GUdevEnumerator *enumerator = g_udev_enumerator_new (client); + devices = g_udev_enumerator_execute (enumerator); + g_assert_cmpint (g_list_length (devices), ==, 1); + g_list_free_full (devices, g_object_unref); +} + +int main(int argc, char **argv) +{ + setlocale (LC_ALL, NULL); + g_test_init (&argc, &argv, NULL); + + g_assert (umockdev_in_mock_environment ()); + + g_test_add_func ("/gudev/enumerator_filter", test_enumerator_filter); + + return g_test_run (); +} |