| 1 |
<?php
|
| 2 |
// $Id: code_coverage.module,v 1.3 2008/06/28 23:33:49 cwgordon7 Exp $
|
| 3 |
|
| 4 |
if (preg_match("/^simpletest\d+$/", $_SERVER['HTTP_USER_AGENT']) && function_exists('xdebug_start_code_coverage')) {
|
| 5 |
xdebug_start_code_coverage(XDEBUG_CC_UNUSED | XDEBUG_CC_DEAD_CODE);
|
| 6 |
register_shutdown_function('code_coverage_log');
|
| 7 |
}
|
| 8 |
|
| 9 |
/**
|
| 10 |
* Checks to make sure our code is where it needs to be. (And if not, displays
|
| 11 |
* a very scary warning to the user).
|
| 12 |
*/
|
| 13 |
function code_coverage_check_hacks() {
|
| 14 |
if (!preg_match("/^simpletest\d+$/", $_SERVER['HTTP_USER_AGENT']) && user_access('administer code coverage')) {
|
| 15 |
// Make sure our stuff is in index.php.
|
| 16 |
if (!variable_get('code_coverage_hacks', FALSE)) {
|
| 17 |
$passes = TRUE;
|
| 18 |
foreach (array('index.php', 'xmlrpc.php') as $file) {
|
| 19 |
$code = 'require_once \'./' . str_replace('\\', '/', substr(str_replace(getcwd(), '', __FILE__), 1)) . '\';' . "\n";
|
| 20 |
$contents = file_get_contents("./$file");
|
| 21 |
if (strpos($contents, $code) === FALSE) {
|
| 22 |
$passes = FALSE;
|
| 23 |
drupal_set_message(t('You must add the following code to the top of your @file file in order for code coverage reporting to work.<code><pre>@code</pre></code>', array('@code' => $code, '@file' => $file)), 'warning', FALSE);
|
| 24 |
}
|
| 25 |
}
|
| 26 |
if ($passes) {
|
| 27 |
variable_set('code_coverage_hacks', TRUE);
|
| 28 |
}
|
| 29 |
}
|
| 30 |
code_coverage_fetch_data();
|
| 31 |
}
|
| 32 |
}
|
| 33 |
|
| 34 |
/**
|
| 35 |
* Implementation of hook_perm().
|
| 36 |
*/
|
| 37 |
function code_coverage_perm() {
|
| 38 |
return array(
|
| 39 |
'view code coverage reports' => t('View the reports on Drupal\'s code coverage. This does not imply any permissions about generation of code coverage reports or changing of code coverage settings.'),
|
| 40 |
'administer code coverage' => t('Administer code coverage reports, code coverage settings, and view code coverage reports.'),
|
| 41 |
);
|
| 42 |
}
|
| 43 |
|
| 44 |
/**
|
| 45 |
* Implementation of hook_menu().
|
| 46 |
*/
|
| 47 |
function code_coverage_menu() {
|
| 48 |
$items = array();
|
| 49 |
|
| 50 |
$items['admin/settings/coverage'] = array(
|
| 51 |
'title' => 'Code coverage',
|
| 52 |
'description' => 'Configure the generation of Drupal\'s code coverage reports.',
|
| 53 |
'page callback' => 'drupal_get_form',
|
| 54 |
'page arguments' => array('code_coverage_settings_form'),
|
| 55 |
'access arguments' => array('administer code coverage'),
|
| 56 |
);
|
| 57 |
|
| 58 |
$items['coverage'] = array(
|
| 59 |
'title' => 'Code coverage',
|
| 60 |
'description' => 'Generate code coverage reports based on the collected coverage data.',
|
| 61 |
'page callback' => 'code_coverage_generate_report',
|
| 62 |
'access arguments' => array('view code coverage reports'),
|
| 63 |
);
|
| 64 |
|
| 65 |
return $items;
|
| 66 |
}
|
| 67 |
|
| 68 |
/**
|
| 69 |
* Implementation of hook_cron().
|
| 70 |
*/
|
| 71 |
function code_coverage_cron() {
|
| 72 |
variable_del('code_coverage_hacks');
|
| 73 |
}
|
| 74 |
|
| 75 |
/**
|
| 76 |
* Implementation of hook_help().
|
| 77 |
*/
|
| 78 |
function code_coverage_help() {
|
| 79 |
static $checked = FALSE;
|
| 80 |
if ($checked) {
|
| 81 |
return;
|
| 82 |
}
|
| 83 |
$checked = TRUE;
|
| 84 |
code_coverage_check_hacks();
|
| 85 |
}
|
| 86 |
|
| 87 |
/**
|
| 88 |
* Stop recording. This is registered as a shutdown function.
|
| 89 |
*/
|
| 90 |
function code_coverage_log() {
|
| 91 |
global $db_prefix, $db_prefix_original;
|
| 92 |
$preserved_prefix = $db_prefix;
|
| 93 |
$db_prefix = $db_prefix_original;
|
| 94 |
$GLOBALS['conf'] = variable_init();
|
| 95 |
$info = xdebug_get_code_coverage();
|
| 96 |
$text = '';
|
| 97 |
foreach ($info as $file => $lines) {
|
| 98 |
$basename = basename($file);
|
| 99 |
if ($basename != 'code_coverage.module' && $basename != 'code_coverage.install' && $basename != 'code_coverage.admin.inc' && $basename != 'settings.php') {
|
| 100 |
$text .= $file;
|
| 101 |
$text .= '$';
|
| 102 |
foreach ($lines as $line => $count) {
|
| 103 |
if ($count == -1) {
|
| 104 |
$count = 0;
|
| 105 |
}
|
| 106 |
$text .= sprintf('%s-%s,', $line, $count);
|
| 107 |
}
|
| 108 |
$text = substr($text, 0, -1);
|
| 109 |
$text .= "\n";
|
| 110 |
}
|
| 111 |
}
|
| 112 |
file_put_contents(variable_get('code_coverage_tmp_storage', '/tmp/') . variable_get('code_coverage_id', 1) . '.txt', $text, FILE_APPEND);
|
| 113 |
$db_prefix = $preserved_prefix;
|
| 114 |
$GLOBALS['conf'] = variable_init();
|
| 115 |
}
|
| 116 |
|
| 117 |
/**
|
| 118 |
* Fetch all the code coverage data from a file and store it to the database.
|
| 119 |
*/
|
| 120 |
function code_coverage_fetch_data() {
|
| 121 |
$file = variable_get('code_coverage_tmp_storage', '/tmp/') . variable_get('code_coverage_id', 1) . '.txt';
|
| 122 |
if (file_exists($file) && is_file($file) && $_GET['q'] == 'admin/build/testing' && user_access('administer code coverage')) {
|
| 123 |
$contents = file_get_contents($file);
|
| 124 |
file_delete($file);
|
| 125 |
$entries = explode("\n", $contents);
|
| 126 |
$data = array();
|
| 127 |
foreach ($entries as $entry) {
|
| 128 |
if (!empty($entry)) {
|
| 129 |
list($filename, $lines) = explode('$', $entry);
|
| 130 |
$packets = explode(',', $lines);
|
| 131 |
while ($line = array_shift($packets)) {
|
| 132 |
list($line_num, $count) = explode('-', $line);
|
| 133 |
$data[$filename][$line_num] = (isset($data[$filename][$line_num]) ? ($data[$filename][$line_num] + $count) : $count);
|
| 134 |
}
|
| 135 |
}
|
| 136 |
}
|
| 137 |
foreach ($data as $filename => $info) {
|
| 138 |
foreach ($info as $line => $times) {
|
| 139 |
db_query("INSERT INTO {code_coverage} VALUES (%d, '%s', %d, %d)", variable_get('code_coverage_id', 1), $filename, $line, $times);
|
| 140 |
}
|
| 141 |
}
|
| 142 |
variable_set('code_coverage_id', variable_get('code_coverage_id', 1) + 1);
|
| 143 |
}
|
| 144 |
}
|