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
16 changes: 11 additions & 5 deletions src/Cli/InitCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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'
<?php

Expand Down
20 changes: 20 additions & 0 deletions tests/Cli/InitCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use function exec;
use function file_exists;
use function file_get_contents;
use function file_put_contents;
use function getcwd;
use function implode;
use function is_dir;
Expand Down Expand Up @@ -149,6 +150,25 @@ public function testInitRejectsInvalidPreset(): void
}
}

public function testInitKeepsExistingDistConfig(): void
{
$basePath = $this->createTempDirectory();
$distFile = $basePath . '/structarmed.dist.php';

file_put_contents($distFile, '<?php');

try {
[$exitCode, $output] = $this->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<string> $arguments
* @return array{int, string}
Expand Down
Loading