Browse Source

Merge branch '2.2' into 2.3

# Conflicts:
#	tests/AutoReview/ProjectCodeTest.php
#	tests/Linter/ProcessLinterTest.php
Dariusz Ruminski 7 years ago
parent
commit
12d4ec7e1c

+ 18 - 4
src/Console/Command/HelpCommand.php

@@ -338,10 +338,14 @@ EOF
             return $version;
         }
 
-        $currentMajor = (int) Application::VERSION;
-        $changelogFile = __DIR__.'/../../../CHANGELOG.md';
-        $changelog = @file_get_contents($changelogFile);
+        $changelogFile = self::getChangeLogFile();
+        if (null === $changelogFile) {
+            $version = Application::VERSION;
 
+            return $version;
+        }
+
+        $changelog = @file_get_contents($changelogFile);
         if (false === $changelog) {
             $error = error_get_last();
 
@@ -352,7 +356,7 @@ EOF
             ));
         }
 
-        for ($i = $currentMajor; $i > 0; --$i) {
+        for ($i = (int) Application::VERSION; $i > 0; --$i) {
             if (1 === preg_match('/Changelog for v('.$i.'.\d+.\d+)/', $changelog, $matches)) {
                 $version = $matches[1];
 
@@ -375,6 +379,16 @@ EOF
         $output->getFormatter()->setStyle('url', new OutputFormatterStyle('blue'));
     }
 
+    /**
+     * @return string|null
+     */
+    private static function getChangeLogFile()
+    {
+        $changelogFile = __DIR__.'/../../../CHANGELOG.md';
+
+        return is_file($changelogFile) ? $changelogFile : null;
+    }
+
     /**
      * @return string
      */

+ 7 - 12
src/Linter/ProcessLinter.php

@@ -16,7 +16,7 @@ use PhpCsFixer\FileRemoval;
 use Symfony\Component\Filesystem\Exception\IOException;
 use Symfony\Component\Process\PhpExecutableFinder;
 use Symfony\Component\Process\Process;
-use Symfony\Component\Process\ProcessUtils;
+use Symfony\Component\Process\ProcessBuilder;
 
 /**
  * Handle PHP code linting using separated process of `php -l _file_`.
@@ -121,7 +121,7 @@ final class ProcessLinter implements LinterInterface
             return $this->createProcessForSource(file_get_contents($path));
         }
 
-        $process = new Process($this->prepareCommand($path));
+        $process = $this->prepareProcess($path);
         $process->setTimeout(null);
         $process->start();
 
@@ -150,22 +150,17 @@ final class ProcessLinter implements LinterInterface
     }
 
     /**
-     * Prepare command that will lint a file.
-     *
      * @param string $path
      *
-     * @return string
+     * @return Process
      */
-    private function prepareCommand($path)
+    private function prepareProcess($path)
     {
-        $executable = ProcessUtils::escapeArgument($this->executable);
-
+        $arguments = ['-l', $path];
         if (defined('HHVM_VERSION')) {
-            $executable .= ' --php';
+            array_unshift($arguments, '--php');
         }
 
-        $path = ProcessUtils::escapeArgument($path);
-
-        return sprintf('%s -l %s', $executable, $path);
+        return ProcessBuilder::create($arguments)->setPrefix($this->executable)->getProcess();
     }
 }

+ 0 - 1
tests/AutoReview/ProjectCodeTest.php

@@ -38,7 +38,6 @@ final class ProjectCodeTest extends TestCase
         \PhpCsFixer\ConfigurationException\InvalidConfigurationException::class,
         \PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException::class,
         \PhpCsFixer\ConfigurationException\RequiredFixerConfigurationException::class,
-        \PhpCsFixer\Console\Command\HelpCommand::class,
         \PhpCsFixer\Console\Command\DescribeNameNotFoundException::class,
         \PhpCsFixer\Console\Command\SelfUpdateCommand::class,
         \PhpCsFixer\Console\Output\NullOutput::class,

+ 39 - 0
tests/Console/Command/HelpCommandTest.php

@@ -0,0 +1,39 @@
+<?php
+
+/*
+ * This file is part of PHP CS Fixer.
+ *
+ * (c) Fabien Potencier <fabien@symfony.com>
+ *     Dariusz Rumiński <dariusz.ruminski@gmail.com>
+ *
+ * This source file is subject to the MIT license that is bundled
+ * with this source code in the file LICENSE.
+ */
+
+namespace PhpCsFixer\Tests\Console\Command;
+
+use PhpCsFixer\Console\Application;
+use PhpCsFixer\Console\Command\HelpCommand;
+use PHPUnit\Framework\TestCase;
+
+/**
+ * @internal
+ *
+ * @covers \PhpCsFixer\Console\Command\HelpCommand
+ */
+final class HelpCommandTest extends TestCase
+{
+    public function testGetLatestReleaseVersionFromChangeLog()
+    {
+        $helpVersion = HelpCommand::getLatestReleaseVersionFromChangeLog();
+        $appVersion = Application::VERSION;
+        $this->assertTrue(
+            version_compare($helpVersion, $appVersion, '<='),
+            sprintf(
+                'Expected version from change log "%s" <= as application version "%s".',
+                $helpVersion,
+                $appVersion
+            )
+        );
+    }
+}

+ 28 - 35
tests/Linter/ProcessLinterTest.php

@@ -14,7 +14,6 @@ namespace PhpCsFixer\Tests\Linter;
 
 use PhpCsFixer\Linter\ProcessLinter;
 use PhpCsFixer\Test\AccessibleObject;
-use Symfony\Component\Process\ProcessUtils;
 
 /**
  * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
@@ -31,20 +30,41 @@ final class ProcessLinterTest extends AbstractLinterTestCase
         $this->assertTrue($this->createLinter()->isAsync());
     }
 
-    public function testPrepareCommandOnPhp()
+    /**
+     * @param string $executable
+     * @param string $file
+     * @param string $expected
+     *
+     * @testWith ["php", "foo.php", "'php' '-l' 'foo.php'"]
+     *           ["C:\\Program Files\\php\\php.exe", "foo bar\\baz.php", "'C:\\Program Files\\php\\php.exe' '-l' 'foo bar\\baz.php'"]
+     * @requires OS Linux
+     */
+    public function testPrepareCommandOnPhpOnLinux($executable, $file, $expected)
     {
         if (defined('HHVM_VERSION')) {
             $this->markTestSkipped('Skip tests for PHP compiler when running on HHVM compiler.');
         }
 
         $this->assertSame(
-            $this->fixEscape('"php" -l "foo.php"'),
-            AccessibleObject::create(new ProcessLinter('php'))->prepareCommand('foo.php')
+            $expected,
+            AccessibleObject::create(new ProcessLinter($executable))->prepareProcess($file)->getCommandLine()
         );
+    }
 
+    /**
+     * @param string $executable
+     * @param string $file
+     * @param string $expected
+     *
+     * @testWith ["php", "foo.php", "php -l foo.php"]
+     *           ["C:\\Program Files\\php\\php.exe", "foo bar\\baz.php", "\"C:\\Program Files\\php\\php.exe\" -l \"foo bar\\baz.php\""]
+     * @requires OS Win
+     */
+    public function testPrepareCommandOnPhpOnWindows($executable, $file, $expected)
+    {
         $this->assertSame(
-            $this->fixEscape('"C:\Program Files\php\php.exe" -l "foo bar\baz.php"'),
-            AccessibleObject::create(new ProcessLinter('C:\Program Files\php\php.exe'))->prepareCommand('foo bar\baz.php')
+            $expected,
+            AccessibleObject::create(new ProcessLinter($executable))->prepareProcess($file)->getCommandLine()
         );
     }
 
@@ -55,8 +75,8 @@ final class ProcessLinterTest extends AbstractLinterTestCase
         }
 
         $this->assertSame(
-            $this->fixEscape('"hhvm" --php -l "foo.php"'),
-            AccessibleObject::create(new ProcessLinter('hhvm'))->prepareCommand('foo.php')
+            "'hhvm' '--php' '-l' 'foo.php'",
+            AccessibleObject::create(new ProcessLinter('hhvm'))->prepareProcess('foo.php')->getCommandLine()
         );
     }
 
@@ -67,31 +87,4 @@ final class ProcessLinterTest extends AbstractLinterTestCase
     {
         return new ProcessLinter();
     }
-
-    /**
-     * Fix escaping character.
-     *
-     * Escape character may be different on various environments.
-     * This method change used escape character into character that is default
-     * for environment.
-     *
-     * @param string $value          value to be fixed
-     * @param string $usedEscapeChar used escape char, may be only ' or "
-     *
-     * @return string
-     */
-    private function fixEscape($value, $usedEscapeChar = '"')
-    {
-        static $escapeChar = null;
-
-        if (null === $escapeChar) {
-            $escapeChar = ProcessUtils::escapeArgument('x')[0];
-        }
-
-        if ($usedEscapeChar === $escapeChar) {
-            return $value;
-        }
-
-        return str_replace($usedEscapeChar, $escapeChar, $value);
-    }
 }