From bb1ae0ba5405f4cffcf0ca59af8ab284c9542fe2 Mon Sep 17 00:00:00 2001 From: jakubvrana Date: Thu, 2 Oct 2008 16:42:55 +0000 Subject: [PATCH] Code coverage git-svn-id: https://adminer.svn.sourceforge.net/svnroot/adminer/trunk@514 7c3ca157-0c34-0410-bff1-cbf682f78f5c --- _compile.php | 1 + _coverage.php | 66 +++++++++++++++++++++++++++++++++++++++++++++ design.inc.php | 2 +- index.php | 13 +++++++++ tests/0-login.html | 5 ++++ tests/coverage.html | 22 +++++++++++++++ 6 files changed, 108 insertions(+), 1 deletion(-) create mode 100644 _coverage.php create mode 100644 tests/coverage.html diff --git a/_compile.php b/_compile.php index b2872bcf..28f13555 100644 --- a/_compile.php +++ b/_compile.php @@ -101,6 +101,7 @@ if ($_SERVER["argc"] > 1) { $filename = "phpMinAdmin" . ($_COOKIE["lang"] ? "-$_COOKIE[lang]" : "") . ".php"; $file = file_get_contents("index.php"); $file = preg_replace_callback('~(<\\?php)?\\s*(include|require) "([^"]*)";(\\s*\\?>)?~', 'put_file', $file); +$file = preg_replace("~if \\(isset\\(\\\$_SESSION\\[\"coverage.*\n}\n| && !isset\\(\\\$_SESSION\\[\"coverage\"\\]\\)~sU", '', $file); if ($_COOKIE["lang"]) { $file = preg_replace_callback("~(<\\?php\\s*echo )?lang\\('((?:[^\\\\']+|\\\\.)*)'([,)])(;\\s*\\?>)?~s", 'remove_lang', $file); $file = str_replace("\n", "", $file); diff --git a/_coverage.php b/_coverage.php new file mode 100644 index 00000000..3c0dd65a --- /dev/null +++ b/_coverage.php @@ -0,0 +1,66 @@ +]+)~', $s, $matches); + foreach ($matches[1] as $val) { + if ($val{0} == "/") { + array_pop($return); + } elseif (substr($val, -1) != "/") { + $return[] = $val; + } + } + return $return; +} + +if ($_GET["start"]) { + $_SESSION["coverage"] = array(); + header("Location: ."); + exit; +} elseif ($_GET["filename"]) { + $filename = basename($_GET["filename"]); + $coverage = $_SESSION["coverage"][realpath($filename)]; + $file = explode("
", highlight_file($filename, true)); + unset($prev_color); + $s = ""; + for ($l=0; $l <= count($file); $l++) { + $line = $file[$l]; + $color = "#C0FFC0"; // tested + switch ($coverage[$l+1]) { + case -1: $color = "#FFC0C0"; break; // untested + case -2: $color = "Silver"; break; // dead code + case null: $color = ""; break; // not executable + } + if (!isset($prev_color)) { + $prev_color = $color; + } + if ($prev_color != $color || !isset($line)) { + echo "" . $s; + $open_tags = xhtml_open_tags($s); + foreach (array_reverse($open_tags) as $tag) { + echo ""; + } + echo "\n"; + $s = ($open_tags ? "<" . implode("><", $open_tags) . ">" : ""); + $prev_color = $color; + } + $s .= "$line
\n"; + } +} elseif (isset($_SESSION["coverage"])) { + echo "\n"; +} +?> +

Start new coverage (requires Xdebug)

diff --git a/design.inc.php b/design.inc.php index 12f3ba4b..577cefa3 100644 --- a/design.inc.php +++ b/design.inc.php @@ -53,7 +53,7 @@ function toggle(id) { if (strlen($_GET["db"]) && $databases && !in_array($_GET["db"], $databases, true)) { $databases = null; } - if (isset($databases) && !isset($_GET["sql"])) { + if (isset($databases) && !isset($_GET["sql"]) && !isset($_SESSION["coverage"])) { session_write_close(); } if ($error) { diff --git a/index.php b/index.php index 1a7e94ba..c8f53085 100644 --- a/index.php +++ b/index.php @@ -12,6 +12,19 @@ if (!ini_get("session.auto_start")) { session_set_cookie_params(ini_get("session.cookie_lifetime"), preg_replace('~\\?.*~', '', $_SERVER["REQUEST_URI"])); session_start(); } +if (isset($_SESSION["coverage"])) { + function save_coverage() { + foreach (xdebug_get_code_coverage() as $filename => $lines) { + foreach ($lines as $l => $val) { + if (!$_SESSION["coverage"][$filename][$l] || $val > 0) { + $_SESSION["coverage"][$filename][$l] = $val; + } + } + } + } + xdebug_start_code_coverage(XDEBUG_CC_UNUSED | XDEBUG_CC_DEAD_CODE); + register_shutdown_function('save_coverage'); +} if (get_magic_quotes_gpc()) { $process = array(&$_GET, &$_POST); while (list($key, $val) = each($process)) { diff --git a/tests/0-login.html b/tests/0-login.html index 4cdf7172..5354cdcb 100644 --- a/tests/0-login.html +++ b/tests/0-login.html @@ -11,6 +11,11 @@ Create database + + open + /phpMinAdmin/_coverage.php?start=1 + + open /phpMinAdmin/?lang=en&username= diff --git a/tests/coverage.html b/tests/coverage.html new file mode 100644 index 00000000..512dd026 --- /dev/null +++ b/tests/coverage.html @@ -0,0 +1,22 @@ + + + + + + +Coverage + + + + + + + + + + + + +
Coverage
open/phpMinAdmin/_coverage.php
+ +