After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 720236 - gtestutils: Allow clean simple use of g_test_trap_subprocess()
gtestutils: Allow clean simple use of g_test_trap_subprocess()
Status: RESOLVED FIXED
Product: glib
Classification: Platform
Component: general
unspecified
Other All
: Normal normal
: ---
Assigned To: gtkdev
gtkdev
Depends on:
Blocks:
 
 
Reported: 2013-12-11 08:40 UTC by Stef Walter
Modified: 2014-01-15 14:49 UTC
See Also:
GNOME target: ---
GNOME version: ---


Attachments
gtestutils: Allow clean simple use of g_test_trap_subprocess() (5.17 KB, patch)
2013-12-11 08:40 UTC, Stef Walter
committed Details | Review

Description Stef Walter 2013-12-11 08:40:50 UTC
My biggest gripe about g_test_trap_subprocess() is how it requires
changes all over the test case C file to get working. A test case needs
to know it's own path, and additional tests need to be added with
appropriate names. 

The above makes sense for pretty advanced cases, but for bog simple
this-should-produce-a-critical-warning test, it's completely overkill.

Luckily we can use g_test_subprocess() to rerun a test case itself
in a subprocess. Like so:

  static void
  test_build_null_string (void)
  {
  	GHashTable *attributes;
  
  	if (g_test_subprocess ()) {
  		attributes = secret_attributes_build (&MOCK_SCHEMA,
  		                                      "number", 4,
  		                                      "string", NULL,
  		                                      "even", TRUE,
  		                                      NULL);
  		g_assert (attributes == NULL);
  		return;
  	}
  	
  	g_test_trap_subprocess ("/attributes/build-null-string", 0, 
	                        G_TEST_SUBPROCESS_INHERIT_STDOUT);
  	g_test_trap_assert_failed ();
  	g_test_trap_assert_stderr ("*attribute*NULL*");
  }

As you can see one complication remains ... a test case must know its
own path. This not only adds complexity, but prevents a single such
testcase function being used with multiple different fixtures, at
different paths.

So attached patch changes g_test_trap_subprocess to accept NULL
as it's test name, and it'll just run the same test in a subprocess.

Allows elegant use of subprocesses.

Updated documentation to remove complex example and replace with
simple one.
Comment 1 Stef Walter 2013-12-11 08:40:53 UTC
Created attachment 263966 [details] [review]
gtestutils: Allow clean simple use of g_test_trap_subprocess()

Allow g_test_trap_subprocess() to be used in a simple cases by
rerunning the same test case itself. This is accomplished by
passing %NULL as the test case name.
Comment 2 Matthias Clasen 2013-12-11 16:19:05 UTC
That is pretty elegant, indeed. I like it. Does rerunning tests recursively work just fine with our reporting ?
Comment 3 Matthias Clasen 2013-12-15 16:50:51 UTC
Attachment 263966 [details] pushed as cd2204b - gtestutils: Allow clean simple use of g_test_trap_subprocess()
Comment 4 Stef Walter 2014-01-15 14:49:05 UTC
Thanks Matthias.