Repository navigation
Expand file tree
/
Copy pathAbstractOrchestrator.php
More file actions
551 lines (448 loc) · 17.1 KB
/
Copy pathAbstractOrchestrator.php
File metadata and controls
551 lines (448 loc) · 17.1 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
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
<?php
declare(strict_types=1);
/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <admin@codeigniter.com>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace CodeIgniter\Test\Orchestrator;
use CodeIgniter\Config\DotEnv;
use CodeIgniter\Exceptions\RuntimeException;
use FilesystemIterator;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
/**
* Base for the orchestrator of a throwaway application skeleton that a package tests against.
*/
abstract class AbstractOrchestrator
{
/**
* Seconds that a timed-out process gets to exit on SIGTERM before it is killed.
*/
private const KILL_GRACE_PERIOD = 2.0;
private const BUILT_MARKER = '.orchestrator-built';
private bool $console = false;
/**
* Absolute path of the skeleton, which is `getSkeletonPath()` followed by:
*
* - `.console` after `bootConsole()`
* - `-` and `ORCHESTRATOR_TEST_TOKEN`, or else a valid `TEST_TOKEN`, so parallel runs do not collide
*/
final public function getPath(): string
{
$token = getenv('ORCHESTRATOR_TEST_TOKEN');
if (is_string($token) && ! $this->isValidToken($token)) {
throw new RuntimeException(
'ORCHESTRATOR_TEST_TOKEN may only contain letters, digits, hyphens and underscores.',
);
}
if ($token === false) {
$token = getenv('TEST_TOKEN');
$token = is_string($token) && $this->isValidToken($token) ? $token : '';
}
return sprintf(
'%s%s%s',
rtrim($this->getSkeletonPath(), '\\/'),
$this->console ? '.console' : '',
is_string($token) && $token !== '' ? '-' . $token : '',
);
}
/**
* Builds the skeleton from the framework that this class ships with, replacing any previous one.
*/
final public function build(): void
{
$framework = $this->getFrameworkPath();
$path = $this->getPath();
require_once $framework . '/system/Helpers/filesystem_helper.php';
$this->ensureUnlinkedAncestors();
$this->removePath($path . '/' . self::BUILT_MARKER);
$this->removePath($path);
foreach (['app', 'public'] as $directory) {
directory_mirror($framework . '/' . $directory, $path . '/' . $directory);
}
$this->mirrorWritable($framework . '/writable', $path . '/writable');
$this->failUnless(copy($framework . '/spark', $path . '/spark'), 'copy spark');
$this->writeAutoloader();
$this->writeEnv();
foreach ($this->getFixtures() as $fixtures) {
directory_mirror($fixtures, $path);
}
$this->pointPathsAtTheFramework();
$this->afterBuild();
$this->failUnless(touch($path . '/' . self::BUILT_MARKER), 'mark the build complete');
}
/**
* Builds the skeleton, then boots the framework in this process against it for `CIUnitTestCase` tests.
*
* Call it from the PHPUnit bootstrap file. The path constants are process-wide, so a process hosts one skeleton.
*/
final public function boot(): void
{
if ($this->console) {
throw new RuntimeException('Cannot call boot() after bootConsole().');
}
$this->build();
$this->defineConstants();
require $this->getFrameworkPath() . '/system/Test/bootstrap.php';
}
/**
* Boots the framework in this process through **system/util_bootstrap.php** against its own skeleton.
*
* `getEnv()` and `afterBuild()` run only when the skeleton is incomplete or `$rebuild` is true.
*/
final public function bootConsole(bool $rebuild = false): void
{
$this->console = true;
if ($rebuild || ! is_file($this->getPath() . '/' . self::BUILT_MARKER)) {
$this->build();
}
require_once $this->getFrameworkPath() . '/system/Config/DotEnv.php';
(new DotEnv($this->getPath()))->load();
$this->defineConstants();
require $this->getFrameworkPath() . '/system/util_bootstrap.php';
}
/**
* Path constants of the built skeleton and the package, keyed by name.
*
* @return array<string, string>
*/
public function getPathConstants(): array
{
$path = $this->getRealRoot();
if ($path === '') {
throw new RuntimeException('Cannot read the path constants before the orchestrated application is built.');
}
$path .= DIRECTORY_SEPARATOR;
$testPath = rtrim($this->getPackageRoot(), '\\/') . '/tests' . DIRECTORY_SEPARATOR;
$constants = [
'HOMEPATH' => $path,
'CONFIGPATH' => $path . 'app/Config' . DIRECTORY_SEPARATOR,
'PUBLICPATH' => $path . 'public' . DIRECTORY_SEPARATOR,
'TESTPATH' => $testPath,
];
if (is_dir($testPath . '_support')) {
$constants['SUPPORTPATH'] = $testPath . '_support' . DIRECTORY_SEPARATOR;
}
return $constants;
}
/**
* Runs spark in the skeleton and returns its outcome.
*
* @param list<string> $arguments
* @param array<string, string|null> $environment Variables to set, or to unset when `null`
* @param float|null $timeout Seconds after which the process is terminated, or `null` to wait indefinitely
*/
public function spark(array $arguments, string $stdin = '', array $environment = [], ?float $timeout = null): SparkResult
{
$input = tmpfile();
$stdout = tmpfile();
$stderr = tmpfile();
if ($input === false || $stdout === false || $stderr === false) {
throw new RuntimeException('Cannot create the temporary streams for spark.');
}
fwrite($input, $stdin);
rewind($input);
$process = proc_open(
[PHP_BINARY, $this->getPath() . '/spark', ...$arguments],
[0 => $input, 1 => $stdout, 2 => $stderr],
$pipes,
$this->getPath(),
array_filter([...getenv(), ...$environment], is_string(...)),
);
if ($process === false) {
throw new RuntimeException('Cannot start spark for the orchestrated application.');
}
[$exitCode, $timedOut] = $this->wait($process, $timeout);
rewind($stdout);
rewind($stderr);
return new SparkResult(
$exitCode,
(string) stream_get_contents($stdout),
(string) stream_get_contents($stderr),
$timedOut,
);
}
/**
* Writes a file of the skeleton, creating its directory when missing.
*/
public function write(string $path, string $contents): void
{
$file = $this->resolve($path);
$root = $this->getRealRoot();
if ($root === '') {
throw new RuntimeException(
sprintf('Cannot write %s before the orchestrated application is built.', $path),
);
}
$this->ensureInside($file, $root, $path);
if (! is_dir(dirname($file))) {
$this->failUnless(mkdir(dirname($file), 0755, true), sprintf('create the directory of %s', $path));
}
$this->failUnless(file_put_contents($file, $contents) !== false, sprintf('write %s', $path));
}
/**
* Deletes a file or a directory tree of the skeleton, if present.
*/
public function delete(string $path): void
{
$target = $this->resolve($path);
$root = $this->getRealRoot();
if ($root === '') {
return;
}
$this->ensureInside(dirname($target), $root, $path);
$this->removePath($target);
}
/**
* Removes the skeleton, if present.
*/
final public function clean(): void
{
$this->ensureUnlinkedAncestors();
$this->removePath($this->getPath());
}
/**
* Absolute path of the framework that the skeleton is copied from.
*/
public function getFrameworkPath(): string
{
return dirname(__DIR__, 3);
}
/**
* Absolute path of the package under test, the directory holding its `vendor/autoload.php`.
*/
abstract protected function getPackageRoot(): string;
/**
* Absolute path of the skeleton before the suffixes added by `getPath()`.
*/
protected function getSkeletonPath(): string
{
return rtrim($this->getPackageRoot(), '\\/') . '/build/orchestrator';
}
/**
* Absolute paths of directories mirrored onto the skeleton, in order, after the framework files.
*
* @return list<string>
*/
protected function getFixtures(): array
{
return [];
}
/**
* Settings written to the skeleton's `.env`, keyed by name.
*
* @return array<non-empty-string, string>
*/
protected function getEnv(): array
{
return [];
}
/**
* Runs at the end of `build()`, once the skeleton is complete.
*/
protected function afterBuild(): void
{
}
private function defineConstants(): void
{
foreach ($this->getPathConstants() as $name => $value) {
if (! defined($name)) {
define($name, $value);
}
}
}
private function isValidToken(string $token): bool
{
return preg_match('/\A[A-Za-z0-9_-]*\z/', $token) === 1;
}
private function resolve(string $path): string
{
$segments = preg_split('#[\\\\/]+#', $path, -1, PREG_SPLIT_NO_EMPTY);
if ($segments === false || $segments === [] || in_array('..', $segments, true)) {
throw new RuntimeException(
sprintf('Cannot use "%s" as a path inside the orchestrated application.', $path),
);
}
return $this->getPath() . '/' . implode('/', $segments);
}
/**
* @return string The resolved skeleton root, or an empty string when the skeleton does not exist
*/
private function getRealRoot(): string
{
$root = rtrim($this->getPath(), '\\/');
$this->ensureUnlinkedAncestors();
if (is_link($root)) {
throw new RuntimeException(
sprintf('Cannot use the symlinked %s as the orchestrated application.', $root),
);
}
$real = realpath($root);
return $real === false ? '' : $real;
}
/**
* Refuses a symlinked parent of the skeleton, and any symlinked directory between it and the package root.
*/
private function ensureUnlinkedAncestors(): void
{
$root = str_replace('\\', '/', rtrim($this->getPackageRoot(), '\\/'));
$directory = dirname(str_replace('\\', '/', rtrim($this->getPath(), '\\/')));
do {
if (is_link($directory)) {
throw new RuntimeException(
sprintf('Cannot place the orchestrated application in the symlinked %s.', $directory),
);
}
$directory = dirname($directory);
} while (str_starts_with($directory, $root . '/'));
}
private function ensureInside(string $path, string $root, string $given): void
{
while (! file_exists($path) && ! is_link($path) && dirname($path) !== $path) {
$path = dirname($path);
}
$real = realpath($path);
if ($real === false || ($real !== $root && ! str_starts_with($real, $root . DIRECTORY_SEPARATOR))) {
throw new RuntimeException(sprintf('Cannot reach %s outside the orchestrated application.', $given));
}
}
private function removePath(string $path): void
{
$path = rtrim($path, '\\/');
if (is_link($path) || is_file($path)) {
$this->removeEntry($path);
} elseif (is_dir($path)) {
$entries = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($path, FilesystemIterator::SKIP_DOTS),
RecursiveIteratorIterator::CHILD_FIRST,
);
foreach ($entries as $entry) {
if ($entry->isDir() && ! $entry->isLink()) {
$this->failUnless(rmdir($entry->getPathname()), sprintf('remove %s', $entry->getPathname()));
} else {
$this->removeEntry($entry->getPathname());
}
}
$this->failUnless(rmdir($path), sprintf('remove %s', $path));
}
}
private function removeEntry(string $path): void
{
$this->failUnless(
PHP_OS_FAMILY === 'Windows' && is_dir($path) ? rmdir($path) : unlink($path),
sprintf('remove %s', $path),
);
}
/**
* Wait for the given process to finish, with an optional timeout.
* Returns the exit code and whether the process was terminated for exceeding the timeout.
*
* @param resource $process
*
* @return array{int, bool}
*/
private function wait(mixed $process, ?float $timeout): array
{
if ($timeout === null) {
return [proc_close($process), false];
}
$deadline = microtime(true) + $timeout;
$exitCode = null;
$timedOut = false;
$killAt = null;
while ($exitCode === null) {
$status = proc_get_status($process);
if (! $status['running']) {
$exitCode = $status['exitcode'];
continue;
}
$now = microtime(true);
if (! $timedOut && $now >= $deadline) {
$timedOut = true;
$killAt = $now + self::KILL_GRACE_PERIOD;
proc_terminate($process);
} elseif ($killAt !== null && $now >= $killAt) {
$killAt = null;
proc_terminate($process, 9);
}
usleep(1000);
}
proc_close($process);
return [$exitCode, $timedOut];
}
private function mirrorWritable(string $origin, string $target): void
{
$this->failUnless(is_dir($target) || mkdir($target, 0755, true), 'create writable');
foreach (['index.html', '.htaccess'] as $file) {
if (is_file($origin . '/' . $file)) {
$this->failUnless(copy($origin . '/' . $file, $target . '/' . $file), sprintf('copy writable/%s', $file));
}
}
$entries = scandir($origin);
$this->failUnless($entries !== false, 'read writable');
foreach (array_diff($entries, ['.', '..']) as $directory) {
if (! is_file($origin . '/' . $directory . '/index.html')) {
continue;
}
$this->failUnless(mkdir($target . '/' . $directory, 0755), sprintf('create writable/%s', $directory));
$this->failUnless(
copy($origin . '/' . $directory . '/index.html', $target . '/' . $directory . '/index.html'),
sprintf('copy writable/%s/index.html', $directory),
);
}
}
private function pointPathsAtTheFramework(): void
{
$paths = $this->getPath() . '/app/Config/Paths.php';
$contents = file_get_contents($paths);
$this->failUnless($contents !== false, 'read Paths.php');
$rewritten = str_replace(
"__DIR__ . '/../../system'",
var_export($this->getFrameworkPath() . '/system', true),
$contents,
$count,
);
$this->failUnless($count === 1, 'find the system directory in Paths.php');
$this->failUnless(write_file($paths, $rewritten), 'rewrite Paths.php');
}
private function writeAutoloader(): void
{
$this->failUnless(mkdir($this->getPath() . '/vendor', 0755, true), 'create vendor');
$this->failUnless(write_file($this->getPath() . '/vendor/autoload.php', sprintf(
"<?php\n\nreturn require %s;\n",
var_export(rtrim($this->getPackageRoot(), '\\/') . '/vendor/autoload.php', true),
)), 'write the autoloader shim');
}
private function writeEnv(): void
{
$contents = '';
foreach (['CI_ENVIRONMENT' => 'development', ...$this->getEnv()] as $name => $value) {
if (preg_match('/\A[A-Za-z0-9_.\\\\-]+\z/', $name) !== 1) {
throw new RuntimeException(sprintf(
'Cannot write the .env setting "%s", whose name may only contain letters, digits, ".", "_", "-" and "\\".',
$name,
));
}
if (strpbrk($value, "\r\n") !== false) {
throw new RuntimeException(
sprintf('Cannot write the .env setting %s, whose value contains a line break.', $name),
);
}
$contents .= sprintf("%s = \"%s\"\n", $name, str_replace(['\\', '"'], ['\\\\', '\\"'], $value));
}
$this->failUnless(write_file($this->getPath() . '/.env', $contents), 'write .env');
}
/**
* @phpstan-assert true $succeeded
*/
private function failUnless(bool $succeeded, string $step): void
{
if (! $succeeded) {
throw new RuntimeException(sprintf('Cannot %s for the orchestrated application.', $step));
}
}
}