From ab706017001531cddd8669e00f9f0e38977d842d Mon Sep 17 00:00:00 2001 From: AnrDaemon Date: Thu, 22 Dec 2016 20:14:57 +0300 Subject: [PATCH 1/2] Skip git-related tests if no git found. --- .../Test/Downloader/GitDownloaderTest.php | 11 ++++++++ .../Archiver/ArchivableFilesFinderTest.php | 25 ++----------------- .../Package/Archiver/ArchiveManagerTest.php | 4 +++ tests/Composer/TestCase.php | 16 ++++++++++++ 4 files changed, 33 insertions(+), 23 deletions(-) diff --git a/tests/Composer/Test/Downloader/GitDownloaderTest.php b/tests/Composer/Test/Downloader/GitDownloaderTest.php index 0228193b8..688b9ceee 100644 --- a/tests/Composer/Test/Downloader/GitDownloaderTest.php +++ b/tests/Composer/Test/Downloader/GitDownloaderTest.php @@ -25,6 +25,17 @@ class GitDownloaderTest extends TestCase /** @var string */ private $workingDir; + private $skipped; + + protected function initialize() + { + try { + $this->skipIfNotExecutable('git'); + } catch (\PHPUnit_Framework_SkippedTestError $e) { + $this->skipped = 'This test needs a git binary in the PATH to be able to run'; + } + } + protected function setUp() { $this->fs = new Filesystem; diff --git a/tests/Composer/Test/Package/Archiver/ArchivableFilesFinderTest.php b/tests/Composer/Test/Package/Archiver/ArchivableFilesFinderTest.php index cce67c1aa..76629276d 100644 --- a/tests/Composer/Test/Package/Archiver/ArchivableFilesFinderTest.php +++ b/tests/Composer/Test/Package/Archiver/ArchivableFilesFinderTest.php @@ -16,7 +16,6 @@ use Composer\Package\Archiver\ArchivableFilesFinder; use Composer\TestCase; use Composer\Util\Filesystem; use Symfony\Component\Process\Process; -use Symfony\Component\Process\ExecutableFinder; class ArchivableFilesFinderTest extends TestCase { @@ -146,10 +145,7 @@ class ArchivableFilesFinderTest extends TestCase public function testGitExcludes() { - // Ensure that git is available for testing. - if (!$this->isProcessAvailable('git')) { - return $this->markTestSkipped('git is not available.'); - } + $this->skipIfNotExecutable('git'); file_put_contents($this->sources.'/.gitignore', implode("\n", array( '# gitignore rules with comments and blank lines', @@ -202,10 +198,7 @@ class ArchivableFilesFinderTest extends TestCase public function testHgExcludes() { - // Ensure that Mercurial is available for testing. - if (!$this->isProcessAvailable('hg')) { - return $this->markTestSkipped('Mercurial is not available.'); - } + $this->skipIfNotExecutable('hg'); file_put_contents($this->sources.'/.hgignore', implode("\n", array( '# hgignore rules with comments, blank lines and syntax changes', @@ -281,18 +274,4 @@ class ArchivableFilesFinderTest extends TestCase $this->assertEquals($expectedFiles, $actualFiles); } - - /** - * Check whether or not the given process is available. - * - * @param string $process The name of the binary to test. - * - * @return bool True if the process is available, false otherwise. - */ - protected function isProcessAvailable($process) - { - $finder = new ExecutableFinder(); - - return (bool) $finder->find($process); - } } diff --git a/tests/Composer/Test/Package/Archiver/ArchiveManagerTest.php b/tests/Composer/Test/Package/Archiver/ArchiveManagerTest.php index 16180bd5d..fa17774ca 100644 --- a/tests/Composer/Test/Package/Archiver/ArchiveManagerTest.php +++ b/tests/Composer/Test/Package/Archiver/ArchiveManagerTest.php @@ -45,6 +45,8 @@ class ArchiveManagerTest extends ArchiverTest public function testArchiveTar() { + $this->skipIfNotExecutable('git'); + $this->setupGitRepo(); $package = $this->setupPackage(); @@ -62,6 +64,8 @@ class ArchiveManagerTest extends ArchiverTest public function testArchiveCustomFileName() { + $this->skipIfNotExecutable('git'); + $this->setupGitRepo(); $package = $this->setupPackage(); diff --git a/tests/Composer/TestCase.php b/tests/Composer/TestCase.php index 44029ad5e..1eca8108b 100644 --- a/tests/Composer/TestCase.php +++ b/tests/Composer/TestCase.php @@ -17,6 +17,7 @@ use Composer\Package\AliasPackage; use Composer\Semver\Constraint\Constraint; use Composer\Util\Filesystem; use Composer\Util\Silencer; +use Symfony\Component\Process\ExecutableFinder; abstract class TestCase extends \PHPUnit_Framework_TestCase { @@ -83,4 +84,19 @@ abstract class TestCase extends \PHPUnit_Framework_TestCase mkdir($directory, 0777, true); } + + /** + * Check whether or not the given name is an available executable. + * + * @param string $executableName The name of the binary to test. + * + * @throws PHPUnit_Framework_SkippedTestError + */ + protected function skipIfNotExecutable($executableName) + { + $finder = new ExecutableFinder(); + + if (!$finder->find($executableName)) + $this->markTestSkipped($executableName . ' is not found or not executable.'); + } } From df5fd3ba0949819120d8e8e3c04456721dca08ee Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Fri, 23 Dec 2016 15:01:05 +0100 Subject: [PATCH 2/2] Cache executable finder calls --- tests/Composer/TestCase.php | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/tests/Composer/TestCase.php b/tests/Composer/TestCase.php index 1eca8108b..1d1784600 100644 --- a/tests/Composer/TestCase.php +++ b/tests/Composer/TestCase.php @@ -22,6 +22,7 @@ use Symfony\Component\Process\ExecutableFinder; abstract class TestCase extends \PHPUnit_Framework_TestCase { private static $parser; + private static $executableCache = array(); public static function getUniqueTmpDirectory() { @@ -94,9 +95,13 @@ abstract class TestCase extends \PHPUnit_Framework_TestCase */ protected function skipIfNotExecutable($executableName) { - $finder = new ExecutableFinder(); + if (!isset(self::$executableCache[$executableName])) { + $finder = new ExecutableFinder(); + self::$executableCache[$executableName] = (bool) $finder->find($executableName); + } - if (!$finder->find($executableName)) + if (false === self::$executableCache[$executableName]) { $this->markTestSkipped($executableName . ' is not found or not executable.'); + } } }