-
-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathTestStartup.php
More file actions
227 lines (196 loc) · 5.43 KB
/
TestStartup.php
File metadata and controls
227 lines (196 loc) · 5.43 KB
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
<?php
/**
*
* EPV :: The phpBB Forum Extension Pre Validator.
*
* @copyright (c) 2014 phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
*/
namespace Phpbb\Epv\Tests;
use Gitonomy\Git\Admin;
use Phpbb\Epv\Output\Output;
use Phpbb\Epv\Output\OutputInterface;
use Phpbb\Epv\Tests\Exception\TestException;
class TestStartup
{
/** @var string */
private $dir = null;
/** @var bool */
private $debug = false;
/** @var int|null */
private $type = null;
/** @var \Phpbb\Epv\Output\OutputInterface */
private $output;
const TYPE_DIRECTORY = 1;
const TYPE_GIT = 2;
const TYPE_GITHUB = 3;
/**
* @param OutputInterface $output Output formatter
* @param $type int Type what the location is
* @param $location string Location where the extension is
* @param $debug boolean if debug is enabled
* @parm $oauth_token string The personal oauth string to use
* @param string $branch When using GIT and GITHUB you can provide a branch name. When empty, defaults to master
* @throws TestException
*/
public function __construct(OutputInterface $output, $type, $location, $debug, $oauth = '', $branch = '')
{
$this->output = $output;
$rundir = true;
if ($type == self::TYPE_GITHUB)
{
$location = 'https://github.com/' . $location;
$type = self::TYPE_GIT;
}
if ($type == self::TYPE_GIT)
{
$location = $this->initGit($location, $branch, $oauth);
$rundir = false;
}
$this->type = $type;
$this->dir = $location;
$this->debug = $debug;
$this->runTests($rundir);
$this->cleanUp();
$this->printResults();
}
/**
* Init a git repository
*
* @param string $git Location of the git repo
* @param string $branch branch to checkout
* @param string $oauth Personal oauth string to use
*
* @throws Exception\TestException
* @return string local directory of the cloned repo
*/
private function initGit($git, $branch, $oauth)
{
if (empty($branch))
{
$branch = 'master';
}
$this->output->writeln(sprintf("Checkout %s from git on branch %s.", $git, $branch));
$tmpdir = sys_get_temp_dir();
$uniq = $tmpdir . DIRECTORY_SEPARATOR . uniqid();
@mkdir($uniq);
if (!file_exists($uniq))
{
throw new TestException('Unable to create tmp directory');
}
if (!empty($oauth) && strpos($git, 'https://github.com') !== false)
{
$oauth = $oauth . ':x-oauth-basic@';
$git = str_replace('github.com', $oauth . 'github.com', $git);
}
Admin::cloneBranchTo($uniq, $git, $branch, false);
return $uniq;
}
/**
* Run the test suite with the current directory.
*
* @param boolean $printDir print directory information
* @throws TestException
*/
private function runTests($printDir = true)
{
$dir = '';
if ($printDir)
{
$dir = "on directory <info>$this->dir</info>";
}
$this->output->writeln("Running Extension Pre Validator {$dir}.");
$runner = new TestRunner($this->output, $this->dir, $this->debug);
if ($this->debug)
{
$this->output->writelnIfDebug("tests to run:");
foreach ($runner->tests as $t => $test)
{
$this->output->writelnIfDebug("<info>$test</info>");
}
}
$runner->runTests();
}
/**
* Print the results from the tests
*/
private function printResults()
{
// Write a empty line
$this->output->writeLn('');
$found_msg = ' ';
$found_msg .= 'Fatal: ' . $this->output->getMessageCount(Output::FATAL);
$found_msg .= ', Error: ' . $this->output->getMessageCount(Output::ERROR);
$found_msg .= ', Warning: ' . $this->output->getMessageCount(Output::WARNING);
$found_msg .= ', Notice: ' . $this->output->getMessageCount(Output::NOTICE);
$found_msg .= ' ';
if ($this->output->getMessageCount(Output::FATAL) > 0 || $this->output->getMessageCount(Output::ERROR) > 0 || $this->output->getMessageCount(Output::WARNING) > 0)
{
$this->output->writeln('<fatal>' . str_repeat(' ', strlen($found_msg)) . '</fatal>');
$this->output->writeln('<fatal> Validation: FAILED' . str_repeat(' ', strlen($found_msg) - 19) . '</fatal>');
$this->output->writeln('<fatal>' . $found_msg . '</fatal>');
$this->output->writeln('<fatal>' . str_repeat(' ', strlen($found_msg)) . '</fatal>');
$this->output->writeln('');
}
else
{
$this->output->writeln('<success>PASSED: ' . $found_msg . '</success>');
}
// Write debug messages.
if ($this->debug)
{
foreach ($this->output->getDebugMessages() as $msg)
{
$this->output->writeln((string)$msg);
}
}
$this->output->writeln("<info>Test results for extension:</info>");
foreach ($this->output->getMessages() as $msg)
{
$this->output->writeln((string)$msg);
}
if (sizeof($this->output->getMessages()) == 0)
{
$this->output->writeln("<success>No issues found </success>");
}
}
/**
* Cleanup the mess we made
*/
private function cleanUp()
{
if ($this->type == self::TYPE_GIT)
{
$this->rrmdir($this->dir);
}
}
/**
* Remove a directory including the contents
*
* @param $dir string Directory to remove
*/
private function rrmdir($dir)
{
if (is_dir($dir))
{
$objects = scandir($dir);
foreach ($objects as $object)
{
if ($object != "." && $object != "..")
{
if (filetype($dir . "/" . $object) == "dir")
{
$this->rrmdir($dir . "/" . $object);
}
else
{
@unlink($dir . "/" . $object);
}
}
}
reset($objects);
rmdir($dir);
}
}
}