forked from PHP-CS-Fixer/PHP-CS-Fixer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CiIntegrationTest.php
274 lines (244 loc) · 8.82 KB
/
CiIntegrationTest.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
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
<?php
/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <fabien@symfony.com>
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace PhpCsFixer\Tests\Smoke;
use Keradus\CliExecutor\CommandExecutor;
use Keradus\CliExecutor\ScriptExecutor;
use PhpCsFixer\Console\Application;
/**
* @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* @internal
*
* @requires OS Linux|Darwin
* @coversNothing
* @group covers-nothing
* @large
*/
final class CiIntegrationTest extends AbstractSmokeTest
{
public static $fixtureDir;
public static function doSetUpBeforeClass()
{
parent::doSetUpBeforeClass();
self::$fixtureDir = __DIR__.'/../Fixtures/ci-integration';
try {
CommandExecutor::create('composer --version', __DIR__)->getResult();
} catch (\RuntimeException $e) {
static::markTestSkippedOrFail('Missing `composer` env script. Details:'."\n".$e->getMessage());
}
try {
CommandExecutor::create('composer check', __DIR__.'/../..')->getResult();
} catch (\RuntimeException $e) {
static::markTestSkippedOrFail('Composer check failed. Details:'."\n".$e->getMessage());
}
try {
self::executeScript([
'rm -rf .git',
'git init -q',
'git config user.name test',
'git config user.email test',
'git add .',
'git commit -m "init" -q',
]);
} catch (\RuntimeException $e) {
static::markTestSkippedOrFail($e->getMessage());
}
}
public static function doTearDownAfterClass()
{
parent::doTearDownAfterClass();
self::executeCommand('rm -rf .git');
}
protected function doTearDown()
{
parent::doTearDown();
self::executeScript([
'git reset . -q',
'git checkout . -q',
'git clean -fdq',
'git checkout master -q',
]);
}
/**
* @param string $branchName
* @param string[] $caseCommands
* @param string[] $expectedResult1Lines
* @param string[] $expectedResult2Lines
* @param string $expectedResult3Files
*
* @dataProvider provideIntegrationCases
*/
public function testIntegration(
$branchName,
array $caseCommands,
array $expectedResult1Lines,
array $expectedResult2Lines,
$expectedResult3Files
) {
self::executeScript(array_merge(
[
"git checkout -b {$branchName} -q",
],
$caseCommands
));
$integrationScript = explode("\n", str_replace('vendor/bin/', './../../../', file_get_contents(__DIR__.'/../../ci-integration.sh')));
$steps = [
"COMMIT_RANGE=\"master..{$branchName}\"",
"{$integrationScript[3]}\n{$integrationScript[4]}",
$integrationScript[5],
$integrationScript[6],
$integrationScript[7],
];
$result1 = self::executeScript([
$steps[0],
$steps[1],
$steps[2],
'echo "$CHANGED_FILES"',
]);
static::assertSame(implode("\n", $expectedResult1Lines)."\n", $result1->getOutput());
$result2 = self::executeScript([
$steps[0],
$steps[1],
$steps[2],
$steps[3],
'echo "${EXTRA_ARGS}"',
]);
static::assertSame(implode("\n", $expectedResult2Lines), $result2->getOutput());
$result3 = self::executeScript([
$steps[0],
$steps[1],
$steps[2],
$steps[3],
$steps[4],
]);
$optionalIncompatibilityWarning = 'PHP needs to be a minimum version of PHP 5.6.0 and maximum version of PHP 7.4.*.
Current PHP version: '.PHP_VERSION.'.
Ignoring environment requirements because `PHP_CS_FIXER_IGNORE_ENV` is set. Execution may be unstable.
';
$optionalXdebugWarning = 'You are running PHP CS Fixer with xdebug enabled. This has a major impact on runtime performance.
If you need help while solving warnings, ask at https://gitter.im/PHP-CS-Fixer, we will help you!
';
$pattern = sprintf(
'/^(?:%s)?(?:%s)?%s\n%s\n%s\n([\.S]{%d})\n%s$/',
preg_quote($optionalIncompatibilityWarning, '/'),
preg_quote($optionalXdebugWarning, '/'),
'PHP CS Fixer '.preg_quote(Application::VERSION, '/').' '.preg_quote(Application::VERSION_CODENAME, '/').' by Fabien Potencier and Dariusz Ruminski',
preg_quote(sprintf('Runtime: PHP %s', PHP_VERSION), '/'),
preg_quote('Loaded config default from ".php_cs.dist".', '/'),
\strlen($expectedResult3Files),
preg_quote('Legend: ?-unknown, I-invalid file syntax (file ignored), S-skipped (cached or empty file), .-no changes, F-fixed, E-error', '/')
);
static::assertMatchesRegularExpression($pattern, $result3->getError());
preg_match($pattern, $result3->getError(), $matches);
static::assertArrayHasKey(1, $matches);
static::assertSame(substr_count($expectedResult3Files, '.'), substr_count($matches[1], '.'));
static::assertSame(substr_count($expectedResult3Files, 'S'), substr_count($matches[1], 'S'));
static::assertMatchesRegularExpression(
'/^\s*Checked all files in \d+\.\d+ seconds, \d+\.\d+ MB memory used\s*$/',
$result3->getOutput()
);
}
public function provideIntegrationCases()
{
return [
'random-changes' => [
'random-changes',
[
'touch dir\ a/file.php',
'rm -r dir\ c',
'echo "" >> dir\ b/file\ b.php',
'echo "echo 1;" >> dir\ b/file\ b.php',
'git add .',
'git commit -m "Random changes" -q',
],
[
'dir a/file.php',
'dir b/file b.php',
],
[
'--path-mode=intersection',
'--',
'dir a/file.php',
'dir b/file b.php',
'',
],
'S.',
],
'changes-including-dist-config-file' => [
'changes-including-dist-config-file',
[
'echo "" >> dir\ b/file\ b.php',
'echo "echo 1;" >> dir\ b/file\ b.php',
// `sed -i ...` is not handled the same on Linux and macOS
'sed -e \'s/@Symfony/@PSR2/\' .php_cs.dist > .php_cs.dist.new',
'mv .php_cs.dist.new .php_cs.dist',
'git add .',
'git commit -m "Random changes including config file" -q',
],
[
'.php_cs.dist',
'dir b/file b.php',
],
[
'',
'',
],
'....',
],
'changes-including-custom-config-file-creation' => [
'changes-including-custom-config-file-creation',
[
'echo "" >> dir\ b/file\ b.php',
'echo "echo 1;" >> dir\ b/file\ b.php',
'sed -e \'s/@Symfony/@PSR2/\' .php_cs.dist > .php_cs',
'git add .',
'git commit -m "Random changes including custom config file creation" -q',
],
[
'.php_cs',
'dir b/file b.php',
],
[
'',
'',
],
'.....',
],
'changes-including-composer-lock' => [
'changes-including-composer-lock',
[
'echo "" >> dir\ b/file\ b.php',
'echo "echo 1;" >> dir\ b/file\ b.php',
'touch composer.lock',
'git add .',
'git commit -m "Random changes including composer.lock" -q',
],
[
'composer.lock',
'dir b/file b.php',
],
[
'',
'',
],
'....',
],
];
}
private static function executeCommand($command)
{
return CommandExecutor::create($command, self::$fixtureDir)->getResult();
}
private static function executeScript(array $scriptParts)
{
return ScriptExecutor::create($scriptParts, self::$fixtureDir)->getResult();
}
}