diff options
author | Vivien Kraus <vivien@planete-kraus.eu> | 2023-03-07 19:41:56 +0100 |
---|---|---|
committer | Vivien Kraus <vivien@planete-kraus.eu> | 2023-03-07 20:44:30 +0100 |
commit | 18a3213d94b7d806e8461d151031484344fcbbf8 (patch) | |
tree | 0d3cabb870beccc54669310e3faa5aa3c3eefbde | |
parent | 3a928301b13fb86ad22a5299a636c9a3cfb2f893 (diff) |
Use libcheck to run the unit tests.
-rw-r--r-- | Makefile.am | 6 | ||||
-rw-r--r-- | bootstrap.conf | 2 | ||||
-rw-r--r-- | include/disfluid.h | 21 | ||||
-rw-r--r-- | po/POTFILES.in | 2 | ||||
-rw-r--r-- | src/disfluid-tests.h | 19 | ||||
-rw-r--r-- | src/libdisfluid.c | 20 | ||||
-rw-r--r-- | src/libdisfluid/disfluid-init.h (renamed from src/disfluid-init.h) | 5 | ||||
-rw-r--r-- | src/libdisfluid/disfluid-tests.h | 76 | ||||
-rw-r--r-- | src/libdisfluid/disfluid-version.h (renamed from src/disfluid-version.h) | 0 | ||||
-rw-r--r-- | src/run-unit-tests.c | 5 | ||||
-rw-r--r-- | src/unit_tests_report.ui | 24 | ||||
-rw-r--r-- | src/vala/about.vala | 2 | ||||
-rw-r--r-- | src/vala/libdisfluid.vala | 2 | ||||
-rw-r--r-- | src/vala/main.vala | 11 | ||||
-rw-r--r-- | src/vala/unit-tests-report.vala | 42 |
15 files changed, 190 insertions, 47 deletions
diff --git a/Makefile.am b/Makefile.am index c432506..3c13f25 100644 --- a/Makefile.am +++ b/Makefile.am @@ -42,9 +42,9 @@ disfluid_LDADD = \ @INTL_MACOSX_LIBS@ libdisfluid_la_SOURCES = \ - src/disfluid-init.h \ - src/disfluid-tests.h \ - src/disfluid-version.h \ + src/libdisfluid/disfluid-init.h \ + src/libdisfluid/disfluid-tests.h \ + src/libdisfluid/disfluid-version.h \ src/libdisfluid.c libdisfluid_la_CPPFLAGS = \ diff --git a/bootstrap.conf b/bootstrap.conf index cce34ab..abac2b8 100644 --- a/bootstrap.conf +++ b/bootstrap.conf @@ -23,7 +23,7 @@ gitlog-to-changelog vc-list-files lib-msvc-compat lib-symbol-visibility relocatable-lib-lgpl locale setlocale localeconv useless-if-before-free do-release-commit-and-tag -announce-gen stdbool +announce-gen stdbool attribute lseek remove stdio " COPYRIGHT_HOLDER='Vivien Kraus' diff --git a/include/disfluid.h b/include/disfluid.h index 4b0f49e..91a284d 100644 --- a/include/disfluid.h +++ b/include/disfluid.h @@ -1,7 +1,7 @@ #ifndef H_DISFLUID_INCLUDED # define H_DISFLUID_INCLUDED -# include <stddef.h> +# include <stdlib.h> # if defined _WIN32 && !defined __CYGWIN__ # define LIBDISFLUID_DLL_MADNESS 1 @@ -22,14 +22,29 @@ # define LIBDISFLUID_API \ LIBDISFLUID_DLL_EXPORTED +# ifdef ATTRIBUTE_DEALLOC +# define LIBDISFLUID_DEALLOC_WITH ATTRIBUTE_DEALLOC +# else +# define LIBDISFLUID_DEALLOC_WITH(f, i) +# endif + +# ifdef ATTRIBUTE_DEALLOC_FREE +# define LIBDISFLUID_DEALLOC_WITH_FREE ATTRIBUTE_DEALLOC_FREE +# else +# define LIBDISFLUID_DEALLOC_WITH_FREE +# endif + # ifdef __cplusplus extern "C" { # endif /* __cplusplus */ - LIBDISFLUID_API const char *disfluid_version (void); + LIBDISFLUID_API extern const char *disfluid_version (void); - LIBDISFLUID_API int disfluid_run_tests (void); + /* Run the disfluid unit tests. The return value must be deallocated + with free. */ + LIBDISFLUID_API LIBDISFLUID_DEALLOC_WITH_FREE char + *disfluid_run_tests (size_t *n_tests, size_t *n_errors); # ifdef __cplusplus } diff --git a/po/POTFILES.in b/po/POTFILES.in index d60f215..f5793f9 100644 --- a/po/POTFILES.in +++ b/po/POTFILES.in @@ -1,6 +1,8 @@ # List of source files which contain translatable strings. src/about.ui src/disfluid.ui +src/libdisfluid/disfluid-tests.h src/unit_tests_report.ui src/vala/about.c src/vala/application-window.c +src/vala/unit-tests-report.c diff --git a/src/disfluid-tests.h b/src/disfluid-tests.h deleted file mode 100644 index 4e8ff5a..0000000 --- a/src/disfluid-tests.h +++ /dev/null @@ -1,19 +0,0 @@ -#ifndef DISFLUID_TESTS_INCLUDED -# define DISFLUID_TESTS_INCLUDED - -static inline int run_tests (void); - -# include "disfluid-init.h" - -static inline int -run_tests (void) -{ - ensure_init (); - if (STRNEQ (version (), VERSION)) - { - return 1; - } - return 0; -} - -#endif /* DISFLUID_TESTS_INCLUDED */ diff --git a/src/libdisfluid.c b/src/libdisfluid.c index 11a7e87..d934694 100644 --- a/src/libdisfluid.c +++ b/src/libdisfluid.c @@ -1,12 +1,22 @@ #include <config.h> +#include "attribute.h" #include <disfluid.h> #define STREQ(a, b) (strcmp ((a), (b)) == 0) #define STRNEQ(a, b) (! (STREQ (a, b))) -#include "disfluid-version.h" -#include "disfluid-tests.h" +#include <locale.h> +#include <stdio.h> +#include <unistd.h> +#include "gettext.h" +#include "relocatable.h" + +#define _(String) dgettext (PACKAGE, (String)) +#define N_(String) (String) + +#include "libdisfluid/disfluid-version.h" +#include "libdisfluid/disfluid-tests.h" const char * disfluid_version (void) @@ -14,8 +24,8 @@ disfluid_version (void) return version (); } -int -disfluid_run_tests (void) +char * +disfluid_run_tests (size_t *n_tests, size_t *n_errors) { - return run_tests (); + return run_tests (n_tests, n_errors); } diff --git a/src/disfluid-init.h b/src/libdisfluid/disfluid-init.h index 48c256c..ae2ebe3 100644 --- a/src/disfluid-init.h +++ b/src/libdisfluid/disfluid-init.h @@ -3,11 +3,6 @@ static inline void ensure_init (void); -# include <locale.h> -# include "gettext.h" -# include "relocatable.h" -# include "disfluid-version.h" - static inline void ensure_init (void) { diff --git a/src/libdisfluid/disfluid-tests.h b/src/libdisfluid/disfluid-tests.h new file mode 100644 index 0000000..ec1774d --- /dev/null +++ b/src/libdisfluid/disfluid-tests.h @@ -0,0 +1,76 @@ +#ifndef DISFLUID_TESTS_INCLUDED +# define DISFLUID_TESTS_INCLUDED + +struct disfluid_tests_report; + +static inline char *run_tests (size_t *n_tests, size_t *n_errors); + +# include <check.h> + +# include "disfluid-init.h" + +/* *INDENT-OFF* */ +START_TEST (test_check_version) +/* *INDENT-ON* */ +{ + const char *lib_version = version (); + ck_assert_str_eq (lib_version, VERSION); +} +/* *INDENT-OFF* */ +END_TEST +/* *INDENT-ON* */ + +static inline char * +tests_read_whole_file (int file) +{ + off_t size = lseek (file, 0, SEEK_END); + lseek (file, 0, SEEK_SET); + char *ret = malloc (size + 1); + if (ret == NULL) + { + return NULL; + } + size_t n_total = 0; + while (n_total < size) + { + ssize_t next = read (file, ret + n_total, size - n_total); + if (next <= 0) + { + free (ret); + return NULL; + } + n_total += next; + } + ret[size] = '\0'; + return ret; +} + +static inline char * +run_tests (size_t *n_tests, size_t *n_errors) +{ + ensure_init (); + Suite *suite = suite_create (_("disfluid unit tests")); + TCase *general = tcase_create (_("disfluid general tests")); + tcase_add_test (general, test_check_version); + suite_add_tcase (suite, general); + SRunner *runner = srunner_create (suite); + char log_file_name[] = "/tmp/disfluid-unit-tests-XXXXXX"; + int log_file = mkstemp (log_file_name); + srunner_set_log (runner, log_file_name); + srunner_run_all (runner, CK_NORMAL); + char *result = tests_read_whole_file (log_file); + if (n_tests) + { + *n_tests = srunner_ntests_run (runner); + } + if (n_errors) + { + *n_errors = srunner_ntests_failed (runner); + } + srunner_free (runner); + close (log_file); + remove (log_file_name); + return result; +} + +#endif /* DISFLUID_TESTS_INCLUDED */ diff --git a/src/disfluid-version.h b/src/libdisfluid/disfluid-version.h index 371f82f..371f82f 100644 --- a/src/disfluid-version.h +++ b/src/libdisfluid/disfluid-version.h diff --git a/src/run-unit-tests.c b/src/run-unit-tests.c index a1fa54d..5361b72 100644 --- a/src/run-unit-tests.c +++ b/src/run-unit-tests.c @@ -13,7 +13,10 @@ main (int argc, char *argv[]) setlocale (LC_ALL, ""); bindtextdomain (PACKAGE, relocate (LOCALEDIR)); textdomain (PACKAGE); - if (disfluid_run_tests () != 0) + size_t n_tests, n_failures; + char *output = disfluid_run_tests (&n_tests, &n_failures); + free (output); + if (n_failures != 0) { return 1; } diff --git a/src/unit_tests_report.ui b/src/unit_tests_report.ui index daedc5c..62c74d7 100644 --- a/src/unit_tests_report.ui +++ b/src/unit_tests_report.ui @@ -41,9 +41,27 @@ <property name="description" translatable="yes">Disfluid unit tests failed. Sorry for the inconvenience.</property> <property name="icon-name">dialog-error</property> <child> - <object class="GtkButton"> - <property name="label" translatable="yes">Retry</property> - <property name="action-name">win.rerun-unit-tests</property> + <object class="GtkBox"> + <property name="orientation">vertical</property> + <child> + <object class="GtkTextView" id="debug_info"> + <property name="editable">false</property> + <property name="cursor-visible">false</property> + <property name="monospace">true</property> + </object> + </child> + <child> + <object class="GtkButton"> + <property name="label" translatable="yes">Retry</property> + <property name="action-name">win.rerun-unit-tests</property> + </object> + </child> + <child> + <object class="GtkLinkButton" id="report_button"> + <property name="label" translatable="yes">Report</property> + <property name="visited">false</property> + </object> + </child> </object> </child> </object> diff --git a/src/vala/about.vala b/src/vala/about.vala index 2943534..2c6e7c8 100644 --- a/src/vala/about.vala +++ b/src/vala/about.vala @@ -10,7 +10,7 @@ namespace Disfluid { window.release_notes = _("<p>Disfluid is very early in its conception.</p>"); window.add_link (_ ("Send feedback and bug reports!"), "mailto:vivien@planete-kraus.eu"); window.support_url = "mailto:vivien@planete-kraus.eu"; - string devs[] = { + const string devs[] = { "Vivien Kraus <vivien@planete-kraus.eu>" }; window.developers = devs; diff --git a/src/vala/libdisfluid.vala b/src/vala/libdisfluid.vala index c419ff1..cd076c3 100644 --- a/src/vala/libdisfluid.vala +++ b/src/vala/libdisfluid.vala @@ -3,5 +3,5 @@ extern const string LIBDISFLUID_VALA; namespace Disfluid { [CCode (cheader_filename = "disfluid.h", cname = "disfluid_run_tests")] - extern int run_tests(); + extern string? run_tests(out size_t n_tests, out size_t n_errors); } diff --git a/src/vala/main.vala b/src/vala/main.vala index f39a94b..8b24cd7 100644 --- a/src/vala/main.vala +++ b/src/vala/main.vala @@ -5,6 +5,10 @@ namespace Disfluid { public class Application: Adw.Application { + private string debug_info = ""; + + public signal void new_debug_info (string debug_info); + public Application () { Object (application_id: "eu.planete_kraus.Disfluid", flags: 0); @@ -14,6 +18,10 @@ namespace Disfluid { this.hold (); var window = Disfluid.About.create (); window.application = this; + window.debug_info = this.debug_info; + this.new_debug_info.connect ((app, di) => { + window.debug_info = di; + }); window.present (); this.release (); }); @@ -23,6 +31,9 @@ namespace Disfluid { run_unit_tests.activate.connect (() => { this.hold (); var dialog = new Disfluid.UnitTestsReportDialog (this); + dialog.new_debug_info.connect ((app, di) => { + this.new_debug_info (di); + }); dialog.present (); this.release (); }); diff --git a/src/vala/unit-tests-report.vala b/src/vala/unit-tests-report.vala index ec0d125..6442e48 100644 --- a/src/vala/unit-tests-report.vala +++ b/src/vala/unit-tests-report.vala @@ -3,11 +3,28 @@ extern const string UNIT_TESTS_REPORT_VALA; namespace Disfluid { - async int do_run_tests_in_bg () throws GLib.ThreadError { + class UnitTestsReport: GLib.Object { + public string output; + public size_t n_tests; + public size_t n_errors; + + public UnitTestsReport (string output, size_t n_tests, size_t n_errors) { + this.output = output; + this.n_tests = n_tests; + this.n_errors = n_errors; + } + } + + async UnitTestsReport do_run_tests_in_bg () throws GLib.ThreadError { GLib.SourceFunc callback = do_run_tests_in_bg.callback; - int[] output = new int[1]; + UnitTestsReport[] output = new UnitTestsReport[1]; ThreadFunc<bool> run = () => { - output[0] = Disfluid.run_tests (); + size_t n_tests, n_errors; + var o = Disfluid.run_tests (out n_tests, out n_errors); + if (o == null) { + o = _ ("The tests did not produce any output."); + } + output[0] = new UnitTestsReport (o, n_tests, n_errors); Idle.add ((owned) callback); return true; }; @@ -31,16 +48,31 @@ namespace Disfluid { [GtkChild] private unowned Adw.StatusPage page_failure; + [GtkChild] + private unowned Gtk.TextView debug_info; + + [GtkChild] + private unowned Gtk.LinkButton report_button; + + public signal void new_debug_info (string debug_info); + public void start () { this.stack.visible_child = this.page_wait; do_run_tests_in_bg.begin ((obj, res) => { try { - int results = do_run_tests_in_bg.end (res); - if (results == 0) { + var results = do_run_tests_in_bg.end (res); + if (results.n_errors == 0) { this.stack.visible_child = this.page_success; } else { this.stack.visible_child = this.page_failure; + this.debug_info.buffer.text = results.output; + // Do not translate the subject + var subject = "[Disfluid unit tests failure] " + _ ("*** Your subject line here, in English ***") ; + var body = _ ("*** Please give as much information as possible, in English, as best as you can. Following your message is the debug information, check if there is no sensitive personal information. ***") + "\n\n" + results.output; + var query = "subject=" + GLib.Uri.escape_string (subject) + "&body=" + GLib.Uri.escape_string (body); + this.report_button.uri = "mailto:vivien@planete-kraus.eu?" + query; + this.new_debug_info (results.output); } } catch (ThreadError e) { |