diff --git a/README.md b/README.md index 9f8a50d..79942fc 100644 --- a/README.md +++ b/README.md @@ -30,6 +30,8 @@ composer global require saucebase/installer Make sure Composer's global bin directory is on your `PATH` (`composer global config bin-dir --absolute`). +To upgrade, run `composer global update saucebase/installer`. Running `require` again keeps the version you have. `saucebase new` also updates the installer itself when a newer release exists, then carries on with it. + ## Commands ### `saucebase new ` @@ -86,7 +88,9 @@ Takes an optional `stack` argument (`vue`/`react`) and the same `--driver`, `--s **Native driver** — copies `.env.example` → `.env`, generates `APP_KEY`, migrates and seeds, wires the frontend, installs modules, and links storage. -**Herd driver** — the native install, then `herd link` and (unless `--ssl=no`) `herd secure`, so the app is served at `https://.test`. The name comes from a `.test` `--domain`, otherwise the directory. +**Herd driver** — the native install, then `herd link` and (unless `--ssl=no`) `herd secure`, so the app is served at `https://.test`. The name comes from a `.test` `--domain`, otherwise the directory. With `npm run dev` running, Vite renders pages on the server. With built assets only, Herd doesn't start the renderer, so pages render in the browser until you run `php artisan inertia:start-ssr`. + +Every driver ends by building the frontend (`npm install && npm run build`) when npm is available, then checks the app boots before reporting the install complete. --- diff --git a/src/Console/Commands/InstallCommand.php b/src/Console/Commands/InstallCommand.php index be3160c..1c96009 100644 --- a/src/Console/Commands/InstallCommand.php +++ b/src/Console/Commands/InstallCommand.php @@ -11,6 +11,7 @@ use Saucebase\Installer\Environments\Environment; use Saucebase\Installer\Environments\HerdEnvironment; use Saucebase\Installer\ModuleRegistry; +use Symfony\Component\Console\Output\OutputInterface; use function Laravel\Prompts\callout; use function Laravel\Prompts\select; @@ -814,7 +815,8 @@ public function applyModulePatches(array $modules): bool } if ($this->gitApply($patch)) { - $this->info('Applied patch: '.basename($patch)); + // Routine wiring, so only with -v. Named by module: several ship a user.patch. + $this->info("Applied patch: {$name}/".basename($patch), OutputInterface::VERBOSITY_VERBOSE); continue; } @@ -909,7 +911,9 @@ protected function successCalloutContent(array $steps): array return array_filter([ $steps ? 'You can start your local development using:' : null, $steps ? Element::numberedList(array_values($steps)) : null, - 'Learn more: '.Element::link('https://github.com/saucebase-dev/saucebase'), + // Plain: a hyperlink's escape codes count towards the box width and push its + // border out, and terminals link a bare URL by themselves. + 'Learn more: https://github.com/saucebase-dev/saucebase', ]); } diff --git a/src/Console/Commands/NewCommand.php b/src/Console/Commands/NewCommand.php index ee344fd..36c09b0 100644 --- a/src/Console/Commands/NewCommand.php +++ b/src/Console/Commands/NewCommand.php @@ -4,6 +4,7 @@ use Illuminate\Console\Command; use Illuminate\Support\Facades\Http; +use Illuminate\Support\Facades\Process; use Illuminate\Support\Str; use Saucebase\Installer\Console\Application; use Saucebase\Installer\Console\Commands\Concerns\DisplaysBanner; @@ -11,6 +12,7 @@ use Saucebase\Installer\Environments\HerdEnvironment; use Saucebase\Installer\ModuleRegistry; use Symfony\Component\Console\Input\ArrayInput; +use Symfony\Component\Process\Process as SymfonyProcess; use function Laravel\Prompts\confirm; use function Laravel\Prompts\select; @@ -355,13 +357,18 @@ protected function displayPrerequisiteHints(Environment $driver): void } /** - * Warn when a newer installer exists; block when a major version behind. + * Create apps with the newest installer: an older one updates itself, then runs this + * command again on the new code. Declined or failed, it warns, or stops when a major + * version behind. + * + * @return int|null An exit code to stop with, null to carry on */ protected function checkForUpdates(): ?int { - $current = ltrim(Application::version(), 'v'); + $current = $this->currentVersion(); - if ($current === '' || str_contains($current, 'dev')) { + // Set for the run after an update: never update twice, whatever Packagist says. + if ($current === '' || str_contains($current, 'dev') || getenv('SAUCEBASE_UPDATED') === '1') { return null; } @@ -371,18 +378,58 @@ protected function checkForUpdates(): ?int return null; } + $wantsUpdate = ! $this->input->isInteractive() || confirm( + label: "A newer Saucebase installer is available ({$current} → {$latest}). Update it first?", + default: true, + ); + + if ($wantsUpdate && $this->updateInstaller($latest)) { + $this->info("Updated the installer to {$latest}. Continuing with it..."); + + return $this->runAgain(); + } + if ((int) explode('.', $latest)[0] > (int) explode('.', $current)[0]) { $this->error("Your installer ({$current}) is a major version behind the latest release ({$latest}) and may not support current Saucebase applications."); - $this->line('Update it first: composer global update saucebase/installer'); + $this->line("Update it first: composer global require saucebase/installer:^{$latest}"); return self::FAILURE; } - $this->components->warn("A new version of the Saucebase installer is available ({$current} → {$latest}). Update with: composer global update saucebase/installer"); + $this->warn("Carrying on with installer {$current}. Update later with: composer global require saucebase/installer:^{$latest}"); return null; } + protected function currentVersion(): string + { + return ltrim(Application::version(), 'v'); + } + + /** Requires the new version itself: `update` keeps the global constraint, so it never crosses a major. */ + protected function updateInstaller(string $latest): bool + { + return Process::timeout(300) + ->run(['composer', 'global', 'require', "saucebase/installer:^{$latest}", '--with-all-dependencies', '--no-interaction']) + ->successful(); + } + + /** The same command on the updated code, attached to this terminal so its prompts still work. */ + protected function runAgain(): int + { + return Process::forever() + ->env(['SAUCEBASE_UPDATED' => '1']) + ->tty(SymfonyProcess::isTtySupported()) + ->run([PHP_BINARY, ...$this->rerunArguments()], fn ($_type, $buffer) => $this->output->write($buffer)) + ->exitCode() ?? self::FAILURE; + } + + /** @return string[] The script and arguments this process was started with. */ + protected function rerunArguments(): array + { + return $_SERVER['argv'] ?? []; + } + /** * The newest published release of the installer, or null when offline. * diff --git a/src/Console/Commands/StackCommand.php b/src/Console/Commands/StackCommand.php index d065302..0c52d6a 100644 --- a/src/Console/Commands/StackCommand.php +++ b/src/Console/Commands/StackCommand.php @@ -312,10 +312,15 @@ private function skipGeneratedFiles(string $framework): void } foreach ($this->generatedFiles($framework) as $file) { + // A generated file git doesn't track (an entry point, gitignored) has nothing to hide. + if (! Process::run(['git', '-C', $this->basePath, 'ls-files', '--error-unmatch', $file])->successful()) { + continue; + } + $result = Process::run(['git', '-C', $this->basePath, 'update-index', '--skip-worktree', $file]); if ($result->exitCode() !== 0) { - $this->warn("Could not skip-worktree {$file} (not in git index)."); + $this->warn("Could not skip-worktree {$file}: ".trim($result->errorOutput())); } } } diff --git a/src/Environments/DockerEnvironment.php b/src/Environments/DockerEnvironment.php index 86ddb09..5197894 100644 --- a/src/Environments/DockerEnvironment.php +++ b/src/Environments/DockerEnvironment.php @@ -773,7 +773,7 @@ protected function nextSteps(InstallCommand $command): array $mailpit = $this->readEnvValue($command, 'FORWARD_MAILPIT_DASHBOARD_PORT') ?? '8025'; return [ - 'Compile frontend assets: `npm install && npm run dev`', + ...($this->frontendBuilt ? [] : ['Compile frontend assets: `npm install && npm run dev`']), 'Open your app: `'.$appUrl.'`', 'Email testing (Mailpit): `http://localhost:'.$mailpit.'`', ]; @@ -785,6 +785,12 @@ protected function createStorageLink(InstallCommand $command): void $this->execInContainer($command, ['php', 'artisan', 'storage:link']); } + /** Inside the app container: on the host, `.env` points at hostnames only Docker resolves. */ + protected function artisan(InstallCommand $command, array $args): bool + { + return $this->execInContainer($command, ['php', 'artisan', ...$args], quiet: true); + } + protected function clearCaches(InstallCommand $command): void { $command->info('Clearing caches...'); diff --git a/src/Environments/Environment.php b/src/Environments/Environment.php index 9fc3b2d..75c518e 100644 --- a/src/Environments/Environment.php +++ b/src/Environments/Environment.php @@ -10,6 +10,9 @@ abstract class Environment /** Label of the step that failed, for the failure callout. Set via fail(). */ protected ?string $failedStep = null; + /** Whether buildFrontend() left built assets, so the next steps skip the build. */ + protected bool $frontendBuilt = false; + public static function make(string $name): Environment { return match ($name) { @@ -47,6 +50,16 @@ public function run(InstallCommand $command): int $result = $this->boot($command); + if ($result === InstallCommand::SUCCESS) { + $this->frontendBuilt = $this->buildFrontend($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'])) { + $result = $this->fail('Checking the app boots'); + } + } + if ($result === InstallCommand::SUCCESS) { $command->displaySuccess(array_merge($this->cdStep($command), $this->nextSteps($command), $this->demoStep($command))); } else { @@ -58,6 +71,33 @@ public function run(InstallCommand $command): int return $result; } + /** + * Builds the assets so the app's URL works before anyone runs Vite. Node isn't a + * prerequisite, so without npm, or when the build fails, it stays a next step. + */ + protected function buildFrontend(InstallCommand $command): bool + { + if (! $this->commandExists('npm')) { + return false; + } + + $command->info('Building the frontend...'); + + if ($command->runProcess(['npm', 'install'], 600) && $command->runProcess(['npm', 'run', 'build'], 600)) { + return true; + } + + $command->warn('Building the frontend failed. Run `npm install && npm run build` in the app to see why.'); + + return false; + } + + /** @param string[] $args */ + protected function artisan(InstallCommand $command, array $args): bool + { + return $command->runArtisan($args); + } + /** Record which step failed, then return FAILURE for boot() to propagate. */ protected function fail(string $step): int { diff --git a/src/Environments/HerdEnvironment.php b/src/Environments/HerdEnvironment.php index 20a096c..0295fe8 100644 --- a/src/Environments/HerdEnvironment.php +++ b/src/Environments/HerdEnvironment.php @@ -57,8 +57,8 @@ protected function boot(InstallCommand $command): int protected function nextSteps(InstallCommand $command): array { return [ - 'Install frontend dependencies: `npm install`', - 'Start Vite: `npm run dev`', + ...($this->frontendBuilt ? [] : ['Install frontend dependencies: `npm install`']), + $this->frontendBuilt ? 'For live reload while you edit: `npm run dev`' : 'Start Vite: `npm run dev`', 'Open your app: `'.($this->readEnvValue($command, 'APP_URL') ?? '').'`', ]; } diff --git a/src/Environments/NativeEnvironment.php b/src/Environments/NativeEnvironment.php index 3f942a7..917756e 100644 --- a/src/Environments/NativeEnvironment.php +++ b/src/Environments/NativeEnvironment.php @@ -35,7 +35,7 @@ protected function boot(InstallCommand $command): int protected function nextSteps(InstallCommand $command): array { return [ - 'Install frontend dependencies: `npm install`', + ...($this->frontendBuilt ? [] : ['Install frontend dependencies: `npm install`']), 'Start the dev server: `composer dev`', 'Open your app: `'.($this->readEnvValue($command, 'APP_URL') ?? 'http://localhost').'`', ]; diff --git a/stubs/docker/docker-compose.yml b/stubs/docker/docker-compose.yml index 3098001..a959ad4 100644 --- a/stubs/docker/docker-compose.yml +++ b/stubs/docker/docker-compose.yml @@ -26,6 +26,7 @@ services: - QUEUE_CONNECTION=${QUEUE_CONNECTION:-redis} - MAIL_HOST=${MAIL_HOST:-mailpit} - MAIL_PORT=${MAIL_PORT:-1025} + - INERTIA_SSR_URL=http://ssr:13714 - XDEBUG_MODE=${XDEBUG_MODE:-debug} - XDEBUG_CONFIG=${XDEBUG_CONFIG:-client_host=host.docker.internal client_port=9003} depends_on: @@ -53,7 +54,7 @@ services: image: 'mysql:8.0' restart: unless-stopped ports: - - '${FORWARD_DB_PORT:-3306}:3306' + - '127.0.0.1:${FORWARD_DB_PORT:-3306}:3306' environment: MYSQL_ROOT_PASSWORD: '${DB_PASSWORD}' MYSQL_DATABASE: '${DB_DATABASE}' @@ -85,7 +86,7 @@ services: image: 'redis:alpine' restart: unless-stopped ports: - - '${FORWARD_REDIS_PORT:-6379}:6379' + - '127.0.0.1:${FORWARD_REDIS_PORT:-6379}:6379' volumes: - 'saucebase-redis:/data' networks: @@ -100,8 +101,8 @@ services: image: 'axllent/mailpit:latest' restart: unless-stopped ports: - - '${FORWARD_MAILPIT_PORT:-1025}:1025' - - '${FORWARD_MAILPIT_DASHBOARD_PORT:-8025}:8025' + - '127.0.0.1:${FORWARD_MAILPIT_PORT:-1025}:1025' + - '127.0.0.1:${FORWARD_MAILPIT_DASHBOARD_PORT:-8025}:8025' networks: - saucebase queue: @@ -137,6 +138,19 @@ services: condition: service_healthy restart: unless-stopped + # The server-side renderer, so pages arrive rendered. Its bundle comes from + # `npm run build`; until the first build it restarts until the file exists. + ssr: + build: + context: . + dockerfile: ./docker/Dockerfile + command: node bootstrap/ssr/app.js + volumes: + - './:/var/www' + networks: + - saucebase + restart: unless-stopped + networks: saucebase: driver: bridge diff --git a/tests/Feature/Environments/FinishingStepsTest.php b/tests/Feature/Environments/FinishingStepsTest.php new file mode 100644 index 0000000..ca30490 --- /dev/null +++ b/tests/Feature/Environments/FinishingStepsTest.php @@ -0,0 +1,127 @@ + Process::result('/usr/local/bin/npm'), + "'npm' 'install'*" => Process::result(), + "'npm' 'run' 'build'*" => Process::result(), + "*'about'*" => Process::result(), + ]); + $command = $this->command(); + + $this->assertSame(Command::SUCCESS, $this->environment()->run($command)); + + Process::assertRan(fn ($process) => $process->command === ['npm', 'install']); + Process::assertRan(fn ($process) => $process->command === ['npm', 'run', 'build']); + $this->assertNotContains('Install frontend dependencies: `npm install`', $command->steps); + } + + public function test_without_npm_the_build_is_left_to_the_next_steps(): void + { + Process::fake([ + "'which' *" => Process::result(''), + "*'about'*" => Process::result(), + ]); + $command = $this->command(); + + $this->assertSame(Command::SUCCESS, $this->environment()->run($command)); + + Process::assertDidntRun(fn ($process) => $process->command === ['npm', 'install']); + $this->assertContains('Install frontend dependencies: `npm install`', $command->steps); + } + + public function test_a_failed_build_warns_but_keeps_the_install(): void + { + Process::fake([ + "'which' *" => Process::result('/usr/local/bin/npm'), + "'npm' 'install'*" => Process::result(), + "'npm' 'run' 'build'*" => Process::result(errorOutput: 'boom', exitCode: 1), + "*'about'*" => Process::result(), + ]); + $command = $this->command(); + + $this->assertSame(Command::SUCCESS, $this->environment()->run($command)); + + $this->assertNotEmpty($command->warnings); + $this->assertContains('Install frontend dependencies: `npm install`', $command->steps); + } + + public function test_an_app_that_does_not_boot_is_not_reported_as_installed(): void + { + Process::fake([ + "'which' *" => Process::result(''), + "*'about'*" => Process::result(errorOutput: 'Class "App\Providers\ModuleServiceProvider" not found', exitCode: 1), + ]); + $command = $this->command(); + + $this->assertSame(Command::FAILURE, $this->environment()->run($command)); + + $this->assertNull($command->steps, 'no success callout'); + $this->assertSame('Checking the app boots', $command->failedStep); + } + + private function environment(): NativeEnvironment + { + return new class extends NativeEnvironment + { + protected function boot(InstallCommand $command): int + { + return InstallCommand::SUCCESS; + } + }; + } + + private function command(): InstallCommand + { + return new class extends InstallCommand + { + /** @var string[]|null */ + public ?array $steps = null; + + public ?string $failedStep = null; + + /** @var string[] */ + public array $warnings = []; + + public function __construct() {} + + public function targetPath(): string + { + return sys_get_temp_dir(); + } + + public function promptForModules(): void {} + + public function displaySuccess(array $steps = []): void + { + $this->steps = $steps; + } + + public function displayFailure(?string $step = null, array $resumeOptions = []): void + { + $this->failedStep = $step; + } + + public function warn($string, $verbosity = null): void + { + $this->warnings[] = $string; + } + + public function line($string, $style = null, $verbosity = null): void {} + + public function info($string, $verbosity = null): void {} + }; + } +} diff --git a/tests/Feature/Environments/HerdEnvironmentTest.php b/tests/Feature/Environments/HerdEnvironmentTest.php index 8045847..edb96bd 100644 --- a/tests/Feature/Environments/HerdEnvironmentTest.php +++ b/tests/Feature/Environments/HerdEnvironmentTest.php @@ -15,6 +15,7 @@ class HerdEnvironmentTest extends TestCase protected function setUp(): void { parent::setUp(); + $this->fakeFinishingSteps(); $this->app = sys_get_temp_dir().'/acme-shop-'.uniqid(); mkdir($this->app); } diff --git a/tests/Feature/Environments/NativeEnvironmentTest.php b/tests/Feature/Environments/NativeEnvironmentTest.php index bb10b53..960eef9 100644 --- a/tests/Feature/Environments/NativeEnvironmentTest.php +++ b/tests/Feature/Environments/NativeEnvironmentTest.php @@ -121,6 +121,7 @@ public function install(): int protected function setUp(): void { parent::setUp(); + $this->fakeFinishingSteps(); $this->originalCwd = getcwd(); $this->sandbox = sys_get_temp_dir().'/saucebase-env-test-'.uniqid(); mkdir($this->sandbox.'/sub/dir', recursive: true); diff --git a/tests/Feature/InstallCommandPatchesTest.php b/tests/Feature/InstallCommandPatchesTest.php index 0bf9654..9667c5d 100644 --- a/tests/Feature/InstallCommandPatchesTest.php +++ b/tests/Feature/InstallCommandPatchesTest.php @@ -4,6 +4,7 @@ use Illuminate\Filesystem\Filesystem; use Saucebase\Installer\Tests\TestCase; +use Symfony\Component\Console\Output\OutputInterface; /** * A module wires itself into the application with patches. One that does not apply @@ -55,6 +56,17 @@ public function test_a_patch_is_applied(): void $this->assertStringContainsString('use Sociable;', $this->files->get($this->dir.'/app/User.php')); } + public function test_an_applied_patch_is_only_reported_when_verbose(): void + { + $this->files->put($this->dir.'/app/User.php', "class User\n{\n}\n"); + $command = $this->command(); + + $command->applyModulePatches(['saucebase/auth']); + + // Several modules ship a `user.patch`, so the line names the module. + $this->assertSame([['Applied patch: auth/user.patch', OutputInterface::VERBOSITY_VERBOSE]], $command->notes); + } + public function test_a_patch_already_applied_is_skipped_without_a_warning(): void { $this->files->put($this->dir.'/app/User.php', "class User\n{\n use Sociable;\n}\n"); @@ -106,7 +118,13 @@ public function warn($string, $verbosity = null): void $this->problems[] = $string; } - public function info($string, $verbosity = null): void {} + /** @var array */ + public array $notes = []; + + public function info($string, $verbosity = null): void + { + $this->notes[] = [$string, $verbosity]; + } }; $command->fakeOptions = ['path' => $this->dir]; diff --git a/tests/Feature/InstallCommandTest.php b/tests/Feature/InstallCommandTest.php index d02af65..0d42740 100644 --- a/tests/Feature/InstallCommandTest.php +++ b/tests/Feature/InstallCommandTest.php @@ -15,6 +15,7 @@ class InstallCommandTest extends TestCase protected function setUp(): void { parent::setUp(); + $this->fakeFinishingSteps(); Http::preventStrayRequests(); } diff --git a/tests/Feature/NewCommandUpdateTest.php b/tests/Feature/NewCommandUpdateTest.php new file mode 100644 index 0000000..53c9e51 --- /dev/null +++ b/tests/Feature/NewCommandUpdateTest.php @@ -0,0 +1,109 @@ + Process::result(), + "*'new' 'acme'*" => Process::result(exitCode: 0), + ]); + + $this->assertSame(0, $this->command(current: '2.9.0', latest: '3.5.0')->check()); + + // `require` with the new version moves the global constraint, so a major update works too. + Process::assertRan(fn ($process) => $process->command === ['composer', 'global', 'require', 'saucebase/installer:^3.5.0', '--with-all-dependencies', '--no-interaction']); + Process::assertRan(fn ($process) => $process->command === [PHP_BINARY, '/usr/local/bin/saucebase', 'new', 'acme', '--no-interaction'] + && ($process->environment['SAUCEBASE_UPDATED'] ?? null) === '1'); + } + + public function test_the_command_run_again_after_an_update_never_updates_again(): void + { + putenv('SAUCEBASE_UPDATED=1'); + + try { + $this->assertNull($this->command(current: '3.2.2', latest: '3.5.0')->check()); + Process::assertNothingRan(); + } finally { + putenv('SAUCEBASE_UPDATED'); + } + } + + public function test_a_failed_update_carries_on_with_a_warning_when_only_a_minor_behind(): void + { + Process::fake(["'composer' *" => Process::result(errorOutput: 'offline', exitCode: 1)]); + $command = $this->command(current: '3.2.2', latest: '3.5.0'); + + $this->assertNull($command->check()); + + Process::assertDidntRun(fn ($process) => str_contains(implode(' ', (array) $process->command), ' new ')); + $this->assertStringContainsString('composer global require saucebase/installer:^3.5.0', $command->output()); + } + + public function test_a_failed_update_stops_a_major_version_behind(): void + { + Process::fake(["'composer' *" => Process::result(exitCode: 1)]); + + $this->assertSame(NewCommand::FAILURE, $this->command(current: '2.9.0', latest: '3.5.0')->check()); + } + + public function test_an_up_to_date_installer_runs_nothing(): void + { + $this->assertNull($this->command(current: '3.5.0', latest: '3.5.0')->check()); + + Process::assertNothingRan(); + } + + private function command(string $current, string $latest): object + { + $command = new class($current, $latest) extends NewCommand + { + public function __construct(private string $current, private string $latest) + { + parent::__construct(); + } + + public function check(): ?int + { + return $this->checkForUpdates(); + } + + public function output(): string + { + return $this->output->getOutput()->fetch(); + } + + protected function currentVersion(): string + { + return $this->current; + } + + protected function latestVersion(): ?string + { + return $this->latest; + } + + protected function rerunArguments(): array + { + return ['/usr/local/bin/saucebase', 'new', 'acme', '--no-interaction']; + } + }; + + $input = new ArrayInput([]); + $input->setInteractive(false); + $command->setInput($input); + $command->setOutput(new OutputStyle($input, new BufferedOutput)); + + return $command; + } +} diff --git a/tests/Feature/PublishDockerCommandTest.php b/tests/Feature/PublishDockerCommandTest.php index db138d7..5051044 100644 --- a/tests/Feature/PublishDockerCommandTest.php +++ b/tests/Feature/PublishDockerCommandTest.php @@ -51,6 +51,31 @@ public function test_mysql_has_time_to_create_its_first_database(): void $this->assertStringContainsString('start_period: 60s', $mysql); } + public function test_backing_services_are_only_reachable_from_this_machine(): void + { + $this->artisan("docker:publish --path={$this->tmp}")->assertSuccessful(); + + // MySQL, Redis and Mailpit run with default credentials; on 0.0.0.0 anyone on + // the network could reach them. The app's own ports stay open for other devices. + $compose = (string) file_get_contents($this->tmp.'/docker-compose.yml'); + foreach (['FORWARD_DB_PORT', 'FORWARD_REDIS_PORT', 'FORWARD_MAILPIT_PORT', 'FORWARD_MAILPIT_DASHBOARD_PORT'] as $port) { + $this->assertStringContainsString("'127.0.0.1:\${{$port}:-", $compose, $port); + } + $this->assertStringContainsString("'\${APP_PORT:-80}:80'", $compose); + } + + public function test_pages_are_server_rendered_by_their_own_service(): void + { + $this->artisan("docker:publish --path={$this->tmp}")->assertSuccessful(); + + // Without a renderer, pages reach the browser empty and only render there. + $compose = (string) file_get_contents($this->tmp.'/docker-compose.yml'); + $ssr = substr($compose, (int) strpos($compose, "\n ssr:\n"), 600); + $this->assertStringContainsString('command: node bootstrap/ssr/app.js', $ssr); + $app = substr($compose, (int) strpos($compose, "\n app:\n"), (int) strpos($compose, "\n nginx:\n")); + $this->assertStringContainsString('INERTIA_SSR_URL=http://ssr:13714', $app); + } + public function test_a_script_the_application_serves_itself_reaches_it(): void { // Livewire serves its script from a route. Answering every missing .js with a 404 diff --git a/tests/Feature/StackCommandTest.php b/tests/Feature/StackCommandTest.php index 95b9c2b..c4a9ff8 100644 --- a/tests/Feature/StackCommandTest.php +++ b/tests/Feature/StackCommandTest.php @@ -319,15 +319,14 @@ protected function runNpmInstall(): void // Warnings // ------------------------------------------------------------------------- - public function test_dev_mode_warns_for_untracked_entry_points(): void + public function test_dev_mode_leaves_untracked_entry_points_alone_without_a_warning(): void { $this->seedFakeStubs('react'); - // app.tsx is created by the command but not in git — warn for it + // app.tsx is generated and gitignored, not tracked: there is nothing to hide from git. $this->artisan('stack react --dev') ->assertSuccessful() - ->expectsOutputToContain('Could not skip-worktree resources/js/app.tsx') - ->doesntExpectOutputToContain('resources/js/ssr.tsx'); + ->doesntExpectOutputToContain('Could not skip-worktree'); } public function test_dev_mode_leaves_module_files_out_of_skip_worktree(): void diff --git a/tests/TestCase.php b/tests/TestCase.php index 32e003a..170802e 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -45,6 +45,20 @@ protected function fakeToolsInstalled(): void Process::fake(["'which' *" => Process::result('/usr/local/bin/tool')]); } + /** + * The steps every install ends with, for tests about something else: no npm, so + * nothing is built (a test that fakes every tool installed gets a passing build), + * and the app boots. + */ + protected function fakeFinishingSteps(): void + { + Process::fake([ + "'which' 'npm'*" => Process::result(''), + "'npm' *" => Process::result(), + "*'about'*" => Process::result(), + ]); + } + /** * Let commands matching the pattern really run, for tests about a tool's real * behaviour in a temporary folder (git applying a patch). Patterns match the diff --git a/tests/Unit/SuccessCalloutTest.php b/tests/Unit/SuccessCalloutTest.php new file mode 100644 index 0000000..423be0f --- /dev/null +++ b/tests/Unit/SuccessCalloutTest.php @@ -0,0 +1,27 @@ + */ + public function content(): array + { + return $this->successCalloutContent([]); + } + }; + + // A terminal hyperlink's escape codes count towards the callout's width and + // push its right border out; terminals link a bare URL by themselves. + $this->assertSame(['Learn more: https://github.com/saucebase-dev/saucebase'], array_values($command->content())); + } +}