TypePHP 编译器
https://swoole.com/aot/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
96 lines
3.1 KiB
96 lines
3.1 KiB
<?php
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
use TypePhp\Cli\BashCompletion;
|
|
use TypePhp\Cli\CompletionCommand;
|
|
use TypePhp\Cli\CompletionMetadata;
|
|
use TypePhp\Metadata\Constants;
|
|
|
|
final class BashCompletionTest extends TestCase
|
|
{
|
|
public function testGeneratedFileMatchesRenderer(): void
|
|
{
|
|
self::assertSame(
|
|
BashCompletion::render(),
|
|
file_get_contents(ROOT_PATH . '/completions/tpc.bash'),
|
|
);
|
|
}
|
|
|
|
public function testEveryPublicCompilerOptionIsCompleted(): void
|
|
{
|
|
$options = CompletionMetadata::options();
|
|
foreach (Constants::COMPILER_OPTIONS as $name => $definition) {
|
|
if ($name === 'debug-line') {
|
|
continue;
|
|
}
|
|
if (isset($definition['prefix'])) {
|
|
self::assertContains('-' . $definition['prefix'], $options);
|
|
}
|
|
if (isset($definition['longPrefix'])) {
|
|
self::assertContains('--' . $definition['longPrefix'], $options);
|
|
}
|
|
}
|
|
}
|
|
|
|
public function testCompletionCommandOnlyAcceptsBash(): void
|
|
{
|
|
self::assertNull(CompletionCommand::execute(['tpc', 'hello.php']));
|
|
self::assertSame(
|
|
BashCompletion::render(),
|
|
$this->runBash(
|
|
escapeshellarg(PHP_BINARY)
|
|
. ' bin/tpc.php --generate-completion=bash',
|
|
),
|
|
);
|
|
}
|
|
|
|
public function testBashScriptSyntaxAndRepresentativeCompletions(): void
|
|
{
|
|
$script = ROOT_PATH . '/completions/tpc.bash';
|
|
self::assertSame('', $this->runBash("bash -n " . escapeshellarg($script)));
|
|
self::assertStringNotContainsString('mapfile', file_get_contents($script));
|
|
self::assertSame(
|
|
"--wasm=component\n",
|
|
$this->complete($script, ['tpc', '--wasm=c']),
|
|
);
|
|
self::assertSame(
|
|
"bin\nlib\next\n",
|
|
$this->complete($script, ['tpc', '--mode', '']),
|
|
);
|
|
self::assertSame(
|
|
"8.4\n",
|
|
$this->complete($script, ['tpc', '--php-version', '8.4']),
|
|
);
|
|
self::assertSame(
|
|
"8.4\n8.5\n",
|
|
$this->complete($script, ['tpc', '--php-version', '']),
|
|
);
|
|
self::assertSame(
|
|
"-O2\n",
|
|
$this->complete($script, ['tpc', '-O2']),
|
|
);
|
|
}
|
|
|
|
/** @param list<string> $words */
|
|
private function complete(string $script, array $words): string
|
|
{
|
|
$assignments = [];
|
|
foreach ($words as $word) {
|
|
$assignments[] = escapeshellarg($word);
|
|
}
|
|
$command = 'source ' . escapeshellarg($script)
|
|
. '; compopt() { :; }'
|
|
. '; COMP_WORDS=(' . implode(' ', $assignments) . ')'
|
|
. '; COMP_CWORD=' . (count($words) - 1)
|
|
. '; _typephp_tpc'
|
|
. '; printf "%s\\n" "${COMPREPLY[@]}"';
|
|
return $this->runBash('bash -c ' . escapeshellarg($command));
|
|
}
|
|
|
|
private function runBash(string $command): string
|
|
{
|
|
exec($command . ' 2>&1', $output, $status);
|
|
self::assertSame(0, $status, implode(PHP_EOL, $output));
|
|
return $output === [] ? '' : implode(PHP_EOL, $output) . PHP_EOL;
|
|
}
|
|
}
|
|
|