Provide debug facility for general and testing

boombatower - August 18, 2008 - 02:26
Project:Drupal
Version:7.x-dev
Component:simpletest.module
Category:feature request
Priority:critical
Assigned:boombatower
Status:closed
Issue tags:Needs Documentation
Description

SimpleTest has gotten to the point where you can no longer simply use drupal_set_message to output debug information. This is partly due to batch API implementation.

I propose adding some helper functions for debugging, something simple.

Either a way to collect debug messages to display on results page (could just use assertions, but not a nice), or just a output console of sorts which dumbs messages to a file.

<?php
$this
->debug('debug information');
?>

Comments?

#1

boombatower - August 18, 2008 - 02:26
Title:Provide a way to output Debugging information in SimpleTest» Provide a way to output debug information in SimpleTest

#2

earnie - August 19, 2008 - 18:12

When I create my own debugging routines I output the debug info to watchdog. This allows me to capture quite a bit of information on and view it on a different window.

#3

boombatower - September 30, 2008 - 20:03

#4

boombatower - October 1, 2008 - 21:19
Title:Provide a way to output debug information in SimpleTest» Provide debug facility for functional testing

Per other issue focusing on unit test debugging facility.

I'll work on a patch.

#5

Damien Tournoud - October 2, 2008 - 18:20

Here is my current state of thoughts for tonight (copied from the other issue):

It would be useful to have additional data when test fails. Those include full page dump for all assertions that check stuff on the page:

Testing | #314112

From a code perspective, this is not too hard to do (the first step is to add an additional_data column to the test result table). But there is a big UI issue. Maybe we should postpone this until we made up our mind on the new test result page?

#6

boombatower - October 2, 2008 - 18:39
Status:postponed (maintainer needs more info)» postponed

I think postponing it would be best...I'll focus on the two UI patches...lets get something figured out.

#7

andreiashu - February 10, 2009 - 20:45

subscribing... is this going to be implemented ?

#8

Rob Loach - March 19, 2009 - 00:46
Status:postponed» active

#5: Dear god, yes.

#9

moshe weitzman - May 20, 2009 - 03:05

Yeah, #5 seems very useful still.

#10

moshe weitzman - June 22, 2009 - 16:58

@boombatower - i think we should unpostpone this. code freeze cometh. any thoughts?

#11

boombatower - June 23, 2009 - 21:16

#12

boombatower - June 23, 2009 - 21:16
Title:Provide debug facility for functional testing» Provide debug facility for testing

I have begun.

#13

boombatower - June 23, 2009 - 21:57

I think I will split this up into two patches:
1) Verbose mode
2) Debug output function.

This issue will focus on the latter this the first item is a new idea.

EDIT: Other issue created: #500280: Provide a verbose mode for SimpleTest

#14

boombatower - June 23, 2009 - 23:34

#15

moshe weitzman - July 3, 2009 - 05:44
Priority:normal» critical

crucial for getting contrib authors on board with testing.

#16

boombatower - July 9, 2009 - 21:04

It seems like this could take advantage of the framework put in place by #443154: Fatal errors in tests not reported as failures.

The patch pulls PHP fatal errors out of the log since it know nows what was the last database prefixed used. Could also pull things out of the prefixed database for debugging.

Something like watchdog() might even make sense:

<?php
watchdog
('simpletest', 'Debug info!');
?>

Probably best to provide SimpleTest function so people are aware of the functionality.

I'll work on this soon.

#17

boombatower - July 9, 2009 - 21:15

The biggest problem with providing a function is that on say code running due to a drupalGet() SimpleTest code is not loaded. So the function won't be available.

If we use watchdog() then at least the code is there, but that seems like something hard to document in code itself, considering not too many ppl may read documentation say at d.o/simpletest.

#18

Damien Tournoud - July 9, 2009 - 21:25

I believe we need a dsm() equivalent function in core. This function would:

* outside of simpletest, do what dsm() does
* in simpletest, simply throw a PHP warning, this warning will be caught by our error handler (from both the parent side and the client side), and could be displayed properly in the simpletest log.

#19

boombatower - July 9, 2009 - 21:32

I would have to agree, I like that solution the best. The question is how far to take it. Make a simple dpm() that does some sort or print_r()/var_export() like stuff (maybe even syntax highlight), and allow devel to override with krumo, or include the krumo stuff in core.

Since Drupal is largely a developer platform and has lots of things useful to developers (not to mention SimpleTest itself) it would seem to make sense to include something like this. Usually the only feature I use from devel.

#20

boombatower - July 13, 2009 - 22:01

Seems like there is a negative reactions to providing devel like functionality in core (from IRC) and I wasn't sure about it myself. So we won't go there.

I do think that providing a debug method in core that is very simple and work both during SimpleTest and regular context is the only clean, simple, and documentable way to do this.

The key is should it integrate with devel so that when enabled when you use it you get the pretty krumo output or should they use different function names. The advantage of integrating with devel is that the extra code in this function to allow it to work when using simpletest would not need to be duplicated in devel...and developers would only need to remember one debug function.

Integrating isn't necessarily simple/pretty. I don't like the idea of:

<?php
if (module_enabled('devel')) {
 
// use its stuff
}
else {
 
// core
}
?>

Seems like if we do implement such a function that is overrideable it should be done with a hook that registers debug functions. That starts to seem like overkill so them I am left thinking that we choose to not integrate or integrate only with devel.

Once I get a consensus and a name for the function I'll go ahead and write it.

#21

boombatower - July 13, 2009 - 22:02
Title:Provide debug facility for testing» Provide debug facility for general and testing

#22

boombatower - July 14, 2009 - 00:18

Just to get things off the ground, this is a very basic patch. No SimpleTest integration yet. drupal_register_debug_function() was DamZ's idea...so that solves our overrideable problem in a simple manor.

AttachmentSizeStatusTest resultOperations
296574-drupal-debug.patch1.33 KBIdleFailed: 11662 passes, 2 fails, 0 exceptionsView details

#23

boombatower - July 14, 2009 - 01:20

#24

boombatower - July 14, 2009 - 01:27
Status:active» needs review

This seems to do the trick.

AttachmentSizeStatusTest resultOperations
296574-drupal-debug.patch2.61 KBIdleFailed: 11662 passes, 2 fails, 0 exceptionsView details

#25

System Message - July 14, 2009 - 01:40
Status:needs review» needs work

The last submitted patch failed testing.

#26

boombatower - July 14, 2009 - 01:43
Status:needs work» needs review

I think this is a very simple base implementation that we can expand on.

What I would like to see is:

  • If page is being completed and about to be rendered dump all debug statements as drupal_set_message(). If a suppress variable is on then they won't be displayed. Krumo (devel) would then run here...need to cache variables in some format. Reason being if site crashes and all my debug statements are dpm() [krumo] then I have to change them to dd() to see anything...should just be automatic.
  • SimpleTest read the log file and place the debug statements in assertion list.
  • Possibly provide backtrace information.
  • Format message similar to PHP's messages. At least include file and line that debug was called in.
  • During verbose mode in SimpleTest read the log after every drupalGet/drupalPost so the messages are displayed in context. (same with fatal errors)

#27

boombatower - July 14, 2009 - 01:48

documentation.

AttachmentSizeStatusTest resultOperations
296574-drupal-debug.patch2.61 KBIdleFailed: 11662 passes, 2 fails, 0 exceptionsView details

#28

System Message - July 14, 2009 - 02:00
Status:needs review» needs work

The last submitted patch failed testing.

#29

boombatower - July 14, 2009 - 02:48

Can we remove that bugged test already??

#30

boombatower - July 14, 2009 - 21:45
Status:needs work» needs review

Part of that test has been removed. Lets see what happens.

#31

kwinters - July 17, 2009 - 19:42

Cross-referencing with possible exception handling change: #522746: Drupal Exception Wrapper Class

If a test throws an exception rather than just doing a watchdog and return or trigger_error, you could potentially pull additional info out of the DrupalException object.

#32

boombatower - July 17, 2009 - 20:05

I think that is separate, but interesting none the less.

#33

boombatower - July 23, 2009 - 01:32

#34

boombatower - July 31, 2009 - 01:00

Provides debug facility for general core use and the SimpleTest integration display debug messages in assertions table with group debug.

#500334: SimpleTest: color debug messages will color the messages so they are easy to distinguish.

The current method is insufficient since SimpleTest reader assumes each message in on a single line and that may not always be so. The question begged, is should they be inserted directly into SimpleTest assertions table or, should we store them in a more complex text format or database.

Eventually I see the general debug statements being displayed via drupal_set_message() in a similar manor to devel, but without krumo.

AttachmentSizeStatusTest resultOperations
296574-drupal-debug.patch4.04 KBIdlePassed: 12088 passes, 0 fails, 0 exceptionsView details

#35

boombatower - July 31, 2009 - 02:00

Much better format output with backtrace details, thus eliminating the multi-line issue above. I'll work on designing and implementing the drupal_set_message() portion for regular debugging.

Note: the screenshot has the debug color patch applied.
drupal debug

Code in block.test:

<?php
 
function testBox() {
   
// Add a new box by filling out the input form on the admin/structure/block/add page.
   
$box = array();
   
$box['info'] = $this->randomName(8);
   
$box['title'] = $this->randomName(8);
   
$box['body'] = $this->randomName(32);
   
$this->drupalPost('admin/structure/block/add', $box, t('Save block'));

   
dd('bar', 'foo');
   
drupal_debug('Some random test of data dump: ' . print_r(array('foo' => 'bar'), TRUE));
   
dd(array('foo' => 'bar'));
  }
?>

Code in block.admin.inc

<?php
function block_admin_display($theme = NULL) {
  global
$custom_theme;

 
// If non-default theme configuration has been selected, set the custom theme.
 
$custom_theme = isset($theme) ? $theme : variable_get('theme_default', 'garland');

 
// Fetch and sort blocks.
 
$blocks = _block_rehash();
 
usort($blocks, '_block_compare');

 
dd('not in test process!');

  return
drupal_get_form('block_admin_display_form', $blocks, $theme);
}
?>

This also demonstrates that the backtrace works properly when using the wrapper dd() or drupal_debug().

AttachmentSizeStatusTest resultOperations
296574-drupal-debug.patch4.9 KBIdlePassed: 12118 passes, 0 fails, 0 exceptionsView details
296574-debug.png24.84 KBIgnoredNoneNone

#36

boombatower - July 31, 2009 - 02:29

Documented and added drupal_set_message() when error_level is set to ERROR_REPORTING_DISPLAY_ALL. Since the default reporting level for Drupal 7 HEAD is ERROR_REPORTING_DISPLAY_ALL I used that as well, but that means it will need to be changed along with the all other defaults when a stable release is made. I am not sure if that needs to be documented somewhere or what.

EDIT: missed the SimpleTest functions when documenting...will fix.

AttachmentSizeStatusTest resultOperations
296574-drupal-debug.patch6.92 KBIdlePassed: 12088 passes, 0 fails, 0 exceptionsView details

#37

boombatower - July 31, 2009 - 02:35

Fully documented.

NOTE: This will break devel 7.x, so once this gets in we should place an issue in devel queue to use the register function to override drupal_debug() behavior. Devel should define devel_debug() or devel_drupal_debug() to be properly namespaced instead of dd() anyway.

AttachmentSizeStatusTest resultOperations
296574-drupal-debug.patch7.82 KBIdlePassed: 12118 passes, 0 fails, 0 exceptionsView details

#38

boombatower - August 3, 2009 - 20:41

After a very long conversation with chx in #drupal-dev we decided to take advantage of the error handling built into PHP through trigger_error() and simply provide a wrapper. The wrapper will server to provide documentation and dump the data passed to it so that dd() is still enough (short and sweet).

The debug messages are now concerted exception which we agreed will ensure that they are removed before committing. I will convert the verbose output to exceptions in a followup patch.

AttachmentSizeStatusTest resultOperations
296574-drupal-debug.patch3.2 KBIdlePassed: 12088 passes, 0 fails, 0 exceptionsView details

#39

boombatower - August 3, 2009 - 20:58

Further reduction, remove drupal_debug() and only provide dd().

AttachmentSizeStatusTest resultOperations
296574-drupal-debug.patch2.63 KBIdlePassed: 12088 passes, 0 fails, 0 exceptionsView details

#40

boombatower - August 3, 2009 - 21:07

More minification. :)

AttachmentSizeStatusTest resultOperations
296574-drupal-debug.patch2.56 KBIdlePassed: 12118 passes, 0 fails, 0 exceptionsView details

#41

chx - August 3, 2009 - 21:08
Status:needs review» reviewed & tested by the community

Now, that's better. Small patches++

#42

boombatower - August 3, 2009 - 21:13

#43

webchick - August 8, 2009 - 21:52
Status:reviewed & tested by the community» needs work

+++ modules/simpletest/drupal_web_test_case.php 3 Aug 2009 21:06:42 -0000
@@ -376,6 +376,11 @@
+    if ($group == 'User notice') {
+      // Instead of 'User notice' set by trigger_error(), use 'Debug'.
+      $group = 'Debug';
+    }

Why?

+++ includes/common.inc 3 Aug 2009 21:06:41 -0000
@@ -895,9 +895,11 @@
+  $blacklist = array('dd', 'drupal_debug');

I thought we removed drupal_debug? If so, we no longer need it here.

+++ includes/common.inc 3 Aug 2009 21:06:41 -0000
@@ -4847,3 +4849,24 @@
+ * Debug function used to outputting debug information.

/for/ outputting, probably.

Also, is it possible to write tests for this?

23 days to code freeze. Better review yourself.

#44

chx - August 9, 2009 - 04:39
Status:needs work» reviewed & tested by the community

Edited the patch file itself so it's good to go.

AttachmentSizeStatusTest resultOperations
296574-drupal-debug_8.patch2.55 KBIdlePassed: 12118 passes, 0 fails, 0 exceptionsView details

#45

webchick - August 9, 2009 - 05:19
Status:reviewed & tested by the community» needs work

Ok, some feedback after having tried this out.

1. Debug messages are currently coming up as exceptions. They shouldn't be. It muddles the results of the test:

Test result | localhost.png

2. When using dd() outside of the SimpleTest context, the results are pretty nasty:

Preview | localhost.png

It'd be nice if we could at least replace that "User notice" with "Debug" and use a neutral grey colour here as well.

3. Core's dd() does nothing like what Devel's dd() does. Yes, I know core takes precedence over contrib, and APIs change on a whim, etc. but it still seems like we're needlessly introducing a major developer WTF here. I'd suggest just using d() for debug, like we have t() for translate and l() for link. There are no core functions that I'm aware of that are two characters, except st() which is an alternate form of t(). Alternately, what the heck is wrong with just plain old "debug()"? :P

AttachmentSizeStatusTest resultOperations
Test result | localhost.png92.92 KBIgnoredNoneNone
Preview | localhost.png118.78 KBIgnoredNoneNone

#46

boombatower - August 12, 2009 - 23:25

The latter part about what it does outside of SimpleTest context is due to the changes made after #37. Chx and I changed it to use trigger_error() instead of my custom logging utility, but as webchick noted it doesn't work so well outside of the context. #37 should work like we want...perhaps we should try that out and we can minimize from there.

#47

boombatower - August 13, 2009 - 00:58
Status:needs work» needs review

Alright.

This should fix all outstanding issue...and even adds a test to simpletest.test.

AttachmentSizeStatusTest resultOperations
296574-drupal-debug.patch8.77 KBIdleFailed: 12087 passes, 2 fails, 0 exceptionsView details

#48

System Message - August 13, 2009 - 01:15
Status:needs review» needs work

The last submitted patch failed testing.

#49

boombatower - August 13, 2009 - 02:31
Status:needs work» needs review

I must say this is a good one:

error_test_generate_warnings() contains:
trigger_error("Drupal is awesome", E_USER_NOTICE);

Notice anything weird there? (no pun intended)

warning != notice

Fixed in patch and solve test issue.

AttachmentSizeStatusTest resultOperations
296574-drupal-debug.patch10.07 KBIdleFailed: 12118 passes, 1 fail, 0 exceptionsView details

#50

System Message - August 13, 2009 - 02:50
Status:needs review» needs work

The last submitted patch failed testing.

#51

boombatower - August 13, 2009 - 02:55
Status:needs work» needs review

Missed another instance of User notice.

AttachmentSizeStatusTest resultOperations
296574-drupal-debug.patch11.13 KBIdleFailed: 12119 passes, 0 fails, 7 exceptionsView details

#52

System Message - August 13, 2009 - 03:10
Status:needs review» needs work

The last submitted patch failed testing.

#53

boombatower - August 13, 2009 - 03:22
Status:needs work» needs review

Small update for t() $args.

AttachmentSizeStatusTest resultOperations
296574-drupal-debug.patch11.13 KBIdlePassed: 12122 passes, 0 fails, 0 exceptionsView details

#54

chx - August 13, 2009 - 04:03
Status:needs review» reviewed & tested by the community

I think this addressed webchick's concerns.

#55

Dries - August 13, 2009 - 11:41

I'll leave it up to webchick to drive this patch home. My personal take is that the way we mess around with PHP error types (e.g. 'User notice') is a bit of a hack.

The code still seems to use 'User notice' instead of 'User warning' but maybe I misunderstood parts of it. Doesn't this patch prevent other code from using 'User warning' and 'User notices'? That would be bad, IMO.

#56

chx - August 13, 2009 - 14:14

To clarify, while we are using user notices for our purposes, it's not like others can't. Currently if you throw a trigger_error it will show up as a Drupal error message after this patch it will be a normal message. It's still there, just the class has changed. And instead of "User notice:" it will say "Debug:". To show an error, everyone uses drupal_set_message, that's why it's there, otherwise you error message includes the "User notice:" which is not cool so I doubt throw_error saw a lot of usage in Drupal (never met anyone using it). And yes using trigger_error is a bit of a hack, however we get a (almost) free ride on the back of the existing error handling infrastructure, otherwise we need to add quite a bit of code -- to get the same infrastructure. That's a waste for debug.

#57

chx - August 14, 2009 - 05:52

And to clarify further, only trigger_error produces E_USER* stuff, that's not something normally comes around.

#58

chx - August 15, 2009 - 03:52

That probably was not clear enough. What trigger_error does is always specified by the error handler running. Drupal's error handler already changes what PHP's error handler does. It's not specified anywhere at all what Drupal's error handler should do with the messages passed through trigger_error. So far, almost as an afterthought we put the displayed drupal message in the error class and prefixed the message with "User notice:". We now give it another class and prefix with "Debug:". As we have no specs neither is more correct than the other. The message is displayed, logged etc all the same.

#59

boombatower - August 15, 2009 - 04:03

Update patch per request by webchick to use warning icon for debug messages. I have attached screenshot.

This patch is ready to go and would be useful to have available during the testing sprint.

debug with warning

AttachmentSizeStatusTest resultOperations
296574-drupal-debug.png4.91 KBIgnoredNoneNone
296574-drupal-debug.patch23.62 KBIdlePassed: 2796 passes, 0 fails, 0 exceptionsView details

#60

boombatower - August 15, 2009 - 04:04

Ugh on laptop...not setup...this is teh patch.

AttachmentSizeStatusTest resultOperations
296574-drupal-debug.patch11.46 KBIdlePassed: 12092 passes, 0 fails, 0 exceptionsView details

#61

webchick - August 15, 2009 - 06:20
Status:reviewed & tested by the community» needs work

Ok, I've committed this to HEAD.

Like Dries, I too worry about the hackishness of taking over trigger_error, and am concerned about the WTF this brings to PHP developers new to Drupal. But chx does have a good point at #58 that Drupal is defining its own error handler which takes precedence over trigger_error() anyway.

With the commit of this patch, we can satisfy the code freeze requirement to get all function changes in, yet clean-up the implementation later if necessary.

This change needs to be documented in the upgrade docs.

#62

boombatower - August 15, 2009 - 22:12
Status:needs work» fixed

Greggles added documentation, but it used the format from the initial post which is incorrect. I correct: http://drupal.org/node/394976.

#63

System Message - August 29, 2009 - 22:20
Status:fixed» closed

Automatically closed -- issue fixed for 2 weeks with no activity.

#64

mgifford - August 30, 2009 - 21:58

Unfortunately the documentation gives no guidance about when/where to insert a debug statement like:

<?php
$this
->debug('debug information');
?>

It's all rather circular.

Can we have a clear example of how one might use a debug feature for simpleTest?

#66

mgifford - September 3, 2009 - 12:46

Thanks!

 
 

Drupal is a registered trademark of Dries Buytaert.