Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/Console/Commands/InstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,9 @@
$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)));

Check warning on line 693 in src/Console/Commands/InstallCommand.php

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Split this 122 characters long line (which is greater than 120 authorized).

See more on https://sonarcloud.io/project/issues?id=saucebase-dev_installer&issues=AaI7f2l_fndDz2_cM9kZ&open=AaI7f2l_fndDz2_cM9kZ&pullRequest=44

return $this->runInstallTask('Installing modules', [
'php',
'artisan',
Expand Down
24 changes: 20 additions & 4 deletions src/Environments/Environment.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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
{
Expand Down
4 changes: 2 additions & 2 deletions src/Environments/LerdEnvironment.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
28 changes: 21 additions & 7 deletions src/Environments/LinkedSiteEnvironment.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Saucebase\Installer\Environments;

use Illuminate\Support\Sleep;
use Illuminate\Support\Str;
use Saucebase\Installer\Console\Commands\InstallCommand;

Expand Down Expand Up @@ -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;
}
Expand Down
61 changes: 61 additions & 0 deletions tests/Feature/Environments/InstallTimeoutTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace Saucebase\Installer\Tests\Feature\Environments;

use Illuminate\Process\Exceptions\ProcessTimedOutException;
use Illuminate\Process\ProcessResult;
use Saucebase\Installer\Console\Commands\InstallCommand;
use Saucebase\Installer\Environments\NativeEnvironment;
use Saucebase\Installer\Tests\Feature\TestableInstallCommand;
use Saucebase\Installer\Tests\TestCase;
use Symfony\Component\Process\Exception\ProcessTimedOutException as SymfonyTimeoutException;
use Symfony\Component\Process\Process;

/** A step that runs out of time ends like any failed step: why, the step, and how to resume. */
class InstallTimeoutTest extends TestCase
{
public function test_a_timed_out_step_names_the_command_and_prints_the_resume_command(): void
{
$command = new class extends TestableInstallCommand
{
/** @var string[] */
public array $warned = [];

public ?string $failedAt = null;

public bool $failureShown = false;

public function install(): int
{
$process = new Process(['docker', 'compose', 'exec', '-T', 'app', 'php', 'artisan', 'optimize:clear']);
$process->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);
}
}
51 changes: 48 additions & 3 deletions tests/Feature/Environments/LinkedSiteEnvironmentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
Expand Down Expand Up @@ -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
{
Expand Down Expand Up @@ -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
{
Expand Down Expand Up @@ -228,13 +263,14 @@ private function steps(Environment $env, InstallCommand $command): array
/**
* @param array<string, string|null> $options
* @param list<string> $failing Commands that fail.
* @param list<string> $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<string> */
public array $ran = [];
Expand All @@ -245,8 +281,9 @@ private function command(bool $installs, string $domain = 'localhost', array $op
/**
* @param array<string, string|null> $options
* @param list<string> $failing
* @param list<string> $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
{
Expand Down Expand Up @@ -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);
}

Expand All @@ -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 {}
Expand Down
2 changes: 2 additions & 0 deletions tests/Feature/InstallCommandCoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
42 changes: 42 additions & 0 deletions tests/Feature/InstallModulesRunnerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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];

Expand All @@ -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
{
Expand Down