Files
MeshVault/tests/Integration/TaskRunnerTest.php

80 lines
3.8 KiB
PHP

<?php
declare(strict_types=1);
namespace MeshVault\Tests\Integration;
use PHPUnit\Framework\TestCase;
final class TaskRunnerTest extends TestCase
{
public function testCommandsFromAnotherWorkingDirectory(): void
{
$root = dirname(__DIR__, 2);
$temporary = sys_get_temp_dir() . '/meshvault-runner-' . bin2hex(random_bytes(6));
mkdir($temporary);
$windows = PHP_OS_FAMILY === 'Windows';
$fake = $temporary . ($windows ? '/docker.cmd' : '/docker');
file_put_contents($fake, $windows
? "@echo off\r\necho cwd=%CD%\r\necho %*\r\nexit /b 37\r\n"
: "#!/bin/sh\nprintf 'cwd=%s\\n' \"\$PWD\"\nprintf '%s\\n' \"\$@\"\nexit 37\n");
chmod($fake, 0755);
$environment = getenv();
$environment[$windows ? 'Path' : 'PATH'] = $temporary . PATH_SEPARATOR . getenv('PATH');
$runner = $windows
? ['powershell.exe', '-NoProfile', '-ExecutionPolicy', 'Bypass', '-File', $root . '/dev.ps1']
: ['sh', $root . '/dev.sh'];
try {
foreach (['up', 'down', 'test', 'lint', 'assets', 'watch'] as $command) {
[$code, $output] = $this->invoke(
[...$runner, $command, '--probe', 'two words'],
$temporary,
$environment
);
self::assertSame(37, $code, $command . ': ' . $output);
self::assertStringContainsString('compose', $output);
self::assertStringContainsString('--probe', $output);
self::assertStringContainsString('two words', $output);
self::assertStringContainsString(str_replace('\\', '/', $root), str_replace('\\', '/', $output));
}
foreach (['migrate', 'seed'] as $command) {
[$code, $output] = $this->invoke([...$runner, $command], $temporary, $environment);
self::assertSame(37, $code, $command . ': ' . $output);
self::assertStringContainsString('compose', $output);
self::assertStringContainsString(str_replace('\\', '/', $root), str_replace('\\', '/', $output));
}
// Import engine entrypoints must reach bin/meshvault with their arguments intact.
foreach (['import', 'worker', 'worker-once'] as $command) {
[$code, $output] = $this->invoke([...$runner, $command, '--probe'], $temporary, $environment);
self::assertSame(37, $code, $command . ': ' . $output);
self::assertStringContainsString('bin/meshvault', $output);
self::assertStringContainsString($command, $output);
self::assertStringContainsString('--probe', $output);
}
foreach (['unknown'] as $command) {
[$code, $output] = $this->invoke([...$runner, $command], $temporary, $environment);
self::assertNotSame(0, $code);
self::assertStringNotContainsString('cwd=', $output);
}
foreach (['-v', '--volumes', '--volumes=true'] as $flag) {
[$code, $output] = $this->invoke([...$runner, 'down', $flag], $temporary, $environment);
self::assertNotSame(0, $code);
self::assertStringNotContainsString('cwd=', $output);
}
} finally {
unlink($fake);
rmdir($temporary);
}
}
private function invoke(array $command, string $cwd, array $environment): array
{
$process = proc_open($command, [1 => ['pipe', 'w'], 2 => ['pipe', 'w']], $pipes, $cwd, $environment);
self::assertIsResource($process);
$output = stream_get_contents($pipes[1]) . stream_get_contents($pipes[2]);
fclose($pipes[1]);
fclose($pipes[2]);
return [proc_close($process), $output];
}
}