Skip to content

Throw exception in ServeCommand when current directory not found #227

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 3, 2025
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- Enh #226: Check empty string as key in command map in `CommandLoader` validation (@vjik)
- Bug #224: Fix typo in `UNAVAILABLE` exit code reason (@vjik)
- Bug #227: Throw `RuntimeException` in `ServeCommand` when it wasn't possible to find out the current directory (@vjik)

## 2.3.0 January 23, 2025

Expand Down
8 changes: 7 additions & 1 deletion src/Command/Serve.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Yiisoft\Yii\Console\Command;

use RuntimeException;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Completion\CompletionInput;
Expand Down Expand Up @@ -221,6 +222,11 @@ private function getRootPath(): string
return rtrim($this->appRootPath, DIRECTORY_SEPARATOR);
}

return getcwd();
$currentDirectory = getcwd();
if ($currentDirectory === false) {
throw new RuntimeException('Failed to get current working directory.');
}

return $currentDirectory;
}
}
1 change: 1 addition & 0 deletions tests/Command/Serve/NonExistsCurrentDirectory/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/test-dir
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Yii\Console\Tests\Command\Serve\NonExistsCurrentDirectory;

use RuntimeException;
use Symfony\Component\Console\Tester\CommandTester;
use Yiisoft\Yii\Console\Tests\TestCase;

final class NonExistsCurrentDirectoryServeTest extends TestCase
{
/**
* @requires OS Linux
*/
public function testBase(): void
{
$command = $this->application()->find('serve');

$commandCreate = new CommandTester($command);

$directory = __DIR__ . '/test-dir';
if (!file_exists($directory)) {
mkdir($directory);
}
chdir($directory);
rmdir($directory);

$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('Failed to get current working directory.');
$commandCreate->execute([]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

declare(strict_types=1);

namespace Yiisoft\Yii\Console\Tests;
namespace Yiisoft\Yii\Console\Tests\Command\Serve;

use Symfony\Component\Console\Tester\CommandCompletionTester;
use Symfony\Component\Console\Tester\CommandTester;
use Yiisoft\Yii\Console\Command\Serve;
use Yiisoft\Yii\Console\ExitCode;
use Yiisoft\Yii\Console\Tests\TestCase;

final class ServeCommandTest extends TestCase
final class ServeTest extends TestCase
{
public function testServeCommandExecuteWithoutArguments(): void
{
Expand Down
Loading