Permalink
Browse files

Release 1.1.0

- added junit.xml export
  • Loading branch information...
1 parent 15c48a3 commit da25419d6ecfb52a38fae8875828a85163162bf2 @DracoBlue committed Apr 8, 2012
View
@@ -2,19 +2,39 @@
class NaithCliRunner
{
static $coverage_file_path = null;
+ static $tests_report_path = null;
+ static $tested_file_name = null;
+ static $start_time = 0;
+ static $assertions_count = 0;
+ static $errors_count = 0;
+ static $error_message = '';
static function setCoverageFilePath($file_path)
{
self::$coverage_file_path = $file_path;
}
+ static function setTestsReportPath($file_path, $tested_file_name)
+ {
+ self::$tests_report_path = $file_path;
+ self::$tested_file_name = $tested_file_name;
+ }
+
static function bootstrapForTest()
{
if (self::$coverage_file_path)
{
xdebug_start_code_coverage(XDEBUG_CC_UNUSED | XDEBUG_CC_DEAD_CODE);
+ }
+ if (self::$coverage_file_path || self::$tests_report_path)
+ {
register_shutdown_function(__CLASS__ . '::onShutdown');
}
+
+ if (self::$tests_report_path)
+ {
+ self::$start_time = microtime(true);
+ }
assert_options(ASSERT_ACTIVE, 1);
assert_options(ASSERT_WARNING, 1);
@@ -29,6 +49,7 @@ static function bootstrapForTest()
static function assertException($callable)
{
+ self::$assertions_count++;
$threw_exception = false;
try
@@ -42,28 +63,46 @@ static function assertException($callable)
if (!$threw_exception)
{
- restore_error_handler();
list($callee) = debug_backtrace(2);
- trigger_error('Expected an exception! in ' . $callee['file'] . ' on line ' . $callee['line'] . "\n", E_USER_ERROR);
+ self::$errors_count++;
+ self::$error_message = 'Expected an exception! in ' . $callee['file'] . ' on line ' . $callee['line'];
+ restore_error_handler();
+ trigger_error(self::$error_message . "\n", E_USER_ERROR);
}
}
static function throwFromAssertion($file, $line, $message)
{
+ self::$errors_count++;
+ self::$error_message = $message . ' in ' . $file . ' on line ' . $line;
restore_error_handler();
- trigger_error($message . ' in ' . $file . ' on line ' . $line . "\n", E_USER_ERROR);
+ trigger_error(self::$error_message . "\n", E_USER_ERROR);
}
static function throwFromError($code, $message, $file, $line)
{
+ self::$errors_count++;
+ self::$error_message = $message . ' in ' . $file . ' on line ' . $line;
restore_error_handler();
- list($me, $callee) = debug_backtrace(2);
- trigger_error($message . ' in ' . $file . ' on line ' . $line . "\n", E_USER_ERROR);
+ trigger_error(self::$error_message . "\n", E_USER_ERROR);
}
static function onShutdown()
{
- file_put_contents(self::$coverage_file_path, json_encode(xdebug_get_code_coverage()) . PHP_EOL, FILE_APPEND);
+ if (self::$tests_report_path)
+ {
+ file_put_contents(self::$tests_report_path, json_encode(array(
+ 'file_name' => self::$tested_file_name,
+ 'assertions' => self::$assertions_count,
+ 'errors' => self::$errors_count,
+ 'error_message' => self::$error_message,
+ 'time' => microtime(true) - self::$start_time
+ )) . PHP_EOL, FILE_APPEND);
+ }
+ if (self::$coverage_file_path)
+ {
+ file_put_contents(self::$coverage_file_path, json_encode(xdebug_get_code_coverage()) . PHP_EOL, FILE_APPEND);
+ }
}
}
View
@@ -0,0 +1,84 @@
+<?php
+class NaithJunitReport
+{
+ /**
+ * The base directory for all covered files
+ */
+ protected $base_directory = null;
+
+ protected $tests_report_path = null;
+
+ protected $excluded_paths = array();
+
+ public function __construct(array $options)
+ {
+ $this->excluded_paths = $options['excluded_paths'];
+ $this->base_directory = $options['base_directory'];
+ $this->tests_report_path = $options['tests_report_path'];
+ }
+
+ public function writeJunitXmlToFile($junit_file_path)
+ {
+ file_put_contents($junit_file_path, '<?xml version="1.0" encoding="UTF-8" ?' . '>');
+
+ $report = array();
+ $errors_count = 0;
+ $assertions_count = 0;
+ $tests_count = 0;
+ $time = 0;
+
+ $body_xml = array();
+
+ foreach (explode(PHP_EOL, file_get_contents($this->tests_report_path)) as $raw_line)
+ {
+ if (empty($raw_line))
+ {
+ continue;
+ }
+ $tests_count++;
+ $test_report_data = json_decode($raw_line, true);
+ $file_name = $test_report_data['file_name'];
+ foreach ($this->excluded_paths as $ignore_path)
+ {
+ if ($ignore_path === substr($file_name, 0, strlen($ignore_path)))
+ {
+ continue 2;
+ }
+ }
+
+ $errors_count += $test_report_data['errors'];
+ $assertions_count += $test_report_data['assertions'];
+ $time += $test_report_data['time'];
+
+ $relative_file_name = substr($file_name, strlen($this->base_directory) + 1);
+
+ $body_xml[] = '<testcase';
+ $body_xml[] = ' classname="'. htmlspecialchars(str_replace('/', '.', dirname($relative_file_name))). '"';
+ $body_xml[] = ' name="'. htmlspecialchars(basename($relative_file_name)). '"';
+ $body_xml[] = ' assertions="'. htmlspecialchars(basename($test_report_data['assertions'])). '"';
+ $body_xml[] = ' errors="'. htmlspecialchars(basename($test_report_data['errors'])). '"';
+ $body_xml[] = ' time="'. htmlspecialchars(basename($test_report_data['time'])). '"';
+
+ if ($test_report_data['errors']) {
+ $body_xml[] = '><error>' . htmlspecialchars($test_report_data['error_message']) . '</error></testcase>';
+ } else {
+ $body_xml[] = ' />';
+ }
+
+ $body_xml[] = PHP_EOL;
+ }
+
+ $head_xml = array();
+ $head_xml[] = '<testsuite ';
+ $head_xml[] = ' name="Naith Results"';
+ $head_xml[] = ' tests="'. htmlspecialchars($tests_count). '"';
+ $head_xml[] = ' time="'. htmlspecialchars($time). '"';
+ $head_xml[] = ' errors="'. htmlspecialchars($errors_count). '"';
+ $head_xml[] = ' assertions="'. htmlspecialchars($assertions_count). '">' . PHP_EOL;
+
+ file_put_contents($junit_file_path, implode('', $head_xml), FILE_APPEND);
+ file_put_contents($junit_file_path, implode('', $body_xml), FILE_APPEND);
+ file_put_contents($junit_file_path, '</testsuite>', FILE_APPEND);
+ }
+
+}
View
@@ -1,7 +1,7 @@
# naith
-* Version: 1.0.0
-* Date: 2012/03/25
+* Version: 1.1.0
+* Date: 2012/04/08
Naith is a small (~100 loc) php test runner and report generator (with code coverage) for the command line.
@@ -40,6 +40,13 @@ Result is:
That's it.
+## Changelog
+
+- 1.1.0 (2012/04/08)
+ - junit.xml generation added
+- 1.0.0 (2012/03/25)
+ - initial release
+
## Todo
* Cleanup the test code
View
28 naith
@@ -6,6 +6,8 @@ naith_directory=`pwd -P`
cd $current_directory
raw_code_coverage_file=$current_directory/coverage_raw.txt
+raw_tests_report_file=$current_directory/tests_report_raw.txt
+junit_report_file=$current_directory/junit.xml
action_name=$1
tests_folder=$2
@@ -32,7 +34,7 @@ then
exit 1
fi
- default_options="--coverage_file_path \"$raw_code_coverage_file\" --base_directory \"$current_directory\""
+ default_options="--tests_report_path \"$raw_tests_report_file\" --coverage_file_path \"$raw_code_coverage_file\" --base_directory \"$current_directory\""
if [ -f "$current_directory/_before_test.php" ]
then
@@ -44,25 +46,39 @@ then
echo "==============="
echo ""
echo "" > $raw_code_coverage_file
- ls *.php | while read file
+ echo "" > $raw_tests_report_file
+
+ test_success=0
+ test_errors=0
+ # We cannot use while read line here
+ # @see http://fvue.nl/wiki/Bash:_Piped_%60while-read'_loop_starts_subshell
+ # for further information
+ for file in `ls *.php`
do
php $naith_directory/naith.php run-test $default_options \
- --test_file "$file"
+ --test_file "$tests_folder/$file"
current_exit_code="${?}"
if [ "${current_exit_code}" -ne "0" ]
then
echo " [ ] $file"
echo " -> broken! (Exit code: $current_exit_code)"
- rm "$raw_code_coverage_file"
- exit $current_exit_code
+ let test_errors=test_errors+1
else
echo " [OK] $file"
+ let test_success=test_success+1
fi
done
- if [ ! -f "$raw_code_coverage_file" ]
+
+ php $naith_directory/naith.php generate-junit-xml $default_options \
+ --junit_xml_path "$junit_report_file"
+
+ rm "$raw_tests_report_file"
+
+ if [ ! $test_errors -eq 0 ]
then
+ rm "$raw_code_coverage_file"
exit 1
fi
View
@@ -6,6 +6,7 @@
require_once (dirname(__FILE__) . '/NaithCliRunner.class.php');
require_once (dirname(__FILE__) . '/NaithCliReport.class.php');
+require_once (dirname(__FILE__) . '/NaithJunitReport.class.php');
/*
* Argument handling!
@@ -75,14 +76,14 @@
{
list($base_directory) = $options['base_directory'];
}
-
+
list($coverage_file_path) = $options['coverage_file_path'];
-
+
$report = new NaithCliReport( array(
'base_directory' => $base_directory,
'excluded_paths' => $excluded_paths,
'coverage_file_path' => $coverage_file_path,
- 'minimum_code_coverage' => $minimum_code_coverage
+ 'minimum_code_coverage' => $minimum_code_coverage,
));
}
@@ -94,6 +95,11 @@
{
NaithCliRunner::setCoverageFilePath($options['coverage_file_path'][0]);
}
+
+ if (isset($options['tests_report_path']))
+ {
+ NaithCliRunner::setTestsReportPath($options['tests_report_path'][0], $options['test_file'][0]);
+ }
NaithCliRunner::bootstrapForTest();
@@ -125,4 +131,43 @@
$report->makeUntestedCodeOverview();
break;
+
+ case "generate-junit-xml":
+
+ $junit_xml_path = null;
+
+ if (isset($options['junit_xml_path']))
+ {
+ list($junit_xml_path) = $options['junit_xml_path'];
+ }
+
+ $excluded_paths = array();
+ if (isset($options['excluded_path']))
+ {
+ $excluded_paths = $options['excluded_path'];
+ }
+
+ $base_directory = dirname(getcwd());
+
+ if (isset($options['base_directory']))
+ {
+ list($base_directory) = $options['base_directory'];
+ }
+
+ $tests_report_path = null;
+
+ if (isset($options['tests_report_path']))
+ {
+ list($tests_report_path) = $options['tests_report_path'];
+ }
+
+ $report = new NaithJunitReport( array(
+ 'base_directory' => $base_directory,
+ 'excluded_paths' => $excluded_paths,
+ 'tests_report_path' => $tests_report_path
+ ));
+
+ $report->writeJunitXmlToFile($junit_xml_path);
+
+ break;
}
Oops, something went wrong.

0 comments on commit da25419

Please sign in to comment.