/
Mocha.php
78 lines (58 loc) · 2.04 KB
/
Mocha.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
<?php
namespace SergiuParaschiv\PHPCI\Plugin;
use PHPCI\Builder;
use PHPCI\Model\Build;
use PHPCI\Plugin\Util\TapParser;
class Mocha implements \PHPCI\Plugin
{
public function __construct(Builder $phpci, Build $build, array $options = array())
{
$this->phpci = $phpci;
$this->build = $build;
$this->directory = '';
$this->command = '';
$this->data_offset = 0;
if (isset($options['directory'])) {
$this->directory = $options['directory'];
}
if (isset($options['command'])) {
$this->command = $options['command'];
}
if (isset($options['data_offset'])) {
$this->data_offset = $options['data_offset'];
}
}
public function execute()
{
if (empty($this->command)) {
$this->phpci->logFailure('Configuration command not found.');
return false;
}
if (empty($this->directory)) {
$this->phpci->logFailure('Configuration directory not found.');
return false;
}
$this->phpci->logExecOutput(false);
$cmd = 'cd ' . $this->directory . '; ' . $this->command;
$this->phpci->executeCommand($cmd);
$tapString = $this->phpci->getLastOutput();
if($this->data_offset) {
$tapString = implode("\n", array_slice(explode("\n", $tapString), $this->data_offset));
}
$tapString = mb_convert_encoding($tapString, "UTF-8", "ISO-8859-1");
$tapString = 'TAP version 13' . "\n" . $tapString;
try {
$tapParser = new TapParser($tapString);
$output = $tapParser->parse();
} catch (\Exception $ex) {
$this->phpci->logFailure($tapString);
throw $ex;
}
$failures = $tapParser->getTotalFailures();
$success = ($failures === 0);
$this->build->storeMeta('mocha-errors', $failures);
$this->build->storeMeta('mocha-data', $output);
$this->phpci->logExecOutput(true);
return $success;
}
}