From afdf1eed1e93b56b579c0eb523d57fefcbe033ad Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Thu, 3 Apr 2025 13:46:47 +0300 Subject: [PATCH 1/4] Throw exception in `ServeCommand` when current directory not found --- CHANGELOG.md | 1 + src/Command/Serve.php | 8 ++++- .../NonExistsCurrentDirectory/.gitignore | 1 + .../NonExistsCurrentDirectoryServeTest.php | 31 +++++++++++++++++++ .../Serve/ServeTest.php} | 5 +-- 5 files changed, 43 insertions(+), 3 deletions(-) create mode 100644 tests/Command/Serve/NonExistsCurrentDirectory/.gitignore create mode 100644 tests/Command/Serve/NonExistsCurrentDirectory/NonExistsCurrentDirectoryServeTest.php rename tests/{ServeCommandTest.php => Command/Serve/ServeTest.php} (97%) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0de83758..f93596f7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/Command/Serve.php b/src/Command/Serve.php index ec40ee89..158d5829 100644 --- a/src/Command/Serve.php +++ b/src/Command/Serve.php @@ -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; @@ -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; } } diff --git a/tests/Command/Serve/NonExistsCurrentDirectory/.gitignore b/tests/Command/Serve/NonExistsCurrentDirectory/.gitignore new file mode 100644 index 00000000..951d23bf --- /dev/null +++ b/tests/Command/Serve/NonExistsCurrentDirectory/.gitignore @@ -0,0 +1 @@ +/test-dir diff --git a/tests/Command/Serve/NonExistsCurrentDirectory/NonExistsCurrentDirectoryServeTest.php b/tests/Command/Serve/NonExistsCurrentDirectory/NonExistsCurrentDirectoryServeTest.php new file mode 100644 index 00000000..5e3d64c9 --- /dev/null +++ b/tests/Command/Serve/NonExistsCurrentDirectory/NonExistsCurrentDirectoryServeTest.php @@ -0,0 +1,31 @@ +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([]); + } +} diff --git a/tests/ServeCommandTest.php b/tests/Command/Serve/ServeTest.php similarity index 97% rename from tests/ServeCommandTest.php rename to tests/Command/Serve/ServeTest.php index e71d42bf..e60fc473 100644 --- a/tests/ServeCommandTest.php +++ b/tests/Command/Serve/ServeTest.php @@ -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 { From fc737d50ef19f4fcab91698963660250e380686a Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Thu, 3 Apr 2025 10:47:06 +0000 Subject: [PATCH 2/4] Apply fixes from StyleCI --- .../NonExistsCurrentDirectoryServeTest.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/Command/Serve/NonExistsCurrentDirectory/NonExistsCurrentDirectoryServeTest.php b/tests/Command/Serve/NonExistsCurrentDirectory/NonExistsCurrentDirectoryServeTest.php index 5e3d64c9..c3830846 100644 --- a/tests/Command/Serve/NonExistsCurrentDirectory/NonExistsCurrentDirectoryServeTest.php +++ b/tests/Command/Serve/NonExistsCurrentDirectory/NonExistsCurrentDirectoryServeTest.php @@ -4,7 +4,6 @@ namespace Yiisoft\Yii\Console\Tests\Command\Serve\NonExistsCurrentDirectory; - use RuntimeException; use Symfony\Component\Console\Tester\CommandTester; use Yiisoft\Yii\Console\Tests\TestCase; @@ -17,7 +16,7 @@ public function testBase(): void $commandCreate = new CommandTester($command); - $directory = __DIR__.'/test-dir'; + $directory = __DIR__ . '/test-dir'; if (!file_exists($directory)) { mkdir($directory); } From 7d46e9445b65947043dcf6f3c6a1649a164635e6 Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Thu, 3 Apr 2025 13:50:09 +0300 Subject: [PATCH 3/4] fix --- .../NonExistsCurrentDirectoryServeTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Command/Serve/NonExistsCurrentDirectory/NonExistsCurrentDirectoryServeTest.php b/tests/Command/Serve/NonExistsCurrentDirectory/NonExistsCurrentDirectoryServeTest.php index c3830846..021783bb 100644 --- a/tests/Command/Serve/NonExistsCurrentDirectory/NonExistsCurrentDirectoryServeTest.php +++ b/tests/Command/Serve/NonExistsCurrentDirectory/NonExistsCurrentDirectoryServeTest.php @@ -16,7 +16,7 @@ public function testBase(): void $commandCreate = new CommandTester($command); - $directory = __DIR__ . '/test-dir'; + $directory = __DIR__ . DIRECTORY_SEPARATOR . 'test-dir'; if (!file_exists($directory)) { mkdir($directory); } From c3ebc5ba45bfd57eff5af71c96b7483492db97c1 Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Thu, 3 Apr 2025 13:53:21 +0300 Subject: [PATCH 4/4] fix --- .../NonExistsCurrentDirectoryServeTest.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tests/Command/Serve/NonExistsCurrentDirectory/NonExistsCurrentDirectoryServeTest.php b/tests/Command/Serve/NonExistsCurrentDirectory/NonExistsCurrentDirectoryServeTest.php index 021783bb..9f6b0ed8 100644 --- a/tests/Command/Serve/NonExistsCurrentDirectory/NonExistsCurrentDirectoryServeTest.php +++ b/tests/Command/Serve/NonExistsCurrentDirectory/NonExistsCurrentDirectoryServeTest.php @@ -10,13 +10,16 @@ final class NonExistsCurrentDirectoryServeTest extends TestCase { + /** + * @requires OS Linux + */ public function testBase(): void { $command = $this->application()->find('serve'); $commandCreate = new CommandTester($command); - $directory = __DIR__ . DIRECTORY_SEPARATOR . 'test-dir'; + $directory = __DIR__ . '/test-dir'; if (!file_exists($directory)) { mkdir($directory); }