Skip to content

Commit

Permalink
Merge pull request #34 from webmozart/fix-tests
Browse files Browse the repository at this point in the history
Fixed PuliRunner tests on Ubuntu
  • Loading branch information
webmozart committed Jan 5, 2016
2 parents 3c9696e + 14ec58e commit 0f22ff8
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 49 deletions.
48 changes: 42 additions & 6 deletions src/PuliRunner.php
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand All @@ -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, '<?php')) {
$this->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.
*
Expand Down Expand Up @@ -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);
}
}
@@ -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 $*
2 changes: 0 additions & 2 deletions tests/Fixtures/scripts/php_classical/puli.bat

This file was deleted.

3 changes: 0 additions & 3 deletions tests/Fixtures/scripts/php_hashbang/puli.bat

This file was deleted.

101 changes: 64 additions & 37 deletions tests/PuliRunnerTest.php
Expand Up @@ -13,65 +13,92 @@

use PHPUnit_Framework_TestCase;
use Puli\ComposerPlugin\PuliRunner;
use ReflectionObject;
use Symfony\Component\Process\PhpExecutableFinder;
use Symfony\Component\Process\ProcessUtils;
use Webmozart\PathUtil\Path;

/**
* @since 1.0
*
* @author Titouan Galopin <galopintitouan@gmail.com>
* @author Bernhard Schussek <bschussek@gmail.com>
*/
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());
}
}

0 comments on commit 0f22ff8

Please sign in to comment.