This repository has been archived by the owner on Sep 7, 2022. It is now read-only.
/
XcodeTestResultParser.php
202 lines (183 loc) · 6.42 KB
/
XcodeTestResultParser.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
<?php
/*
Copyright 2016-present Google Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/**
* Arcanist test parser for xcodebuild + llvm-cov output.
*
* Provide parseTestResults with stdout of `xcodebuild`.
* Provide setCoverageFile with stdout of `llvm-cov show`.
*/
final class XcodeTestResultParser extends ArcanistTestResultParser {
private $xcodeargs;
public function setXcodeArgs($args) {
$this->xcodeargs = $args;
return $this;
}
/**
* Parses stdout of `xcodebuild` as provided via $test_results and returns an
* array of ArcanistUnitTestResult instances.
*/
public function parseTestResults($path, $test_results) {
if (!$test_results) {
$result = id(new ArcanistUnitTestResult())
->setName("Xcode test engine")
->setUserData($this->stderr)
->setResult(ArcanistUnitTestResult::RESULT_BROKEN);
return array($result);
}
$arccovmap = null;
if ($this->enableCoverage) {
$arccovmap = $this->parseCoverageResults();
}
$results = array();
$accumulator = array();
$suite = null;
$currentTestCase = null;
// Break the test result stdout into lines.
foreach(preg_split("/((\r?\n)|(\r\n?))/", $test_results) as $line) {
if ($this->startsWith($line, 'Test Suite')) {
if (preg_match('/Test Suite \'(.+?)\' started/', $line, $matches)) {
$suite = $matches[1];
} else {
$suite = null;
}
continue;
}
// Get the current test case name
$testCase = $this->testCaseNameFromLine($line);
if ($testCase != null) {
$currentTestCase = $testCase;
}
// Check if the 'error:' exits and and this error is from a test case and NOT a log.
// The test failure contains the test case name and we check that.
if (strpos($line, 'error:') !== false && $currentTestCase != null && strpos($line, $currentTestCase) !== false) {
$result = new ArcanistUnitTestResult();
$result->setName('xcode-unit-engine');
foreach ($this->xcodeargs as $arg) {
if (preg_match('/-scheme (.+)/', $arg, $matches)) {
$result->setName(trim($matches[1], "'\""));
break;
}
}
$result->setResult(ArcanistUnitTestResult::RESULT_BROKEN);
$result->setUserData($line);
$results []= $result;
continue;
}
// Test has started?
if ($this->endsWith($line, 'started.')) {
$accumulator = array();
continue;
}
// Within a test, informational line.
if (!$this->startsWith($line, 'Test Case')) {
// Treat these lines as information for the test case.
$accumulator []= $line;
continue;
}
// End of a test.
if (!preg_match('/\'-\[\S*\s(?:test)*(.+?)\]\' (.+?) \((.+?) seconds\).$/', $line, $matches)) {
echo "Unable to parse line:\n$line\n";
continue;
}
$userdata = implode("\n", $accumulator);
$userdata = str_replace($this->projectRoot.'/', '', $userdata);
$result = new ArcanistUnitTestResult();
$result->setName($matches[1]);
if ($suite) {
$result->setNamespace($suite);
}
switch ($matches[2]) {
case 'passed':
$result->setResult(ArcanistUnitTestResult::RESULT_PASS);
break;
case 'failed':
$result->setResult(ArcanistUnitTestResult::RESULT_FAIL);
break;
}
$result->setDuration(floatval($matches[3]));
$result->setUserData($userdata);
if ($arccovmap) {
// TODO(featherless): Rather than provide the same coverage to every
// test object, identify whether we can provide the coverage once
// somewhere else.
$result->setCoverage($arccovmap);
}
$results []= $result;
$accumulator = array();
}
return $results;
}
private function testCaseNameFromLine($line) {
if ($this->startsWith($line, 'Test Case')) {
$components = explode("'", $line);
if (sizeof($components) >= 2) {
return $components[1];
}
}
return null;
}
/**
* Return a map of filename (relative to $this->projectRoot) to <coverage string>
* https://secure.phabricator.com/book/phabricator/article/arcanist_coverage/
*/
private function parseCoverageResults() {
$filemap = array();
$filename = null;
$file = array();
foreach(preg_split("/((\r?\n)|(\r\n?))/", $this->coverageFile) as $line) {
if (substr($line, 0, 1) === '/') {
if ($filename && $this->startsWith($filename, $this->projectRoot)) {
$filemap[$filename] = $file;
}
$file = array();
$filename = $line;
continue;
}
$file []= $line;
}
if ($file && $filename) {
// Commit the hanging file.
$filemap[$filename] = $file;
}
$arccovmap = array();
foreach ($filemap as $filename => $coverage) {
$cov = '';
foreach ($coverage as $line) {
if (preg_match('/^\s+\d+\|\s+(\d+)\|/', $line, $matches)
&& count($matches) > 1) {
if (intval($matches[1]) > 0) {
$cov .= 'C'; // Covered
} else {
$cov .= 'U'; // Uncovered
}
continue;
}
// Couldn't find a number
$cov .= 'N'; // Not executable
}
$arcname = str_replace($this->projectRoot.'/', '', $filename);
$arcname = str_replace(':', '', $arcname);
$arccovmap[$arcname] = $cov;
}
return $arccovmap;
}
// http://stackoverflow.com/questions/834303/startswith-and-endswith-functions-in-php
private function startsWith($haystack, $needle) {
return $needle === "" || strrpos($haystack, $needle, -strlen($haystack)) !== false;
}
// http://stackoverflow.com/questions/834303/startswith-and-endswith-functions-in-php
function endsWith($haystack, $needle) {
return $needle === "" || (($temp = strlen($haystack) - strlen($needle)) >= 0 && strpos($haystack, $needle, $temp) !== false);
}
}