diff --git a/src/Console/Commands/InstallCommand.php b/src/Console/Commands/InstallCommand.php index 5593ff2..1e80a65 100644 --- a/src/Console/Commands/InstallCommand.php +++ b/src/Console/Commands/InstallCommand.php @@ -689,6 +689,9 @@ public function doInstallModules(array $selected, ?\Closure $run = null): bool $this->demoPassword(); } + // Here, not in the task label: a long list wraps the label and pushes its DONE off the line. + $this->line(' Modules: '.implode(', ', array_map(fn (string $package) => Str::after($package, '/'), $selected))); + return $this->runInstallTask('Installing modules', [ 'php', 'artisan', diff --git a/src/Environments/Environment.php b/src/Environments/Environment.php index 8f468f5..93e99e2 100644 --- a/src/Environments/Environment.php +++ b/src/Environments/Environment.php @@ -2,6 +2,7 @@ namespace Saucebase\Installer\Environments; +use Illuminate\Process\Exceptions\ProcessTimedOutException; use Illuminate\Support\Facades\Process; use Illuminate\Support\ProcessUtils; use Saucebase\Installer\Console\Commands\InstallCommand; @@ -40,16 +41,25 @@ public function run(InstallCommand $command): int $command->promptForModules(); - $result = $this->boot($command); + try { + $result = $this->boot($command); - if ($result === InstallCommand::SUCCESS) { - $this->frontendBuilt = $this->buildFrontend($command); + if ($result === InstallCommand::SUCCESS) { + $this->frontendBuilt = $this->buildFrontend($command); + // Served only once built: a request before the build finds no manifest and 500s. + $result = $this->serve($command); + } // A module that doesn't fit the installed core installs fine and then breaks // every request, so "complete" waits until the app has actually booted. - if (! $this->artisan($command, ['about', '--only=environment'])) { + if ($result === InstallCommand::SUCCESS && ! $this->artisan($command, ['about', '--only=environment'])) { $result = $this->failWithReason($command, 'Checking the app boots'); } + } catch (ProcessTimedOutException $e) { + // Any step's command, on any driver: without this the install ends on a stack trace. + $line = str_replace("'", '', $e->result->command()); + $command->warn("`{$line}` ran longer than {$e->exceededTimeout()} seconds, so it was stopped."); + $result = $this->fail("Running `{$line}`"); } if ($result === InstallCommand::SUCCESS) { @@ -84,6 +94,12 @@ protected function buildFrontend(InstallCommand $command): bool return false; } + /** Makes the built app reachable (a site server's link). Nothing for most drivers. */ + protected function serve(InstallCommand $command): int + { + return InstallCommand::SUCCESS; + } + /** @param string[] $args */ protected function artisan(InstallCommand $command, array $args): bool { diff --git a/src/Environments/LerdEnvironment.php b/src/Environments/LerdEnvironment.php index ac2c435..1c55e1c 100644 --- a/src/Environments/LerdEnvironment.php +++ b/src/Environments/LerdEnvironment.php @@ -84,9 +84,9 @@ protected function beforePrompts(InstallCommand $command): ?int return null; } - protected function boot(InstallCommand $command): int + protected function serve(InstallCommand $command): int { - if (($result = parent::boot($command)) !== InstallCommand::SUCCESS || ! $this->workers) { + if (($result = parent::serve($command)) !== InstallCommand::SUCCESS || ! $this->workers) { return $result; } diff --git a/src/Environments/LinkedSiteEnvironment.php b/src/Environments/LinkedSiteEnvironment.php index d6082a1..060f3f4 100644 --- a/src/Environments/LinkedSiteEnvironment.php +++ b/src/Environments/LinkedSiteEnvironment.php @@ -2,6 +2,7 @@ namespace Saucebase\Installer\Environments; +use Illuminate\Support\Sleep; use Illuminate\Support\Str; use Saucebase\Installer\Console\Commands\InstallCommand; @@ -29,20 +30,33 @@ protected function boot(InstallCommand $command): int return $result; } + $site = $this->site($command); + $env = (string) file_get_contents($command->path('.env')); + $env = InstallCommand::setEnvLine($env, 'APP_HOST', "{$site}.{$this->tld()}"); + $env = InstallCommand::setEnvLine($env, 'APP_URL', $this->siteUrl($site, $this->secure($command))); + file_put_contents($command->path('.env'), $env); + + return InstallCommand::SUCCESS; + } + + protected function serve(InstallCommand $command): int + { $site = $this->site($command); if (! $command->runProcess([$this->name(), 'link', $site])) { return $this->failWithReason($command, "Linking the site to {$this->tool()}"); } - if ($this->secure($command) && ! $command->runProcess([$this->name(), 'secure', $site])) { - return $this->failWithReason($command, "Securing the site with {$this->tool()}"); - } + $secure = [$this->name(), 'secure', $site]; - $env = (string) file_get_contents($command->path('.env')); - $env = InstallCommand::setEnvLine($env, 'APP_HOST', "{$site}.{$this->tld()}"); - $env = InstallCommand::setEnvLine($env, 'APP_URL', $this->siteUrl($site, $this->secure($command))); - file_put_contents($command->path('.env'), $env); + if ($this->secure($command) && ! $command->runProcess($secure)) { + // Lerd's nginx runs in a container, which can see the config `link` just wrote a moment late. + Sleep::for(1)->second(); + + if (! $command->runProcess($secure)) { + return $this->failWithReason($command, "Securing the site with {$this->tool()}"); + } + } return InstallCommand::SUCCESS; } diff --git a/tests/Feature/Environments/InstallTimeoutTest.php b/tests/Feature/Environments/InstallTimeoutTest.php new file mode 100644 index 0000000..5874f2d --- /dev/null +++ b/tests/Feature/Environments/InstallTimeoutTest.php @@ -0,0 +1,61 @@ +setTimeout(120); + + throw ProcessTimedOutException::make( + new SymfonyTimeoutException($process, SymfonyTimeoutException::TYPE_GENERAL), + new ProcessResult($process), + ); + } + + public function warn($string, $verbosity = null): void + { + $this->warned[] = $string; + } + + public function promptForModules(): void {} + + public function displayFailure(?string $step = null, array $resumeOptions = []): void + { + $this->failedAt = $step; + $this->failureShown = true; + } + }; + $command->fakeOptions = ['path' => sys_get_temp_dir()]; + + $result = (new NativeEnvironment)->run($command); + + $this->assertSame(InstallCommand::FAILURE, $result); + $this->assertTrue($command->failureShown); + $this->assertSame('Running `docker compose exec -T app php artisan optimize:clear`', $command->failedAt); + $this->assertSame(['`docker compose exec -T app php artisan optimize:clear` ran longer than 120 seconds, so it was stopped.'], $command->warned); + } +} diff --git a/tests/Feature/Environments/LinkedSiteEnvironmentTest.php b/tests/Feature/Environments/LinkedSiteEnvironmentTest.php index 33b4de1..a0faead 100644 --- a/tests/Feature/Environments/LinkedSiteEnvironmentTest.php +++ b/tests/Feature/Environments/LinkedSiteEnvironmentTest.php @@ -4,6 +4,7 @@ use Illuminate\Console\Command; use Illuminate\Support\Facades\Process; +use Illuminate\Support\Sleep; use Laravel\Prompts\Prompt; use PHPUnit\Framework\Attributes\DataProvider; use Saucebase\Installer\Console\Commands\InstallCommand; @@ -21,6 +22,7 @@ protected function setUp(): void { parent::setUp(); $this->fakeFinishingSteps(); + Sleep::fake(); $this->app = sys_get_temp_dir().'/acme-shop-'.uniqid(); mkdir($this->app); } @@ -63,6 +65,18 @@ public function test_it_links_and_secures_the_app_after_installing_it(string $na $this->assertStringContainsString('APP_HOST=shop.test', (string) file_get_contents($this->app.'/.env')); } + /** Linked before the build, the site answers 500 (no Vite manifest) until the build ends. */ + #[DataProvider('drivers')] + public function test_it_links_the_app_only_once_the_frontend_is_built(string $name): void + { + Process::fake(["'which' 'npm'*" => Process::result('/usr/local/bin/npm'), "*'about'*" => Process::result()]); + $command = $this->command(installs: true, domain: 'shop.test'); + + Environment::make($name)->run($command); + + $this->assertSame(['npm install', 'npm run build', "{$name} link shop", "{$name} secure shop"], $command->ran); + } + #[DataProvider('drivers')] public function test_the_summary_says_the_tool_already_serves_the_app(string $name): void { @@ -102,6 +116,27 @@ public function test_a_failed_link_or_secure_says_why(string $name): void } } + /** Lerd's nginx container can miss the config `link` just wrote, so securing right after it fails. */ + #[DataProvider('drivers')] + public function test_a_secure_that_fails_once_is_tried_again(string $name): void + { + $command = $this->command(installs: true, domain: 'shop.test', failingOnce: ["{$name} secure shop"]); + + $this->assertSame(Command::SUCCESS, Environment::make($name)->run($command)); + $this->assertSame(["{$name} link shop", "{$name} secure shop", "{$name} secure shop"], $command->ran); + $this->assertSame([], $command->warned); + Sleep::assertSleptTimes(1); + } + + #[DataProvider('drivers')] + public function test_a_secure_that_fails_twice_stops_the_install(string $name): void + { + $command = $this->command(installs: true, domain: 'shop.test', failing: ["{$name} secure shop"]); + + $this->assertSame(Command::FAILURE, Environment::make($name)->run($command)); + $this->assertSame(["{$name} link shop", "{$name} secure shop", "{$name} secure shop"], $command->ran); + } + #[DataProvider('drivers')] public function test_a_failed_install_is_not_linked(string $name): void { @@ -228,13 +263,14 @@ private function steps(Environment $env, InstallCommand $command): array /** * @param array $options * @param list $failing Commands that fail. + * @param list $failingOnce Commands that fail the first time only. */ - private function command(bool $installs, string $domain = 'localhost', array $options = [], array $failing = []): InstallCommand + private function command(bool $installs, string $domain = 'localhost', array $options = [], array $failing = [], array $failingOnce = []): InstallCommand { // Other tests aren't about the workers: declined, so only link and secure run. $options += ['workers' => 'no']; - return new class($this->app, $installs, $domain, $options, $failing) extends InstallCommand + return new class($this->app, $installs, $domain, $options, $failing, $failingOnce) extends InstallCommand { /** @var list */ public array $ran = []; @@ -245,8 +281,9 @@ private function command(bool $installs, string $domain = 'localhost', array $op /** * @param array $options * @param list $failing + * @param list $failingOnce */ - public function __construct(private string $app, private bool $installs, private string $site, private array $options, private array $failing) {} + public function __construct(private string $app, private bool $installs, private string $site, private array $options, private array $failing, private array $failingOnce) {} public function targetPath(): string { @@ -274,6 +311,12 @@ public function runProcess(array $command, int $timeout = 300): bool { $this->ran[] = $line = implode(' ', $command); + if (in_array($line, $this->failingOnce, true)) { + $this->failingOnce = array_values(array_diff($this->failingOnce, [$line])); + + return false; + } + return ! in_array($line, $this->failing, true); } @@ -284,6 +327,8 @@ public function warnFailed(string $label): void public function promptForModules(): void {} + public function info($string, $verbosity = null): void {} + public function displaySuccess(array $steps = []): void {} public function displayFailure(?string $step = null, array $resumeOptions = []): void {} diff --git a/tests/Feature/InstallCommandCoreTest.php b/tests/Feature/InstallCommandCoreTest.php index 60f3371..8d55399 100644 --- a/tests/Feature/InstallCommandCoreTest.php +++ b/tests/Feature/InstallCommandCoreTest.php @@ -232,6 +232,8 @@ public function warn(string $message): void {} public function info($string, $verbosity = null): void {} + public function line($string, $style = null, $verbosity = null): void {} + public function newLine($count = 1): static { return $this; diff --git a/tests/Feature/InstallModulesRunnerTest.php b/tests/Feature/InstallModulesRunnerTest.php index f1efc47..021cd48 100644 --- a/tests/Feature/InstallModulesRunnerTest.php +++ b/tests/Feature/InstallModulesRunnerTest.php @@ -64,6 +64,8 @@ public function newLine($count = 1): static { return $this; } + + public function line($string, $style = null, $verbosity = null): void {} }; $command->fakeOptions = ['path' => $this->tmp, 'demo' => true]; @@ -87,6 +89,46 @@ public function test_every_step_runs_through_the_given_runner(): void $this->assertStringContainsString('DEMO_PASSWORD=', (string) file_get_contents($this->tmp.'/.env')); } + /** The task line only says "Installing modules": the line above it says which. */ + public function test_it_names_the_modules_it_installs(): void + { + $command = new class extends TestableInstallCommand + { + /** @var string[] */ + public array $printed = []; + + public function installWith(array $selected): bool + { + $this->components = new class + { + public function task(string $label, callable $task): void + { + $task(); + } + + public function warn(string $message): void {} + }; + + return InstallCommand::doInstallModules($selected, fn (): bool => true); + } + + public function line($string, $style = null, $verbosity = null): void + { + $this->printed[] = $string; + } + + public function newLine($count = 1): static + { + return $this; + } + }; + $command->fakeOptions = ['path' => $this->tmp]; + + $command->installWith(['saucebase/auth', 'saucebase/ai-chat']); + + $this->assertContains(' Modules: auth, ai-chat', $command->printed); + } + /** Core's template-install requires the template with its modules and copies its stubs; no demo unless asked. */ public function test_a_template_installs_clean_through_template_install(): void {