Skip to content

Commit 2d6bf12

Browse files
committed
Expand Doctor tests and more windows fixes
1 parent b0e500d commit 2d6bf12

3 files changed

Lines changed: 127 additions & 4 deletions

File tree

‎src/QuickInstall/Sandbox/DoctorService.php‎

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,26 @@ public function __construct(Project $project, ?ProcessRunner $runner = null)
2323

2424
public function checks(): array
2525
{
26+
$iniPath = php_ini_loaded_file();
27+
$projectWritable = $this->projectWritable();
2628
$checks = [
2729
$this->check('PHP 8+', PHP_VERSION_ID >= 80000, PHP_VERSION),
28-
$this->check('JSON extension', extension_loaded('json'), extension_loaded('json') ? 'loaded' : 'missing'),
30+
$this->check('PHP CLI', PHP_SAPI === 'cli', PHP_SAPI),
31+
$this->check('PHP configuration', true, $iniPath !== false ? $iniPath : 'no php.ini loaded; using built-in defaults'),
32+
$this->extensionCheck('JSON', 'json'),
33+
$this->extensionCheck('OpenSSL', 'openssl', 'extension=openssl'),
34+
$this->extensionCheck('Phar', 'phar'),
35+
$this->extensionCheck('Filter', 'filter'),
36+
$this->extensionCheck('Hash', 'hash'),
37+
$this->extensionCheck('ZIP', 'zip', 'extension=zip'),
38+
$this->check(
39+
'URL streams',
40+
filter_var(ini_get('allow_url_fopen'), FILTER_VALIDATE_BOOLEAN),
41+
filter_var(ini_get('allow_url_fopen'), FILTER_VALIDATE_BOOLEAN) ? 'allow_url_fopen enabled' : 'enable allow_url_fopen=On in php.ini'
42+
),
2943
$this->check('Process execution', function_exists('proc_open'), function_exists('proc_open') ? 'available' : 'proc_open is disabled'),
44+
$this->check('Network sockets', function_exists('fsockopen'), function_exists('fsockopen') ? 'available' : 'fsockopen is disabled'),
45+
$this->check('Project writable', $projectWritable, $projectWritable ? $this->project->rootPath() : 'QuickInstall project directory is not writable'),
3046
];
3147

3248
$docker = $this->capture(['docker', 'version', '--format', '{{.Server.Version}}']);
@@ -44,9 +60,15 @@ public function checks(): array
4460

4561
$git = $this->capture(['git', '--version']);
4662
$checks[] = $this->check('Git', $git['exit_code'] === 0, $this->detail($git, 'not available'));
47-
if (is_file($this->project->rootPath('composer.phar')))
63+
$composerPhar = $this->project->rootPath('composer.phar');
64+
if (is_file($composerPhar))
4865
{
49-
$checks[] = $this->check('Composer', true, 'bundled composer.phar');
66+
$composer = $this->capture([PHP_BINARY, $composerPhar, '--version', '--no-ansi']);
67+
$checks[] = $this->check(
68+
'Composer',
69+
$composer['exit_code'] === 0,
70+
$composer['exit_code'] === 0 ? 'bundled ' . $this->firstDetail($composer, 'composer.phar') : $this->detail($composer, 'bundled composer.phar could not run')
71+
);
5072
}
5173
else
5274
{
@@ -57,6 +79,29 @@ public function checks(): array
5779
return $checks;
5880
}
5981

82+
private function extensionCheck(string $label, string $extension, ?string $setting = null): array
83+
{
84+
$loaded = extension_loaded($extension);
85+
$detail = 'loaded';
86+
if (!$loaded)
87+
{
88+
$detail = "missing PHP $extension extension";
89+
if ($setting !== null)
90+
{
91+
$iniPath = php_ini_loaded_file();
92+
$detail .= '; enable ' . $setting . ' in ' . ($iniPath !== false ? $iniPath : 'php.ini');
93+
}
94+
}
95+
96+
return $this->check("$label extension", $loaded, $detail);
97+
}
98+
99+
private function projectWritable(): bool
100+
{
101+
$path = is_dir($this->project->workspacePath()) ? $this->project->workspacePath() : $this->project->rootPath();
102+
return is_dir($path) && is_writable($path);
103+
}
104+
60105
private function capture(array $command): array
61106
{
62107
try
@@ -84,4 +129,15 @@ private function detail(array $result, string $fallback): string
84129
$lines = preg_split('/\R/', $output) ?: [];
85130
return trim((string) end($lines));
86131
}
132+
133+
private function firstDetail(array $result, string $fallback): string
134+
{
135+
$output = trim((string) ($result['output'] ?? ''));
136+
if ($output === '')
137+
{
138+
return $fallback;
139+
}
140+
$lines = preg_split('/\R/', $output) ?: [];
141+
return trim((string) reset($lines));
142+
}
87143
}

‎src/QuickInstall/Sandbox/ProcessRunner.php‎

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ public function capture(array $command, ?string $cwd = null): array
4242
private function execute(array $command, bool $stream, ?string $cwd): array
4343
{
4444
$command = $this->platformCommand($command);
45+
if ($this->osFamily === 'Windows')
46+
{
47+
return $this->executeWithFiles($command, $stream, $cwd);
48+
}
49+
4550
$descriptor = [
4651
0 => ['pipe', 'r'],
4752
1 => ['pipe', 'w'],
@@ -112,6 +117,50 @@ private function execute(array $command, bool $stream, ?string $cwd): array
112117
];
113118
}
114119

120+
private function executeWithFiles(array $command, bool $stream, ?string $cwd): array
121+
{
122+
$stdoutPath = tempnam(sys_get_temp_dir(), 'qi-process-out-');
123+
$stderrPath = tempnam(sys_get_temp_dir(), 'qi-process-err-');
124+
if ($stdoutPath === false || $stderrPath === false)
125+
{
126+
if ($stdoutPath !== false)
127+
{
128+
@unlink($stdoutPath);
129+
}
130+
if ($stderrPath !== false)
131+
{
132+
@unlink($stderrPath);
133+
}
134+
return ['exit_code' => 1, 'output' => 'Unable to create temporary process output files.'];
135+
}
136+
137+
$descriptor = [
138+
0 => ['file', $this->nullDevice(), 'r'],
139+
1 => ['file', $stdoutPath, 'w'],
140+
2 => ['file', $stderrPath, 'w'],
141+
];
142+
$process = @proc_open($command, $descriptor, $pipes, $cwd);
143+
if (!is_resource($process))
144+
{
145+
@unlink($stdoutPath);
146+
@unlink($stderrPath);
147+
return ['exit_code' => 1, 'output' => ''];
148+
}
149+
150+
$exitCode = proc_close($process);
151+
$stdout = (string) @file_get_contents($stdoutPath);
152+
$stderr = (string) @file_get_contents($stderrPath);
153+
@unlink($stdoutPath);
154+
@unlink($stderrPath);
155+
if ($stream)
156+
{
157+
$this->output->write($stdout);
158+
$this->output->error($stderr);
159+
}
160+
161+
return ['exit_code' => $exitCode, 'output' => $stdout . $stderr];
162+
}
163+
115164
private function failureDetails(array $command, array $result, bool $includeOutputSummary = true): string
116165
{
117166
$details = $includeOutputSummary ? $this->outputSummary((string) ($result['output'] ?? '')) : '';
@@ -124,7 +173,7 @@ private function runWithStreamOutput(array $command, ?string $cwd): array
124173
{
125174
$command = $this->platformCommand($command);
126175
$descriptor = [
127-
0 => ['file', $this->osFamily === 'Windows' ? 'NUL' : '/dev/null', 'r'],
176+
0 => ['file', $this->nullDevice(), 'r'],
128177
1 => $this->output->stdout(),
129178
2 => $this->output->stderr(),
130179
];
@@ -158,6 +207,11 @@ private function platformCommand(array $command): array
158207
return [getenv('COMSPEC') ?: 'cmd.exe', '/D', '/S', '/C', '"' . $commandLine . '"'];
159208
}
160209

210+
private function nullDevice(): string
211+
{
212+
return $this->osFamily === 'Windows' && PHP_OS_FAMILY === 'Windows' ? 'NUL' : '/dev/null';
213+
}
214+
161215
private function escapeWindowsBatchArgument(string $argument): string
162216
{
163217
// cmd.exe expands percent variables even inside quotes. Doubling percent signs

‎tests/Unit/ProcessRunnerTest.php‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,19 @@
1010

1111
class ProcessRunnerTest extends TestCase
1212
{
13+
public function testWindowsCaptureUsesFilesWithoutPipeDeadlock(): void
14+
{
15+
$runner = new ProcessRunner(new BufferedOutput(), 'Windows');
16+
$script = 'fwrite(STDOUT, str_repeat("o", 200000)); fwrite(STDERR, str_repeat("e", 200000));';
17+
18+
$result = $runner->capture([PHP_BINARY, '-r', $script]);
19+
20+
self::assertSame(0, $result['exit_code']);
21+
self::assertSame(400000, strlen($result['output']));
22+
self::assertStringStartsWith('oooo', $result['output']);
23+
self::assertStringEndsWith('eeee', $result['output']);
24+
}
25+
1326
public function testMissingCommandFailsWithoutPhpWarning(): void
1427
{
1528
$warnings = [];

0 commit comments

Comments
 (0)