From 14ec58e8b5528b780752db3769c02add8f87f908 Mon Sep 17 00:00:00 2001 From: Bernhard Schussek Date: Mon, 4 Jan 2016 13:27:12 +0100 Subject: [PATCH] Fixed PuliRunner tests on Ubuntu --- src/PuliRunner.php | 48 +++++++-- tests/Fixtures/scripts/{bash => bat}/puli.bat | 2 +- tests/Fixtures/scripts/php_classical/puli.bat | 2 - tests/Fixtures/scripts/php_hashbang/puli.bat | 3 - tests/PuliRunnerTest.php | 101 +++++++++++------- 5 files changed, 107 insertions(+), 49 deletions(-) rename tests/Fixtures/scripts/{bash => bat}/puli.bat (54%) delete mode 100644 tests/Fixtures/scripts/php_classical/puli.bat delete mode 100644 tests/Fixtures/scripts/php_hashbang/puli.bat diff --git a/src/PuliRunner.php b/src/PuliRunner.php index cae772b..29131b2 100644 --- a/src/PuliRunner.php +++ b/src/PuliRunner.php @@ -16,7 +16,6 @@ use Symfony\Component\Process\PhpExecutableFinder; use Symfony\Component\Process\Process; use Symfony\Component\Process\ProcessUtils; -use Webmozart\PathUtil\Path; /** * Executes the "puli" command. @@ -45,7 +44,7 @@ public function __construct($binDir = null) throw new RuntimeException('The "php" command could not be found.'); } - $puliFinder = new ExecutableFinder(); + $finder = new ExecutableFinder(); // Search: // 1. in the current working directory @@ -54,22 +53,36 @@ public function __construct($binDir = null) $searchPath = array_merge(array(getcwd()), (array) $binDir); // Search "puli.phar" in the PATH and the current directory - if (!($puli = $puliFinder->find('puli.phar', null, $searchPath))) { + if (!($puli = $this->find('puli.phar', $searchPath, $finder))) { // Search "puli" in the PATH and Composer's "bin-dir" - if (!($puli = $puliFinder->find('puli', null, $searchPath))) { + if (!($puli = $this->find('puli', $searchPath, $finder))) { throw new RuntimeException('The "puli"/"puli.phar" command could not be found.'); } } + // Fix slashes + $php = strtr($php, '\\', '/'); + $puli = strtr($puli, '\\', '/'); + $content = file_get_contents($puli, null, null, -1, 18); if ($content === '#!/usr/bin/env php' || 0 === strpos($content, 'puli = escapeshellcmd($php).' '.ProcessUtils::escapeArgument($puli); + $this->puli = ProcessUtils::escapeArgument($php).' '.ProcessUtils::escapeArgument($puli); } else { - $this->puli = escapeshellcmd($puli); + $this->puli = ProcessUtils::escapeArgument($puli); } } + /** + * Returns the command used to execute Puli. + * + * @return string The "puli" command. + */ + public function getPuliCommand() + { + return $this->puli; + } + /** * Runs a Puli command. * @@ -102,4 +115,27 @@ public function run($command, array $args = array()) // Normalize line endings across systems return str_replace("\r\n", "\n", $process->getOutput()); } + + private function find($name, array $dirs, ExecutableFinder $finder) + { + $suffixes = array(''); + + if ('\\' === DIRECTORY_SEPARATOR) { + $suffixes[] = '.bat'; + } + + // The finder first looks in the system directories and then in the + // user-defined ones. We want to check the user-defined ones first. + foreach ($dirs as $dir) { + foreach ($suffixes as $suffix) { + $file = $dir.DIRECTORY_SEPARATOR.$name.$suffix; + + if (is_file($file) && ('\\' === DIRECTORY_SEPARATOR || is_executable($file))) { + return $file; + } + } + } + + return $finder->find($name); + } } diff --git a/tests/Fixtures/scripts/bash/puli.bat b/tests/Fixtures/scripts/bat/puli.bat similarity index 54% rename from tests/Fixtures/scripts/bash/puli.bat rename to tests/Fixtures/scripts/bat/puli.bat index 17ac5f1..8a73bd6 100644 --- a/tests/Fixtures/scripts/bash/puli.bat +++ b/tests/Fixtures/scripts/bat/puli.bat @@ -1 +1 @@ -/usr/bin/env php -d allow_url_fopen=On -d detect_unicode=Off /usr/local/Cellar/puli/1.0.0-beta9/libexec/puli.phar $* +/usr/bin/env php -d allow_url_fopen=On -d detect_unicode=Off /usr/local/bin/puli.phar $* diff --git a/tests/Fixtures/scripts/php_classical/puli.bat b/tests/Fixtures/scripts/php_classical/puli.bat deleted file mode 100644 index 2b79be2..0000000 --- a/tests/Fixtures/scripts/php_classical/puli.bat +++ /dev/null @@ -1,2 +0,0 @@ - + * @author Bernhard Schussek */ class PuliRunnerTest extends PHPUnit_Framework_TestCase { + /** + * @var string + */ + private $fixturesDir; + + /** + * @var string + */ + private $previousWd; + + /** + * @var string + */ + private $php; + + protected function setUp() + { + $phpFinder = new PhpExecutableFinder(); + + $this->fixturesDir = Path::normalize(__DIR__.'/Fixtures/scripts'); + $this->previousWd = getcwd(); + $this->php = strtr($phpFinder->find(), '\\', '/'); + } + + protected function tearDown() + { + chdir($this->previousWd); + } + public function testRunnerBash() { - $fixturesDir = Path::normalize(__DIR__.'/Fixtures/scripts/bash'); - $runner = new PuliRunner($fixturesDir); + chdir($this->fixturesDir.'/bash'); - $this->assertRunnerUseScript($fixturesDir, $runner, false); + $runner = new PuliRunner(); + + $expected = ProcessUtils::escapeArgument($this->fixturesDir.'/bash/puli'); + + $this->assertSame($expected, $runner->getPuliCommand()); } - public function testRunnerPhpClassical() + public function testRunnerBat() { - $fixturesDir = Path::normalize(__DIR__.'/Fixtures/scripts/php_classical'); - $runner = new PuliRunner($fixturesDir); + if ('\\' !== DIRECTORY_SEPARATOR) { + $this->markTestSkipped('Requires Windows to run'); + } + + chdir($this->fixturesDir.'/bat'); + + $runner = new PuliRunner(); + + $expected = ProcessUtils::escapeArgument($this->fixturesDir.'/bat/puli.bat'); - $this->assertRunnerUseScript($fixturesDir, $runner, true); + $this->assertSame($expected, $runner->getPuliCommand()); } - public function testRunnerPhpHashbang() + public function testRunnerPhpClassical() { - $fixturesDir = Path::normalize(__DIR__.'/Fixtures/scripts/php_hashbang'); - $runner = new PuliRunner($fixturesDir); + chdir($this->fixturesDir.'/php_classical'); + + $runner = new PuliRunner(); - $this->assertRunnerUseScript($fixturesDir, $runner, true); + $expected = ProcessUtils::escapeArgument($this->php).' '.ProcessUtils::escapeArgument($this->fixturesDir.'/php_classical/puli'); + + $this->assertSame($expected, $runner->getPuliCommand()); } - private function assertRunnerUseScript($fixturesDir, PuliRunner $runner, $throughPhp = false) + public function testRunnerPhpHashbang() { - $reflection = new ReflectionObject($runner); - - $property = $reflection->getProperty('puli'); - $property->setAccessible(true); - - $runnerScript = Path::normalize($property->getValue($runner)); - - if (!$throughPhp) { - if ('\\' === DIRECTORY_SEPARATOR) { - // Windows - $this->assertSame(Path::normalize(escapeshellcmd($fixturesDir.'\puli.BAT')), $runnerScript); - } else { - $this->assertSame($fixturesDir.'/puli', $runnerScript); - } - } else { - if ('\\' === DIRECTORY_SEPARATOR) { - // Windows - $this->assertContains('php.exe', $runnerScript); - $this->assertContains(' "'.$fixturesDir.'/puli.BAT"', $runnerScript); - } else { - $this->assertContains('php', $runnerScript); - $this->assertContains(' \''.$fixturesDir.'/puli\'', $runnerScript); - } - } + chdir($this->fixturesDir.'/php_hashbang'); + + $runner = new PuliRunner(); + + $expected = ProcessUtils::escapeArgument($this->php).' '.ProcessUtils::escapeArgument($this->fixturesDir.'/php_hashbang/puli'); + + $this->assertSame($expected, $runner->getPuliCommand()); } }