diff --git a/src/Cli/InitCommand.php b/src/Cli/InitCommand.php index 8de2642d..e733a2de 100644 --- a/src/Cli/InitCommand.php +++ b/src/Cli/InitCommand.php @@ -4,8 +4,11 @@ namespace Boundwize\StructArmed\Cli; +use Boundwize\StructArmed\Config\ConfigLoader; +use RuntimeException; + +use function basename; use function count; -use function file_exists; use function file_put_contents; use function sprintf; use function str_starts_with; @@ -53,14 +56,17 @@ public function run(array $arguments, string $basePath): int return 1; } - $target = $basePath . '/structarmed.php'; - - if (file_exists($target)) { - echo "structarmed.php already exists.\n"; + // Any discoverable config stops init: a new structarmed.php would shadow + // an existing structarmed.dist.php, since discovery prefers it. + try { + echo sprintf("%s already exists.\n", basename(ConfigLoader::discover($basePath))); return 0; + } catch (RuntimeException) { } + $target = $basePath . '/structarmed.php'; + file_put_contents($target, <<<'PHP' createTempDirectory(); + $distFile = $basePath . '/structarmed.dist.php'; + + file_put_contents($distFile, 'runInit($basePath, []); + + $this->assertSame(0, $exitCode, $output); + $this->assertStringContainsString('structarmed.dist.php already exists.', $output); + $this->assertFileDoesNotExist($basePath . '/structarmed.php'); + } finally { + unlink($distFile); + $this->removeTempDirectory($basePath); + } + } + /** * @param list $arguments * @return array{int, string}