-
Notifications
You must be signed in to change notification settings - Fork 4
/
checkstyle2junit.php
executable file
·103 lines (88 loc) · 3.21 KB
/
checkstyle2junit.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<?php
/**
* checkstyle2junit is tool for converting checkstyle xml report to junit xml report
*
* PHP version 5
*
* @category PHP
* @package checkstyle2junit
* @author Boris Gorbylev <[email protected]>
* @copyright 2005-2013
* @license http://github.com/i-ekho/checkstyle2junit/blob/master/LICENCE LGPLv3 Licence
* @link http://github.com/i-ekho/checkstyle2junit
*/
if (empty($_SERVER['argv'][1])) {
echo 'ERROR: input checkstyle xml file does not specified', PHP_EOL;
exit(1);
}
$inFile = $_SERVER['argv'][1];
if (!file_exists($inFile)) {
echo 'ERROR: input checkstyle xml file does not exists', PHP_EOL;
exit(1);
}
if (!is_readable($inFile)) {
echo 'ERROR: can not read input checkstyle xml file', PHP_EOL;
exit(1);
}
$outFile = empty($_SERVER['argv'][2]) ? null : $_SERVER['argv'][2];
if ($outFile) {
if (file_exists($outFile)) {
if (!is_writeable($outFile)) {
echo 'ERROR: out phpunit xml file exists but not writeable', PHP_EOL;
exit(1);
}
} elseif (file_exists(dirname($outFile))) {
if (!is_writeable(dirname($outFile))) {
echo 'ERROR: directory of out phpunit xml file exists but not writeable', PHP_EOL;
exit(1);
}
} else {
echo 'ERROR: can not create out phpunit xml file exists because parent directory does not exists', PHP_EOL;
exit(1);
}
}
libxml_use_internal_errors(true);
$checkstyleXml = simplexml_load_file($inFile);
if ($checkstyleXml === false) {
echo 'ERROR: Failed loading input checkstyle xml file', PHP_EOL;
foreach (libxml_get_errors() as $error) {
echo "\t", $error->message, PHP_EOL;
}
exit(1);
}
$phpunitXml = new SimpleXMLElement('<testsuites/>');
$mainSuite = $phpunitXml->addChild('testsuite');
$mainSuite->addAttribute('errors', 0);
$mainSuite->addAttribute('name', '.');
$mainSuite->addAttribute('tests', 0);
$mainSuite->addAttribute('assertions', 0);
$mainSuite->addAttribute('failures', 0);
foreach ($checkstyleXml as $file) {
$mainSuite['tests'] += count($file);
$mainSuite['assertions'] += count($file);
$mainSuite['failures'] += count($file);
$fileSuite = $mainSuite->addChild('testsuite');
$fileSuite->addAttribute('errors', 0);
$fileSuite->addAttribute('name', basename($file['name'], '.php'));
$fileSuite->addAttribute('file', $file['name']);
$fileSuite->addAttribute('tests', count($file));
$fileSuite->addAttribute('assertions', count($file));
$fileSuite->addAttribute('failures', count($file));
foreach ($file as $error) {
$case = $fileSuite->addChild('testcase');
$case->addAttribute('name', preg_replace('@[^a-zA-Z0-9]@', ' ', $error['source']));
$case->addAttribute('file', $file['name']);
$case->addAttribute('line', $error['line']);
$case->addAttribute('column', $error['column']);
$failure = $case->addChild(
'failure',
$error['source'] . PHP_EOL . $error['message'] . PHP_EOL . PHP_EOL . $file['name'] . ':' . $error['line'] . ':' . $error['column']
);
$failure->addAttribute('type', $error['source']);
}
}
if ($outFile) {
$phpunitXml->asXML($outFile);
} else {
echo $phpunitXml->asXML();
}