gkdbus: Fix underflow and unreachable code bug
[platform/upstream/glib.git] / glib / tests / testing.c
1 /* GLib testing framework examples and tests
2  * Copyright (C) 2007 Imendio AB
3  * Authors: Tim Janik
4  *
5  * SPDX-License-Identifier: LicenseRef-old-glib-tests
6  *
7  * This work is provided "as is"; redistribution and modification
8  * in whole or in part, in any medium, physical or electronic is
9  * permitted without restriction.
10  *
11  * This work is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14  *
15  * In no event shall the authors or contributors be liable for any
16  * direct, indirect, incidental, special, exemplary, or consequential
17  * damages (including, but not limited to, procurement of substitute
18  * goods or services; loss of use, data, or profits; or business
19  * interruption) however caused and on any theory of liability, whether
20  * in contract, strict liability, or tort (including negligence or
21  * otherwise) arising in any way out of the use of this software, even
22  * if advised of the possibility of such damage.
23  */
24
25 #include "config.h"
26
27 /* We want to distinguish between messages originating from libglib
28  * and messages originating from this program.
29  */
30 #undef G_LOG_DOMAIN
31 #define G_LOG_DOMAIN "testing"
32
33 #include <glib.h>
34 #include <locale.h>
35 #include <stdlib.h>
36 #include <string.h>
37
38 #define TAP_VERSION G_STRINGIFY (13)
39 #define TAP_SUBTEST_PREFIX "#    "
40
41 /* test assertion variants */
42 static void
43 test_assertions_bad_cmpvariant_types (void)
44 {
45   GVariant *v1, *v2;
46
47   v1 = g_variant_new_boolean (TRUE);
48   v2 = g_variant_new_string ("hello");
49
50   g_assert_cmpvariant (v1, v2);
51
52   g_variant_unref (v2);
53   g_variant_unref (v1);
54
55   exit (0);
56 }
57
58 static void
59 test_assertions_bad_cmpvariant_values (void)
60 {
61   GVariant *v1, *v2;
62
63   v1 = g_variant_new_string ("goodbye");
64   v2 = g_variant_new_string ("hello");
65
66   g_assert_cmpvariant (v1, v2);
67
68   g_variant_unref (v2);
69   g_variant_unref (v1);
70
71   exit (0);
72 }
73
74 static void
75 test_assertions_bad_cmpstrv_null1 (void)
76 {
77   const char *strv[] = { "one", "two", "three", NULL };
78   g_assert_cmpstrv (strv, NULL);
79   exit (0);
80 }
81
82 static void
83 test_assertions_bad_cmpstrv_null2 (void)
84 {
85   const char *strv[] = { "one", "two", "three", NULL };
86   g_assert_cmpstrv (NULL, strv);
87   exit (0);
88 }
89
90 static void
91 test_assertions_bad_cmpstrv_length (void)
92 {
93   const char *strv1[] = { "one", "two", "three", NULL };
94   const char *strv2[] = { "one", "two", NULL };
95   g_assert_cmpstrv (strv1, strv2);
96   exit (0);
97 }
98
99 static void
100 test_assertions_bad_cmpstrv_values (void)
101 {
102   const char *strv1[] = { "one", "two", "three", NULL };
103   const char *strv2[] = { "one", "too", "three", NULL };
104   g_assert_cmpstrv (strv1, strv2);
105   exit (0);
106 }
107
108 static void
109 test_assertions_bad_cmpstr (void)
110 {
111   g_assert_cmpstr ("fzz", !=, "fzz");
112   exit (0);
113 }
114
115 static void
116 test_assertions_bad_cmpint (void)
117 {
118   g_assert_cmpint (4, !=, 4);
119   exit (0);
120 }
121
122 static void
123 test_assertions_bad_cmpmem_len (void)
124 {
125   g_assert_cmpmem ("foo", 3, "foot", 4);
126   exit (0);
127 }
128
129 static void
130 test_assertions_bad_cmpmem_data (void)
131 {
132   g_assert_cmpmem ("foo", 3, "fzz", 3);
133   exit (0);
134 }
135
136 static void
137 test_assertions_bad_cmpmem_null (void)
138 {
139   g_assert_cmpmem (NULL, 3, NULL, 3);
140   exit (0);
141 }
142
143 static void
144 test_assertions_bad_cmpfloat_epsilon (void)
145 {
146   g_assert_cmpfloat_with_epsilon (3.14, 3.15, 0.001);
147   exit (0);
148 }
149
150 /* Emulates something like rmdir() failing. */
151 static int
152 return_errno (void)
153 {
154   errno = ERANGE;  /* arbitrary non-zero value */
155   return -1;
156 }
157
158 /* Emulates something like rmdir() succeeding. */
159 static int
160 return_no_errno (void)
161 {
162   return 0;
163 }
164
165 static void
166 test_assertions_bad_no_errno (void)
167 {
168   g_assert_no_errno (return_errno ());
169 }
170
171 static void
172 test_assertions (void)
173 {
174   const char *strv1[] = { "one", "two", "three", NULL };
175   const char *strv2[] = { "one", "two", "three", NULL };
176   GVariant *v1, *v2;
177   gchar *fuu;
178
179   g_assert_cmpint (1, >, 0);
180   g_assert_cmphex (2, ==, 2);
181   g_assert_cmpfloat (3.3, !=, 7);
182   g_assert_cmpfloat (7, <=, 3 + 4);
183   g_assert_cmpfloat_with_epsilon (3.14, 3.15, 0.01);
184   g_assert_cmpfloat_with_epsilon (3.14159, 3.1416, 0.0001);
185   g_assert (TRUE);
186   g_assert_true (TRUE);
187   g_assert_cmpstr ("foo", !=, "faa");
188   fuu = g_strdup_printf ("f%s", "uu");
189   g_test_queue_free (fuu);
190   g_assert_cmpstr ("foo", !=, fuu);
191   g_assert_cmpstr ("fuu", ==, fuu);
192   g_assert_cmpstr (NULL, <, "");
193   g_assert_cmpstr (NULL, ==, NULL);
194   g_assert_cmpstr ("", >, NULL);
195   g_assert_cmpstr ("foo", <, "fzz");
196   g_assert_cmpstr ("fzz", >, "faa");
197   g_assert_cmpstr ("fzz", ==, "fzz");
198   g_assert_cmpmem ("foo", 3, "foot", 3);
199   g_assert_cmpmem (NULL, 0, NULL, 0);
200   g_assert_cmpmem (NULL, 0, "foot", 0);
201   g_assert_cmpmem ("foo", 0, NULL, 0);
202   g_assert_no_errno (return_no_errno ());
203
204   g_assert_cmpstrv (NULL, NULL);
205   g_assert_cmpstrv (strv1, strv2);
206
207   v1 = g_variant_new_parsed ("['hello', 'there']");
208   v2 = g_variant_new_parsed ("['hello', 'there']");
209
210   g_assert_cmpvariant (v1, v1);
211   g_assert_cmpvariant (v1, v2);
212
213   g_variant_unref (v2);
214   g_variant_unref (v1);
215
216   g_test_trap_subprocess ("/misc/assertions/subprocess/bad_cmpvariant_types", 0,
217                           G_TEST_SUBPROCESS_DEFAULT);
218   g_test_trap_assert_failed ();
219   g_test_trap_assert_stderr ("*assertion failed*");
220
221   g_test_trap_subprocess ("/misc/assertions/subprocess/bad_cmpvariant_values", 0,
222                           G_TEST_SUBPROCESS_DEFAULT);
223   g_test_trap_assert_failed ();
224   g_test_trap_assert_stderr ("*assertion failed*");
225
226   g_test_trap_subprocess ("/misc/assertions/subprocess/bad_cmpstr", 0,
227                           G_TEST_SUBPROCESS_DEFAULT);
228   g_test_trap_assert_failed ();
229   g_test_trap_assert_stderr ("*assertion failed*");
230
231   g_test_trap_subprocess ("/misc/assertions/subprocess/bad_cmpstrv_null1", 0,
232                           G_TEST_SUBPROCESS_DEFAULT);
233   g_test_trap_assert_failed ();
234   g_test_trap_assert_stderr ("*assertion failed*");
235
236   g_test_trap_subprocess ("/misc/assertions/subprocess/bad_cmpstrv_null2", 0,
237                           G_TEST_SUBPROCESS_DEFAULT);
238   g_test_trap_assert_failed ();
239   g_test_trap_assert_stderr ("*assertion failed*");
240
241   g_test_trap_subprocess ("/misc/assertions/subprocess/bad_cmpstrv_length", 0,
242                           G_TEST_SUBPROCESS_DEFAULT);
243   g_test_trap_assert_failed ();
244   g_test_trap_assert_stderr ("*assertion failed*");
245
246   g_test_trap_subprocess ("/misc/assertions/subprocess/bad_cmpstrv_values", 0,
247                           G_TEST_SUBPROCESS_DEFAULT);
248   g_test_trap_assert_failed ();
249   g_test_trap_assert_stderr ("*assertion failed*");
250
251   g_test_trap_subprocess ("/misc/assertions/subprocess/bad_cmpint", 0,
252                           G_TEST_SUBPROCESS_DEFAULT);
253   g_test_trap_assert_failed ();
254   g_test_trap_assert_stderr ("*assertion failed*");
255
256   g_test_trap_subprocess ("/misc/assertions/subprocess/bad_cmpmem_len", 0,
257                           G_TEST_SUBPROCESS_DEFAULT);
258   g_test_trap_assert_failed ();
259   g_test_trap_assert_stderr ("*assertion failed*len*");
260
261   g_test_trap_subprocess ("/misc/assertions/subprocess/bad_cmpmem_data", 0,
262                           G_TEST_SUBPROCESS_DEFAULT);
263   g_test_trap_assert_failed ();
264   g_test_trap_assert_stderr ("*assertion failed*");
265   g_test_trap_assert_stderr_unmatched ("*assertion failed*len*");
266
267   g_test_trap_subprocess ("/misc/assertions/subprocess/bad_cmpmem_null", 0,
268                           G_TEST_SUBPROCESS_DEFAULT);
269   g_test_trap_assert_failed ();
270   g_test_trap_assert_stderr ("*assertion failed*NULL*");
271
272   g_test_trap_subprocess ("/misc/assertions/subprocess/bad_cmpfloat_epsilon", 0,
273                           G_TEST_SUBPROCESS_DEFAULT);
274   g_test_trap_assert_failed ();
275   g_test_trap_assert_stderr ("*assertion failed*");
276
277   g_test_trap_subprocess ("/misc/assertions/subprocess/bad_no_errno", 0,
278                           G_TEST_SUBPROCESS_DEFAULT);
279   g_test_trap_assert_failed ();
280   g_test_trap_assert_stderr ("*assertion failed*");
281 }
282
283 /* test g_test_timer* API */
284 static void
285 test_timer (void)
286 {
287   double ttime;
288   g_test_timer_start();
289   g_assert_cmpfloat (g_test_timer_last(), ==, 0);
290   g_usleep (25 * 1000);
291   ttime = g_test_timer_elapsed();
292   g_assert_cmpfloat (ttime, >, 0);
293   g_assert_cmpfloat (g_test_timer_last(), ==, ttime);
294   g_test_minimized_result (ttime, "timer-test-time: %fsec", ttime);
295   g_test_maximized_result (5, "bogus-quantity: %ddummies", 5); /* simple API test */
296 }
297
298 #ifdef G_OS_UNIX
299 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
300
301 /* fork out for a failing test */
302 static void
303 test_fork_fail (void)
304 {
305   if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
306     {
307       g_assert_not_reached();
308     }
309   g_test_trap_assert_failed();
310   g_test_trap_assert_stderr ("*ERROR*test_fork_fail*should not be reached*");
311 }
312
313 /* fork out to assert stdout and stderr patterns */
314 static void
315 test_fork_patterns (void)
316 {
317   if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDOUT | G_TEST_TRAP_SILENCE_STDERR))
318     {
319       g_print ("some stdout text: somagic17\n");
320       g_printerr ("some stderr text: semagic43\n");
321       exit (0);
322     }
323   g_test_trap_assert_passed();
324   g_test_trap_assert_stdout ("*somagic17*");
325   g_test_trap_assert_stderr ("*semagic43*");
326 }
327
328 /* fork out for a timeout test */
329 static void
330 test_fork_timeout (void)
331 {
332   /* allow child to run for only a fraction of a second */
333   if (g_test_trap_fork (0.11 * 1000000, G_TEST_TRAP_DEFAULT))
334     {
335       /* loop and sleep forever */
336       while (TRUE)
337         g_usleep (1000 * 1000);
338     }
339   g_test_trap_assert_failed();
340   g_assert_true (g_test_trap_reached_timeout());
341 }
342
343 G_GNUC_END_IGNORE_DEPRECATIONS
344 #endif /* G_OS_UNIX */
345
346 static void
347 test_subprocess_fail (void)
348 {
349   if (g_test_subprocess ())
350     {
351       g_assert_not_reached ();
352       return;
353     }
354
355   g_test_trap_subprocess (NULL, 0, G_TEST_SUBPROCESS_DEFAULT);
356   g_test_trap_assert_failed ();
357   g_test_trap_assert_stderr ("*ERROR*test_subprocess_fail*should not be reached*");
358 }
359
360 static void
361 test_subprocess_no_such_test (void)
362 {
363   if (g_test_subprocess ())
364     {
365       g_test_trap_subprocess ("/trap_subprocess/this-test-does-not-exist", 0,
366                               G_TEST_SUBPROCESS_DEFAULT);
367       g_assert_not_reached ();
368       return;
369     }
370   g_test_trap_subprocess (NULL, 0, G_TEST_SUBPROCESS_DEFAULT);
371   g_test_trap_assert_failed ();
372   g_test_trap_assert_stderr ("*test does not exist*");
373   g_test_trap_assert_stderr_unmatched ("*should not be reached*");
374 }
375
376 static void
377 test_subprocess_patterns (void)
378 {
379   if (g_test_subprocess ())
380     {
381       g_print ("some stdout text: somagic17\n");
382       g_printerr ("some stderr text: semagic43\n");
383       exit (0);
384     }
385   g_test_trap_subprocess (NULL, 0, G_TEST_SUBPROCESS_DEFAULT);
386   g_test_trap_assert_passed ();
387   g_test_trap_assert_stdout ("*somagic17*");
388   g_test_trap_assert_stderr ("*semagic43*");
389 }
390
391 static void
392 test_subprocess_timeout (void)
393 {
394   if (g_test_subprocess ())
395     {
396       /* loop and sleep forever */
397       while (TRUE)
398         g_usleep (1000 * 1000);
399       return;
400     }
401   /* allow child to run for only a fraction of a second */
402   g_test_trap_subprocess (NULL, 0.11 * 1000000, G_TEST_SUBPROCESS_DEFAULT);
403   g_test_trap_assert_failed ();
404   g_assert_true (g_test_trap_reached_timeout ());
405 }
406
407 /* run a test with fixture setup and teardown */
408 typedef struct {
409   guint  seed;
410   guint  prime;
411   gchar *msg;
412 } Fixturetest;
413 static void
414 fixturetest_setup (Fixturetest  *fix,
415                    gconstpointer test_data)
416 {
417   g_assert_true (test_data == (void*) 0xc0cac01a);
418   fix->seed = 18;
419   fix->prime = 19;
420   fix->msg = g_strdup_printf ("%d", fix->prime);
421 }
422 static void
423 fixturetest_test (Fixturetest  *fix,
424                   gconstpointer test_data)
425 {
426   guint prime = g_spaced_primes_closest (fix->seed);
427   g_assert_cmpint (prime, ==, fix->prime);
428   prime = g_ascii_strtoull (fix->msg, NULL, 0);
429   g_assert_cmpint (prime, ==, fix->prime);
430   g_assert_true (test_data == (void*) 0xc0cac01a);
431 }
432 static void
433 fixturetest_teardown (Fixturetest  *fix,
434                       gconstpointer test_data)
435 {
436   g_assert_true (test_data == (void*) 0xc0cac01a);
437   g_free (fix->msg);
438 }
439
440 static struct {
441   int bit, vint1, vint2, irange;
442   long double vdouble, drange;
443 } shared_rand_state;
444
445 static void
446 test_rand1 (void)
447 {
448   shared_rand_state.bit = g_test_rand_bit();
449   shared_rand_state.vint1 = g_test_rand_int();
450   shared_rand_state.vint2 = g_test_rand_int();
451   g_assert_cmpint (shared_rand_state.vint1, !=, shared_rand_state.vint2);
452   shared_rand_state.irange = g_test_rand_int_range (17, 35);
453   g_assert_cmpint (shared_rand_state.irange, >=, 17);
454   g_assert_cmpint (shared_rand_state.irange, <=, 35);
455   shared_rand_state.vdouble = g_test_rand_double();
456   shared_rand_state.drange = g_test_rand_double_range (-999, +17);
457   g_assert_cmpfloat (shared_rand_state.drange, >=, -999);
458   g_assert_cmpfloat (shared_rand_state.drange, <=, +17);
459 }
460
461 static void
462 test_rand2 (void)
463 {
464   /* this test only works if run after test1.
465    * we do this to check that random number generators
466    * are reseeded upon fixture setup.
467    */
468   g_assert_cmpint (shared_rand_state.bit, ==, g_test_rand_bit());
469   g_assert_cmpint (shared_rand_state.vint1, ==, g_test_rand_int());
470   g_assert_cmpint (shared_rand_state.vint2, ==, g_test_rand_int());
471   g_assert_cmpint (shared_rand_state.irange, ==, g_test_rand_int_range (17, 35));
472   g_assert_cmpfloat (shared_rand_state.vdouble, ==, g_test_rand_double());
473   g_assert_cmpfloat (shared_rand_state.drange, ==, g_test_rand_double_range (-999, +17));
474 }
475
476 static void
477 test_data_test (gconstpointer test_data)
478 {
479   g_assert_true (test_data == (void*) 0xc0c0baba);
480 }
481
482 static void
483 test_random_conversions (void)
484 {
485   /* very simple conversion test using random numbers */
486   int vint = g_test_rand_int();
487   char *err, *str = g_strdup_printf ("%d", vint);
488   gint64 vint64 = g_ascii_strtoll (str, &err, 10);
489   g_assert_cmpint (vint, ==, vint64);
490   g_assert_true (!err || *err == 0);
491   g_free (str);
492 }
493
494 static gboolean
495 fatal_handler (const gchar    *log_domain,
496                GLogLevelFlags  log_level,
497                const gchar    *message,
498                gpointer        user_data)
499 {
500   return FALSE;
501 }
502
503 static void
504 test_fatal_log_handler_critical_pass (void)
505 {
506   g_test_log_set_fatal_handler (fatal_handler, NULL);
507   g_str_has_prefix (NULL, "file://");
508   g_critical ("Test passing");
509   exit (0);
510 }
511
512 static void
513 test_fatal_log_handler_error_fail (void)
514 {
515   g_error ("Test failing");
516   exit (0);
517 }
518
519 static void
520 test_fatal_log_handler_critical_fail (void)
521 {
522   g_str_has_prefix (NULL, "file://");
523   g_critical ("Test passing");
524   exit (0);
525 }
526
527 static void
528 test_fatal_log_handler (void)
529 {
530   g_test_trap_subprocess ("/misc/fatal-log-handler/subprocess/critical-pass", 0,
531                           G_TEST_SUBPROCESS_DEFAULT);
532   g_test_trap_assert_passed ();
533   g_test_trap_assert_stderr ("*CRITICAL*g_str_has_prefix*");
534   g_test_trap_assert_stderr ("*CRITICAL*Test passing*");
535
536   g_test_trap_subprocess ("/misc/fatal-log-handler/subprocess/error-fail", 0,
537                           G_TEST_SUBPROCESS_DEFAULT);
538   g_test_trap_assert_failed ();
539   g_test_trap_assert_stderr ("*ERROR*Test failing*");
540
541   g_test_trap_subprocess ("/misc/fatal-log-handler/subprocess/critical-fail", 0,
542                           G_TEST_SUBPROCESS_DEFAULT);
543   g_test_trap_assert_failed ();
544   g_test_trap_assert_stderr ("*CRITICAL*g_str_has_prefix*");
545   g_test_trap_assert_stderr_unmatched ("*CRITICAL*Test passing*");
546 }
547
548 static void
549 test_expected_messages_warning (void)
550 {
551   g_warning ("This is a %d warning", g_random_int ());
552   g_return_if_reached ();
553 }
554
555 static void
556 test_expected_messages_expect_warning (void)
557 {
558   g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING,
559                          "This is a * warning");
560   test_expected_messages_warning ();
561 }
562
563 static void
564 test_expected_messages_wrong_warning (void)
565 {
566   g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
567                          "*should not be *");
568   test_expected_messages_warning ();
569 }
570
571 static void
572 test_expected_messages_expected (void)
573 {
574   g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING,
575                          "This is a * warning");
576   g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
577                          "*should not be reached");
578
579   test_expected_messages_warning ();
580
581   g_test_assert_expected_messages ();
582   exit (0);
583 }
584
585 static void
586 test_expected_messages_null_domain (void)
587 {
588   g_test_expect_message (NULL, G_LOG_LEVEL_WARNING, "no domain");
589   g_log (NULL, G_LOG_LEVEL_WARNING, "no domain");
590   g_test_assert_expected_messages ();
591 }
592
593 static void
594 test_expected_messages_expect_error (void)
595 {
596   /* make sure we can't try to expect a g_error() */
597   g_test_expect_message ("GLib", G_LOG_LEVEL_CRITICAL, "*G_LOG_LEVEL_ERROR*");
598   g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_ERROR, "this won't work");
599   g_test_assert_expected_messages ();
600 }
601
602 static void
603 test_expected_messages_extra_warning (void)
604 {
605   g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING,
606                          "This is a * warning");
607   g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
608                          "*should not be reached");
609   g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
610                          "nope");
611
612   test_expected_messages_warning ();
613
614   /* If we don't assert, it won't notice the missing message */
615   exit (0);
616 }
617
618 static void
619 test_expected_messages_unexpected_extra_warning (void)
620 {
621   g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING,
622                          "This is a * warning");
623   g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
624                          "*should not be reached");
625   g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
626                          "nope");
627
628   test_expected_messages_warning ();
629
630   g_test_assert_expected_messages ();
631   exit (0);
632 }
633
634 static void
635 test_expected_messages (void)
636 {
637   g_test_trap_subprocess ("/misc/expected-messages/subprocess/warning", 0,
638                           G_TEST_SUBPROCESS_DEFAULT);
639   g_test_trap_assert_failed ();
640   g_test_trap_assert_stderr ("*This is a * warning*");
641   g_test_trap_assert_stderr_unmatched ("*should not be reached*");
642
643   g_test_trap_subprocess ("/misc/expected-messages/subprocess/expect-warning", 0,
644                           G_TEST_SUBPROCESS_DEFAULT);
645   g_test_trap_assert_failed ();
646   g_test_trap_assert_stderr_unmatched ("*This is a * warning*");
647   g_test_trap_assert_stderr ("*should not be reached*");
648
649   g_test_trap_subprocess ("/misc/expected-messages/subprocess/wrong-warning", 0,
650                           G_TEST_SUBPROCESS_DEFAULT);
651   g_test_trap_assert_failed ();
652   g_test_trap_assert_stderr_unmatched ("*should not be reached*");
653   g_test_trap_assert_stderr ("*GLib-CRITICAL*Did not see expected message testing-CRITICAL*should not be *WARNING*This is a * warning*");
654
655   g_test_trap_subprocess ("/misc/expected-messages/subprocess/expected", 0,
656                           G_TEST_SUBPROCESS_DEFAULT);
657   g_test_trap_assert_passed ();
658   g_test_trap_assert_stderr ("");
659
660   g_test_trap_subprocess ("/misc/expected-messages/subprocess/null-domain", 0,
661                           G_TEST_SUBPROCESS_DEFAULT);
662   g_test_trap_assert_passed ();
663   g_test_trap_assert_stderr ("");
664
665   g_test_trap_subprocess ("/misc/expected-messages/subprocess/extra-warning", 0,
666                           G_TEST_SUBPROCESS_DEFAULT);
667   g_test_trap_assert_passed ();
668   g_test_trap_assert_stderr ("");
669
670   g_test_trap_subprocess ("/misc/expected-messages/subprocess/unexpected-extra-warning", 0,
671                           G_TEST_SUBPROCESS_DEFAULT);
672   g_test_trap_assert_failed ();
673   g_test_trap_assert_stderr ("*GLib:ERROR*Did not see expected message testing-CRITICAL*nope*");
674 }
675
676 static void
677 test_messages (void)
678 {
679   g_test_trap_subprocess ("/misc/messages/subprocess/use-stderr", 0,
680                           G_TEST_SUBPROCESS_DEFAULT);
681   g_test_trap_assert_stderr ("*message is in stderr*");
682   g_test_trap_assert_stderr ("*warning is in stderr*");
683   g_test_trap_has_passed ();
684 }
685
686 static void
687 test_messages_use_stderr (void)
688 {
689   g_message ("message is in stderr");
690   g_warning ("warning is in stderr");
691 }
692
693 static void
694 test_expected_messages_debug (void)
695 {
696   g_test_expect_message ("Test", G_LOG_LEVEL_WARNING, "warning message");
697   g_log ("Test", G_LOG_LEVEL_DEBUG, "should be ignored");
698   g_log ("Test", G_LOG_LEVEL_WARNING, "warning message");
699   g_test_assert_expected_messages ();
700
701   g_test_expect_message ("Test", G_LOG_LEVEL_DEBUG, "debug message");
702   g_log ("Test", G_LOG_LEVEL_DEBUG, "debug message");
703   g_test_assert_expected_messages ();
704 }
705
706 static void
707 test_dash_p_hidden (void)
708 {
709   if (!g_test_subprocess ())
710     g_assert_not_reached ();
711
712   g_print ("Test /misc/dash-p/subprocess/hidden ran\n");
713 }
714
715 static void
716 test_dash_p_hidden_sub (void)
717 {
718   if (!g_test_subprocess ())
719     g_assert_not_reached ();
720
721   g_print ("Test /misc/dash-p/subprocess/hidden/sub ran\n");
722 }
723
724 /* The rest of the dash_p tests will get run by the toplevel test
725  * process, but they shouldn't do anything there.
726  */
727
728 static void
729 test_dash_p_child (void)
730 {
731   if (!g_test_subprocess ())
732     return;
733
734   g_print ("Test /misc/dash-p/child ran\n");
735 }
736
737 static void
738 test_dash_p_child_sub (void)
739 {
740   if (!g_test_subprocess ())
741     return;
742
743   g_print ("Test /misc/dash-p/child/sub ran\n");
744 }
745
746 static void
747 test_dash_p_child_sub2 (void)
748 {
749   if (!g_test_subprocess ())
750     return;
751
752   g_print ("Test /misc/dash-p/child/sub2 ran\n");
753 }
754
755 static void
756 test_dash_p_child_sub_child (void)
757 {
758   if (!g_test_subprocess ())
759     return;
760
761   g_print ("Test /misc/dash-p/child/subprocess ran\n");
762 }
763
764 static void
765 test_dash_p (void)
766 {
767   g_test_trap_subprocess ("/misc/dash-p/subprocess/hidden", 0,
768                           G_TEST_SUBPROCESS_DEFAULT);
769   g_test_trap_assert_passed ();
770   g_test_trap_assert_stdout ("*Test /misc/dash-p/subprocess/hidden ran*");
771   g_test_trap_assert_stdout_unmatched ("*Test /misc/dash-p/subprocess/hidden/sub ran*");
772   g_test_trap_assert_stdout_unmatched ("*Test /misc/dash-p/subprocess/hidden/sub2 ran*");
773   g_test_trap_assert_stdout_unmatched ("*Test /misc/dash-p/subprocess/hidden/sub/subprocess ran*");
774   g_test_trap_assert_stdout_unmatched ("*Test /misc/dash-p/child*");
775
776   g_test_trap_subprocess ("/misc/dash-p/subprocess/hidden/sub", 0,
777                           G_TEST_SUBPROCESS_DEFAULT);
778   g_test_trap_assert_passed ();
779   g_test_trap_assert_stdout ("*Test /misc/dash-p/subprocess/hidden/sub ran*");
780   g_test_trap_assert_stdout_unmatched ("*Test /misc/dash-p/subprocess/hidden ran*");
781   g_test_trap_assert_stdout_unmatched ("*Test /misc/dash-p/subprocess/hidden/sub2 ran*");
782   g_test_trap_assert_stdout_unmatched ("*Test /misc/dash-p/subprocess/hidden/subprocess ran*");
783   g_test_trap_assert_stdout_unmatched ("*Test /misc/dash-p/child*");
784
785   g_test_trap_subprocess ("/misc/dash-p/child", 0,
786                           G_TEST_SUBPROCESS_DEFAULT);
787   g_test_trap_assert_passed ();
788   g_test_trap_assert_stdout ("*Test /misc/dash-p/child ran*");
789   g_test_trap_assert_stdout ("*Test /misc/dash-p/child/sub ran*");
790   g_test_trap_assert_stdout ("*Test /misc/dash-p/child/sub2 ran*");
791   g_test_trap_assert_stdout_unmatched ("*Test /misc/dash-p/child/subprocess ran*");
792   g_test_trap_assert_stdout_unmatched ("*Test /misc/dash-p/subprocess/hidden*");
793
794   g_test_trap_subprocess ("/misc/dash-p/child/sub", 0,
795                           G_TEST_SUBPROCESS_DEFAULT);
796   g_test_trap_assert_passed ();
797   g_test_trap_assert_stdout ("*Test /misc/dash-p/child/sub ran*");
798   g_test_trap_assert_stdout_unmatched ("*Test /misc/dash-p/child ran*");
799   g_test_trap_assert_stdout_unmatched ("*Test /misc/dash-p/child/sub2 ran*");
800   g_test_trap_assert_stdout_unmatched ("*Test /misc/dash-p/child/subprocess ran*");
801   g_test_trap_assert_stdout_unmatched ("*Test /misc/dash-p/subprocess/hidden*");
802 }
803
804 static void
805 test_nonfatal (void)
806 {
807   if (g_test_subprocess ())
808     {
809       g_test_set_nonfatal_assertions ();
810       g_assert_cmpint (4, ==, 5);
811       g_print ("The End\n");
812       return;
813     }
814   g_test_trap_subprocess (NULL, 0, G_TEST_SUBPROCESS_DEFAULT);
815   g_test_trap_assert_failed ();
816   g_test_trap_assert_stderr ("*assertion failed*4 == 5*");
817   g_test_trap_assert_stdout ("*The End*");
818 }
819
820 static void
821 test_skip (void)
822 {
823   g_test_skip ("Skipped should count as passed, not failed");
824   /* This function really means "the test concluded with a non-successful
825    * status" rather than "the test failed": it is documented to return
826    * true for skipped and incomplete tests, not just for failures. */
827   g_assert_true (g_test_failed ());
828 }
829
830 static void
831 test_pass (void)
832 {
833 }
834
835 static void
836 subprocess_fail (void)
837 {
838   /* Exit 1 instead of raising SIGABRT so that we can make assertions about
839    * how this combines with skipped/incomplete tests */
840   g_test_set_nonfatal_assertions ();
841   g_test_fail ();
842   g_assert_true (g_test_failed ());
843 }
844
845 static void
846 test_fail (void)
847 {
848   if (g_test_subprocess ())
849     {
850       subprocess_fail ();
851       return;
852     }
853   g_test_trap_subprocess (NULL, 0, G_TEST_SUBPROCESS_DEFAULT);
854   g_test_trap_assert_failed ();
855 }
856
857 static void
858 subprocess_incomplete (void)
859 {
860   g_test_incomplete ("not done");
861   /* This function really means "the test concluded with a non-successful
862    * status" rather than "the test failed": it is documented to return
863    * true for skipped and incomplete tests, not just for failures. */
864   g_assert_true (g_test_failed ());
865 }
866
867 static void
868 test_incomplete (void)
869 {
870   if (g_test_subprocess ())
871     {
872       subprocess_incomplete ();
873       return;
874     }
875   g_test_trap_subprocess (NULL, 0, G_TEST_SUBPROCESS_DEFAULT);
876   /* An incomplete test represents functionality that is known not to be
877    * implemented yet (an expected failure), so it does not cause test
878    * failure; but it does count as the test having been skipped, which
879    * causes nonzero exit status 77, which is treated as failure by
880    * g_test_trap_subprocess(). */
881   g_test_trap_assert_failed ();
882 }
883
884 static void
885 test_subprocess_timed_out (void)
886 {
887   if (g_test_subprocess ())
888     {
889       g_usleep (1000000);
890       return;
891     }
892   g_test_trap_subprocess (NULL, 50000, G_TEST_SUBPROCESS_DEFAULT);
893   g_assert_true (g_test_trap_reached_timeout ());
894 }
895
896 static void
897 test_path_first (void)
898 {
899   g_assert_cmpstr (g_test_get_path (), ==, "/misc/path/first");
900 }
901
902 static void
903 test_path_second (void)
904 {
905   g_assert_cmpstr (g_test_get_path (), ==, "/misc/path/second");
906 }
907
908 static const char *argv0;
909
910 static void
911 test_combining (void)
912 {
913   GPtrArray *argv;
914   GError *error = NULL;
915   int status;
916
917   g_test_message ("single test case skipped -> overall status 77");
918   argv = g_ptr_array_new ();
919   g_ptr_array_add (argv, (char *) argv0);
920   g_ptr_array_add (argv, "--GTestSubprocess");
921   g_ptr_array_add (argv, "-p");
922   g_ptr_array_add (argv, "/misc/skip");
923   g_ptr_array_add (argv, NULL);
924
925   g_spawn_sync (NULL, (char **) argv->pdata, NULL,
926                 G_SPAWN_STDOUT_TO_DEV_NULL | G_SPAWN_STDERR_TO_DEV_NULL,
927                 NULL, NULL, NULL, NULL, &status,
928                 &error);
929   g_assert_no_error (error);
930
931   g_spawn_check_wait_status (status, &error);
932   g_assert_error (error, G_SPAWN_EXIT_ERROR, 77);
933   g_clear_error (&error);
934
935   g_test_message ("each test case skipped -> overall status 77");
936   g_ptr_array_set_size (argv, 0);
937   g_ptr_array_add (argv, (char *) argv0);
938   g_ptr_array_add (argv, "--GTestSubprocess");
939   g_ptr_array_add (argv, "-p");
940   g_ptr_array_add (argv, "/misc/skip");
941   g_ptr_array_add (argv, "-p");
942   g_ptr_array_add (argv, "/misc/combining/subprocess/skip1");
943   g_ptr_array_add (argv, "-p");
944   g_ptr_array_add (argv, "/misc/combining/subprocess/skip2");
945   g_ptr_array_add (argv, NULL);
946
947   g_spawn_sync (NULL, (char **) argv->pdata, NULL,
948                 G_SPAWN_STDOUT_TO_DEV_NULL | G_SPAWN_STDERR_TO_DEV_NULL,
949                 NULL, NULL, NULL, NULL, &status,
950                 &error);
951   g_assert_no_error (error);
952
953   g_spawn_check_wait_status (status, &error);
954   g_assert_error (error, G_SPAWN_EXIT_ERROR, 77);
955   g_clear_error (&error);
956
957   g_test_message ("single test case incomplete -> overall status 77");
958   g_ptr_array_set_size (argv, 0);
959   g_ptr_array_add (argv, (char *) argv0);
960   g_ptr_array_add (argv, "--GTestSubprocess");
961   g_ptr_array_add (argv, "-p");
962   g_ptr_array_add (argv, "/misc/combining/subprocess/incomplete");
963   g_ptr_array_add (argv, NULL);
964
965   g_spawn_sync (NULL, (char **) argv->pdata, NULL,
966                 G_SPAWN_STDOUT_TO_DEV_NULL | G_SPAWN_STDERR_TO_DEV_NULL,
967                 NULL, NULL, NULL, NULL, &status,
968                 &error);
969   g_assert_no_error (error);
970
971   g_spawn_check_wait_status (status, &error);
972   g_assert_error (error, G_SPAWN_EXIT_ERROR, 77);
973   g_clear_error (&error);
974
975   g_test_message ("one pass and some skipped -> overall status 0");
976   g_ptr_array_set_size (argv, 0);
977   g_ptr_array_add (argv, (char *) argv0);
978   g_ptr_array_add (argv, "--GTestSubprocess");
979   g_ptr_array_add (argv, "-p");
980   g_ptr_array_add (argv, "/misc/skip");
981   g_ptr_array_add (argv, "-p");
982   g_ptr_array_add (argv, "/misc/combining/subprocess/pass");
983   g_ptr_array_add (argv, "-p");
984   g_ptr_array_add (argv, "/misc/combining/subprocess/skip1");
985   g_ptr_array_add (argv, NULL);
986
987   g_spawn_sync (NULL, (char **) argv->pdata, NULL,
988                 G_SPAWN_STDOUT_TO_DEV_NULL | G_SPAWN_STDERR_TO_DEV_NULL,
989                 NULL, NULL, NULL, NULL, &status,
990                 &error);
991   g_assert_no_error (error);
992
993   g_spawn_check_wait_status (status, &error);
994   g_assert_no_error (error);
995
996   g_test_message ("one pass and some incomplete -> overall status 0");
997   g_ptr_array_set_size (argv, 0);
998   g_ptr_array_add (argv, (char *) argv0);
999   g_ptr_array_add (argv, "--GTestSubprocess");
1000   g_ptr_array_add (argv, "-p");
1001   g_ptr_array_add (argv, "/misc/combining/subprocess/pass");
1002   g_ptr_array_add (argv, "-p");
1003   g_ptr_array_add (argv, "/misc/combining/subprocess/incomplete");
1004   g_ptr_array_add (argv, NULL);
1005
1006   g_spawn_sync (NULL, (char **) argv->pdata, NULL,
1007                 G_SPAWN_STDOUT_TO_DEV_NULL | G_SPAWN_STDERR_TO_DEV_NULL,
1008                 NULL, NULL, NULL, NULL, &status,
1009                 &error);
1010   g_assert_no_error (error);
1011
1012   g_spawn_check_wait_status (status, &error);
1013   g_assert_no_error (error);
1014
1015   g_test_message ("one pass and mix of skipped and incomplete -> overall status 0");
1016   g_ptr_array_set_size (argv, 0);
1017   g_ptr_array_add (argv, (char *) argv0);
1018   g_ptr_array_add (argv, "--GTestSubprocess");
1019   g_ptr_array_add (argv, "-p");
1020   g_ptr_array_add (argv, "/misc/combining/subprocess/pass");
1021   g_ptr_array_add (argv, "-p");
1022   g_ptr_array_add (argv, "/misc/combining/subprocess/skip1");
1023   g_ptr_array_add (argv, "-p");
1024   g_ptr_array_add (argv, "/misc/combining/subprocess/incomplete");
1025   g_ptr_array_add (argv, NULL);
1026
1027   g_spawn_sync (NULL, (char **) argv->pdata, NULL,
1028                 G_SPAWN_STDOUT_TO_DEV_NULL | G_SPAWN_STDERR_TO_DEV_NULL,
1029                 NULL, NULL, NULL, NULL, &status,
1030                 &error);
1031   g_assert_no_error (error);
1032
1033   g_spawn_check_wait_status (status, &error);
1034   g_assert_no_error (error);
1035
1036   g_test_message ("one fail and some skipped -> overall status fail");
1037   g_ptr_array_set_size (argv, 0);
1038   g_ptr_array_add (argv, (char *) argv0);
1039   g_ptr_array_add (argv, "--GTestSubprocess");
1040   g_ptr_array_add (argv, "-p");
1041   g_ptr_array_add (argv, "/misc/skip");
1042   g_ptr_array_add (argv, "-p");
1043   g_ptr_array_add (argv, "/misc/combining/subprocess/fail");
1044   g_ptr_array_add (argv, "-p");
1045   g_ptr_array_add (argv, "/misc/combining/subprocess/skip1");
1046   g_ptr_array_add (argv, NULL);
1047
1048   g_spawn_sync (NULL, (char **) argv->pdata, NULL,
1049                 G_SPAWN_STDOUT_TO_DEV_NULL | G_SPAWN_STDERR_TO_DEV_NULL,
1050                 NULL, NULL, NULL, NULL, &status,
1051                 &error);
1052   g_assert_no_error (error);
1053
1054   g_spawn_check_wait_status (status, &error);
1055   g_assert_error (error, G_SPAWN_EXIT_ERROR, 1);
1056   g_clear_error (&error);
1057
1058   g_test_message ("one fail and some incomplete -> overall status fail");
1059   g_ptr_array_set_size (argv, 0);
1060   g_ptr_array_add (argv, (char *) argv0);
1061   g_ptr_array_add (argv, "--GTestSubprocess");
1062   g_ptr_array_add (argv, "-p");
1063   g_ptr_array_add (argv, "/misc/combining/subprocess/fail");
1064   g_ptr_array_add (argv, "-p");
1065   g_ptr_array_add (argv, "/misc/combining/subprocess/incomplete");
1066   g_ptr_array_add (argv, NULL);
1067
1068   g_spawn_sync (NULL, (char **) argv->pdata, NULL,
1069                 G_SPAWN_STDOUT_TO_DEV_NULL | G_SPAWN_STDERR_TO_DEV_NULL,
1070                 NULL, NULL, NULL, NULL, &status,
1071                 &error);
1072   g_assert_no_error (error);
1073
1074   g_spawn_check_wait_status (status, &error);
1075   g_assert_error (error, G_SPAWN_EXIT_ERROR, 1);
1076   g_clear_error (&error);
1077
1078   g_test_message ("one fail and mix of skipped and incomplete -> overall status fail");
1079   g_ptr_array_set_size (argv, 0);
1080   g_ptr_array_add (argv, (char *) argv0);
1081   g_ptr_array_add (argv, "--GTestSubprocess");
1082   g_ptr_array_add (argv, "-p");
1083   g_ptr_array_add (argv, "/misc/combining/subprocess/fail");
1084   g_ptr_array_add (argv, "-p");
1085   g_ptr_array_add (argv, "/misc/combining/subprocess/skip1");
1086   g_ptr_array_add (argv, "-p");
1087   g_ptr_array_add (argv, "/misc/combining/subprocess/incomplete");
1088   g_ptr_array_add (argv, NULL);
1089
1090   g_spawn_sync (NULL, (char **) argv->pdata, NULL,
1091                 G_SPAWN_STDOUT_TO_DEV_NULL | G_SPAWN_STDERR_TO_DEV_NULL,
1092                 NULL, NULL, NULL, NULL, &status,
1093                 &error);
1094   g_assert_no_error (error);
1095
1096   g_spawn_check_wait_status (status, &error);
1097   g_assert_error (error, G_SPAWN_EXIT_ERROR, 1);
1098   g_clear_error (&error);
1099
1100   g_ptr_array_unref (argv);
1101 }
1102
1103 /* Test the TAP output when a test suite is run with --tap. */
1104 static void
1105 test_tap (void)
1106 {
1107   const char *testing_helper;
1108   GPtrArray *argv;
1109   GError *error = NULL;
1110   int status;
1111   gchar *output;
1112   char **envp;
1113
1114   testing_helper = g_test_get_filename (G_TEST_BUILT, "testing-helper" EXEEXT, NULL);
1115
1116   g_test_message ("pass");
1117   argv = g_ptr_array_new ();
1118   g_ptr_array_add (argv, (char *) testing_helper);
1119   g_ptr_array_add (argv, "pass");
1120   g_ptr_array_add (argv, "--tap");
1121   g_ptr_array_add (argv, NULL);
1122
1123   /* Remove the G_TEST_ROOT_PROCESS env so it will be considered a standalone test */
1124   envp = g_get_environ ();
1125   g_assert_nonnull (g_environ_getenv (envp, "G_TEST_ROOT_PROCESS"));
1126   envp = g_environ_unsetenv (g_steal_pointer (&envp), "G_TEST_ROOT_PROCESS");
1127
1128   g_spawn_sync (NULL, (char **) argv->pdata, envp,
1129                 G_SPAWN_STDERR_TO_DEV_NULL,
1130                 NULL, NULL, &output, NULL, &status,
1131                 &error);
1132   g_assert_no_error (error);
1133
1134   g_spawn_check_wait_status (status, &error);
1135   g_assert_no_error (error);
1136   g_assert_true (g_str_has_prefix (output, "TAP version " TAP_VERSION));
1137   g_assert_null (strstr (output, "# Subtest: "));
1138   g_assert_nonnull (strstr (output, "\nok 1 /pass\n"));
1139   g_free (output);
1140   g_ptr_array_unref (argv);
1141
1142   g_test_message ("skip");
1143   argv = g_ptr_array_new ();
1144   g_ptr_array_add (argv, (char *) testing_helper);
1145   g_ptr_array_add (argv, "skip");
1146   g_ptr_array_add (argv, "--tap");
1147   g_ptr_array_add (argv, NULL);
1148
1149   g_spawn_sync (NULL, (char **) argv->pdata, envp,
1150                 G_SPAWN_STDERR_TO_DEV_NULL,
1151                 NULL, NULL, &output, NULL, &status,
1152                 &error);
1153   g_assert_no_error (error);
1154
1155   g_spawn_check_wait_status (status, &error);
1156   g_assert_no_error (error);
1157   g_assert_true (g_str_has_prefix (output, "TAP version " TAP_VERSION));
1158   g_assert_null (strstr (output, "# Subtest: "));
1159   g_assert_nonnull (strstr (output, "\nok 1 /skip # SKIP not enough tea\n"));
1160   g_free (output);
1161   g_ptr_array_unref (argv);
1162
1163   g_test_message ("skip with printf format");
1164   argv = g_ptr_array_new ();
1165   g_ptr_array_add (argv, (char *) testing_helper);
1166   g_ptr_array_add (argv, "skip-printf");
1167   g_ptr_array_add (argv, "--tap");
1168   g_ptr_array_add (argv, NULL);
1169
1170   g_spawn_sync (NULL, (char **) argv->pdata, envp,
1171                 G_SPAWN_STDERR_TO_DEV_NULL,
1172                 NULL, NULL, &output, NULL, &status,
1173                 &error);
1174   g_assert_no_error (error);
1175
1176   g_spawn_check_wait_status (status, &error);
1177   g_assert_no_error (error);
1178   g_assert_true (g_str_has_prefix (output, "TAP version " TAP_VERSION));
1179   g_assert_null (strstr (output, "# Subtest: "));
1180   g_assert_nonnull (strstr (output, "\nok 1 /skip-printf # SKIP not enough coffee\n"));
1181   g_free (output);
1182   g_ptr_array_unref (argv);
1183
1184   g_test_message ("incomplete");
1185   argv = g_ptr_array_new ();
1186   g_ptr_array_add (argv, (char *) testing_helper);
1187   g_ptr_array_add (argv, "incomplete");
1188   g_ptr_array_add (argv, "--tap");
1189   g_ptr_array_add (argv, NULL);
1190
1191   g_spawn_sync (NULL, (char **) argv->pdata, envp,
1192                 G_SPAWN_STDERR_TO_DEV_NULL,
1193                 NULL, NULL, &output, NULL, &status,
1194                 &error);
1195   g_assert_no_error (error);
1196
1197   g_spawn_check_wait_status (status, &error);
1198   g_assert_no_error (error);
1199   g_assert_true (g_str_has_prefix (output, "TAP version " TAP_VERSION));
1200   g_assert_null (strstr (output, "# Subtest: "));
1201   g_assert_nonnull (strstr (output, "\nnot ok 1 /incomplete # TODO mind reading not implemented yet\n"));
1202   g_free (output);
1203   g_ptr_array_unref (argv);
1204
1205   g_test_message ("incomplete with printf format");
1206   argv = g_ptr_array_new ();
1207   g_ptr_array_add (argv, (char *) testing_helper);
1208   g_ptr_array_add (argv, "incomplete-printf");
1209   g_ptr_array_add (argv, "--tap");
1210   g_ptr_array_add (argv, NULL);
1211
1212   g_spawn_sync (NULL, (char **) argv->pdata, envp,
1213                 G_SPAWN_STDERR_TO_DEV_NULL,
1214                 NULL, NULL, &output, NULL, &status,
1215                 &error);
1216   g_assert_no_error (error);
1217
1218   g_spawn_check_wait_status (status, &error);
1219   g_assert_no_error (error);
1220   g_assert_true (g_str_has_prefix (output, "TAP version " TAP_VERSION));
1221   g_assert_null (strstr (output, "# Subtest: "));
1222   g_assert_nonnull (strstr (output, "\nnot ok 1 /incomplete-printf # TODO telekinesis not implemented yet\n"));
1223   g_free (output);
1224   g_ptr_array_unref (argv);
1225
1226   g_test_message ("fail");
1227   argv = g_ptr_array_new ();
1228   g_ptr_array_add (argv, (char *) testing_helper);
1229   g_ptr_array_add (argv, "fail");
1230   g_ptr_array_add (argv, "--tap");
1231   g_ptr_array_add (argv, NULL);
1232
1233   g_spawn_sync (NULL, (char **) argv->pdata, envp,
1234                 G_SPAWN_STDERR_TO_DEV_NULL,
1235                 NULL, NULL, &output, NULL, &status,
1236                 &error);
1237   g_assert_no_error (error);
1238
1239   g_spawn_check_wait_status (status, &error);
1240   g_assert_error (error, G_SPAWN_EXIT_ERROR, 1);
1241   g_assert_true (g_str_has_prefix (output, "TAP version " TAP_VERSION));
1242   g_assert_null (strstr (output, "# Subtest: "));
1243   g_assert_nonnull (strstr (output, "\nnot ok 1 /fail\n"));
1244   g_free (output);
1245   g_clear_error (&error);
1246   g_ptr_array_unref (argv);
1247
1248   g_test_message ("fail with message");
1249   argv = g_ptr_array_new ();
1250   g_ptr_array_add (argv, (char *) testing_helper);
1251   g_ptr_array_add (argv, "fail-printf");
1252   g_ptr_array_add (argv, "--tap");
1253   g_ptr_array_add (argv, NULL);
1254
1255   g_spawn_sync (NULL, (char **) argv->pdata, envp,
1256                 G_SPAWN_STDERR_TO_DEV_NULL,
1257                 NULL, NULL, &output, NULL, &status,
1258                 &error);
1259   g_assert_no_error (error);
1260
1261   g_spawn_check_wait_status (status, &error);
1262   g_assert_error (error, G_SPAWN_EXIT_ERROR, 1);
1263   g_assert_true (g_str_has_prefix (output, "TAP version " TAP_VERSION));
1264   g_assert_null (strstr (output, "# Subtest: "));
1265   g_assert_nonnull (strstr (output, "\nnot ok 1 /fail-printf - this test intentionally left failing\n"));
1266   g_free (output);
1267   g_clear_error (&error);
1268   g_ptr_array_unref (argv);
1269
1270   g_test_message ("all");
1271   argv = g_ptr_array_new ();
1272   g_ptr_array_add (argv, (char *) testing_helper);
1273   g_ptr_array_add (argv, "all");
1274   g_ptr_array_add (argv, "--tap");
1275   g_ptr_array_add (argv, NULL);
1276
1277   g_spawn_sync (NULL, (char **) argv->pdata, envp,
1278                 G_SPAWN_STDOUT_TO_DEV_NULL | G_SPAWN_STDERR_TO_DEV_NULL,
1279                 NULL, NULL, NULL, NULL, &status,
1280                 &error);
1281   g_assert_no_error (error);
1282
1283   g_spawn_check_wait_status (status, &error);
1284   g_assert_error (error, G_SPAWN_EXIT_ERROR, 1);
1285   g_clear_error (&error);
1286   g_ptr_array_unref (argv);
1287
1288   g_test_message ("all-non-failures");
1289   argv = g_ptr_array_new ();
1290   g_ptr_array_add (argv, (char *) testing_helper);
1291   g_ptr_array_add (argv, "all-non-failures");
1292   g_ptr_array_add (argv, "--tap");
1293   g_ptr_array_add (argv, NULL);
1294
1295   g_spawn_sync (NULL, (char **) argv->pdata, envp,
1296                 G_SPAWN_STDOUT_TO_DEV_NULL | G_SPAWN_STDERR_TO_DEV_NULL,
1297                 NULL, NULL, NULL, NULL, &status,
1298                 &error);
1299   g_assert_no_error (error);
1300
1301   g_spawn_check_wait_status (status, &error);
1302   g_assert_no_error (error);
1303
1304   g_ptr_array_unref (argv);
1305
1306   g_test_message ("--GTestSkipCount");
1307   argv = g_ptr_array_new ();
1308   g_ptr_array_add (argv, (char *) testing_helper);
1309   g_ptr_array_add (argv, "skip-options");
1310   g_ptr_array_add (argv, "--tap");
1311   g_ptr_array_add (argv, "--GTestSkipCount");
1312   g_ptr_array_add (argv, "2");
1313   g_ptr_array_add (argv, NULL);
1314
1315   g_spawn_sync (NULL, (char **) argv->pdata, envp,
1316                 G_SPAWN_STDERR_TO_DEV_NULL,
1317                 NULL, NULL, &output, NULL, &status,
1318                 &error);
1319   g_assert_no_error (error);
1320   g_assert_true (g_str_has_prefix (output, "TAP version " TAP_VERSION));
1321   g_assert_null (strstr (output, "# Subtest: "));
1322   g_assert_nonnull (strstr (output, "1..10\n"));
1323   g_assert_nonnull (strstr (output, "\nok 1 /a # SKIP\n"));
1324   g_assert_nonnull (strstr (output, "\nok 2 /b # SKIP\n"));
1325   g_assert_nonnull (strstr (output, "\nok 3 /b/a\n"));
1326   g_assert_nonnull (strstr (output, "\nok 4 /b/b\n"));
1327   g_assert_nonnull (strstr (output, "\nok 5 /b/b/a\n"));
1328   g_assert_nonnull (strstr (output, "\nok 6 /prefix/a\n"));
1329   g_assert_nonnull (strstr (output, "\nok 7 /prefix/b/b\n"));
1330   g_assert_nonnull (strstr (output, "\nok 8 /prefix-long/a\n"));
1331   g_assert_nonnull (strstr (output, "\nok 9 /c/a\n"));
1332   g_assert_nonnull (strstr (output, "\nok 10 /d/a\n"));
1333
1334   g_spawn_check_wait_status (status, &error);
1335   g_assert_no_error (error);
1336
1337   g_free (output);
1338   g_ptr_array_unref (argv);
1339
1340   g_test_message ("--GTestSkipCount=0 is the same as omitting it");
1341   argv = g_ptr_array_new ();
1342   g_ptr_array_add (argv, (char *) testing_helper);
1343   g_ptr_array_add (argv, "skip-options");
1344   g_ptr_array_add (argv, "--tap");
1345   g_ptr_array_add (argv, "--GTestSkipCount");
1346   g_ptr_array_add (argv, "0");
1347   g_ptr_array_add (argv, NULL);
1348
1349   g_spawn_sync (NULL, (char **) argv->pdata, envp,
1350                 G_SPAWN_STDERR_TO_DEV_NULL,
1351                 NULL, NULL, &output, NULL, &status,
1352                 &error);
1353   g_assert_no_error (error);
1354   g_assert_true (g_str_has_prefix (output, "TAP version " TAP_VERSION));
1355   g_assert_null (strstr (output, "# Subtest: "));
1356   g_assert_nonnull (strstr (output, "1..10\n"));
1357   g_assert_nonnull (strstr (output, "\nok 1 /a\n"));
1358   g_assert_nonnull (strstr (output, "\nok 2 /b\n"));
1359   g_assert_nonnull (strstr (output, "\nok 3 /b/a\n"));
1360   g_assert_nonnull (strstr (output, "\nok 4 /b/b\n"));
1361   g_assert_nonnull (strstr (output, "\nok 5 /b/b/a\n"));
1362   g_assert_nonnull (strstr (output, "\nok 6 /prefix/a\n"));
1363   g_assert_nonnull (strstr (output, "\nok 7 /prefix/b/b\n"));
1364   g_assert_nonnull (strstr (output, "\nok 8 /prefix-long/a\n"));
1365   g_assert_nonnull (strstr (output, "\nok 9 /c/a\n"));
1366   g_assert_nonnull (strstr (output, "\nok 10 /d/a\n"));
1367
1368   g_spawn_check_wait_status (status, &error);
1369   g_assert_no_error (error);
1370
1371   g_free (output);
1372   g_ptr_array_unref (argv);
1373
1374   g_test_message ("--GTestSkipCount > number of tests skips all");
1375   argv = g_ptr_array_new ();
1376   g_ptr_array_add (argv, (char *) testing_helper);
1377   g_ptr_array_add (argv, "skip-options");
1378   g_ptr_array_add (argv, "--tap");
1379   g_ptr_array_add (argv, "--GTestSkipCount");
1380   g_ptr_array_add (argv, "11");
1381   g_ptr_array_add (argv, NULL);
1382
1383   g_spawn_sync (NULL, (char **) argv->pdata, envp,
1384                 G_SPAWN_STDERR_TO_DEV_NULL,
1385                 NULL, NULL, &output, NULL, &status,
1386                 &error);
1387   g_assert_no_error (error);
1388   g_assert_true (g_str_has_prefix (output, "TAP version " TAP_VERSION));
1389   g_assert_null (strstr (output, "# Subtest: "));
1390   g_assert_nonnull (strstr (output, "1..10\n"));
1391   g_assert_nonnull (strstr (output, "\nok 1 /a # SKIP\n"));
1392   g_assert_nonnull (strstr (output, "\nok 2 /b # SKIP\n"));
1393   g_assert_nonnull (strstr (output, "\nok 3 /b/a # SKIP\n"));
1394   g_assert_nonnull (strstr (output, "\nok 4 /b/b # SKIP\n"));
1395   g_assert_nonnull (strstr (output, "\nok 5 /b/b/a # SKIP\n"));
1396   g_assert_nonnull (strstr (output, "\nok 6 /prefix/a # SKIP\n"));
1397   g_assert_nonnull (strstr (output, "\nok 7 /prefix/b/b # SKIP\n"));
1398   g_assert_nonnull (strstr (output, "\nok 8 /prefix-long/a # SKIP\n"));
1399   g_assert_nonnull (strstr (output, "\nok 9 /c/a # SKIP\n"));
1400   g_assert_nonnull (strstr (output, "\nok 10 /d/a # SKIP\n"));
1401
1402   g_spawn_check_wait_status (status, &error);
1403   g_assert_no_error (error);
1404
1405   g_free (output);
1406   g_ptr_array_unref (argv);
1407
1408   g_test_message ("-p");
1409   argv = g_ptr_array_new ();
1410   g_ptr_array_add (argv, (char *) testing_helper);
1411   g_ptr_array_add (argv, "skip-options");
1412   g_ptr_array_add (argv, "--tap");
1413   g_ptr_array_add (argv, "-p");
1414   g_ptr_array_add (argv, "/c/a");
1415   g_ptr_array_add (argv, "-p");
1416   g_ptr_array_add (argv, "/c/a");
1417   g_ptr_array_add (argv, "-p");
1418   g_ptr_array_add (argv, "/b");
1419   g_ptr_array_add (argv, NULL);
1420
1421   g_spawn_sync (NULL, (char **) argv->pdata, envp,
1422                 G_SPAWN_STDERR_TO_DEV_NULL,
1423                 NULL, NULL, &output, NULL, &status,
1424                 &error);
1425   g_assert_no_error (error);
1426   g_assert_true (g_str_has_prefix (output, "TAP version " TAP_VERSION));
1427   g_assert_null (strstr (output, "# Subtest: "));
1428   g_assert_nonnull (strstr (output, "\nok 1 /c/a\n"));
1429   g_assert_nonnull (strstr (output, "\nok 2 /c/a\n"));
1430   g_assert_nonnull (strstr (output, "\nok 3 /b\n"));
1431   g_assert_nonnull (strstr (output, "\nok 4 /b/a\n"));
1432   g_assert_nonnull (strstr (output, "\nok 5 /b/b\n"));
1433   g_assert_nonnull (strstr (output, "\n1..5\n"));
1434
1435   g_spawn_check_wait_status (status, &error);
1436   g_assert_no_error (error);
1437
1438   g_free (output);
1439   g_ptr_array_unref (argv);
1440
1441   g_test_message ("--run-prefix");
1442   argv = g_ptr_array_new ();
1443   g_ptr_array_add (argv, (char *) testing_helper);
1444   g_ptr_array_add (argv, "skip-options");
1445   g_ptr_array_add (argv, "--tap");
1446   g_ptr_array_add (argv, "-r");
1447   g_ptr_array_add (argv, "/c/a");
1448   g_ptr_array_add (argv, "-r");
1449   g_ptr_array_add (argv, "/c/a");
1450   g_ptr_array_add (argv, "--run-prefix");
1451   g_ptr_array_add (argv, "/b");
1452   g_ptr_array_add (argv, NULL);
1453
1454   g_spawn_sync (NULL, (char **) argv->pdata, envp,
1455                 G_SPAWN_STDERR_TO_DEV_NULL,
1456                 NULL, NULL, &output, NULL, &status,
1457                 &error);
1458   g_assert_no_error (error);
1459   g_assert_true (g_str_has_prefix (output, "TAP version " TAP_VERSION));
1460   g_assert_null (strstr (output, "# Subtest: "));
1461   g_assert_nonnull (strstr (output, "\nok 1 /c/a\n"));
1462   g_assert_nonnull (strstr (output, "\nok 2 /c/a\n"));
1463   g_assert_nonnull (strstr (output, "\nok 3 /b\n"));
1464   g_assert_nonnull (strstr (output, "\nok 4 /b/a\n"));
1465   g_assert_nonnull (strstr (output, "\nok 5 /b/b\n"));
1466   g_assert_nonnull (strstr (output, "\nok 6 /b/b/a\n"));
1467   g_assert_nonnull (strstr (output, "\n1..6\n"));
1468
1469   g_spawn_check_wait_status (status, &error);
1470   g_assert_no_error (error);
1471
1472   g_free (output);
1473   g_ptr_array_unref (argv);
1474
1475   g_test_message ("--run-prefix 2");
1476   argv = g_ptr_array_new ();
1477   g_ptr_array_add (argv, (char *) testing_helper);
1478   g_ptr_array_add (argv, "skip-options");
1479   g_ptr_array_add (argv, "--tap");
1480   g_ptr_array_add (argv, "-r");
1481   g_ptr_array_add (argv, "/pre");
1482   g_ptr_array_add (argv, "--run-prefix");
1483   g_ptr_array_add (argv, "/b/b");
1484   g_ptr_array_add (argv, NULL);
1485
1486   g_spawn_sync (NULL, (char **) argv->pdata, envp,
1487                 G_SPAWN_STDERR_TO_DEV_NULL,
1488                 NULL, NULL, &output, NULL, &status,
1489                 &error);
1490   g_assert_no_error (error);
1491   g_assert_true (g_str_has_prefix (output, "TAP version " TAP_VERSION));
1492   g_assert_null (strstr (output, "# Subtest: "));
1493   g_assert_nonnull (strstr (output, "\nok 1 /b/b\n"));
1494   g_assert_nonnull (strstr (output, "\nok 2 /b/b/a\n"));
1495   g_assert_nonnull (strstr (output, "\n1..2\n"));
1496
1497   g_spawn_check_wait_status (status, &error);
1498   g_assert_no_error (error);
1499
1500   g_free (output);
1501   g_ptr_array_unref (argv);
1502
1503   g_test_message ("--run-prefix conflict");
1504   argv = g_ptr_array_new ();
1505   g_ptr_array_add (argv, (char *) testing_helper);
1506   g_ptr_array_add (argv, "skip-options");
1507   g_ptr_array_add (argv, "--tap");
1508   g_ptr_array_add (argv, "-r");
1509   g_ptr_array_add (argv, "/c/a");
1510   g_ptr_array_add (argv, "-p");
1511   g_ptr_array_add (argv, "/c/a");
1512   g_ptr_array_add (argv, "--run-prefix");
1513   g_ptr_array_add (argv, "/b");
1514   g_ptr_array_add (argv, NULL);
1515
1516   g_spawn_sync (NULL, (char **) argv->pdata, envp,
1517                 G_SPAWN_STDERR_TO_DEV_NULL,
1518                 NULL, NULL, &output, NULL, &status,
1519                 &error);
1520   g_assert_no_error (error);
1521   g_spawn_check_wait_status (status, &error);
1522   g_assert_nonnull (error);
1523   g_assert_false (g_str_has_prefix (output, "TAP version " TAP_VERSION));
1524   g_assert_nonnull (strstr (output, "do not mix [-r | --run-prefix] with '-p'\n"));
1525   g_clear_error (&error);
1526
1527   g_free (output);
1528   g_ptr_array_unref (argv);
1529
1530   g_test_message ("-s");
1531   argv = g_ptr_array_new ();
1532   g_ptr_array_add (argv, (char *) testing_helper);
1533   g_ptr_array_add (argv, "skip-options");
1534   g_ptr_array_add (argv, "--tap");
1535   g_ptr_array_add (argv, "-s");
1536   g_ptr_array_add (argv, "/a");
1537   g_ptr_array_add (argv, "-s");
1538   g_ptr_array_add (argv, "/b");
1539   g_ptr_array_add (argv, "-s");
1540   g_ptr_array_add (argv, "/pre");
1541   g_ptr_array_add (argv, "-s");
1542   g_ptr_array_add (argv, "/c/a");
1543   g_ptr_array_add (argv, NULL);
1544
1545   g_spawn_sync (NULL, (char **) argv->pdata, envp,
1546                 G_SPAWN_STDERR_TO_DEV_NULL,
1547                 NULL, NULL, &output, NULL, &status,
1548                 &error);
1549   g_assert_no_error (error);
1550   g_assert_true (g_str_has_prefix (output, "TAP version " TAP_VERSION));
1551   g_assert_null (strstr (output, "# Subtest: "));
1552   g_assert_nonnull (strstr (output, "1..10\n"));
1553   g_assert_nonnull (strstr (output, "\nok 1 /a # SKIP by request"));
1554   g_assert_nonnull (strstr (output, "\nok 2 /b # SKIP by request"));
1555   /* "-s /b" would skip a test named exactly /b, but not a test named
1556    * /b/anything */
1557   g_assert_nonnull (strstr (output, "\nok 3 /b/a\n"));
1558   g_assert_nonnull (strstr (output, "\nok 4 /b/b\n"));
1559   g_assert_nonnull (strstr (output, "\nok 5 /b/b/a\n"));
1560   g_assert_nonnull (strstr (output, "\nok 6 /prefix/a\n"));
1561   g_assert_nonnull (strstr (output, "\nok 7 /prefix/b/b\n"));
1562   g_assert_nonnull (strstr (output, "\nok 8 /prefix-long/a\n"));
1563   g_assert_nonnull (strstr (output, "\nok 9 /c/a # SKIP by request"));
1564   g_assert_nonnull (strstr (output, "\nok 10 /d/a\n"));
1565
1566   g_spawn_check_wait_status (status, &error);
1567   g_assert_no_error (error);
1568
1569   g_free (output);
1570   g_ptr_array_unref (argv);
1571
1572   g_test_message ("--skip-prefix");
1573   argv = g_ptr_array_new ();
1574   g_ptr_array_add (argv, (char *) testing_helper);
1575   g_ptr_array_add (argv, "skip-options");
1576   g_ptr_array_add (argv, "--tap");
1577   g_ptr_array_add (argv, "-x");
1578   g_ptr_array_add (argv, "/a");
1579   g_ptr_array_add (argv, "--skip-prefix");
1580   g_ptr_array_add (argv, "/pre");
1581   g_ptr_array_add (argv, "-x");
1582   g_ptr_array_add (argv, "/c/a");
1583   g_ptr_array_add (argv, NULL);
1584
1585   g_spawn_sync (NULL, (char **) argv->pdata, envp,
1586                 G_SPAWN_STDERR_TO_DEV_NULL,
1587                 NULL, NULL, &output, NULL, &status,
1588                 &error);
1589   g_assert_no_error (error);
1590   g_assert_true (g_str_has_prefix (output, "TAP version " TAP_VERSION));
1591   g_assert_null (strstr (output, "# Subtest: "));
1592   g_assert_nonnull (strstr (output, "1..10\n"));
1593   g_assert_nonnull (strstr (output, "\nok 1 /a # SKIP by request"));
1594   g_assert_nonnull (strstr (output, "\nok 2 /b\n"));
1595   g_assert_nonnull (strstr (output, "\nok 3 /b/a\n"));
1596   g_assert_nonnull (strstr (output, "\nok 4 /b/b\n"));
1597   g_assert_nonnull (strstr (output, "\nok 5 /b/b/a\n"));
1598   /* "--skip-prefix /pre" will skip all test path which begins with /pre */
1599   g_assert_nonnull (strstr (output, "\nok 6 /prefix/a # SKIP by request"));
1600   g_assert_nonnull (strstr (output, "\nok 7 /prefix/b/b # SKIP by request"));
1601   g_assert_nonnull (strstr (output, "\nok 8 /prefix-long/a # SKIP by request"));
1602   g_assert_nonnull (strstr (output, "\nok 9 /c/a # SKIP by request"));
1603   g_assert_nonnull (strstr (output, "\nok 10 /d/a\n"));
1604
1605   g_spawn_check_wait_status (status, &error);
1606   g_assert_no_error (error);
1607
1608   g_free (output);
1609   g_ptr_array_unref (argv);
1610
1611   g_test_message ("--skip-prefix conflict");
1612   argv = g_ptr_array_new ();
1613   g_ptr_array_add (argv, (char *) testing_helper);
1614   g_ptr_array_add (argv, "skip-options");
1615   g_ptr_array_add (argv, "--tap");
1616   g_ptr_array_add (argv, "-s");
1617   g_ptr_array_add (argv, "/a");
1618   g_ptr_array_add (argv, "--skip-prefix");
1619   g_ptr_array_add (argv, "/pre");
1620   g_ptr_array_add (argv, "-x");
1621   g_ptr_array_add (argv, "/c/a");
1622   g_ptr_array_add (argv, NULL);
1623
1624   g_spawn_sync (NULL, (char **) argv->pdata, envp,
1625                 G_SPAWN_STDERR_TO_DEV_NULL,
1626                 NULL, NULL, &output, NULL, &status,
1627                 &error);
1628   g_assert_no_error (error);
1629   g_spawn_check_wait_status (status, &error);
1630   g_assert_nonnull (error);
1631   g_assert_false (g_str_has_prefix (output, "TAP version " TAP_VERSION));
1632   g_assert_nonnull (strstr (output, "do not mix [-x | --skip-prefix] with '-s'\n"));
1633   g_clear_error (&error);
1634
1635   g_free (output);
1636   g_ptr_array_unref (argv);
1637   g_strfreev (envp);
1638 }
1639
1640 /* Test the TAP output when a test suite is run with --tap. */
1641 static void
1642 test_tap_subtest (void)
1643 {
1644   const char *testing_helper;
1645   GPtrArray *argv;
1646   GError *error = NULL;
1647   int status;
1648   gchar *output;
1649   char** envp = NULL;
1650
1651   testing_helper = g_test_get_filename (G_TEST_BUILT, "testing-helper" EXEEXT, NULL);
1652
1653   g_test_message ("pass");
1654   argv = g_ptr_array_new ();
1655   g_ptr_array_add (argv, (char *) testing_helper);
1656   g_ptr_array_add (argv, "pass");
1657   g_ptr_array_add (argv, "--tap");
1658   g_ptr_array_add (argv, NULL);
1659
1660   envp = g_get_environ ();
1661   g_assert_nonnull (g_environ_getenv (envp, "G_TEST_ROOT_PROCESS"));
1662   g_clear_pointer (&envp, g_strfreev);
1663
1664   g_spawn_sync (NULL, (char **) argv->pdata, envp,
1665                 G_SPAWN_STDERR_TO_DEV_NULL,
1666                 NULL, NULL, &output, NULL, &status,
1667                 &error);
1668   g_assert_no_error (error);
1669
1670   g_spawn_check_wait_status (status, &error);
1671   g_assert_no_error (error);
1672   g_assert_null (strstr (output, "TAP version " TAP_VERSION));
1673   g_assert_true (g_str_has_prefix (output, "# Subtest: "));
1674   g_assert_nonnull (strstr (output,
1675     "\n" TAP_SUBTEST_PREFIX "ok 1 /pass\n"));
1676   g_free (output);
1677   g_ptr_array_unref (argv);
1678
1679   g_test_message ("skip");
1680   argv = g_ptr_array_new ();
1681   g_ptr_array_add (argv, (char *) testing_helper);
1682   g_ptr_array_add (argv, "skip");
1683   g_ptr_array_add (argv, "--tap");
1684   g_ptr_array_add (argv, NULL);
1685
1686   g_spawn_sync (NULL, (char **) argv->pdata, envp,
1687                 G_SPAWN_STDERR_TO_DEV_NULL,
1688                 NULL, NULL, &output, NULL, &status,
1689                 &error);
1690   g_assert_no_error (error);
1691
1692   g_spawn_check_wait_status (status, &error);
1693   g_assert_no_error (error);
1694   g_assert_null (strstr (output, "TAP version " TAP_VERSION));
1695   g_assert_true (g_str_has_prefix (output, "# Subtest: "));
1696   g_assert_nonnull (strstr (output,
1697     "\n" TAP_SUBTEST_PREFIX "ok 1 /skip # SKIP not enough tea\n"));
1698   g_free (output);
1699   g_ptr_array_unref (argv);
1700
1701   g_test_message ("skip with printf format");
1702   argv = g_ptr_array_new ();
1703   g_ptr_array_add (argv, (char *) testing_helper);
1704   g_ptr_array_add (argv, "skip-printf");
1705   g_ptr_array_add (argv, "--tap");
1706   g_ptr_array_add (argv, NULL);
1707
1708   g_spawn_sync (NULL, (char **) argv->pdata, envp,
1709                 G_SPAWN_STDERR_TO_DEV_NULL,
1710                 NULL, NULL, &output, NULL, &status,
1711                 &error);
1712   g_assert_no_error (error);
1713
1714   g_spawn_check_wait_status (status, &error);
1715   g_assert_no_error (error);
1716   g_assert_null (strstr (output, "TAP version " TAP_VERSION));
1717   g_assert_true (g_str_has_prefix (output, "# Subtest: "));
1718   g_assert_nonnull (strstr (output,
1719     "\n" TAP_SUBTEST_PREFIX "ok 1 /skip-printf # SKIP not enough coffee\n"));
1720   g_free (output);
1721   g_ptr_array_unref (argv);
1722
1723   g_test_message ("incomplete");
1724   argv = g_ptr_array_new ();
1725   g_ptr_array_add (argv, (char *) testing_helper);
1726   g_ptr_array_add (argv, "incomplete");
1727   g_ptr_array_add (argv, "--tap");
1728   g_ptr_array_add (argv, NULL);
1729
1730   g_spawn_sync (NULL, (char **) argv->pdata, envp,
1731                 G_SPAWN_STDERR_TO_DEV_NULL,
1732                 NULL, NULL, &output, NULL, &status,
1733                 &error);
1734   g_assert_no_error (error);
1735
1736   g_spawn_check_wait_status (status, &error);
1737   g_assert_no_error (error);
1738   g_assert_null (strstr (output, "TAP version " TAP_VERSION));
1739   g_assert_null (strstr (output, "\n# Subtest: "));
1740   g_assert_nonnull (strstr (output,
1741     "\n" TAP_SUBTEST_PREFIX "not ok 1 /incomplete # TODO mind reading not implemented yet\n"));
1742   g_free (output);
1743   g_ptr_array_unref (argv);
1744
1745   g_test_message ("incomplete with printf format");
1746   argv = g_ptr_array_new ();
1747   g_ptr_array_add (argv, (char *) testing_helper);
1748   g_ptr_array_add (argv, "incomplete-printf");
1749   g_ptr_array_add (argv, "--tap");
1750   g_ptr_array_add (argv, NULL);
1751
1752   g_spawn_sync (NULL, (char **) argv->pdata, envp,
1753                 G_SPAWN_STDERR_TO_DEV_NULL,
1754                 NULL, NULL, &output, NULL, &status,
1755                 &error);
1756   g_assert_no_error (error);
1757
1758   g_spawn_check_wait_status (status, &error);
1759   g_assert_no_error (error);
1760   g_assert_null (strstr (output, "TAP version " TAP_VERSION));
1761   g_assert_null( strstr (output, "\n# Subtest: "));
1762   g_assert_nonnull (strstr (output,
1763     "\n" TAP_SUBTEST_PREFIX "not ok 1 /incomplete-printf # TODO telekinesis not implemented yet\n"));
1764   g_free (output);
1765   g_ptr_array_unref (argv);
1766
1767   g_test_message ("fail");
1768   argv = g_ptr_array_new ();
1769   g_ptr_array_add (argv, (char *) testing_helper);
1770   g_ptr_array_add (argv, "fail");
1771   g_ptr_array_add (argv, "--tap");
1772   g_ptr_array_add (argv, NULL);
1773
1774   g_spawn_sync (NULL, (char **) argv->pdata, envp,
1775                 G_SPAWN_STDERR_TO_DEV_NULL,
1776                 NULL, NULL, &output, NULL, &status,
1777                 &error);
1778   g_assert_no_error (error);
1779
1780   g_spawn_check_wait_status (status, &error);
1781   g_assert_error (error, G_SPAWN_EXIT_ERROR, 1);
1782   g_assert_null (strstr (output, "TAP version " TAP_VERSION));
1783   g_assert_null( strstr (output, "\n# Subtest: "));
1784   g_assert_nonnull (strstr (output,
1785     "\n" TAP_SUBTEST_PREFIX "not ok 1 /fail\n"));
1786   g_free (output);
1787   g_clear_error (&error);
1788   g_ptr_array_unref (argv);
1789
1790   g_test_message ("fail with message");
1791   argv = g_ptr_array_new ();
1792   g_ptr_array_add (argv, (char *) testing_helper);
1793   g_ptr_array_add (argv, "fail-printf");
1794   g_ptr_array_add (argv, "--tap");
1795   g_ptr_array_add (argv, NULL);
1796
1797   g_spawn_sync (NULL, (char **) argv->pdata, envp,
1798                 G_SPAWN_STDERR_TO_DEV_NULL,
1799                 NULL, NULL, &output, NULL, &status,
1800                 &error);
1801   g_assert_no_error (error);
1802
1803   g_spawn_check_wait_status (status, &error);
1804   g_assert_error (error, G_SPAWN_EXIT_ERROR, 1);
1805   g_assert_null (strstr (output, "TAP version " TAP_VERSION));
1806   g_assert_null( strstr (output, "\n# Subtest: "));
1807   g_assert_nonnull (strstr (output,
1808     "\n" TAP_SUBTEST_PREFIX "not ok 1 /fail-printf - this test intentionally left failing\n"));
1809   g_free (output);
1810   g_clear_error (&error);
1811   g_ptr_array_unref (argv);
1812
1813   g_test_message ("all");
1814   argv = g_ptr_array_new ();
1815   g_ptr_array_add (argv, (char *) testing_helper);
1816   g_ptr_array_add (argv, "all");
1817   g_ptr_array_add (argv, "--tap");
1818   g_ptr_array_add (argv, NULL);
1819
1820   g_spawn_sync (NULL, (char **) argv->pdata, envp,
1821                 G_SPAWN_STDOUT_TO_DEV_NULL | G_SPAWN_STDERR_TO_DEV_NULL,
1822                 NULL, NULL, NULL, NULL, &status,
1823                 &error);
1824   g_assert_no_error (error);
1825
1826   g_spawn_check_wait_status (status, &error);
1827   g_assert_error (error, G_SPAWN_EXIT_ERROR, 1);
1828   g_clear_error (&error);
1829   g_ptr_array_unref (argv);
1830
1831   g_test_message ("all-non-failures");
1832   argv = g_ptr_array_new ();
1833   g_ptr_array_add (argv, (char *) testing_helper);
1834   g_ptr_array_add (argv, "all-non-failures");
1835   g_ptr_array_add (argv, "--tap");
1836   g_ptr_array_add (argv, NULL);
1837
1838   g_spawn_sync (NULL, (char **) argv->pdata, envp,
1839                 G_SPAWN_STDOUT_TO_DEV_NULL | G_SPAWN_STDERR_TO_DEV_NULL,
1840                 NULL, NULL, NULL, NULL, &status,
1841                 &error);
1842   g_assert_no_error (error);
1843
1844   g_spawn_check_wait_status (status, &error);
1845   g_assert_no_error (error);
1846
1847   g_ptr_array_unref (argv);
1848
1849   g_test_message ("--GTestSkipCount");
1850   argv = g_ptr_array_new ();
1851   g_ptr_array_add (argv, (char *) testing_helper);
1852   g_ptr_array_add (argv, "skip-options");
1853   g_ptr_array_add (argv, "--tap");
1854   g_ptr_array_add (argv, "--GTestSkipCount");
1855   g_ptr_array_add (argv, "2");
1856   g_ptr_array_add (argv, NULL);
1857
1858   g_spawn_sync (NULL, (char **) argv->pdata, envp,
1859                 G_SPAWN_STDERR_TO_DEV_NULL,
1860                 NULL, NULL, &output, NULL, &status,
1861                 &error);
1862   g_assert_no_error (error);
1863   g_assert_null (strstr (output, "TAP version " TAP_VERSION));
1864   g_assert_null( strstr (output, "\n# Subtest: "));
1865   g_assert_nonnull (strstr (output, TAP_SUBTEST_PREFIX "1..10\n"));
1866   g_assert_nonnull (strstr (output,
1867     "\n" TAP_SUBTEST_PREFIX "ok 1 /a # SKIP\n"));
1868   g_assert_nonnull (strstr (output,
1869     "\n" TAP_SUBTEST_PREFIX "ok 2 /b # SKIP\n"));
1870   g_assert_nonnull (strstr (output,
1871     "\n" TAP_SUBTEST_PREFIX "ok 3 /b/a\n"));
1872   g_assert_nonnull (strstr (output,
1873     "\n" TAP_SUBTEST_PREFIX "ok 4 /b/b\n"));
1874   g_assert_nonnull (strstr (output,
1875     "\n" TAP_SUBTEST_PREFIX "ok 5 /b/b/a\n"));
1876   g_assert_nonnull (strstr (output,
1877     "\n" TAP_SUBTEST_PREFIX "ok 6 /prefix/a\n"));
1878   g_assert_nonnull (strstr (output,
1879     "\n" TAP_SUBTEST_PREFIX "ok 7 /prefix/b/b\n"));
1880   g_assert_nonnull (strstr (output,
1881     "\n" TAP_SUBTEST_PREFIX "ok 8 /prefix-long/a\n"));
1882   g_assert_nonnull (strstr (output,
1883     "\n" TAP_SUBTEST_PREFIX "ok 9 /c/a\n"));
1884   g_assert_nonnull (strstr (output,
1885     "\n" TAP_SUBTEST_PREFIX "ok 10 /d/a\n"));
1886
1887   g_spawn_check_wait_status (status, &error);
1888   g_assert_no_error (error);
1889
1890   g_free (output);
1891   g_ptr_array_unref (argv);
1892
1893   g_test_message ("--GTestSkipCount=0 is the same as omitting it");
1894   argv = g_ptr_array_new ();
1895   g_ptr_array_add (argv, (char *) testing_helper);
1896   g_ptr_array_add (argv, "skip-options");
1897   g_ptr_array_add (argv, "--tap");
1898   g_ptr_array_add (argv, "--GTestSkipCount");
1899   g_ptr_array_add (argv, "0");
1900   g_ptr_array_add (argv, NULL);
1901
1902   g_spawn_sync (NULL, (char **) argv->pdata, envp,
1903                 G_SPAWN_STDERR_TO_DEV_NULL,
1904                 NULL, NULL, &output, NULL, &status,
1905                 &error);
1906   g_assert_no_error (error);
1907   g_assert_null (strstr (output, "TAP version " TAP_VERSION));
1908   g_assert_null( strstr (output, "\n# Subtest: "));
1909   g_assert_nonnull (strstr (output, TAP_SUBTEST_PREFIX "1..10\n"));
1910   g_assert_nonnull (strstr (output,
1911     "\n" TAP_SUBTEST_PREFIX "ok 1 /a\n"));
1912   g_assert_nonnull (strstr (output,
1913     "\n" TAP_SUBTEST_PREFIX "ok 2 /b\n"));
1914   g_assert_nonnull (strstr (output,
1915     "\n" TAP_SUBTEST_PREFIX "ok 3 /b/a\n"));
1916   g_assert_nonnull (strstr (output,
1917     "\n" TAP_SUBTEST_PREFIX "ok 4 /b/b\n"));
1918   g_assert_nonnull (strstr (output,
1919     "\n" TAP_SUBTEST_PREFIX "ok 5 /b/b/a\n"));
1920   g_assert_nonnull (strstr (output,
1921     "\n" TAP_SUBTEST_PREFIX "ok 6 /prefix/a\n"));
1922   g_assert_nonnull (strstr (output,
1923     "\n" TAP_SUBTEST_PREFIX "ok 7 /prefix/b/b\n"));
1924   g_assert_nonnull (strstr (output,
1925     "\n" TAP_SUBTEST_PREFIX "ok 8 /prefix-long/a\n"));
1926   g_assert_nonnull (strstr (output,
1927     "\n" TAP_SUBTEST_PREFIX "ok 9 /c/a\n"));
1928   g_assert_nonnull (strstr (output,
1929     "\n" TAP_SUBTEST_PREFIX "ok 10 /d/a\n"));
1930
1931   g_spawn_check_wait_status (status, &error);
1932   g_assert_no_error (error);
1933
1934   g_free (output);
1935   g_ptr_array_unref (argv);
1936
1937   g_test_message ("--GTestSkipCount > number of tests skips all");
1938   argv = g_ptr_array_new ();
1939   g_ptr_array_add (argv, (char *) testing_helper);
1940   g_ptr_array_add (argv, "skip-options");
1941   g_ptr_array_add (argv, "--tap");
1942   g_ptr_array_add (argv, "--GTestSkipCount");
1943   g_ptr_array_add (argv, "11");
1944   g_ptr_array_add (argv, NULL);
1945
1946   g_spawn_sync (NULL, (char **) argv->pdata, envp,
1947                 G_SPAWN_STDERR_TO_DEV_NULL,
1948                 NULL, NULL, &output, NULL, &status,
1949                 &error);
1950   g_assert_no_error (error);
1951   g_assert_null (strstr (output, "TAP version " TAP_VERSION));
1952   g_assert_null( strstr (output, "\n# Subtest: "));
1953   g_assert_nonnull (strstr (output, TAP_SUBTEST_PREFIX "1..10\n"));
1954   g_assert_nonnull (strstr (output,
1955     "\n" TAP_SUBTEST_PREFIX "ok 1 /a # SKIP\n"));
1956   g_assert_nonnull (strstr (output,
1957     "\n" TAP_SUBTEST_PREFIX "ok 2 /b # SKIP\n"));
1958   g_assert_nonnull (strstr (output,
1959     "\n" TAP_SUBTEST_PREFIX "ok 3 /b/a # SKIP\n"));
1960   g_assert_nonnull (strstr (output,
1961     "\n" TAP_SUBTEST_PREFIX "ok 4 /b/b # SKIP\n"));
1962   g_assert_nonnull (strstr (output,
1963     "\n" TAP_SUBTEST_PREFIX "ok 5 /b/b/a # SKIP\n"));
1964   g_assert_nonnull (strstr (output,
1965     "\n" TAP_SUBTEST_PREFIX "ok 6 /prefix/a # SKIP\n"));
1966   g_assert_nonnull (strstr (output,
1967     "\n" TAP_SUBTEST_PREFIX "ok 7 /prefix/b/b # SKIP\n"));
1968   g_assert_nonnull (strstr (output,
1969     "\n" TAP_SUBTEST_PREFIX "ok 8 /prefix-long/a # SKIP\n"));
1970   g_assert_nonnull (strstr (output,
1971     "\n" TAP_SUBTEST_PREFIX "ok 9 /c/a # SKIP\n"));
1972   g_assert_nonnull (strstr (output,
1973     "\n" TAP_SUBTEST_PREFIX "ok 10 /d/a # SKIP\n"));
1974
1975   g_spawn_check_wait_status (status, &error);
1976   g_assert_no_error (error);
1977
1978   g_free (output);
1979   g_ptr_array_unref (argv);
1980
1981   g_test_message ("-p");
1982   argv = g_ptr_array_new ();
1983   g_ptr_array_add (argv, (char *) testing_helper);
1984   g_ptr_array_add (argv, "skip-options");
1985   g_ptr_array_add (argv, "--tap");
1986   g_ptr_array_add (argv, "-p");
1987   g_ptr_array_add (argv, "/c/a");
1988   g_ptr_array_add (argv, "-p");
1989   g_ptr_array_add (argv, "/c/a");
1990   g_ptr_array_add (argv, "-p");
1991   g_ptr_array_add (argv, "/b");
1992   g_ptr_array_add (argv, NULL);
1993
1994   g_spawn_sync (NULL, (char **) argv->pdata, envp,
1995                 G_SPAWN_STDERR_TO_DEV_NULL,
1996                 NULL, NULL, &output, NULL, &status,
1997                 &error);
1998   g_assert_no_error (error);
1999   g_assert_null (strstr (output, "TAP version " TAP_VERSION));
2000   g_assert_null( strstr (output, "\n# Subtest: "));
2001   g_assert_nonnull (strstr (output,
2002     "\n" TAP_SUBTEST_PREFIX "ok 1 /c/a\n"));
2003   g_assert_nonnull (strstr (output,
2004     "\n" TAP_SUBTEST_PREFIX "ok 2 /c/a\n"));
2005   g_assert_nonnull (strstr (output,
2006     "\n" TAP_SUBTEST_PREFIX "ok 3 /b\n"));
2007   g_assert_nonnull (strstr (output,
2008     "\n" TAP_SUBTEST_PREFIX "ok 4 /b/a\n"));
2009   g_assert_nonnull (strstr (output,
2010     "\n" TAP_SUBTEST_PREFIX "ok 5 /b/b\n"));
2011   g_assert_nonnull (strstr (output,
2012     "\n" TAP_SUBTEST_PREFIX "1..5\n"));
2013
2014   g_spawn_check_wait_status (status, &error);
2015   g_assert_no_error (error);
2016
2017   g_free (output);
2018   g_ptr_array_unref (argv);
2019
2020   g_test_message ("--run-prefix");
2021   argv = g_ptr_array_new ();
2022   g_ptr_array_add (argv, (char *) testing_helper);
2023   g_ptr_array_add (argv, "skip-options");
2024   g_ptr_array_add (argv, "--tap");
2025   g_ptr_array_add (argv, "-r");
2026   g_ptr_array_add (argv, "/c/a");
2027   g_ptr_array_add (argv, "-r");
2028   g_ptr_array_add (argv, "/c/a");
2029   g_ptr_array_add (argv, "--run-prefix");
2030   g_ptr_array_add (argv, "/b");
2031   g_ptr_array_add (argv, NULL);
2032
2033   g_spawn_sync (NULL, (char **) argv->pdata, envp,
2034                 G_SPAWN_STDERR_TO_DEV_NULL,
2035                 NULL, NULL, &output, NULL, &status,
2036                 &error);
2037   g_assert_no_error (error);
2038   g_assert_null (strstr (output, "TAP version " TAP_VERSION));
2039   g_assert_null( strstr (output, "\n# Subtest: "));
2040   g_assert_nonnull (strstr (output,
2041     "\n" TAP_SUBTEST_PREFIX "ok 1 /c/a\n"));
2042   g_assert_nonnull (strstr (output,
2043     "\n" TAP_SUBTEST_PREFIX "ok 2 /c/a\n"));
2044   g_assert_nonnull (strstr (output,
2045     "\n" TAP_SUBTEST_PREFIX "ok 3 /b\n"));
2046   g_assert_nonnull (strstr (output,
2047     "\n" TAP_SUBTEST_PREFIX "ok 4 /b/a\n"));
2048   g_assert_nonnull (strstr (output,
2049     "\n" TAP_SUBTEST_PREFIX "ok 5 /b/b\n"));
2050   g_assert_nonnull (strstr (output,
2051     "\n" TAP_SUBTEST_PREFIX "ok 6 /b/b/a\n"));
2052   g_assert_nonnull (strstr (output,
2053     "\n" TAP_SUBTEST_PREFIX "1..6\n"));
2054
2055   g_spawn_check_wait_status (status, &error);
2056   g_assert_no_error (error);
2057
2058   g_free (output);
2059   g_ptr_array_unref (argv);
2060
2061   g_test_message ("--run-prefix 2");
2062   argv = g_ptr_array_new ();
2063   g_ptr_array_add (argv, (char *) testing_helper);
2064   g_ptr_array_add (argv, "skip-options");
2065   g_ptr_array_add (argv, "--tap");
2066   g_ptr_array_add (argv, "-r");
2067   g_ptr_array_add (argv, "/pre");
2068   g_ptr_array_add (argv, "--run-prefix");
2069   g_ptr_array_add (argv, "/b/b");
2070   g_ptr_array_add (argv, NULL);
2071
2072   g_spawn_sync (NULL, (char **) argv->pdata, envp,
2073                 G_SPAWN_STDERR_TO_DEV_NULL,
2074                 NULL, NULL, &output, NULL, &status,
2075                 &error);
2076   g_assert_no_error (error);
2077   g_assert_null (strstr (output, "TAP version " TAP_VERSION));
2078   g_assert_null( strstr (output, "\n# Subtest: "));
2079   g_assert_nonnull (strstr (output,
2080     "\n" TAP_SUBTEST_PREFIX "ok 1 /b/b\n"));
2081   g_assert_nonnull (strstr (output,
2082     "\n" TAP_SUBTEST_PREFIX "ok 2 /b/b/a\n"));
2083   g_assert_nonnull (strstr (output,
2084     "\n" TAP_SUBTEST_PREFIX "1..2\n"));
2085
2086   g_spawn_check_wait_status (status, &error);
2087   g_assert_no_error (error);
2088
2089   g_free (output);
2090   g_ptr_array_unref (argv);
2091
2092   g_test_message ("--run-prefix conflict");
2093   argv = g_ptr_array_new ();
2094   g_ptr_array_add (argv, (char *) testing_helper);
2095   g_ptr_array_add (argv, "skip-options");
2096   g_ptr_array_add (argv, "--tap");
2097   g_ptr_array_add (argv, "-r");
2098   g_ptr_array_add (argv, "/c/a");
2099   g_ptr_array_add (argv, "-p");
2100   g_ptr_array_add (argv, "/c/a");
2101   g_ptr_array_add (argv, "--run-prefix");
2102   g_ptr_array_add (argv, "/b");
2103   g_ptr_array_add (argv, NULL);
2104
2105   g_spawn_sync (NULL, (char **) argv->pdata, envp,
2106                 G_SPAWN_STDERR_TO_DEV_NULL,
2107                 NULL, NULL, &output, NULL, &status,
2108                 &error);
2109   g_assert_no_error (error);
2110   g_spawn_check_wait_status (status, &error);
2111   g_assert_nonnull (error);
2112   g_assert_null (strstr (output, "TAP version " TAP_VERSION));
2113   g_assert_null( strstr (output, "\n# Subtest: "));
2114   g_assert_nonnull (strstr (output, "do not mix [-r | --run-prefix] with '-p'\n"));
2115   g_clear_error (&error);
2116
2117   g_free (output);
2118   g_ptr_array_unref (argv);
2119
2120   g_test_message ("-s");
2121   argv = g_ptr_array_new ();
2122   g_ptr_array_add (argv, (char *) testing_helper);
2123   g_ptr_array_add (argv, "skip-options");
2124   g_ptr_array_add (argv, "--tap");
2125   g_ptr_array_add (argv, "-s");
2126   g_ptr_array_add (argv, "/a");
2127   g_ptr_array_add (argv, "-s");
2128   g_ptr_array_add (argv, "/b");
2129   g_ptr_array_add (argv, "-s");
2130   g_ptr_array_add (argv, "/pre");
2131   g_ptr_array_add (argv, "-s");
2132   g_ptr_array_add (argv, "/c/a");
2133   g_ptr_array_add (argv, NULL);
2134
2135   g_spawn_sync (NULL, (char **) argv->pdata, envp,
2136                 G_SPAWN_STDERR_TO_DEV_NULL,
2137                 NULL, NULL, &output, NULL, &status,
2138                 &error);
2139   g_assert_no_error (error);
2140   g_assert_null (strstr (output, "TAP version " TAP_VERSION));
2141   g_assert_null( strstr (output, "\n# Subtest: "));
2142   g_assert_nonnull (strstr (output, TAP_SUBTEST_PREFIX "1..10\n"));
2143   g_assert_nonnull (strstr (output,
2144     "\n" TAP_SUBTEST_PREFIX "ok 1 /a # SKIP by request"));
2145   g_assert_nonnull (strstr (output,
2146     "\n" TAP_SUBTEST_PREFIX "ok 2 /b # SKIP by request"));
2147   /* "-s /b" would skip a test named exactly /b, but not a test named
2148    * /b/anything */
2149   g_assert_nonnull (strstr (output,
2150     "\n" TAP_SUBTEST_PREFIX "ok 3 /b/a\n"));
2151   g_assert_nonnull (strstr (output,
2152     "\n" TAP_SUBTEST_PREFIX "ok 4 /b/b\n"));
2153   g_assert_nonnull (strstr (output,
2154     "\n" TAP_SUBTEST_PREFIX "ok 5 /b/b/a\n"));
2155   g_assert_nonnull (strstr (output,
2156     "\n" TAP_SUBTEST_PREFIX "ok 6 /prefix/a\n"));
2157   g_assert_nonnull (strstr (output,
2158     "\n" TAP_SUBTEST_PREFIX "ok 7 /prefix/b/b\n"));
2159   g_assert_nonnull (strstr (output,
2160     "\n" TAP_SUBTEST_PREFIX "ok 8 /prefix-long/a\n"));
2161   g_assert_nonnull (strstr (output,
2162     "\n" TAP_SUBTEST_PREFIX "ok 9 /c/a # SKIP by request"));
2163   g_assert_nonnull (strstr (output,
2164     "\n" TAP_SUBTEST_PREFIX "ok 10 /d/a\n"));
2165
2166   g_spawn_check_wait_status (status, &error);
2167   g_assert_no_error (error);
2168
2169   g_free (output);
2170   g_ptr_array_unref (argv);
2171
2172   g_test_message ("--skip-prefix");
2173   argv = g_ptr_array_new ();
2174   g_ptr_array_add (argv, (char *) testing_helper);
2175   g_ptr_array_add (argv, "skip-options");
2176   g_ptr_array_add (argv, "--tap");
2177   g_ptr_array_add (argv, "-x");
2178   g_ptr_array_add (argv, "/a");
2179   g_ptr_array_add (argv, "--skip-prefix");
2180   g_ptr_array_add (argv, "/pre");
2181   g_ptr_array_add (argv, "-x");
2182   g_ptr_array_add (argv, "/c/a");
2183   g_ptr_array_add (argv, NULL);
2184
2185   g_spawn_sync (NULL, (char **) argv->pdata, envp,
2186                 G_SPAWN_STDERR_TO_DEV_NULL,
2187                 NULL, NULL, &output, NULL, &status,
2188                 &error);
2189   g_assert_no_error (error);
2190   g_assert_null (strstr (output, "TAP version " TAP_VERSION));
2191   g_assert_true (g_str_has_prefix (output, "# Subtest: "));
2192   g_assert_nonnull (strstr (output, TAP_SUBTEST_PREFIX "1..10\n"));
2193   g_assert_nonnull (strstr (output,
2194     "\n" TAP_SUBTEST_PREFIX "ok 1 /a # SKIP by request"));
2195   g_assert_nonnull (strstr (output,
2196     "\n" TAP_SUBTEST_PREFIX "ok 2 /b\n"));
2197   g_assert_nonnull (strstr (output,
2198     "\n" TAP_SUBTEST_PREFIX "ok 3 /b/a\n"));
2199   g_assert_nonnull (strstr (output,
2200     "\n" TAP_SUBTEST_PREFIX "ok 4 /b/b\n"));
2201   g_assert_nonnull (strstr (output,
2202     "\n" TAP_SUBTEST_PREFIX "ok 5 /b/b/a\n"));
2203   /* "--skip-prefix /pre" will skip all test path which begins with /pre */
2204   g_assert_nonnull (strstr (output,
2205     "\n" TAP_SUBTEST_PREFIX "ok 6 /prefix/a # SKIP by request"));
2206   g_assert_nonnull (strstr (output,
2207     "\n" TAP_SUBTEST_PREFIX "ok 7 /prefix/b/b # SKIP by request"));
2208   g_assert_nonnull (strstr (output,
2209     "\n" TAP_SUBTEST_PREFIX "ok 8 /prefix-long/a # SKIP by request"));
2210   g_assert_nonnull (strstr (output,
2211     "\n" TAP_SUBTEST_PREFIX "ok 9 /c/a # SKIP by request"));
2212   g_assert_nonnull (strstr (output,
2213     "\n" TAP_SUBTEST_PREFIX "ok 10 /d/a\n"));
2214
2215   g_spawn_check_wait_status (status, &error);
2216   g_assert_no_error (error);
2217
2218   g_free (output);
2219   g_ptr_array_unref (argv);
2220 }
2221
2222 static void
2223 test_tap_summary (void)
2224 {
2225   const char *testing_helper;
2226   GPtrArray *argv;
2227   GError *error = NULL;
2228   int status;
2229   gchar *output;
2230   char **envp;
2231
2232   g_test_summary ("Test the output of g_test_summary() from the TAP output of a test.");
2233
2234   testing_helper = g_test_get_filename (G_TEST_BUILT, "testing-helper" EXEEXT, NULL);
2235
2236   argv = g_ptr_array_new ();
2237   g_ptr_array_add (argv, (char *) testing_helper);
2238   g_ptr_array_add (argv, "summary");
2239   g_ptr_array_add (argv, "--tap");
2240   g_ptr_array_add (argv, NULL);
2241
2242   /* Remove the G_TEST_ROOT_PROCESS env so it will be considered a standalone test */
2243   envp = g_get_environ ();
2244   g_assert_nonnull (g_environ_getenv (envp, "G_TEST_ROOT_PROCESS"));
2245   envp = g_environ_unsetenv (g_steal_pointer (&envp), "G_TEST_ROOT_PROCESS");
2246
2247   g_spawn_sync (NULL, (char **) argv->pdata, envp,
2248                 G_SPAWN_STDERR_TO_DEV_NULL,
2249                 NULL, NULL, &output, NULL, &status,
2250                 &error);
2251   g_assert_no_error (error);
2252
2253   g_spawn_check_wait_status (status, &error);
2254   g_assert_no_error (error);
2255   g_assert_null (strstr (output, "# Subtest: "));
2256
2257   /* Note: The test path in the output is not `/tap/summary` because it’s the
2258    * test path from testing-helper, not from this function. */g_assert_null (strstr (output, "# Subtest: "));
2259   g_assert_nonnull (strstr (output, "\n# /summary summary: Tests that g_test_summary() "
2260                                     "works with TAP, by outputting a known "
2261                                     "summary message in testing-helper, and "
2262                                     "checking for it in the TAP output later.\n"));
2263   g_free (output);
2264   g_ptr_array_unref (argv);
2265   g_strfreev (envp);
2266 }
2267
2268 static void
2269 test_tap_subtest_summary (void)
2270 {
2271   const char *testing_helper;
2272   GPtrArray *argv;
2273   GError *error = NULL;
2274   int status;
2275   gchar *output;
2276
2277   g_test_summary ("Test the output of g_test_summary() from the TAP output of a test.");
2278
2279   testing_helper = g_test_get_filename (G_TEST_BUILT, "testing-helper" EXEEXT, NULL);
2280
2281   argv = g_ptr_array_new ();
2282   g_ptr_array_add (argv, (char *) testing_helper);
2283   g_ptr_array_add (argv, "summary");
2284   g_ptr_array_add (argv, "--tap");
2285   g_ptr_array_add (argv, NULL);
2286
2287   g_spawn_sync (NULL, (char **) argv->pdata, NULL,
2288                 G_SPAWN_STDERR_TO_DEV_NULL,
2289                 NULL, NULL, &output, NULL, &status,
2290                 &error);
2291   g_assert_no_error (error);
2292
2293   g_spawn_check_wait_status (status, &error);
2294   g_assert_no_error (error);
2295   /* Note: The test path in the output is not `/tap/summary` because it’s the
2296    * test path from testing-helper, not from this function. */
2297   g_assert_true (g_str_has_prefix (output, "# Subtest: "));
2298   g_assert_nonnull (strstr (output,
2299     "\n" TAP_SUBTEST_PREFIX
2300     "# /summary summary: Tests that g_test_summary() "
2301     "works with TAP, by outputting a known "
2302     "summary message in testing-helper, and "
2303     "checking for it in the TAP output later.\n"));
2304   g_free (output);
2305   g_ptr_array_unref (argv);
2306 }
2307
2308 static void
2309 test_tap_message (void)
2310 {
2311   const char *testing_helper;
2312   GPtrArray *argv;
2313   GError *error = NULL;
2314   int status;
2315   gchar *output;
2316   char **output_lines;
2317   char **envp;
2318
2319   g_test_summary ("Test the output of g_test_message() from the TAP output of a test.");
2320
2321   testing_helper = g_test_get_filename (G_TEST_BUILT, "testing-helper" EXEEXT, NULL);
2322
2323   argv = g_ptr_array_new ();
2324   g_ptr_array_add (argv, (char *) testing_helper);
2325   g_ptr_array_add (argv, "message");
2326   g_ptr_array_add (argv, "--tap");
2327   g_ptr_array_add (argv, NULL);
2328
2329   /* Remove the G_TEST_ROOT_PROCESS env so it will be considered a standalone test */
2330   envp = g_get_environ ();
2331   g_assert_nonnull (g_environ_getenv (envp, "G_TEST_ROOT_PROCESS"));
2332   envp = g_environ_unsetenv (g_steal_pointer (&envp), "G_TEST_ROOT_PROCESS");
2333
2334   g_spawn_sync (NULL, (char **) argv->pdata, envp,
2335                 G_SPAWN_STDERR_TO_DEV_NULL,
2336                 NULL, NULL, &output, NULL, &status,
2337                 &error);
2338   g_assert_no_error (error);
2339
2340   g_spawn_check_wait_status (status, &error);
2341   g_assert_no_error (error);
2342
2343   g_assert_null (strstr (output, "# Subtest: "));
2344
2345   const char *expected_tap_header = "\n1..1\n";
2346   const char *interesting_lines = strstr (output, expected_tap_header);
2347   g_assert_nonnull (interesting_lines);
2348   interesting_lines += strlen (expected_tap_header);
2349
2350   output_lines = g_strsplit (interesting_lines, "\n", -1);
2351   g_assert_cmpuint (g_strv_length (output_lines), >=, 12);
2352
2353   guint i = 0;
2354   g_assert_cmpstr (output_lines[i++], ==, "# Tests that single line message works");
2355   g_assert_cmpstr (output_lines[i++], ==, "# Tests that multi");
2356   g_assert_cmpstr (output_lines[i++], ==, "# ");
2357   g_assert_cmpstr (output_lines[i++], ==, "# line");
2358   g_assert_cmpstr (output_lines[i++], ==, "# message");
2359   g_assert_cmpstr (output_lines[i++], ==, "# works");
2360   g_assert_cmpstr (output_lines[i++], ==, "# ");
2361   g_assert_cmpstr (output_lines[i++], ==, "# Tests that multi");
2362   g_assert_cmpstr (output_lines[i++], ==, "# line");
2363   g_assert_cmpstr (output_lines[i++], ==, "# message");
2364   g_assert_cmpstr (output_lines[i++], ==, "# works with leading and trailing too");
2365   g_assert_cmpstr (output_lines[i++], ==, "# ");
2366
2367   g_free (output);
2368   g_strfreev (output_lines);
2369   g_strfreev (envp);
2370   g_ptr_array_unref (argv);
2371 }
2372
2373 static void
2374 test_tap_subtest_message (void)
2375 {
2376   const char *testing_helper;
2377   GPtrArray *argv;
2378   GError *error = NULL;
2379   int status;
2380   gchar *output;
2381   char **output_lines;
2382
2383   g_test_summary ("Test the output of g_test_message() from the TAP output of a sub-test.");
2384
2385   testing_helper = g_test_get_filename (G_TEST_BUILT, "testing-helper" EXEEXT, NULL);
2386
2387   argv = g_ptr_array_new ();
2388   g_ptr_array_add (argv, (char *) testing_helper);
2389   g_ptr_array_add (argv, "message");
2390   g_ptr_array_add (argv, "--tap");
2391   g_ptr_array_add (argv, NULL);
2392
2393   g_spawn_sync (NULL, (char **) argv->pdata, NULL,
2394                 G_SPAWN_STDERR_TO_DEV_NULL,
2395                 NULL, NULL, &output, NULL, &status,
2396                 &error);
2397   g_assert_no_error (error);
2398
2399   g_spawn_check_wait_status (status, &error);
2400   g_assert_no_error (error);
2401
2402   g_assert_true (g_str_has_prefix (output, "# Subtest: "));
2403
2404   const char *expected_tap_header = "\n" TAP_SUBTEST_PREFIX "1..1\n";
2405   const char *interesting_lines = strstr (output, expected_tap_header);
2406   g_assert_nonnull (interesting_lines);
2407   interesting_lines += strlen (expected_tap_header);
2408
2409   output_lines = g_strsplit (interesting_lines, "\n", -1);
2410   g_assert_cmpuint (g_strv_length (output_lines), >=, 12);
2411
2412   guint i = 0;
2413   g_assert_cmpstr (output_lines[i++], ==, TAP_SUBTEST_PREFIX "# Tests that single line message works");
2414   g_assert_cmpstr (output_lines[i++], ==, TAP_SUBTEST_PREFIX "# Tests that multi");
2415   g_assert_cmpstr (output_lines[i++], ==, TAP_SUBTEST_PREFIX "# ");
2416   g_assert_cmpstr (output_lines[i++], ==, TAP_SUBTEST_PREFIX "# line");
2417   g_assert_cmpstr (output_lines[i++], ==, TAP_SUBTEST_PREFIX "# message");
2418   g_assert_cmpstr (output_lines[i++], ==, TAP_SUBTEST_PREFIX "# works");
2419   g_assert_cmpstr (output_lines[i++], ==, TAP_SUBTEST_PREFIX "# ");
2420   g_assert_cmpstr (output_lines[i++], ==, TAP_SUBTEST_PREFIX "# Tests that multi");
2421   g_assert_cmpstr (output_lines[i++], ==, TAP_SUBTEST_PREFIX "# line");
2422   g_assert_cmpstr (output_lines[i++], ==, TAP_SUBTEST_PREFIX "# message");
2423   g_assert_cmpstr (output_lines[i++], ==, TAP_SUBTEST_PREFIX "# works with leading and trailing too");
2424   g_assert_cmpstr (output_lines[i++], ==, TAP_SUBTEST_PREFIX "# ");
2425
2426   g_free (output);
2427   g_strfreev (output_lines);
2428   g_ptr_array_unref (argv);
2429 }
2430
2431 static void
2432 test_tap_print (void)
2433 {
2434   const char *testing_helper;
2435   GPtrArray *argv;
2436   GError *error = NULL;
2437   int status;
2438   gchar *output;
2439   char **output_lines;
2440   char **envp;
2441
2442   g_test_summary ("Test the output of g_print() from the TAP output of a test.");
2443
2444   testing_helper = g_test_get_filename (G_TEST_BUILT, "testing-helper" EXEEXT, NULL);
2445
2446   argv = g_ptr_array_new ();
2447   g_ptr_array_add (argv, (char *) testing_helper);
2448   g_ptr_array_add (argv, "print");
2449   g_ptr_array_add (argv, "--tap");
2450   g_ptr_array_add (argv, NULL);
2451
2452   /* Remove the G_TEST_ROOT_PROCESS env so it will be considered a standalone test */
2453   envp = g_get_environ ();
2454   g_assert_nonnull (g_environ_getenv (envp, "G_TEST_ROOT_PROCESS"));
2455   envp = g_environ_unsetenv (g_steal_pointer (&envp), "G_TEST_ROOT_PROCESS");
2456
2457   g_spawn_sync (NULL, (char **) argv->pdata, envp,
2458                 G_SPAWN_STDERR_TO_DEV_NULL,
2459                 NULL, NULL, &output, NULL, &status,
2460                 &error);
2461   g_assert_no_error (error);
2462
2463   g_spawn_check_wait_status (status, &error);
2464   g_assert_no_error (error);
2465
2466   const char *expected_tap_header = "\n1..1\n";
2467   const char *interesting_lines = strstr (output, expected_tap_header);
2468   g_assert_nonnull (interesting_lines);
2469   interesting_lines += strlen (expected_tap_header);
2470
2471   output_lines = g_strsplit (interesting_lines, "\n", -1);
2472   g_assert_cmpuint (g_strv_length (output_lines), >=, 3);
2473
2474   guint i = 0;
2475   g_assert_cmpstr (output_lines[i++], ==, "# Tests that single line message works");
2476   g_assert_cmpstr (output_lines[i++], ==, "# test that multiple");
2477   g_assert_cmpstr (output_lines[i++], ==, "# lines can be written separately");
2478
2479   g_free (output);
2480   g_strfreev (envp);
2481   g_strfreev (output_lines);
2482   g_ptr_array_unref (argv);
2483 }
2484
2485 static void
2486 test_tap_subtest_print (void)
2487 {
2488   const char *testing_helper;
2489   GPtrArray *argv;
2490   GError *error = NULL;
2491   int status;
2492   gchar *output;
2493   char **output_lines;
2494
2495   g_test_summary ("Test the output of g_test_print() from the TAP output of a sub-test.");
2496
2497   testing_helper = g_test_get_filename (G_TEST_BUILT, "testing-helper" EXEEXT, NULL);
2498
2499   argv = g_ptr_array_new ();
2500   g_ptr_array_add (argv, (char *) testing_helper);
2501   g_ptr_array_add (argv, "print");
2502   g_ptr_array_add (argv, "--tap");
2503   g_ptr_array_add (argv, NULL);
2504
2505   g_spawn_sync (NULL, (char **) argv->pdata, NULL,
2506                 G_SPAWN_STDERR_TO_DEV_NULL,
2507                 NULL, NULL, &output, NULL, &status,
2508                 &error);
2509   g_assert_no_error (error);
2510
2511   g_spawn_check_wait_status (status, &error);
2512   g_assert_no_error (error);
2513
2514   const char *expected_tap_header = "\n" TAP_SUBTEST_PREFIX "1..1\n";
2515   const char *interesting_lines = strstr (output, expected_tap_header);
2516   g_assert_nonnull (interesting_lines);
2517   interesting_lines += strlen (expected_tap_header);
2518
2519   output_lines = g_strsplit (interesting_lines, "\n", -1);
2520   g_assert_cmpuint (g_strv_length (output_lines), >=, 3);
2521
2522   guint i = 0;
2523   g_assert_cmpstr (output_lines[i++], ==, TAP_SUBTEST_PREFIX "# Tests that single line message works");
2524   g_assert_cmpstr (output_lines[i++], ==, TAP_SUBTEST_PREFIX "# test that multiple");
2525   g_assert_cmpstr (output_lines[i++], ==, TAP_SUBTEST_PREFIX "# lines can be written separately");
2526
2527   g_free (output);
2528   g_strfreev (output_lines);
2529   g_ptr_array_unref (argv);
2530 }
2531
2532 static void
2533 test_tap_subtest_stdout (void)
2534 {
2535   const char *testing_helper;
2536   GPtrArray *argv;
2537   GError *error = NULL;
2538   int status;
2539   gchar *output;
2540   char **output_lines;
2541
2542   g_test_summary ("Test the stdout from the TAP output of a sub-test.");
2543
2544   testing_helper = g_test_get_filename (G_TEST_BUILT, "testing-helper" EXEEXT, NULL);
2545
2546   argv = g_ptr_array_new ();
2547   g_ptr_array_add (argv, (char *) testing_helper);
2548   g_ptr_array_add (argv, "subprocess-stdout");
2549   g_ptr_array_add (argv, "--tap");
2550   g_ptr_array_add (argv, NULL);
2551
2552   g_spawn_sync (NULL, (char **) argv->pdata, NULL,
2553                 G_SPAWN_STDERR_TO_DEV_NULL,
2554                 NULL, NULL, &output, NULL, &status,
2555                 &error);
2556   g_assert_no_error (error);
2557
2558   g_spawn_check_wait_status (status, &error);
2559   g_assert_no_error (error);
2560
2561   const char *expected_tap_header = "\n" TAP_SUBTEST_PREFIX "1..1\n";
2562   const char *interesting_lines = strstr (output, expected_tap_header);
2563   g_assert_nonnull (interesting_lines);
2564
2565   interesting_lines = strstr (interesting_lines, TAP_SUBTEST_PREFIX "# /sub-stdout");
2566   g_assert_nonnull (interesting_lines);
2567
2568   output_lines = g_strsplit (interesting_lines, "\n", -1);
2569   g_assert_cmpuint (g_strv_length (output_lines), >=, 5);
2570
2571   guint i = 0;
2572   g_assert_cmpstr (output_lines[i++], ==,
2573                    TAP_SUBTEST_PREFIX "# /sub-stdout: Tests that single line message works");
2574   g_assert_cmpstr (output_lines[i++], ==,
2575                    TAP_SUBTEST_PREFIX "# test that multiple");
2576   g_assert_cmpstr (output_lines[i++], ==,
2577                    TAP_SUBTEST_PREFIX "# lines can be written separately");
2578   g_assert_cmpstr (output_lines[i++], ==,
2579                    TAP_SUBTEST_PREFIX "# And another line has been put");
2580   g_assert_cmpstr (output_lines[i++], ==,
2581                    TAP_SUBTEST_PREFIX "ok 1 /sub-stdout");
2582
2583   g_free (output);
2584   g_strfreev (output_lines);
2585   g_ptr_array_unref (argv);
2586 }
2587
2588 static void
2589 test_tap_subtest_stdout_no_new_line (void)
2590 {
2591   const char *testing_helper;
2592   GPtrArray *argv;
2593   GError *error = NULL;
2594   int status;
2595   gchar *output;
2596   char **output_lines;
2597
2598   g_test_summary ("Test the stdout from the TAP output of a sub-test.");
2599
2600   testing_helper = g_test_get_filename (G_TEST_BUILT, "testing-helper" EXEEXT, NULL);
2601
2602   argv = g_ptr_array_new ();
2603   g_ptr_array_add (argv, (char *) testing_helper);
2604   g_ptr_array_add (argv, "subprocess-stdout-no-nl");
2605   g_ptr_array_add (argv, "--tap");
2606   g_ptr_array_add (argv, NULL);
2607
2608   g_spawn_sync (NULL, (char **) argv->pdata, NULL,
2609                 G_SPAWN_STDERR_TO_DEV_NULL,
2610                 NULL, NULL, &output, NULL, &status,
2611                 &error);
2612   g_assert_no_error (error);
2613
2614   g_spawn_check_wait_status (status, &error);
2615   g_assert_no_error (error);
2616
2617   const char *expected_tap_header = "\n" TAP_SUBTEST_PREFIX "1..1\n";
2618   const char *interesting_lines = strstr (output, expected_tap_header);
2619   g_assert_nonnull (interesting_lines);
2620
2621   interesting_lines = strstr (interesting_lines, TAP_SUBTEST_PREFIX "# /sub-stdout-no-nl");
2622   g_assert_nonnull (interesting_lines);
2623
2624   output_lines = g_strsplit (interesting_lines, "\n", -1);
2625   g_assert_cmpuint (g_strv_length (output_lines), >=, 2);
2626
2627   guint i = 0;
2628   g_assert_cmpstr (output_lines[i++], ==,
2629                    TAP_SUBTEST_PREFIX "# /sub-stdout-no-nl: A message without trailing new line");
2630   g_assert_cmpstr (output_lines[i++], ==,
2631                    TAP_SUBTEST_PREFIX "ok 1 /sub-stdout-no-nl");
2632
2633   g_free (output);
2634   g_strfreev (output_lines);
2635   g_ptr_array_unref (argv);
2636 }
2637
2638 static void
2639 test_tap_error (void)
2640 {
2641   const char *testing_helper;
2642   GPtrArray *argv;
2643   GError *error = NULL;
2644   int status;
2645   gchar *output;
2646   char **envp;
2647
2648   g_test_summary ("Test that g_error() generates Bail out TAP output of a test.");
2649
2650   testing_helper = g_test_get_filename (G_TEST_BUILT, "testing-helper" EXEEXT, NULL);
2651
2652   argv = g_ptr_array_new ();
2653   g_ptr_array_add (argv, (char *) testing_helper);
2654   g_ptr_array_add (argv, "error");
2655   g_ptr_array_add (argv, "--tap");
2656   g_ptr_array_add (argv, NULL);
2657
2658   /* Remove the G_TEST_ROOT_PROCESS env so it will be considered a standalone test */
2659   envp = g_get_environ ();
2660   g_assert_nonnull (g_environ_getenv (envp, "G_TEST_ROOT_PROCESS"));
2661   envp = g_environ_unsetenv (g_steal_pointer (&envp), "G_TEST_ROOT_PROCESS");
2662
2663   g_spawn_sync (NULL, (char **) argv->pdata, envp,
2664                 G_SPAWN_STDERR_TO_DEV_NULL,
2665                 NULL, NULL, &output, NULL, &status,
2666                 &error);
2667   g_assert_no_error (error);
2668
2669   g_spawn_check_wait_status (status, &error);
2670   g_assert_nonnull (error);
2671
2672   g_assert_false (g_str_has_prefix (output, "# Subtest: "));
2673
2674   const char *expected_tap_header = "\n1..1\n";
2675   const char *interesting_lines = strstr (output, expected_tap_header);
2676   g_assert_nonnull (interesting_lines);
2677   interesting_lines += strlen (expected_tap_header);
2678
2679   g_assert_cmpstr (interesting_lines, ==, "not ok /error - GLib-FATAL-ERROR: This should error out "
2680                    "Because it's just wrong!\n"
2681                    "Bail out!\n");
2682
2683   g_free (output);
2684   g_strfreev (envp);
2685   g_ptr_array_unref (argv);
2686   g_clear_error (&error);
2687 }
2688
2689 static void
2690 test_tap_subtest_error (void)
2691 {
2692   const char *testing_helper;
2693   GPtrArray *argv;
2694   GError *error = NULL;
2695   int status;
2696   gchar *output;
2697
2698   g_test_summary ("Test that g_error() generates Bail out TAP output of a test.");
2699
2700   testing_helper = g_test_get_filename (G_TEST_BUILT, "testing-helper" EXEEXT, NULL);
2701
2702   argv = g_ptr_array_new ();
2703   g_ptr_array_add (argv, (char *) testing_helper);
2704   g_ptr_array_add (argv, "error");
2705   g_ptr_array_add (argv, "--tap");
2706   g_ptr_array_add (argv, NULL);
2707
2708   g_spawn_sync (NULL, (char **) argv->pdata, NULL,
2709                 G_SPAWN_STDERR_TO_DEV_NULL,
2710                 NULL, NULL, &output, NULL, &status,
2711                 &error);
2712   g_assert_no_error (error);
2713
2714   g_spawn_check_wait_status (status, &error);
2715   g_assert_nonnull (error);
2716
2717   g_assert_true (g_str_has_prefix (output, "# Subtest: "));
2718
2719   const char *expected_tap_header = "\n" TAP_SUBTEST_PREFIX "1..1\n";
2720   const char *interesting_lines = strstr (output, expected_tap_header);
2721   g_assert_nonnull (interesting_lines);
2722   interesting_lines += strlen (expected_tap_header);
2723
2724   g_assert_cmpstr (interesting_lines, ==,
2725                    TAP_SUBTEST_PREFIX "not ok /error - GLib-FATAL-ERROR: This should error out "
2726                    "Because it's just wrong!\n"
2727                    TAP_SUBTEST_PREFIX "Bail out!\n");
2728
2729   g_free (output);
2730   g_ptr_array_unref (argv);
2731   g_clear_error (&error);
2732 }
2733
2734 static void
2735 test_tap_error_and_pass (void)
2736 {
2737   const char *testing_helper;
2738   GPtrArray *argv;
2739   GError *error = NULL;
2740   int status;
2741   gchar *output;
2742   char **envp;
2743
2744   g_test_summary ("Test that g_error() generates Bail out TAP output of a test.");
2745
2746   testing_helper = g_test_get_filename (G_TEST_BUILT, "testing-helper" EXEEXT, NULL);
2747
2748   /* Remove the G_TEST_ROOT_PROCESS env so it will be considered a standalone test */
2749   envp = g_get_environ ();
2750   g_assert_nonnull (g_environ_getenv (envp, "G_TEST_ROOT_PROCESS"));
2751   envp = g_environ_unsetenv (g_steal_pointer (&envp), "G_TEST_ROOT_PROCESS");
2752
2753   argv = g_ptr_array_new ();
2754   g_ptr_array_add (argv, (char *) testing_helper);
2755   g_ptr_array_add (argv, "error-and-pass");
2756   g_ptr_array_add (argv, "--tap");
2757   g_ptr_array_add (argv, NULL);
2758
2759   g_spawn_sync (NULL, (char **) argv->pdata, envp,
2760                 G_SPAWN_STDERR_TO_DEV_NULL,
2761                 NULL, NULL, &output, NULL, &status,
2762                 &error);
2763   g_assert_no_error (error);
2764
2765   g_spawn_check_wait_status (status, &error);
2766   g_assert_nonnull (error);
2767
2768   const char *expected_tap_header = "\n1..2\n";
2769   const char *interesting_lines = strstr (output, expected_tap_header);
2770   g_assert_nonnull (interesting_lines);
2771   interesting_lines += strlen (expected_tap_header);
2772
2773   g_assert_cmpstr (interesting_lines, ==, "not ok /error - GLib-FATAL-ERROR: This should error out "
2774                    "Because it's just wrong!\n"
2775                    "Bail out!\n");
2776
2777   g_free (output);
2778   g_strfreev (envp);
2779   g_ptr_array_unref (argv);
2780   g_clear_error (&error);
2781 }
2782
2783 static void
2784 test_tap_subtest_error_and_pass (void)
2785 {
2786   const char *testing_helper;
2787   GPtrArray *argv;
2788   GError *error = NULL;
2789   int status;
2790   gchar *output;
2791
2792   g_test_summary ("Test that g_error() generates Bail out TAP output of a test.");
2793
2794   testing_helper = g_test_get_filename (G_TEST_BUILT, "testing-helper" EXEEXT, NULL);
2795
2796   argv = g_ptr_array_new ();
2797   g_ptr_array_add (argv, (char *) testing_helper);
2798   g_ptr_array_add (argv, "error-and-pass");
2799   g_ptr_array_add (argv, "--tap");
2800   g_ptr_array_add (argv, NULL);
2801
2802   g_spawn_sync (NULL, (char **) argv->pdata, NULL,
2803                 G_SPAWN_STDERR_TO_DEV_NULL,
2804                 NULL, NULL, &output, NULL, &status,
2805                 &error);
2806   g_assert_no_error (error);
2807
2808   g_spawn_check_wait_status (status, &error);
2809   g_assert_nonnull (error);
2810
2811   g_assert_true (g_str_has_prefix (output, "# Subtest: "));
2812
2813   const char *expected_tap_header = "\n" TAP_SUBTEST_PREFIX "1..2\n";
2814   const char *interesting_lines = strstr (output, expected_tap_header);
2815   g_assert_nonnull (interesting_lines);
2816   interesting_lines += strlen (expected_tap_header);
2817
2818   g_assert_cmpstr (interesting_lines, ==,
2819                    TAP_SUBTEST_PREFIX "not ok /error - GLib-FATAL-ERROR: This should error out "
2820                    "Because it's just wrong!\n"
2821                    TAP_SUBTEST_PREFIX "Bail out!\n");
2822
2823   g_free (output);
2824   g_ptr_array_unref (argv);
2825   g_clear_error (&error);
2826 }
2827
2828 static void
2829 test_init_no_argv0 (void)
2830 {
2831   const char *testing_helper;
2832   GPtrArray *argv;
2833   GError *error = NULL;
2834   int status;
2835   gchar *output;
2836
2837   g_test_summary ("Test that g_test_init() can be called safely with argc == 0.");
2838
2839   testing_helper = g_test_get_filename (G_TEST_BUILT, "testing-helper" EXEEXT, NULL);
2840
2841   argv = g_ptr_array_new ();
2842   g_ptr_array_add (argv, (char *) testing_helper);
2843   g_ptr_array_add (argv, "init-null-argv0");
2844   g_ptr_array_add (argv, NULL);
2845
2846   /* This has to be spawned manually and can’t be run with g_test_subprocess()
2847    * because the test helper can’t be run after `g_test_init()` has been called
2848    * in the process. */
2849   g_spawn_sync (NULL, (char **) argv->pdata, NULL,
2850                 G_SPAWN_STDERR_TO_DEV_NULL,
2851                 NULL, NULL, &output, NULL, &status,
2852                 &error);
2853   g_assert_no_error (error);
2854
2855   g_spawn_check_wait_status (status, &error);
2856   g_assert_no_error (error);
2857   g_assert_nonnull (strstr (output, "# random seed:"));
2858   g_free (output);
2859   g_ptr_array_unref (argv);
2860 }
2861
2862 int
2863 main (int   argc,
2864       char *argv[])
2865 {
2866   int ret;
2867   char *filename, *filename2;
2868
2869   argv0 = argv[0];
2870
2871   setlocale (LC_ALL, "");
2872
2873   g_test_init (&argc, &argv, NULL);
2874
2875   /* Part of a test for
2876    * https://gitlab.gnome.org/GNOME/glib/-/issues/2563, see below */
2877   filename = g_test_build_filename (G_TEST_BUILT, "nonexistent", NULL);
2878
2879   g_test_add_func ("/random-generator/rand-1", test_rand1);
2880   g_test_add_func ("/random-generator/rand-2", test_rand2);
2881   g_test_add_func ("/random-generator/random-conversions", test_random_conversions);
2882   g_test_add_func ("/misc/assertions", test_assertions);
2883   g_test_add_func ("/misc/assertions/subprocess/bad_cmpvariant_types", test_assertions_bad_cmpvariant_types);
2884   g_test_add_func ("/misc/assertions/subprocess/bad_cmpvariant_values", test_assertions_bad_cmpvariant_values);
2885   g_test_add_func ("/misc/assertions/subprocess/bad_cmpstr", test_assertions_bad_cmpstr);
2886   g_test_add_func ("/misc/assertions/subprocess/bad_cmpstrv_null1", test_assertions_bad_cmpstrv_null1);
2887   g_test_add_func ("/misc/assertions/subprocess/bad_cmpstrv_null2", test_assertions_bad_cmpstrv_null2);
2888   g_test_add_func ("/misc/assertions/subprocess/bad_cmpstrv_length", test_assertions_bad_cmpstrv_length);
2889   g_test_add_func ("/misc/assertions/subprocess/bad_cmpstrv_values", test_assertions_bad_cmpstrv_values);
2890   g_test_add_func ("/misc/assertions/subprocess/bad_cmpint", test_assertions_bad_cmpint);
2891   g_test_add_func ("/misc/assertions/subprocess/bad_cmpmem_len", test_assertions_bad_cmpmem_len);
2892   g_test_add_func ("/misc/assertions/subprocess/bad_cmpmem_data", test_assertions_bad_cmpmem_data);
2893   g_test_add_func ("/misc/assertions/subprocess/bad_cmpmem_null", test_assertions_bad_cmpmem_null);
2894   g_test_add_func ("/misc/assertions/subprocess/bad_cmpfloat_epsilon", test_assertions_bad_cmpfloat_epsilon);
2895   g_test_add_func ("/misc/assertions/subprocess/bad_no_errno", test_assertions_bad_no_errno);
2896   g_test_add_data_func ("/misc/test-data", (void*) 0xc0c0baba, test_data_test);
2897   g_test_add ("/misc/primetoul", Fixturetest, (void*) 0xc0cac01a, fixturetest_setup, fixturetest_test, fixturetest_teardown);
2898   if (g_test_perf())
2899     g_test_add_func ("/misc/timer", test_timer);
2900
2901 #ifdef G_OS_UNIX
2902   g_test_add_func ("/forking/fail assertion", test_fork_fail);
2903   g_test_add_func ("/forking/patterns", test_fork_patterns);
2904   if (g_test_slow())
2905     g_test_add_func ("/forking/timeout", test_fork_timeout);
2906 #endif
2907
2908   g_test_add_func ("/trap_subprocess/fail", test_subprocess_fail);
2909   g_test_add_func ("/trap_subprocess/no-such-test", test_subprocess_no_such_test);
2910   if (g_test_slow ())
2911     g_test_add_func ("/trap_subprocess/timeout", test_subprocess_timeout);
2912
2913   g_test_add_func ("/trap_subprocess/patterns", test_subprocess_patterns);
2914
2915   g_test_add_func ("/misc/fatal-log-handler", test_fatal_log_handler);
2916   g_test_add_func ("/misc/fatal-log-handler/subprocess/critical-pass", test_fatal_log_handler_critical_pass);
2917   g_test_add_func ("/misc/fatal-log-handler/subprocess/error-fail", test_fatal_log_handler_error_fail);
2918   g_test_add_func ("/misc/fatal-log-handler/subprocess/critical-fail", test_fatal_log_handler_critical_fail);
2919
2920   g_test_add_func ("/misc/expected-messages", test_expected_messages);
2921   g_test_add_func ("/misc/expected-messages/subprocess/warning", test_expected_messages_warning);
2922   g_test_add_func ("/misc/expected-messages/subprocess/expect-warning", test_expected_messages_expect_warning);
2923   g_test_add_func ("/misc/expected-messages/subprocess/wrong-warning", test_expected_messages_wrong_warning);
2924   g_test_add_func ("/misc/expected-messages/subprocess/expected", test_expected_messages_expected);
2925   g_test_add_func ("/misc/expected-messages/subprocess/null-domain", test_expected_messages_null_domain);
2926   g_test_add_func ("/misc/expected-messages/subprocess/extra-warning", test_expected_messages_extra_warning);
2927   g_test_add_func ("/misc/expected-messages/subprocess/unexpected-extra-warning", test_expected_messages_unexpected_extra_warning);
2928   g_test_add_func ("/misc/expected-messages/expect-error", test_expected_messages_expect_error);
2929   g_test_add_func ("/misc/expected-messages/skip-debug", test_expected_messages_debug);
2930
2931   g_test_add_func ("/misc/messages", test_messages);
2932   g_test_add_func ("/misc/messages/subprocess/use-stderr", test_messages_use_stderr);
2933
2934   g_test_add_func ("/misc/dash-p", test_dash_p);
2935   g_test_add_func ("/misc/dash-p/child", test_dash_p_child);
2936   g_test_add_func ("/misc/dash-p/child/sub", test_dash_p_child_sub);
2937   g_test_add_func ("/misc/dash-p/child/sub/subprocess", test_dash_p_child_sub_child);
2938   g_test_add_func ("/misc/dash-p/child/sub/subprocess/child", test_dash_p_child_sub_child);
2939   g_test_add_func ("/misc/dash-p/child/sub2", test_dash_p_child_sub2);
2940   g_test_add_func ("/misc/dash-p/subprocess/hidden", test_dash_p_hidden);
2941   g_test_add_func ("/misc/dash-p/subprocess/hidden/sub", test_dash_p_hidden_sub);
2942
2943   g_test_add_func ("/misc/nonfatal", test_nonfatal);
2944
2945   g_test_add_func ("/misc/skip", test_skip);
2946   g_test_add_func ("/misc/combining", test_combining);
2947   g_test_add_func ("/misc/combining/subprocess/fail", subprocess_fail);
2948   g_test_add_func ("/misc/combining/subprocess/skip1", test_skip);
2949   g_test_add_func ("/misc/combining/subprocess/skip2", test_skip);
2950   g_test_add_func ("/misc/combining/subprocess/incomplete", subprocess_incomplete);
2951   g_test_add_func ("/misc/combining/subprocess/pass", test_pass);
2952   g_test_add_func ("/misc/fail", test_fail);
2953   g_test_add_func ("/misc/incomplete", test_incomplete);
2954   g_test_add_func ("/misc/timeout", test_subprocess_timed_out);
2955
2956   g_test_add_func ("/misc/path/first", test_path_first);
2957   g_test_add_func ("/misc/path/second", test_path_second);
2958
2959   g_test_add_func ("/tap", test_tap);
2960   g_test_add_func ("/tap/subtest", test_tap_subtest);
2961   g_test_add_func ("/tap/summary", test_tap_summary);
2962   g_test_add_func ("/tap/subtest/summary", test_tap_subtest_summary);
2963   g_test_add_func ("/tap/message", test_tap_message);
2964   g_test_add_func ("/tap/subtest/message", test_tap_subtest_message);
2965   g_test_add_func ("/tap/print", test_tap_print);
2966   g_test_add_func ("/tap/subtest/print", test_tap_subtest_print);
2967   g_test_add_func ("/tap/subtest/stdout", test_tap_subtest_stdout);
2968   g_test_add_func ("/tap/subtest/stdout-no-new-line", test_tap_subtest_stdout_no_new_line);
2969   g_test_add_func ("/tap/error", test_tap_error);
2970   g_test_add_func ("/tap/subtest/error", test_tap_subtest_error);
2971   g_test_add_func ("/tap/error-and-pass", test_tap_error_and_pass);
2972   g_test_add_func ("/tap/subtest/error-and-pass", test_tap_subtest_error_and_pass);
2973
2974   g_test_add_func ("/init/no_argv0", test_init_no_argv0);
2975
2976   ret = g_test_run ();
2977
2978   /* We can't test for https://gitlab.gnome.org/GNOME/glib/-/issues/2563
2979    * from a test-case, because the whole point of that issue is that it's
2980    * about whether certain patterns are valid after g_test_run() has
2981    * returned... so put an ad-hoc test here, and just crash if it fails. */
2982   filename2 = g_test_build_filename (G_TEST_BUILT, "nonexistent", NULL);
2983   g_assert_cmpstr (filename, ==, filename2);
2984
2985   g_free (filename);
2986   g_free (filename2);
2987   return ret;
2988 }