From 261eae970f351a0cfc8006cbf7ea97080f24cef8 Mon Sep 17 00:00:00 2001 From: Alex Bowers Date: Fri, 28 Jul 2017 22:09:12 +0100 Subject: [PATCH] Added deprecation to cwd not existing Fixes #18249 --- UPGRADE-3.4.md | 2 + UPGRADE-4.0.md | 2 + src/Symfony/Component/Process/CHANGELOG.md | 1 + src/Symfony/Component/Process/Process.php | 16 +++++-- .../Component/Process/Tests/ProcessTest.php | 44 ++++++++++++++++++- 5 files changed, 59 insertions(+), 6 deletions(-) diff --git a/UPGRADE-3.4.md b/UPGRADE-3.4.md index 2c80252b5b..9ad662ffed 100644 --- a/UPGRADE-3.4.md +++ b/UPGRADE-3.4.md @@ -210,6 +210,8 @@ Process * The `Symfony\Component\Process\ProcessBuilder` class has been deprecated, use the `Symfony\Component\Process\Process` class directly instead. + * Calling `Process::start()` without setting a valid working directory (via `setWorkingDirectory()` or constructor) beforehand is deprecated and will throw an exception in 4.0. + Profiler -------- diff --git a/UPGRADE-4.0.md b/UPGRADE-4.0.md index 9f8017c47e..59f4607863 100644 --- a/UPGRADE-4.0.md +++ b/UPGRADE-4.0.md @@ -567,6 +567,8 @@ Ldap Process ------- + * Passing a not existing working directory to the constructor of the `Symfony\Component\Process\Process` class is not supported anymore. + * The `Symfony\Component\Process\ProcessBuilder` class has been removed, use the `Symfony\Component\Process\Process` class directly instead. diff --git a/src/Symfony/Component/Process/CHANGELOG.md b/src/Symfony/Component/Process/CHANGELOG.md index 7193c498d4..c5cdb99441 100644 --- a/src/Symfony/Component/Process/CHANGELOG.md +++ b/src/Symfony/Component/Process/CHANGELOG.md @@ -5,6 +5,7 @@ CHANGELOG ----- * deprecated the ProcessBuilder class + * deprecated calling `Process::start()` without setting a valid working directory beforehand (via `setWorkingDirectory()` or constructor) 3.3.0 ----- diff --git a/src/Symfony/Component/Process/Process.php b/src/Symfony/Component/Process/Process.php index e925e91386..79cb20cb66 100644 --- a/src/Symfony/Component/Process/Process.php +++ b/src/Symfony/Component/Process/Process.php @@ -334,6 +334,14 @@ class Process implements \IteratorAggregate $ptsWorkaround = fopen(__FILE__, 'r'); } + if (!is_dir($this->cwd)) { + if ('\\' === DIRECTORY_SEPARATOR) { + throw new RuntimeException('The provided cwd does not exist.'); + } + + @trigger_error('The provided cwd does not exist. Command is currently ran against getcwd(). This behavior is deprecated since version 3.4 and will be removed in 4.0.', E_USER_DEPRECATED); + } + $this->process = proc_open($commandline, $descriptors, $this->processPipes->pipes, $this->cwd, $env, $this->options); foreach ($envBackup as $k => $v) { @@ -831,7 +839,7 @@ class Process implements \IteratorAggregate */ public function isStarted() { - return $this->status != self::STATUS_READY; + return self::STATUS_READY != $this->status; } /** @@ -843,7 +851,7 @@ class Process implements \IteratorAggregate { $this->updateStatus(false); - return $this->status == self::STATUS_TERMINATED; + return self::STATUS_TERMINATED == $this->status; } /** @@ -1322,7 +1330,7 @@ class Process implements \IteratorAggregate */ public function checkTimeout() { - if ($this->status !== self::STATUS_STARTED) { + if (self::STATUS_STARTED !== $this->status) { return; } @@ -1513,7 +1521,7 @@ class Process implements \IteratorAggregate $callback = $this->callback; foreach ($result as $type => $data) { if (3 !== $type) { - $callback($type === self::STDOUT ? self::OUT : self::ERR, $data); + $callback(self::STDOUT === $type ? self::OUT : self::ERR, $data); } elseif (!isset($this->fallbackStatus['signaled'])) { $this->fallbackStatus['exitcode'] = (int) $data; } diff --git a/src/Symfony/Component/Process/Tests/ProcessTest.php b/src/Symfony/Component/Process/Tests/ProcessTest.php index 267257117d..1ad601c715 100644 --- a/src/Symfony/Component/Process/Tests/ProcessTest.php +++ b/src/Symfony/Component/Process/Tests/ProcessTest.php @@ -48,6 +48,46 @@ class ProcessTest extends TestCase } } + /** + * @group legacy + * @expectedDeprecation The provided cwd does not exist. Command is currently ran against getcwd(). This behavior is deprecated since version 3.4 and will be removed in 4.0. + */ + public function testInvalidCwd() + { + if ('\\' === DIRECTORY_SEPARATOR) { + $this->markTestSkipped('Windows handles this automatically.'); + } + + // Check that it works fine if the CWD exists + $cmd = new Process('echo test', __DIR__); + $cmd->run(); + + $cmd = new Process('echo test', __DIR__.'/notfound/'); + $cmd->run(); + } + + /** + * @expectedException \Symfony\Component\Process\Exception\RuntimeException + * @expectedExceptionMessage The provided cwd does not exist. + */ + public function testInvalidCwdOnWindows() + { + if ('\\' !== DIRECTORY_SEPARATOR) { + $this->markTestSkipped('Unix handles this automatically.'); + } + + try { + // Check that it works fine if the CWD exists + $cmd = new Process('echo test', __DIR__); + $cmd->run(); + } catch (\Exception $e) { + $this->fail($e); + } + + $cmd = new Process('echo test', __DIR__.'/notfound/'); + $cmd->run(); + } + public function testThatProcessDoesNotThrowWarningDuringRun() { if ('\\' === DIRECTORY_SEPARATOR) { @@ -313,7 +353,7 @@ class ProcessTest extends TestCase $called = false; $p->run(function ($type, $buffer) use (&$called) { - $called = $buffer === 'foo'; + $called = 'foo' === $buffer; }); $this->assertTrue($called, 'The callback should be executed with the output'); @@ -326,7 +366,7 @@ class ProcessTest extends TestCase $called = false; $p->run(function ($type, $buffer) use (&$called) { - $called = $buffer === 'foo'; + $called = 'foo' === $buffer; }); $this->assertTrue($called, 'The callback should be executed with the output');