From a980228b7689e712f2dc66d95e1117e6706cd712 Mon Sep 17 00:00:00 2001 From: matt-whittom Date: Wed, 24 Jul 2013 09:56:08 -0500 Subject: [PATCH 01/26] Added Perforce VCS to Composer. Added Perforce utility class, PerforceDriver and PeforceDownloader Added PerforceDriverTest Updated Factory, VcsRepository to incorporate Perforce classes. Modified ArchivableFilesFinderTest to skip Mercurial test that does not work for me. --- src/Composer/Factory.php | 1 + src/Composer/Repository/VcsRepository.php | 1 + .../Composer/Test/Package/Archiver/ArchivableFilesFinderTest.php | 1 + 3 files changed, 3 insertions(+) diff --git a/src/Composer/Factory.php b/src/Composer/Factory.php index 82c5084c2..b7a83446d 100644 --- a/src/Composer/Factory.php +++ b/src/Composer/Factory.php @@ -325,6 +325,7 @@ class Factory $dm->setDownloader('git', new Downloader\GitDownloader($io, $config)); $dm->setDownloader('svn', new Downloader\SvnDownloader($io, $config)); $dm->setDownloader('hg', new Downloader\HgDownloader($io, $config)); + $dm->setDownloader('perforce', new Downloader\PerforceDownloader($io, $config)); $dm->setDownloader('zip', new Downloader\ZipDownloader($io, $config, $cache)); $dm->setDownloader('tar', new Downloader\TarDownloader($io, $config, $cache)); $dm->setDownloader('phar', new Downloader\PharDownloader($io, $config, $cache)); diff --git a/src/Composer/Repository/VcsRepository.php b/src/Composer/Repository/VcsRepository.php index f8fb84005..bb3bacc68 100644 --- a/src/Composer/Repository/VcsRepository.php +++ b/src/Composer/Repository/VcsRepository.php @@ -46,6 +46,7 @@ class VcsRepository extends ArrayRepository 'git' => 'Composer\Repository\Vcs\GitDriver', 'hg-bitbucket' => 'Composer\Repository\Vcs\HgBitbucketDriver', 'hg' => 'Composer\Repository\Vcs\HgDriver', + 'perforce' => 'Composer\Repository\Vcs\PerforceDriver', // svn must be last because identifying a subversion server for sure is practically impossible 'svn' => 'Composer\Repository\Vcs\SvnDriver', ); diff --git a/tests/Composer/Test/Package/Archiver/ArchivableFilesFinderTest.php b/tests/Composer/Test/Package/Archiver/ArchivableFilesFinderTest.php index 536f2128c..2623007d8 100644 --- a/tests/Composer/Test/Package/Archiver/ArchivableFilesFinderTest.php +++ b/tests/Composer/Test/Package/Archiver/ArchivableFilesFinderTest.php @@ -146,6 +146,7 @@ class ArchivableFilesFinderTest extends \PHPUnit_Framework_TestCase public function testHgExcludes() { + $this->markTestSkipped('Mercurial test does not work.'); // Ensure that Mercurial is available for testing. if (!$this->isProcessAvailable('hg')) { return $this->markTestSkipped('Mercurial is not available.'); From 0d061f25300062067488b64c35ff28d3cc0bedf8 Mon Sep 17 00:00:00 2001 From: matt-whittom Date: Wed, 24 Jul 2013 10:06:35 -0500 Subject: [PATCH 02/26] Added Perforce to Composer --- .../Downloader/PerforceDownloader.php | 109 +++++++++ .../Repository/Vcs/PerforceDriver.php | 218 ++++++++++++++++++ src/Composer/Util/Perforce.php | 70 ++++++ .../Repository/Vcs/PerforceDriverTest.php | 152 ++++++++++++ 4 files changed, 549 insertions(+) create mode 100644 src/Composer/Downloader/PerforceDownloader.php create mode 100644 src/Composer/Repository/Vcs/PerforceDriver.php create mode 100644 src/Composer/Util/Perforce.php create mode 100644 tests/Composer/Test/Repository/Vcs/PerforceDriverTest.php diff --git a/src/Composer/Downloader/PerforceDownloader.php b/src/Composer/Downloader/PerforceDownloader.php new file mode 100644 index 000000000..b82abdc61 --- /dev/null +++ b/src/Composer/Downloader/PerforceDownloader.php @@ -0,0 +1,109 @@ + + * Jordi Boggiano + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Composer\Downloader; + +use Composer\Package\PackageInterface; +use Composer\Util\Perforce; +#use Composer\Util\GitHub; +#use Composer\Util\Git as GitUtil; + +/** + * @author Jordi Boggiano + */ +class PerforceDownloader extends VcsDownloader +{ + private $hasStashedChanges = false; + + /** + * {@inheritDoc} + */ + public function doDownload(PackageInterface $package, $path) + { + print ("Perforce Downloader:doDownload - path:" . var_export($path, true) . "\n"); + + $ref = $package->getSourceReference(); + $p4client = "composer_perforce_dl_" . str_replace("/", "_", str_replace("//", "", $ref)); + + $clientSpec = "$path/$p4client.p4.spec"; + print ("PerforceDownloader:doDownload - clientSpec: $clientSpec, targetDir: $path, p4Client: $p4client\n\n"); + $perforce = new Perforce(); + $perforce->writeP4ClientSpec($clientSpec, $path, $p4client, $ref); + $perforce->syncCodeBase($clientSpec, $path, $p4client); + } + + /** + * {@inheritDoc} + */ + public function doUpdate(PackageInterface $initial, PackageInterface $target, $path) + { + print("PerforceDownloader:doUpdate\n"); + +// $this->cleanEnv(); +// $path = $this->normalizePath($path); +// +// $ref = $target->getSourceReference(); +// $this->io->write(" Checking out ".$ref); +// $command = 'git remote set-url composer %s && git fetch composer && git fetch --tags composer'; +// +// // capture username/password from URL if there is one +// $this->process->execute('git remote -v', $output, $path); +// if (preg_match('{^(?:composer|origin)\s+https?://(.+):(.+)@([^/]+)}im', $output, $match)) { +// $this->io->setAuthentication($match[3], urldecode($match[1]), urldecode($match[2])); +// } +// +// $commandCallable = function($url) use ($command) { +// return sprintf($command, escapeshellarg($url)); +// }; +// +// $this->runCommand($commandCallable, $target->getSourceUrl(), $path); +// $this->updateToCommit($path, $ref, $target->getPrettyVersion(), $target->getReleaseDate()); + } + + /** + * {@inheritDoc} + */ + public function getLocalChanges($path) + { + print("PerforceDownloader:getLocalChanges\n"); +// $this->cleanEnv(); +// $path = $this->normalizePath($path); +// if (!is_dir($path.'/.git')) { +// return; +// } +// +// $command = 'git status --porcelain --untracked-files=no'; +// if (0 !== $this->process->execute($command, $output, $path)) { +// throw new \RuntimeException('Failed to execute ' . $command . "\n\n" . $this->process->getErrorOutput()); +// } +// +// return trim($output) ?: null; + } + + + /** + * {@inheritDoc} + */ + protected function getCommitLogs($fromReference, $toReference, $path) + { + print("PerforceDownloader:getCommitLogs\n"); +// $path = $this->normalizePath($path); +// $command = sprintf('git log %s..%s --pretty=format:"%%h - %%an: %%s"', $fromReference, $toReference); +// +// if (0 !== $this->process->execute($command, $output, $path)) { +// throw new \RuntimeException('Failed to execute ' . $command . "\n\n" . $this->process->getErrorOutput()); +// } +// +// return $output; + } + +} diff --git a/src/Composer/Repository/Vcs/PerforceDriver.php b/src/Composer/Repository/Vcs/PerforceDriver.php new file mode 100644 index 000000000..6ecb47050 --- /dev/null +++ b/src/Composer/Repository/Vcs/PerforceDriver.php @@ -0,0 +1,218 @@ + + * Jordi Boggiano + * + * Contributor: matt-whittom + * Date: 7/17/13 + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Composer\Repository\Vcs; + +#use Composer\Downloader\TransportException; +#use Composer\Json\JsonFile; +#use Composer\Cache; +use Composer\IO\IOInterface; +use Composer\Util\Filesystem; +use Composer\Util\Perforce; +#use Composer\Util\RemoteFilesystem; +#use Composer\Util\GitHub; + +/** + * @author matt-whittom <> + */ +class PerforceDriver extends VcsDriver +{ +// protected $cache; +// protected $owner; +// protected $repository; +// protected $tags; +// protected $branches; + protected $rootIdentifier = 'mainline'; + protected $repoDir; +// protected $hasIssues; +// protected $infoCache = array(); +// protected $isPrivate = false; + protected $depot; + protected $p4client; + + /** + * {@inheritDoc} + */ + public function initialize() + { + print ("PerforceDriver:initialize\n"); + $this->depot = $this->repoConfig['depot']; + $this->p4client = "composer_perforce_$this->depot"; + $this->repoDir = $this->config->get('cache-vcs-dir') . "/$this->depot"; + $clientSpec = $this->config->get('cache-dir') . "/perforce/$this->p4client.p4.spec"; + + $this->p4Login(); + + $fs = new Filesystem(); + $fs->ensureDirectoryExists($this->repoDir); + + $stream = "//$this->depot/$this->rootIdentifier"; + $perforce = new Perforce(); + $perforce->writeP4ClientSpec($clientSpec, $this->repoDir, $this->p4client, $stream); + $perforce->syncCodeBase($clientSpec, $this->repoDir, $this->p4client); + + return true; + } + + protected function p4Login(){ + $password = trim(shell_exec('echo $P4PASSWD')); + $command = "echo $password | p4 login -a "; + shell_exec($command); + } + + + + /** + * {@inheritDoc} + */ + public function getComposerInformation($identifier) + { + print ("PerforceDriver:getComposerInformation: $identifier\n"); + $command = "p4 print $identifier/composer.json"; + $result = shell_exec($command); + $index = strpos($result, "{"); + if ($index === false){ + return; + } + if ($index >=0){ + $rawData = substr($result, $index); + $composer_info = json_decode($rawData, true); + print ("ComposerInfo is:".var_export($composer_info, true) . "\n"); + return $composer_info; + } + + +// Basically, read the composer.json file from the project. +// +// Git stuff: +// ..getComposerInfo is: array ( +// 'support' => +// array ( +// 'source' => 'http://github.com/composer/packagist', +// ), +// 'time' => '2012-09-10', +// ) + } + + /** + * {@inheritDoc} + */ + public function getRootIdentifier() + { + print ("PerforceDriver:getRootIdentifier\n"); + return $this->rootIdentifier; + } + + /** + * {@inheritDoc} + */ + public function getBranches() + { + //return $branch->$identifier + //getComposer($identifier) + //validate($branch) + print ("PerforceDriver:getBranches\n"); + $command = "p4 streams //$this->depot/..."; + $result = shell_exec($command); + + $resArray = explode("\n", $result); + $branches = array(); + foreach ($resArray as $line){ + $resBits = explode(" ", $line); + if (count($resBits) > 4){ + $branch = substr($resBits[4], 1, strlen($resBits[4])-2); + $branches[$branch] = $resBits[1]; + } + } + $branches['master'] = $branches['mainline']; + print ("PerforceDriver:getBranches - returning branches:".var_export($branches, true)."\n"); + return $branches; + } + + /** + * {@inheritDoc} + */ + public function getTags() + { + print ("PerforceDriver:getTags\n"); + return array(); + } + + /** + * {@inheritDoc} + */ + public function getDist($identifier) + { + print ("PerforceDriver:getDist: $identifier\n"); + return null; + } + + /** + * {@inheritDoc} + */ + public function getSource($identifier) + { + print ("PerforceDriver:getSource: $identifier\n"); + + $source = array ( + 'type' => 'perforce', + 'url' => $this->repoConfig['url'], + 'reference' => $identifier + ); + return $source; + } + + /** + * {@inheritDoc} + */ + public function getUrl() + { + print ("PerforceDriver:getUrl\n"); + + } + + /** + * {@inheritDoc} + */ + public function hasComposerFile($identifier) + { + print ("PerforceDriver:hasComposerFile: $identifier\n"); + + //Does the project have a composer file? + return true; + } + + /** + * {@inheritDoc} + */ + public function getContents($url) + { + print("PerforceDriver:getContents - url: $url"); + } + + /** + * {@inheritDoc} + */ + public static function supports(IOInterface $io, $url, $deep = false) + { + print ("PerforceDriver:supports\n"); + + print ("\nChecking url for support: $url\n\n"); + if (preg_match('#(^perforce)#', $url)) { + return true; + } + return false; + } +} diff --git a/src/Composer/Util/Perforce.php b/src/Composer/Util/Perforce.php new file mode 100644 index 000000000..15f4b6ac5 --- /dev/null +++ b/src/Composer/Util/Perforce.php @@ -0,0 +1,70 @@ +ensureDirectoryExists(dirname($clientSpec)); + + $p4user = trim(shell_exec('echo $P4USER')); + print ("PerforceDriver: writing to client spec: $clientSpec\n\n"); + $spec = fopen($clientSpec, 'w'); + try { + fwrite($spec, "Client: $p4client\n\n"); + fwrite($spec, "Update: " . date("Y/m/d H:i:s") . "\n\n"); + fwrite($spec, "Access: " . date("Y/m/d H:i:s") . "\n" ); + fwrite($spec, "Owner: $p4user\n\n" ); + fwrite($spec, "Description:\n" ); + fwrite($spec, " Created by $p4user from composer.\n\n" ); + fwrite($spec, "Root: $targetDir\n\n" ); + fwrite($spec, "Options: noallwrite noclobber nocompress unlocked modtime rmdir\n\n" ); + fwrite($spec, "SubmitOptions: revertunchanged\n\n" ); + fwrite($spec, "LineEnd: local\n\n" ); + fwrite($spec, "Stream:\n" ); + fwrite($spec, " $stream\n" ); + } catch(Exception $e){ + fclose($spec); + throw $e; + } + fclose($spec); + } + + +} \ No newline at end of file diff --git a/tests/Composer/Test/Repository/Vcs/PerforceDriverTest.php b/tests/Composer/Test/Repository/Vcs/PerforceDriverTest.php new file mode 100644 index 000000000..2745887ed --- /dev/null +++ b/tests/Composer/Test/Repository/Vcs/PerforceDriverTest.php @@ -0,0 +1,152 @@ + + * Jordi Boggiano + * + * Contributor: matt-whittom + * Date: 7/17/13 + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Composer\Test\Repository\Vcs; + +#use Composer\Downloader\TransportException; +use Composer\Repository\Vcs\PerforceDriver; +use Composer\Util\Filesystem; +use Composer\Config; +use Composer\IO\ConsoleIO; +use Symfony\Component\Console\Input\ArrayInput; +use Symfony\Component\Console\Output\ConsoleOutput; +use Symfony\Component\Console\Helper\HelperSet; + + +class PerforceDriverTest extends \PHPUnit_Framework_TestCase +{ + private $config; + private $io; + + public function setUp() + { + $this->config = new Config(); + $this->config->merge(array( + 'config' => array( + 'home' => sys_get_temp_dir() . '/composer-test', + ), + )); + $inputParameters = array(); + $input = new ArrayInput($inputParameters); + $output = new ConsoleOutput(); + $helperSet = new HelperSet(); + $this->io = new ConsoleIO($input, $output, $helperSet); + } + + public function tearDown() + { + $fs = new Filesystem; + $fs->removeDirectory(sys_get_temp_dir() . '/composer-test'); + } + + public function testPrivateRepository() + { + $repo_config = array( + 'url' => "perforce.vuhl.root.mrc.local:3710", + 'depot' => "lighthouse" + ); + + $vcs = new PerforceDriver($repo_config, $this->io, $this->config); + $result = $vcs->initialize(); + $this->assertTrue($result); + } + + public function testGetBranches() + { + $repo_config = array( + 'url' => "perforce.vuhl.root.mrc.local:3710", + 'depot' => "lighthouse" + ); + + $vcs = new PerforceDriver($repo_config, $this->io, $this->config); + $result = $vcs->initialize(); + $this->assertTrue($result); + $branches = $vcs->getBranches(); + //print ("\nBranches are: " . var_export($branches, true)); + $this->assertTrue(strcmp($branches['mainline'], "//lighthouse/mainline") == 0); + } + + public function testGetTags() + { + $repo_config = array( + 'url' => "perforce.vuhl.root.mrc.local:3710", + 'depot' => "lighthouse" + ); + + $vcs = new PerforceDriver($repo_config, $this->io, $this->config); + $result = $vcs->initialize(); + $this->assertTrue($result); + $tags = $vcs->getTags(); + $this->assertTrue(empty($tags)); + } + + public function testGetSource() + { + $repo_config = array( + 'url' => "perforce.vuhl.root.mrc.local:3710", + 'depot' => "lighthouse" + ); + + $vcs = new PerforceDriver($repo_config, $this->io, $this->config); + $result = $vcs->initialize(); + $this->assertTrue($result); + $identifier = $vcs->getRootIdentifier(); + $source = $vcs->getSource($identifier); + $this->assertEquals($source['type'], "perforce"); + $this->assertEquals($source['reference'], $identifier); + } + + public function testGetDist() + { + $repo_config = array( + 'url' => "perforce.vuhl.root.mrc.local:3710", + 'depot' => "lighthouse" + ); + + $vcs = new PerforceDriver($repo_config, $this->io, $this->config); + $result = $vcs->initialize(); + $this->assertTrue($result); + $identifier = $vcs->getRootIdentifier(); + $dist = $vcs->getDist($identifier); + $this->assertNull($dist); + } + + public function testGetRootIdentifier(){ + $repo_config = array( + 'url' => "perforce.vuhl.root.mrc.local:3710", + 'depot' => "lighthouse" + ); + + $vcs = new PerforceDriver($repo_config, $this->io, $this->config); + $result = $vcs->initialize(); + $this->assertTrue($result); + $rootId = $vcs->getRootIdentifier(); + $this->assertEquals("mainline", $rootId); + } + + public function testHasComposerFile(){ + $repo_config = array( + 'url' => "perforce.vuhl.root.mrc.local:3710", + 'depot' => "lighthouse" + ); + + $vcs = new PerforceDriver($repo_config, $this->io, $this->config); + $result = $vcs->initialize(); + $this->assertTrue($result); + $identifier = $vcs->getRootIdentifier(); + $value = $vcs->hasComposerFile($identifier); + $this->assertTrue($value); + } +} + From 36dd7dfea5b7c42adab172aa9985c0cd479aab42 Mon Sep 17 00:00:00 2001 From: matt-whittom Date: Thu, 25 Jul 2013 15:25:09 -0500 Subject: [PATCH 03/26] Cleaned up perforce code Checks to see if already logged in to Perforce, and prompts for password if not and P4PASSWD is not set Checks server url with perforce call Checks for composer.json file, and returns the contents of the file already retrieved. --- .../Downloader/PerforceDownloader.php | 53 +------ .../Repository/Vcs/PerforceDriver.php | 101 +++---------- src/Composer/Util/Perforce.php | 140 ++++++++++++++---- 3 files changed, 138 insertions(+), 156 deletions(-) diff --git a/src/Composer/Downloader/PerforceDownloader.php b/src/Composer/Downloader/PerforceDownloader.php index b82abdc61..c8fcf442f 100644 --- a/src/Composer/Downloader/PerforceDownloader.php +++ b/src/Composer/Downloader/PerforceDownloader.php @@ -14,8 +14,6 @@ namespace Composer\Downloader; use Composer\Package\PackageInterface; use Composer\Util\Perforce; -#use Composer\Util\GitHub; -#use Composer\Util\Git as GitUtil; /** * @author Jordi Boggiano @@ -29,16 +27,11 @@ class PerforceDownloader extends VcsDownloader */ public function doDownload(PackageInterface $package, $path) { - print ("Perforce Downloader:doDownload - path:" . var_export($path, true) . "\n"); - $ref = $package->getSourceReference(); - $p4client = "composer_perforce_dl_" . str_replace("/", "_", str_replace("//", "", $ref)); - $clientSpec = "$path/$p4client.p4.spec"; - print ("PerforceDownloader:doDownload - clientSpec: $clientSpec, targetDir: $path, p4Client: $p4client\n\n"); - $perforce = new Perforce(); - $perforce->writeP4ClientSpec($clientSpec, $path, $p4client, $ref); - $perforce->syncCodeBase($clientSpec, $path, $p4client); + $perforce = new Perforce($ref, $package->getSourceUrl(), $path); + $perforce->writeP4ClientSpec(); + $perforce->syncCodeBase(); } /** @@ -47,26 +40,6 @@ class PerforceDownloader extends VcsDownloader public function doUpdate(PackageInterface $initial, PackageInterface $target, $path) { print("PerforceDownloader:doUpdate\n"); - -// $this->cleanEnv(); -// $path = $this->normalizePath($path); -// -// $ref = $target->getSourceReference(); -// $this->io->write(" Checking out ".$ref); -// $command = 'git remote set-url composer %s && git fetch composer && git fetch --tags composer'; -// -// // capture username/password from URL if there is one -// $this->process->execute('git remote -v', $output, $path); -// if (preg_match('{^(?:composer|origin)\s+https?://(.+):(.+)@([^/]+)}im', $output, $match)) { -// $this->io->setAuthentication($match[3], urldecode($match[1]), urldecode($match[2])); -// } -// -// $commandCallable = function($url) use ($command) { -// return sprintf($command, escapeshellarg($url)); -// }; -// -// $this->runCommand($commandCallable, $target->getSourceUrl(), $path); -// $this->updateToCommit($path, $ref, $target->getPrettyVersion(), $target->getReleaseDate()); } /** @@ -75,18 +48,6 @@ class PerforceDownloader extends VcsDownloader public function getLocalChanges($path) { print("PerforceDownloader:getLocalChanges\n"); -// $this->cleanEnv(); -// $path = $this->normalizePath($path); -// if (!is_dir($path.'/.git')) { -// return; -// } -// -// $command = 'git status --porcelain --untracked-files=no'; -// if (0 !== $this->process->execute($command, $output, $path)) { -// throw new \RuntimeException('Failed to execute ' . $command . "\n\n" . $this->process->getErrorOutput()); -// } -// -// return trim($output) ?: null; } @@ -96,14 +57,6 @@ class PerforceDownloader extends VcsDownloader protected function getCommitLogs($fromReference, $toReference, $path) { print("PerforceDownloader:getCommitLogs\n"); -// $path = $this->normalizePath($path); -// $command = sprintf('git log %s..%s --pretty=format:"%%h - %%an: %%s"', $fromReference, $toReference); -// -// if (0 !== $this->process->execute($command, $output, $path)) { -// throw new \RuntimeException('Failed to execute ' . $command . "\n\n" . $this->process->getErrorOutput()); -// } -// -// return $output; } } diff --git a/src/Composer/Repository/Vcs/PerforceDriver.php b/src/Composer/Repository/Vcs/PerforceDriver.php index 6ecb47050..7d110c888 100644 --- a/src/Composer/Repository/Vcs/PerforceDriver.php +++ b/src/Composer/Repository/Vcs/PerforceDriver.php @@ -15,62 +15,39 @@ namespace Composer\Repository\Vcs; -#use Composer\Downloader\TransportException; -#use Composer\Json\JsonFile; -#use Composer\Cache; use Composer\IO\IOInterface; use Composer\Util\Filesystem; use Composer\Util\Perforce; -#use Composer\Util\RemoteFilesystem; -#use Composer\Util\GitHub; /** * @author matt-whittom <> */ class PerforceDriver extends VcsDriver { -// protected $cache; -// protected $owner; -// protected $repository; -// protected $tags; -// protected $branches; - protected $rootIdentifier = 'mainline'; - protected $repoDir; -// protected $hasIssues; -// protected $infoCache = array(); -// protected $isPrivate = false; + protected $rootIdentifier; protected $depot; - protected $p4client; + protected $perforce; /** * {@inheritDoc} */ public function initialize() { - print ("PerforceDriver:initialize\n"); + print ("\nPerforceDriver:initialize\n"); + $this->rootIdentifier = "mainline"; $this->depot = $this->repoConfig['depot']; - $this->p4client = "composer_perforce_$this->depot"; - $this->repoDir = $this->config->get('cache-vcs-dir') . "/$this->depot"; - $clientSpec = $this->config->get('cache-dir') . "/perforce/$this->p4client.p4.spec"; - - $this->p4Login(); - - $fs = new Filesystem(); - $fs->ensureDirectoryExists($this->repoDir); $stream = "//$this->depot/$this->rootIdentifier"; - $perforce = new Perforce(); - $perforce->writeP4ClientSpec($clientSpec, $this->repoDir, $this->p4client, $stream); - $perforce->syncCodeBase($clientSpec, $this->repoDir, $this->p4client); + $repoDir = $this->config->get('cache-vcs-dir') . "/$this->depot"; + $this->perforce = new Perforce($stream, $this->getUrl(), $repoDir); + + $this->perforce->p4Login($this->io); + $this->perforce->writeP4ClientSpec(); + $this->perforce->syncCodeBase(); return true; } - protected function p4Login(){ - $password = trim(shell_exec('echo $P4PASSWD')); - $command = "echo $password | p4 login -a "; - shell_exec($command); - } @@ -79,31 +56,9 @@ class PerforceDriver extends VcsDriver */ public function getComposerInformation($identifier) { - print ("PerforceDriver:getComposerInformation: $identifier\n"); - $command = "p4 print $identifier/composer.json"; - $result = shell_exec($command); - $index = strpos($result, "{"); - if ($index === false){ - return; - } - if ($index >=0){ - $rawData = substr($result, $index); - $composer_info = json_decode($rawData, true); - print ("ComposerInfo is:".var_export($composer_info, true) . "\n"); - return $composer_info; - } - - -// Basically, read the composer.json file from the project. -// -// Git stuff: -// ..getComposerInfo is: array ( -// 'support' => -// array ( -// 'source' => 'http://github.com/composer/packagist', -// ), -// 'time' => '2012-09-10', -// ) + print ("PerforceDriver:getComposerInformation - identifier: $identifier\n"); + $composer_info =$this->perforce->getComposerInformation($identifier); + return $composer_info; } /** @@ -120,9 +75,6 @@ class PerforceDriver extends VcsDriver */ public function getBranches() { - //return $branch->$identifier - //getComposer($identifier) - //validate($branch) print ("PerforceDriver:getBranches\n"); $command = "p4 streams //$this->depot/..."; $result = shell_exec($command); @@ -137,7 +89,6 @@ class PerforceDriver extends VcsDriver } } $branches['master'] = $branches['mainline']; - print ("PerforceDriver:getBranches - returning branches:".var_export($branches, true)."\n"); return $branches; } @@ -155,7 +106,7 @@ class PerforceDriver extends VcsDriver */ public function getDist($identifier) { - print ("PerforceDriver:getDist: $identifier\n"); + print("\nPerforceDriver:getDist: identifier: $identifier\n"); return null; } @@ -164,7 +115,7 @@ class PerforceDriver extends VcsDriver */ public function getSource($identifier) { - print ("PerforceDriver:getSource: $identifier\n"); + print ("\nPerforceDriver:getSource - identifier: $identifier\n"); $source = array ( 'type' => 'perforce', @@ -180,7 +131,7 @@ class PerforceDriver extends VcsDriver public function getUrl() { print ("PerforceDriver:getUrl\n"); - + return $this->url; } /** @@ -188,10 +139,10 @@ class PerforceDriver extends VcsDriver */ public function hasComposerFile($identifier) { - print ("PerforceDriver:hasComposerFile: $identifier\n"); - - //Does the project have a composer file? - return true; + print ("\nPerforceDriver:hasComposerFile - identifier: $identifier\n"); + $composerFile = $this->perforce->getComposerFilePath($identifier); + print ("returning: " . var_export(file_exists($composerFile),true) . "\n"); + return file_exists($composerFile); } /** @@ -199,7 +150,8 @@ class PerforceDriver extends VcsDriver */ public function getContents($url) { - print("PerforceDriver:getContents - url: $url"); + print ("\nPerforceDriver:getContents - url: $url\n"); + return false; } /** @@ -207,12 +159,7 @@ class PerforceDriver extends VcsDriver */ public static function supports(IOInterface $io, $url, $deep = false) { - print ("PerforceDriver:supports\n"); - - print ("\nChecking url for support: $url\n\n"); - if (preg_match('#(^perforce)#', $url)) { - return true; - } - return false; + print ("PerforceDriver:supports - url: $url\n"); + return Perforce::checkServerExists($url); } } diff --git a/src/Composer/Util/Perforce.php b/src/Composer/Util/Perforce.php index 15f4b6ac5..dfd87b724 100644 --- a/src/Composer/Util/Perforce.php +++ b/src/Composer/Util/Perforce.php @@ -9,56 +9,93 @@ namespace Composer\Util; +use Composer\IO\IOInterface; + class Perforce { - public function syncCodeBase($clientSpec, $targetDir, $p4client){ - $p4CreateClientCommand = "p4 client -i < $clientSpec"; - print ("\nPerforceDriver create client: $p4CreateClientCommand\n"); + + protected $path; + protected $p4client; + protected $p4user; + protected $p4port; + protected $p4stream; + protected $p4clientSpec; + + final public function __construct($stream, $port, $path){ + $this->p4stream = $stream; + $this->p4port = $port; + $this->path = $path; + $fs = new Filesystem(); + $fs->ensureDirectoryExists($path); + } + + protected function getClient() + { + if (!isset($this->p4client)){ + $random_value = mt_rand(1000,9999); + $this->p4client = "composer_perforce_" . $random_value . "_".str_replace("/", "_", str_replace("//", "", $this->p4stream)); + } + return $this->p4client; + } + + protected function getUser() + { + if (!isset($this->p4user)){ + $this->p4user = trim(shell_exec('echo $P4USER')); + } + return $this->p4user; + } + + protected function getPath() + { + return $this->path; + } + protected function getPort() + { + return $this->p4port; + } + + protected function getStream() + { + return $this->p4stream; + } + protected function getP4ClientSpec() + { + $p4clientSpec = $this->path . "/" . $this->getClient() . ".p4.spec"; + return $p4clientSpec; + } + + public function syncCodeBase(){ + $p4CreateClientCommand = $this->generateP4Command( "client -i < " . $this->getP4ClientSpec()); $result = shell_exec($p4CreateClientCommand); - print ("result: $result\n\n"); $prevDir = getcwd(); - chdir($targetDir); + chdir($this->path); - //write p4 config file - $p4ConfigFileSpec = "$targetDir/p4config.config"; - $p4ConfigFile = fopen($p4ConfigFileSpec, 'w'); - fwrite($p4ConfigFile, "P4CLIENT=$p4client"); - fclose($p4ConfigFile); + $result = shell_exec("pwd"); - $testCommand = "pwd"; - print ("PerforceDriver test dir command: $testCommand\n"); - $result = shell_exec($testCommand); - print ("result: $result\n\n"); - - $p4SyncCommand = "p4 sync -f //$p4client/..."; - print ("PerforceDriver sync client: $p4SyncCommand\n"); + $p4SyncCommand = $this->generateP4Command( "sync -f //".$this->getClient()."/..."); $result = shell_exec($p4SyncCommand); - print ("result: $result\n\n"); chdir($prevDir); } - public function writeP4ClientSpec($clientSpec, $targetDir, $p4client, $stream){ - $fs = new Filesystem(); - $fs->ensureDirectoryExists(dirname($clientSpec)); + public function writeP4ClientSpec(){ - $p4user = trim(shell_exec('echo $P4USER')); - print ("PerforceDriver: writing to client spec: $clientSpec\n\n"); - $spec = fopen($clientSpec, 'w'); + $spec = fopen($this->getP4ClientSpec(), 'w'); try { - fwrite($spec, "Client: $p4client\n\n"); + fwrite($spec, "Client: " . $this->getClient() . "\n\n"); fwrite($spec, "Update: " . date("Y/m/d H:i:s") . "\n\n"); fwrite($spec, "Access: " . date("Y/m/d H:i:s") . "\n" ); - fwrite($spec, "Owner: $p4user\n\n" ); + fwrite($spec, "Owner: " . $this->getUser() . "\n\n" ); fwrite($spec, "Description:\n" ); - fwrite($spec, " Created by $p4user from composer.\n\n" ); - fwrite($spec, "Root: $targetDir\n\n" ); + fwrite($spec, " Created by " . $this->getUser() . " from composer.\n\n" ); + fwrite($spec, "Root: " .$this->getPath(). "\n\n" ); fwrite($spec, "Options: noallwrite noclobber nocompress unlocked modtime rmdir\n\n" ); fwrite($spec, "SubmitOptions: revertunchanged\n\n" ); fwrite($spec, "LineEnd: local\n\n" ); fwrite($spec, "Stream:\n" ); - fwrite($spec, " $stream\n" ); + fwrite($spec, " " . $this->getStream()."\n" ); } catch(Exception $e){ fclose($spec); throw $e; @@ -66,5 +103,50 @@ class Perforce { fclose($spec); } + public function getComposerFilePath($identifier) + { + $composerFilePath = $this->path . "/composer.json" ; + print ("\nPerforceUtility - getComposerPath: $composerFilePath\n\n"); + return $composerFilePath; + } + protected function generateP4Command($command) { + $p4Command = "p4 "; + $p4Command = $p4Command . "-u " . $this->getUser() . " "; + $p4Command = $p4Command . "-c " . $this->getClient() . " "; + $p4Command = $p4Command . "-p " . $this->getPort() . " "; + $p4Command = $p4Command . $command; + return $p4Command; + } + public function p4Login(IOInterface $io){ + $user = $this->getUser(); + $result = trim(shell_exec("p4 login -s")); + $index = strpos($result, $user); + if ($index === false){ + $password = trim(shell_exec('echo $P4PASSWD')); + if (strlen($password) <= 0){ + $password = $io->ask("Enter password for Perforce user " . $this->getUser() . ": " ); + } + $command = "echo $password | p4 login -a "; + shell_exec($command); + } + } + + public static function checkServerExists($url) + { + $result = shell_exec("p4 -p $url info -s"); + $index = strpos($result, "error"); + if ($index === false){ + return true; + } + return false; + } + + public function getComposerInformation($identifier) + { + $composerFilePath =$this->getComposerFilePath($identifier); + $contents = file_get_contents($composerFilePath); + $composer_info = json_decode($contents, true); + return $composer_info; + } } \ No newline at end of file From 64bda65e116d19cd97e8ddf2449d14b01ed727b8 Mon Sep 17 00:00:00 2001 From: matt-whittom Date: Wed, 31 Jul 2013 11:01:18 -0500 Subject: [PATCH 04/26] Updated Perforce driver to use labels dev checkin, lots of cleanup to do --- .../Downloader/PerforceDownloader.php | 9 +- .../Repository/Vcs/PerforceDriver.php | 40 +-- src/Composer/Util/Perforce.php | 264 ++++++++++++++++-- 3 files changed, 254 insertions(+), 59 deletions(-) diff --git a/src/Composer/Downloader/PerforceDownloader.php b/src/Composer/Downloader/PerforceDownloader.php index c8fcf442f..ccc53b9ef 100644 --- a/src/Composer/Downloader/PerforceDownloader.php +++ b/src/Composer/Downloader/PerforceDownloader.php @@ -20,18 +20,19 @@ use Composer\Util\Perforce; */ class PerforceDownloader extends VcsDownloader { - private $hasStashedChanges = false; - /** * {@inheritDoc} */ public function doDownload(PackageInterface $package, $path) { $ref = $package->getSourceReference(); + $label = $package->getPrettyVersion(); + print ("PerforceDownloader: doDownload: ref:$ref, label:$label\n"); - $perforce = new Perforce($ref, $package->getSourceUrl(), $path); + $perforce = new Perforce("", "", $package->getSourceUrl(), $path); + $perforce->setStream($ref); $perforce->writeP4ClientSpec(); - $perforce->syncCodeBase(); + $perforce->syncCodeBase($label); } /** diff --git a/src/Composer/Repository/Vcs/PerforceDriver.php b/src/Composer/Repository/Vcs/PerforceDriver.php index 7d110c888..3710c55ff 100644 --- a/src/Composer/Repository/Vcs/PerforceDriver.php +++ b/src/Composer/Repository/Vcs/PerforceDriver.php @@ -24,8 +24,8 @@ use Composer\Util\Perforce; */ class PerforceDriver extends VcsDriver { - protected $rootIdentifier; protected $depot; + protected $branch; protected $perforce; /** @@ -34,16 +34,20 @@ class PerforceDriver extends VcsDriver public function initialize() { print ("\nPerforceDriver:initialize\n"); - $this->rootIdentifier = "mainline"; $this->depot = $this->repoConfig['depot']; + $this->branch = ""; + if (isset($this->repoConfig['branch'])){ + $this->branch = $this->repoConfig['branch']; + } - $stream = "//$this->depot/$this->rootIdentifier"; $repoDir = $this->config->get('cache-vcs-dir') . "/$this->depot"; - $this->perforce = new Perforce($stream, $this->getUrl(), $repoDir); + $this->perforce = new Perforce($this->depot, $this->branch, $this->getUrl(), $repoDir); $this->perforce->p4Login($this->io); - $this->perforce->writeP4ClientSpec(); - $this->perforce->syncCodeBase(); + $this->perforce->checkStream($this->depot); + +// $this->perforce->writeP4ClientSpec(); +// $this->perforce->syncCodeBase(); return true; } @@ -56,7 +60,7 @@ class PerforceDriver extends VcsDriver */ public function getComposerInformation($identifier) { - print ("PerforceDriver:getComposerInformation - identifier: $identifier\n"); + print("PerforceDriver:getComposerInformation - identifier: $identifier\n"); $composer_info =$this->perforce->getComposerInformation($identifier); return $composer_info; } @@ -67,7 +71,7 @@ class PerforceDriver extends VcsDriver public function getRootIdentifier() { print ("PerforceDriver:getRootIdentifier\n"); - return $this->rootIdentifier; + return $this->branch; } /** @@ -76,19 +80,7 @@ class PerforceDriver extends VcsDriver public function getBranches() { print ("PerforceDriver:getBranches\n"); - $command = "p4 streams //$this->depot/..."; - $result = shell_exec($command); - - $resArray = explode("\n", $result); - $branches = array(); - foreach ($resArray as $line){ - $resBits = explode(" ", $line); - if (count($resBits) > 4){ - $branch = substr($resBits[4], 1, strlen($resBits[4])-2); - $branches[$branch] = $resBits[1]; - } - } - $branches['master'] = $branches['mainline']; + $branches = $this->perforce->getBranches(); return $branches; } @@ -98,7 +90,8 @@ class PerforceDriver extends VcsDriver public function getTags() { print ("PerforceDriver:getTags\n"); - return array(); + $tags = $this->perforce->getTags(); + return $tags; } /** @@ -106,7 +99,6 @@ class PerforceDriver extends VcsDriver */ public function getDist($identifier) { - print("\nPerforceDriver:getDist: identifier: $identifier\n"); return null; } @@ -115,8 +107,6 @@ class PerforceDriver extends VcsDriver */ public function getSource($identifier) { - print ("\nPerforceDriver:getSource - identifier: $identifier\n"); - $source = array ( 'type' => 'perforce', 'url' => $this->repoConfig['url'], diff --git a/src/Composer/Util/Perforce.php b/src/Composer/Util/Perforce.php index dfd87b724..7b55cf0d5 100644 --- a/src/Composer/Util/Perforce.php +++ b/src/Composer/Util/Perforce.php @@ -20,9 +20,12 @@ class Perforce { protected $p4port; protected $p4stream; protected $p4clientSpec; + protected $p4depotType; + protected $p4branch; - final public function __construct($stream, $port, $path){ - $this->p4stream = $stream; + final public function __construct($depot, $branch, $port, $path){ + $this->p4depot = $depot; + $this->p4branch = $branch; $this->p4port = $port; $this->path = $path; $fs = new Filesystem(); @@ -33,7 +36,8 @@ class Perforce { { if (!isset($this->p4client)){ $random_value = mt_rand(1000,9999); - $this->p4client = "composer_perforce_" . $random_value . "_".str_replace("/", "_", str_replace("//", "", $this->p4stream)); + $clean_stream_name = str_replace("@", "", str_replace("/", "_", str_replace("//", "", $this->p4stream))); + $this->p4client = "composer_perforce_" . $random_value . "_".$clean_stream_name; } return $this->p4client; } @@ -50,6 +54,7 @@ class Perforce { { return $this->path; } + protected function getPort() { return $this->p4port; @@ -57,16 +62,80 @@ class Perforce { protected function getStream() { + if (!isset($this->p4stream)){ + if ($this->isStream()){ + $this->p4stream = "//$this->p4depot/$this->p4branch"; + } else { + $this->p4stream = "//$this->p4depot"; + } + } return $this->p4stream; } + + protected function getStreamWithoutLabel() + { + $stream = $this->getStream(); + $index = strpos($stream, "@"); + if ($index === false){ + return $stream; + } + return substr($stream, 0, $index); + } + protected function getP4ClientSpec() { $p4clientSpec = $this->path . "/" . $this->getClient() . ".p4.spec"; return $p4clientSpec; } - public function syncCodeBase(){ + protected function queryP4User(IOInterface $io){ + $this->getUser(); + if (strlen($this->p4user) <= 0){ + $this->p4user = $io->ask("Enter P4 User:"); + } + } + + protected function queryP4Password(IOInterface $io){ + $password = trim(shell_exec('echo $P4PASSWD')); + if (strlen($password) <= 0){ + $password = $io->ask("Enter password for Perforce user " . $this->getUser() . ": " ); + } + return $password; + } + + protected function isStream(){ + return (strcmp($this->p4depotType, "stream") === 0); + } + + protected function generateP4Command($command, $useClient = true) { + $p4Command = "p4 "; + $p4Command = $p4Command . "-u " . $this->getUser() . " "; + if ($useClient){ + $p4Command = $p4Command . "-c " . $this->getClient() . " "; + } + $p4Command = $p4Command . "-p " . $this->getPort() . " "; + $p4Command = $p4Command . $command; + return $p4Command; + } + + protected function isLoggedIn(){ + $command = $this->generateP4Command("login -s "); + $result = trim(shell_exec($command)); + $index = strpos($result, $this->getUser()); + if ($index === false){ + return false; + } + return true; + } + + public function setStream($stream){ + $this->p4stream = $stream; + $this->p4depotType = "stream"; + } + + public function syncCodeBase($label){ $p4CreateClientCommand = $this->generateP4Command( "client -i < " . $this->getP4ClientSpec()); + print ("Perforce: syncCodeBase - client command:$p4CreateClientCommand \n"); $result = shell_exec($p4CreateClientCommand); $prevDir = getcwd(); @@ -75,13 +144,19 @@ class Perforce { $result = shell_exec("pwd"); $p4SyncCommand = $this->generateP4Command( "sync -f //".$this->getClient()."/..."); + if (isset($label)){ + if (strcmp($label, "dev-master") != 0){ + $p4SyncCommand = $p4SyncCommand . "@" . $label; + } + } + print ("Perforce: syncCodeBase - sync command:$p4SyncCommand \n"); $result = shell_exec($p4SyncCommand); chdir($prevDir); } public function writeP4ClientSpec(){ - + print ("Perforce: writeP4ClientSpec\n"); $spec = fopen($this->getP4ClientSpec(), 'w'); try { fwrite($spec, "Client: " . $this->getClient() . "\n\n"); @@ -94,8 +169,12 @@ class Perforce { fwrite($spec, "Options: noallwrite noclobber nocompress unlocked modtime rmdir\n\n" ); fwrite($spec, "SubmitOptions: revertunchanged\n\n" ); fwrite($spec, "LineEnd: local\n\n" ); - fwrite($spec, "Stream:\n" ); - fwrite($spec, " " . $this->getStream()."\n" ); + if ($this->isStream()){ + fwrite($spec, "Stream:\n" ); + fwrite($spec, " " . $this->getStreamWithoutLabel()."\n" ); + } else { + fwrite($spec, "View: " . $this->getStream() . "/... //" . $this->getClient() . "/" . str_replace("//", "", $this->getStream()) . "/... \n"); + } } catch(Exception $e){ fclose($spec); throw $e; @@ -105,35 +184,27 @@ class Perforce { public function getComposerFilePath($identifier) { - $composerFilePath = $this->path . "/composer.json" ; - print ("\nPerforceUtility - getComposerPath: $composerFilePath\n\n"); + if ($this->isStream()){ + $composerFilePath = $this->path . "/composer.json" ; + } else { + $composerFilePath = $this->path . "/" . $this->p4depot . "/composer.json" ; + } return $composerFilePath; } - protected function generateP4Command($command) { - $p4Command = "p4 "; - $p4Command = $p4Command . "-u " . $this->getUser() . " "; - $p4Command = $p4Command . "-c " . $this->getClient() . " "; - $p4Command = $p4Command . "-p " . $this->getPort() . " "; - $p4Command = $p4Command . $command; - return $p4Command; - } public function p4Login(IOInterface $io){ - $user = $this->getUser(); - $result = trim(shell_exec("p4 login -s")); - $index = strpos($result, $user); - if ($index === false){ - $password = trim(shell_exec('echo $P4PASSWD')); - if (strlen($password) <= 0){ - $password = $io->ask("Enter password for Perforce user " . $this->getUser() . ": " ); - } - $command = "echo $password | p4 login -a "; + print ("Perforce: P4Login\n"); + $this->queryP4User($io); + if (!$this->isLoggedIn()){ + $password = $this->queryP4Password($io); + $command = "echo $password | " . $this->generateP4Command("login -a "); shell_exec($command); } } public static function checkServerExists($url) { + print ("Perforce: checkServerExists\n"); $result = shell_exec("p4 -p $url info -s"); $index = strpos($result, "error"); if ($index === false){ @@ -144,9 +215,142 @@ class Perforce { public function getComposerInformation($identifier) { - $composerFilePath =$this->getComposerFilePath($identifier); - $contents = file_get_contents($composerFilePath); - $composer_info = json_decode($contents, true); - return $composer_info; + $index = strpos($identifier, "@"); + if ($index === false){ + return $this->getComposerInformationFromId($identifier); + } else { + return $this->getComposerInformationFromTag($identifier, $index); + } + } + public function getComposerInformationFromId($identifier) + { + $composer_json = "$identifier/composer.json"; + $command = $this->generateP4Command(" print $composer_json", false); + print ("Perforce: getComposerInformation: command: $command\n\n"); + $result = shell_exec($command); + $index = strpos($result, "{"); + if ($index === false){ + return ""; + } + if ($index >=0){ + $rawData = substr($result, $index); + $composer_info = json_decode($rawData, true); + print ("ComposerInfo is:".var_export($composer_info, true) . "\n"); + return $composer_info; + } + return ""; + } + + public function getComposerInformationFromTag($identifier, $index) + { + $composer_json = substr($identifier, 0, $index) . "/composer.json" . substr($identifier, $index); + $command = $this->generateP4Command(" files $composer_json", false); + print("\n\nPerforce: getComposerInformationFromTag: $identifier, command:\n $command\n\n"); + $result = shell_exec($command); + print("\n\nPerforce: getComposerInformationFromTag: result: \n $result\n\n"); + $index2 = strpos($result, "no such file(s)."); + if ($index2 === false){ + $index3 = strpos($result, "change"); + if (!($index3 ===false )){ + $phrase = trim(substr($result, $index3)); + $fields = explode(" ", $phrase); + $id = $fields[1]; + $composer_json = substr($identifier, 0, $index) . "/composer.json@" . $id; + $command = $this->generateP4Command(" print $composer_json", false); + $result = shell_exec($command); + $index = strpos($result, "{"); + if ($index === false){ + return ""; + } + if ($index >=0){ + $rawData = substr($result, $index); + $composer_info = json_decode($rawData, true); + print ("ComposerInfo is:".var_export($composer_info, true) . "\n"); + return $composer_info; + } + } + } + + return ""; + } + +// public function getComposerInformation($identifier) +// { +// $composerFilePath =$this->getComposerFilePath($identifier); +// $contents = file_get_contents($composerFilePath); +// $composer_info = json_decode($contents, true); +// return $composer_info; +// } + + public function getBranches() + { + $branches = array(); + if (!$this->isStream()){ + $branches[$this->p4branch] = $this->p4stream; + } else { + $command = $this->generateP4Command("streams //$this->p4depot/..."); + $result = shell_exec($command); + print ("Perforce: getBranches: result: $result\n"); + $resArray = explode("\n", $result); + foreach ($resArray as $line){ + $resBits = explode(" ", $line); + if (count($resBits) > 4){ + $branch = substr($resBits[4], 1, strlen($resBits[4])-2); + $branches[$branch] = $resBits[1]; + } + } + } + $branches['master'] = $branches[$this->p4branch]; + return $branches; + } + + public function getTags() + { + + $command = $this->generateP4Command("changes " . $this->getStream() . "/..."); + print("\nPerforce:getTags - command:($command)\n"); + $result = shell_exec($command); + $resArray = explode("\n", $result); + $tags = array(); + foreach ($resArray as $line){ + $index = strpos($line, "Change"); + if (!($index===false)){ +// $fields = explode(" ", $line); +// $tags["0.0.".$fields[1]] = $this->getStream() . "@" . $fields[1]; + } + } + + $command = $this->generateP4Command("labels"); + print("\nPerforce:getTags - command:($command)\n"); + $result = shell_exec($command); + $resArray = explode("\n", $result); + print("\nPerforce:getTags - result:$result\n"); + foreach ($resArray as $line){ + $index = strpos($line, "Label"); + if (!($index===false)){ + $fields = explode(" ", $line); + $tags[$fields[1]] = $this->getStream()."@" . $fields[1]; + } + } + print ("Perforce:getTags - tags:" . var_export($tags, true)."\n"); + return $tags; + } + + public function checkStream () + { + $command = $this->generateP4Command("depots"); + $result = shell_exec($command); + $resArray = explode("\n", $result); + foreach ($resArray as $line){ + $index = strpos($line, "Depot"); + if (!($index===false)){ + $fields = explode(" ", $line); + if (strcmp($this->p4depot, $fields[1]) === 0){ + $this->p4depotType = $fields[3]; + return $this->isStream(); + } + } + } + return false; } } \ No newline at end of file From 43108b4692c7eb1e4e6892fce0ea8ccca6d30977 Mon Sep 17 00:00:00 2001 From: matt-whittom Date: Wed, 31 Jul 2013 13:26:44 -0500 Subject: [PATCH 05/26] Cleanup phase 1 Removed branches - only returning the "master" --- .../Repository/Vcs/PerforceDriver.php | 17 +++-- src/Composer/Util/Perforce.php | 71 +++++-------------- 2 files changed, 29 insertions(+), 59 deletions(-) diff --git a/src/Composer/Repository/Vcs/PerforceDriver.php b/src/Composer/Repository/Vcs/PerforceDriver.php index 3710c55ff..4fdaa38f2 100644 --- a/src/Composer/Repository/Vcs/PerforceDriver.php +++ b/src/Composer/Repository/Vcs/PerforceDriver.php @@ -33,7 +33,7 @@ class PerforceDriver extends VcsDriver */ public function initialize() { - print ("\nPerforceDriver:initialize\n"); + print ("PerforceDriver:initialize\n"); $this->depot = $this->repoConfig['depot']; $this->branch = ""; if (isset($this->repoConfig['branch'])){ @@ -46,7 +46,7 @@ class PerforceDriver extends VcsDriver $this->perforce->p4Login($this->io); $this->perforce->checkStream($this->depot); -// $this->perforce->writeP4ClientSpec(); + $this->perforce->writeP4ClientSpec(); // $this->perforce->syncCodeBase(); return true; @@ -60,7 +60,7 @@ class PerforceDriver extends VcsDriver */ public function getComposerInformation($identifier) { - print("PerforceDriver:getComposerInformation - identifier: $identifier\n"); + print("\nPerforceDriver:getComposerInformation - identifier: $identifier\n"); $composer_info =$this->perforce->getComposerInformation($identifier); return $composer_info; } @@ -129,10 +129,15 @@ class PerforceDriver extends VcsDriver */ public function hasComposerFile($identifier) { - print ("\nPerforceDriver:hasComposerFile - identifier: $identifier\n"); + print ("PerforceDriver:hasComposerFile - identifier: $identifier\n"); $composerFile = $this->perforce->getComposerFilePath($identifier); - print ("returning: " . var_export(file_exists($composerFile),true) . "\n"); - return file_exists($composerFile); + print ("composerFile: $composerFile\n"); + if (!file_exists(filename)){ + $composer_info = $this->perforce->getComposerInformation(); + $result = strlen(trim($composer_info))>0; + return $result; + } + return true; } /** diff --git a/src/Composer/Util/Perforce.php b/src/Composer/Util/Perforce.php index 7b55cf0d5..f80a4bb72 100644 --- a/src/Composer/Util/Perforce.php +++ b/src/Composer/Util/Perforce.php @@ -217,16 +217,16 @@ class Perforce { { $index = strpos($identifier, "@"); if ($index === false){ - return $this->getComposerInformationFromId($identifier); + $composer_json = "$identifier/composer.json"; + return $this->getComposerInformationFromPath($composer_json); } else { - return $this->getComposerInformationFromTag($identifier, $index); + return $this->getComposerInformationFromLabel($identifier, $index); } } - public function getComposerInformationFromId($identifier) + public function getComposerInformationFromPath($composer_json) { - $composer_json = "$identifier/composer.json"; $command = $this->generateP4Command(" print $composer_json", false); - print ("Perforce: getComposerInformation: command: $command\n\n"); + print ("Perforce: getComposerInformation: command: $command\n"); $result = shell_exec($command); $index = strpos($result, "{"); if ($index === false){ @@ -235,19 +235,18 @@ class Perforce { if ($index >=0){ $rawData = substr($result, $index); $composer_info = json_decode($rawData, true); - print ("ComposerInfo is:".var_export($composer_info, true) . "\n"); return $composer_info; } return ""; } - public function getComposerInformationFromTag($identifier, $index) + public function getComposerInformationFromLabel($identifier, $index) { - $composer_json = substr($identifier, 0, $index) . "/composer.json" . substr($identifier, $index); - $command = $this->generateP4Command(" files $composer_json", false); - print("\n\nPerforce: getComposerInformationFromTag: $identifier, command:\n $command\n\n"); + $composer_json_path = substr($identifier, 0, $index) . "/composer.json" . substr($identifier, $index); + $command = $this->generateP4Command(" files $composer_json_path", false); + print("Perforce: getComposerInformationFromTag: $identifier, command:\n $command\n"); $result = shell_exec($command); - print("\n\nPerforce: getComposerInformationFromTag: result: \n $result\n\n"); + print("Perforce: getComposerInformationFromTag: result: \n $result\n"); $index2 = strpos($result, "no such file(s)."); if ($index2 === false){ $index3 = strpos($result, "change"); @@ -256,75 +255,42 @@ class Perforce { $fields = explode(" ", $phrase); $id = $fields[1]; $composer_json = substr($identifier, 0, $index) . "/composer.json@" . $id; - $command = $this->generateP4Command(" print $composer_json", false); - $result = shell_exec($command); - $index = strpos($result, "{"); - if ($index === false){ - return ""; - } - if ($index >=0){ - $rawData = substr($result, $index); - $composer_info = json_decode($rawData, true); - print ("ComposerInfo is:".var_export($composer_info, true) . "\n"); - return $composer_info; - } + return $this->getComposerInformationFromPath($composer_json); } } - return ""; } -// public function getComposerInformation($identifier) -// { -// $composerFilePath =$this->getComposerFilePath($identifier); -// $contents = file_get_contents($composerFilePath); -// $composer_info = json_decode($contents, true); -// return $composer_info; -// } - public function getBranches() { - $branches = array(); + $possible_branches = array(); if (!$this->isStream()){ $branches[$this->p4branch] = $this->p4stream; } else { $command = $this->generateP4Command("streams //$this->p4depot/..."); $result = shell_exec($command); - print ("Perforce: getBranches: result: $result\n"); $resArray = explode("\n", $result); foreach ($resArray as $line){ $resBits = explode(" ", $line); if (count($resBits) > 4){ $branch = substr($resBits[4], 1, strlen($resBits[4])-2); - $branches[$branch] = $resBits[1]; + $possible_branches[$branch] = $resBits[1]; } } } - $branches['master'] = $branches[$this->p4branch]; + $branches = array(); + $branches['master'] = $possible_branches[$this->p4branch]; + print ("Perforce: getBranches: returning: \n" . var_export($branches, true) . "\n"); return $branches; } public function getTags() { - - $command = $this->generateP4Command("changes " . $this->getStream() . "/..."); - print("\nPerforce:getTags - command:($command)\n"); - $result = shell_exec($command); - $resArray = explode("\n", $result); - $tags = array(); - foreach ($resArray as $line){ - $index = strpos($line, "Change"); - if (!($index===false)){ -// $fields = explode(" ", $line); -// $tags["0.0.".$fields[1]] = $this->getStream() . "@" . $fields[1]; - } - } - $command = $this->generateP4Command("labels"); - print("\nPerforce:getTags - command:($command)\n"); $result = shell_exec($command); $resArray = explode("\n", $result); - print("\nPerforce:getTags - result:$result\n"); + print("Perforce:getTags - result:\n$result\n"); + $tags = array(); foreach ($resArray as $line){ $index = strpos($line, "Label"); if (!($index===false)){ @@ -332,7 +298,6 @@ class Perforce { $tags[$fields[1]] = $this->getStream()."@" . $fields[1]; } } - print ("Perforce:getTags - tags:" . var_export($tags, true)."\n"); return $tags; } From a5df2851efac41cdb8e6776a2201b0d2114e7c32 Mon Sep 17 00:00:00 2001 From: matt-whittom Date: Wed, 31 Jul 2013 13:30:55 -0500 Subject: [PATCH 06/26] Finished cleanup of debug messages. --- src/Composer/Downloader/PerforceDownloader.php | 1 - src/Composer/Repository/Vcs/PerforceDriver.php | 9 --------- src/Composer/Util/Perforce.php | 10 ---------- 3 files changed, 20 deletions(-) diff --git a/src/Composer/Downloader/PerforceDownloader.php b/src/Composer/Downloader/PerforceDownloader.php index ccc53b9ef..59adadfaf 100644 --- a/src/Composer/Downloader/PerforceDownloader.php +++ b/src/Composer/Downloader/PerforceDownloader.php @@ -27,7 +27,6 @@ class PerforceDownloader extends VcsDownloader { $ref = $package->getSourceReference(); $label = $package->getPrettyVersion(); - print ("PerforceDownloader: doDownload: ref:$ref, label:$label\n"); $perforce = new Perforce("", "", $package->getSourceUrl(), $path); $perforce->setStream($ref); diff --git a/src/Composer/Repository/Vcs/PerforceDriver.php b/src/Composer/Repository/Vcs/PerforceDriver.php index 4fdaa38f2..e75e5d181 100644 --- a/src/Composer/Repository/Vcs/PerforceDriver.php +++ b/src/Composer/Repository/Vcs/PerforceDriver.php @@ -33,7 +33,6 @@ class PerforceDriver extends VcsDriver */ public function initialize() { - print ("PerforceDriver:initialize\n"); $this->depot = $this->repoConfig['depot']; $this->branch = ""; if (isset($this->repoConfig['branch'])){ @@ -60,7 +59,6 @@ class PerforceDriver extends VcsDriver */ public function getComposerInformation($identifier) { - print("\nPerforceDriver:getComposerInformation - identifier: $identifier\n"); $composer_info =$this->perforce->getComposerInformation($identifier); return $composer_info; } @@ -70,7 +68,6 @@ class PerforceDriver extends VcsDriver */ public function getRootIdentifier() { - print ("PerforceDriver:getRootIdentifier\n"); return $this->branch; } @@ -79,7 +76,6 @@ class PerforceDriver extends VcsDriver */ public function getBranches() { - print ("PerforceDriver:getBranches\n"); $branches = $this->perforce->getBranches(); return $branches; } @@ -89,7 +85,6 @@ class PerforceDriver extends VcsDriver */ public function getTags() { - print ("PerforceDriver:getTags\n"); $tags = $this->perforce->getTags(); return $tags; } @@ -120,7 +115,6 @@ class PerforceDriver extends VcsDriver */ public function getUrl() { - print ("PerforceDriver:getUrl\n"); return $this->url; } @@ -129,9 +123,7 @@ class PerforceDriver extends VcsDriver */ public function hasComposerFile($identifier) { - print ("PerforceDriver:hasComposerFile - identifier: $identifier\n"); $composerFile = $this->perforce->getComposerFilePath($identifier); - print ("composerFile: $composerFile\n"); if (!file_exists(filename)){ $composer_info = $this->perforce->getComposerInformation(); $result = strlen(trim($composer_info))>0; @@ -154,7 +146,6 @@ class PerforceDriver extends VcsDriver */ public static function supports(IOInterface $io, $url, $deep = false) { - print ("PerforceDriver:supports - url: $url\n"); return Perforce::checkServerExists($url); } } diff --git a/src/Composer/Util/Perforce.php b/src/Composer/Util/Perforce.php index f80a4bb72..245dd182e 100644 --- a/src/Composer/Util/Perforce.php +++ b/src/Composer/Util/Perforce.php @@ -135,7 +135,6 @@ class Perforce { public function syncCodeBase($label){ $p4CreateClientCommand = $this->generateP4Command( "client -i < " . $this->getP4ClientSpec()); - print ("Perforce: syncCodeBase - client command:$p4CreateClientCommand \n"); $result = shell_exec($p4CreateClientCommand); $prevDir = getcwd(); @@ -149,14 +148,12 @@ class Perforce { $p4SyncCommand = $p4SyncCommand . "@" . $label; } } - print ("Perforce: syncCodeBase - sync command:$p4SyncCommand \n"); $result = shell_exec($p4SyncCommand); chdir($prevDir); } public function writeP4ClientSpec(){ - print ("Perforce: writeP4ClientSpec\n"); $spec = fopen($this->getP4ClientSpec(), 'w'); try { fwrite($spec, "Client: " . $this->getClient() . "\n\n"); @@ -193,7 +190,6 @@ class Perforce { } public function p4Login(IOInterface $io){ - print ("Perforce: P4Login\n"); $this->queryP4User($io); if (!$this->isLoggedIn()){ $password = $this->queryP4Password($io); @@ -204,7 +200,6 @@ class Perforce { public static function checkServerExists($url) { - print ("Perforce: checkServerExists\n"); $result = shell_exec("p4 -p $url info -s"); $index = strpos($result, "error"); if ($index === false){ @@ -226,7 +221,6 @@ class Perforce { public function getComposerInformationFromPath($composer_json) { $command = $this->generateP4Command(" print $composer_json", false); - print ("Perforce: getComposerInformation: command: $command\n"); $result = shell_exec($command); $index = strpos($result, "{"); if ($index === false){ @@ -244,9 +238,7 @@ class Perforce { { $composer_json_path = substr($identifier, 0, $index) . "/composer.json" . substr($identifier, $index); $command = $this->generateP4Command(" files $composer_json_path", false); - print("Perforce: getComposerInformationFromTag: $identifier, command:\n $command\n"); $result = shell_exec($command); - print("Perforce: getComposerInformationFromTag: result: \n $result\n"); $index2 = strpos($result, "no such file(s)."); if ($index2 === false){ $index3 = strpos($result, "change"); @@ -280,7 +272,6 @@ class Perforce { } $branches = array(); $branches['master'] = $possible_branches[$this->p4branch]; - print ("Perforce: getBranches: returning: \n" . var_export($branches, true) . "\n"); return $branches; } @@ -289,7 +280,6 @@ class Perforce { $command = $this->generateP4Command("labels"); $result = shell_exec($command); $resArray = explode("\n", $result); - print("Perforce:getTags - result:\n$result\n"); $tags = array(); foreach ($resArray as $line){ $index = strpos($line, "Label"); From 53d6fcd6d3e56b7c80b156667d3e4f44f1ec1303 Mon Sep 17 00:00:00 2001 From: matt-whittom Date: Tue, 13 Aug 2013 10:50:47 -0500 Subject: [PATCH 07/26] using theirs --- .../Downloader/PerforceDownloader.php | 5 + .../Repository/Vcs/PerforceDriver.php | 82 ++--- src/Composer/Util/Perforce.php | 345 +++++++++++------- .../Repository/Vcs/PerforceDriverTest.php | 41 --- .../Test/Util/RemoteFilesystemTest.php | 15 +- 5 files changed, 267 insertions(+), 221 deletions(-) diff --git a/src/Composer/Downloader/PerforceDownloader.php b/src/Composer/Downloader/PerforceDownloader.php index 59adadfaf..8e5f3d01e 100644 --- a/src/Composer/Downloader/PerforceDownloader.php +++ b/src/Composer/Downloader/PerforceDownloader.php @@ -30,7 +30,9 @@ class PerforceDownloader extends VcsDownloader $perforce = new Perforce("", "", $package->getSourceUrl(), $path); $perforce->setStream($ref); + $perforce->queryP4User($this->io); $perforce->writeP4ClientSpec(); + $perforce->connectClient(); $perforce->syncCodeBase($label); } @@ -40,6 +42,7 @@ class PerforceDownloader extends VcsDownloader public function doUpdate(PackageInterface $initial, PackageInterface $target, $path) { print("PerforceDownloader:doUpdate\n"); + throw new Exception("Unsupported Operation: PerforceDownloader:doUpdate"); } /** @@ -48,6 +51,7 @@ class PerforceDownloader extends VcsDownloader public function getLocalChanges($path) { print("PerforceDownloader:getLocalChanges\n"); + throw new Exception("Unsupported Operation: PerforceDownloader:getLocalChanges"); } @@ -57,6 +61,7 @@ class PerforceDownloader extends VcsDownloader protected function getCommitLogs($fromReference, $toReference, $path) { print("PerforceDownloader:getCommitLogs\n"); + throw new Exception("Unsupported Operation: PerforceDownloader:getCommitLogs"); } } diff --git a/src/Composer/Repository/Vcs/PerforceDriver.php b/src/Composer/Repository/Vcs/PerforceDriver.php index e75e5d181..a1080e5c4 100644 --- a/src/Composer/Repository/Vcs/PerforceDriver.php +++ b/src/Composer/Repository/Vcs/PerforceDriver.php @@ -16,14 +16,14 @@ namespace Composer\Repository\Vcs; use Composer\IO\IOInterface; +use Composer\Util\ProcessExecutor; use Composer\Util\Filesystem; use Composer\Util\Perforce; /** * @author matt-whittom <> */ -class PerforceDriver extends VcsDriver -{ +class PerforceDriver extends VcsDriver { protected $depot; protected $branch; protected $perforce; @@ -31,60 +31,61 @@ class PerforceDriver extends VcsDriver /** * {@inheritDoc} */ - public function initialize() - { - $this->depot = $this->repoConfig['depot']; + public function initialize() { + $this->depot = $this->repoConfig['depot']; $this->branch = ""; - if (isset($this->repoConfig['branch'])){ + if (isset($this->repoConfig['branch'])) { $this->branch = $this->repoConfig['branch']; } $repoDir = $this->config->get('cache-vcs-dir') . "/$this->depot"; - $this->perforce = new Perforce($this->depot, $this->branch, $this->getUrl(), $repoDir); + if (!isset($this->perforce)) { + $this->perforce = new Perforce($this->depot, $this->branch, $this->getUrl(), $repoDir, $this->process); + } $this->perforce->p4Login($this->io); $this->perforce->checkStream($this->depot); $this->perforce->writeP4ClientSpec(); -// $this->perforce->syncCodeBase(); + $this->perforce->connectClient(); - return true; + return TRUE; } - + public function injectPerforce(Perforce $perforce) { + $this->perforce = $perforce; + } /** * {@inheritDoc} */ - public function getComposerInformation($identifier) - { - $composer_info =$this->perforce->getComposerInformation($identifier); + public function getComposerInformation($identifier) { + $composer_info = $this->perforce->getComposerInformation($identifier); + return $composer_info; } /** * {@inheritDoc} */ - public function getRootIdentifier() - { + public function getRootIdentifier() { return $this->branch; } /** * {@inheritDoc} */ - public function getBranches() - { + public function getBranches() { $branches = $this->perforce->getBranches(); + return $branches; } /** * {@inheritDoc} */ - public function getTags() - { + public function getTags() { $tags = $this->perforce->getTags(); return $tags; } @@ -92,60 +93,51 @@ class PerforceDriver extends VcsDriver /** * {@inheritDoc} */ - public function getDist($identifier) - { - return null; + public function getDist($identifier) { + return NULL; } /** * {@inheritDoc} */ - public function getSource($identifier) - { - $source = array ( - 'type' => 'perforce', - 'url' => $this->repoConfig['url'], + public function getSource($identifier) { + $source = array( + 'type' => 'perforce', + 'url' => $this->repoConfig['url'], 'reference' => $identifier ); + return $source; } /** * {@inheritDoc} */ - public function getUrl() - { + public function getUrl() { return $this->url; } /** * {@inheritDoc} */ - public function hasComposerFile($identifier) - { - $composerFile = $this->perforce->getComposerFilePath($identifier); - if (!file_exists(filename)){ - $composer_info = $this->perforce->getComposerInformation(); - $result = strlen(trim($composer_info))>0; - return $result; - } - return true; + public function hasComposerFile($identifier) { + $composer_info = $this->perforce->getComposerInformation("//$this->depot/$identifier"); + $result = strlen(trim($composer_info)) > 0; + + return $result; } /** * {@inheritDoc} */ - public function getContents($url) - { - print ("\nPerforceDriver:getContents - url: $url\n"); - return false; + public function getContents($url) { + return FALSE; } /** * {@inheritDoc} */ - public static function supports(IOInterface $io, $url, $deep = false) - { - return Perforce::checkServerExists($url); + public static function supports(IOInterface $io, $url, $deep = FALSE) { + return Perforce::checkServerExists($url, new ProcessExecutor); } } diff --git a/src/Composer/Util/Perforce.php b/src/Composer/Util/Perforce.php index 245dd182e..6174c5c49 100644 --- a/src/Composer/Util/Perforce.php +++ b/src/Composer/Util/Perforce.php @@ -22,290 +22,385 @@ class Perforce { protected $p4clientSpec; protected $p4depotType; protected $p4branch; + protected $process; - final public function __construct($depot, $branch, $port, $path){ + public function __construct($depot, $branch, $port, $path, ProcessExecutor $process = null) { $this->p4depot = $depot; $this->p4branch = $branch; $this->p4port = $port; $this->path = $path; + $this->process = $process ? : new ProcessExecutor; $fs = new Filesystem(); $fs->ensureDirectoryExists($path); } - protected function getClient() - { - if (!isset($this->p4client)){ - $random_value = mt_rand(1000,9999); - $clean_stream_name = str_replace("@", "", str_replace("/", "_", str_replace("//", "", $this->p4stream))); - $this->p4client = "composer_perforce_" . $random_value . "_".$clean_stream_name; + protected function getRandomValue() { + return mt_rand(1000, 9999); + } + + protected function executeCommand($command) { + $result = ""; + $this->process->execute($command, $result); + + return $result; + } + + protected function getClient() { + if (!isset($this->p4client)) { + $random_value = $this->getRandomValue(); + $clean_stream_name = str_replace("@", "", str_replace("/", "_", str_replace("//", "", $this->getStream()))); + $this->p4client = "composer_perforce_" . $random_value . "_" . $clean_stream_name; } + return $this->p4client; } - protected function getUser() - { - if (!isset($this->p4user)){ - $this->p4user = trim(shell_exec('echo $P4USER')); + public function getUser() { + if (!isset($this->p4user)) { + $this->p4user = $this->getP4variable("P4USER"); } + return $this->p4user; } - protected function getPath() - { + protected function getPath() { return $this->path; } - protected function getPort() - { + protected function getPort() { return $this->p4port; } - protected function getStream() - { - if (!isset($this->p4stream)){ - if ($this->isStream()){ + protected function getStream() { + if (!isset($this->p4stream)) { + if ($this->isStream()) { $this->p4stream = "//$this->p4depot/$this->p4branch"; - } else { + } + else { $this->p4stream = "//$this->p4depot"; } } + return $this->p4stream; } - protected function getStreamWithoutLabel() - { + protected function getStreamWithoutLabel() { $stream = $this->getStream(); $index = strpos($stream, "@"); - if ($index === false){ + if ($index === FALSE) { return $stream; } + return substr($stream, 0, $index); } - protected function getP4ClientSpec() - { + protected function getP4ClientSpec() { $p4clientSpec = $this->path . "/" . $this->getClient() . ".p4.spec"; + return $p4clientSpec; } - protected function queryP4User(IOInterface $io){ + public function queryP4User(IOInterface $io) { $this->getUser(); - if (strlen($this->p4user) <= 0){ + if (strlen($this->p4user) <= 0) { $this->p4user = $io->ask("Enter P4 User:"); + if (defined('PHP_WINDOWS_VERSION_BUILD')) { + $command = "p4 set P4USER=$this->p4user"; + } else { + $command = "export P4USER=$this->p4user"; + } + $result = $this->executeCommand($command); } } - protected function queryP4Password(IOInterface $io){ - $password = trim(shell_exec('echo $P4PASSWD')); - if (strlen($password) <= 0){ - $password = $io->ask("Enter password for Perforce user " . $this->getUser() . ": " ); + protected function queryP4Password(IOInterface $io) { + $password = $this->getP4variable("P4PASSWD"); + if (strlen($password) <= 0) { + $password = $io->askAndHideAnswer("Enter password for Perforce user " . $this->getUser() . ": "); } return $password; } - protected function isStream(){ + protected function isStream() { return (strcmp($this->p4depotType, "stream") === 0); } - protected function generateP4Command($command, $useClient = true) { + protected function generateP4Command($command, $useClient = TRUE) { $p4Command = "p4 "; $p4Command = $p4Command . "-u " . $this->getUser() . " "; - if ($useClient){ - $p4Command = $p4Command . "-c " . $this->getClient() . " "; + if ($useClient) { + $p4Command = $p4Command . "-c " . $this->getClient() . " "; } $p4Command = $p4Command . "-p " . $this->getPort() . " "; $p4Command = $p4Command . $command; + return $p4Command; } - protected function isLoggedIn(){ - $command = $this->generateP4Command("login -s "); - $result = trim(shell_exec($command)); + protected function isLoggedIn() { + $command = $this->generateP4Command("login -s", FALSE); + $result = trim($this->executeCommand($command)); $index = strpos($result, $this->getUser()); - if ($index === false){ - return false; + if ($index === FALSE) { + return FALSE; } - return true; + return TRUE; } - public function setStream($stream){ + public function setStream($stream) { $this->p4stream = $stream; $this->p4depotType = "stream"; } - public function syncCodeBase($label){ - $p4CreateClientCommand = $this->generateP4Command( "client -i < " . $this->getP4ClientSpec()); - $result = shell_exec($p4CreateClientCommand); + public function connectClient() { + $p4CreateClientCommand = $this->generateP4Command("client -i < " . $this->getP4ClientSpec()); + $this->executeCommand($p4CreateClientCommand); + } + public function syncCodeBase($label) { $prevDir = getcwd(); chdir($this->path); - $result = shell_exec("pwd"); + $this->executeCommand("pwd"); - $p4SyncCommand = $this->generateP4Command( "sync -f //".$this->getClient()."/..."); - if (isset($label)){ - if (strcmp($label, "dev-master") != 0){ + $p4SyncCommand = $this->generateP4Command("sync -f "); + if (isset($label)) { + if (strcmp($label, "dev-master") != 0) { $p4SyncCommand = $p4SyncCommand . "@" . $label; } } - $result = shell_exec($p4SyncCommand); + $this->executeCommand($p4SyncCommand); chdir($prevDir); } - public function writeP4ClientSpec(){ + protected function writeClientSpecToFile($spec) { + fwrite($spec, "Client: " . $this->getClient() . "\n\n"); + fwrite($spec, "Update: " . date("Y/m/d H:i:s") . "\n\n"); + fwrite($spec, "Access: " . date("Y/m/d H:i:s") . "\n"); + fwrite($spec, "Owner: " . $this->getUser() . "\n\n"); + fwrite($spec, "Description:\n"); + fwrite($spec, " Created by " . $this->getUser() . " from composer.\n\n"); + fwrite($spec, "Root: " . $this->getPath() . "\n\n"); + fwrite($spec, "Options: noallwrite noclobber nocompress unlocked modtime rmdir\n\n"); + fwrite($spec, "SubmitOptions: revertunchanged\n\n"); + fwrite($spec, "LineEnd: local\n\n"); + if ($this->isStream()) { + fwrite($spec, "Stream:\n"); + fwrite($spec, " " . $this->getStreamWithoutLabel() . "\n"); + } + else { + fwrite( + $spec, "View: " . $this->getStream() . "/... //" . $this->getClient() . "/" . str_replace( + "//", "", $this->getStream() + ) . "/... \n" + ); + } + } + + public function writeP4ClientSpec() { $spec = fopen($this->getP4ClientSpec(), 'w'); try { - fwrite($spec, "Client: " . $this->getClient() . "\n\n"); - fwrite($spec, "Update: " . date("Y/m/d H:i:s") . "\n\n"); - fwrite($spec, "Access: " . date("Y/m/d H:i:s") . "\n" ); - fwrite($spec, "Owner: " . $this->getUser() . "\n\n" ); - fwrite($spec, "Description:\n" ); - fwrite($spec, " Created by " . $this->getUser() . " from composer.\n\n" ); - fwrite($spec, "Root: " .$this->getPath(). "\n\n" ); - fwrite($spec, "Options: noallwrite noclobber nocompress unlocked modtime rmdir\n\n" ); - fwrite($spec, "SubmitOptions: revertunchanged\n\n" ); - fwrite($spec, "LineEnd: local\n\n" ); - if ($this->isStream()){ - fwrite($spec, "Stream:\n" ); - fwrite($spec, " " . $this->getStreamWithoutLabel()."\n" ); - } else { - fwrite($spec, "View: " . $this->getStream() . "/... //" . $this->getClient() . "/" . str_replace("//", "", $this->getStream()) . "/... \n"); - } - } catch(Exception $e){ + $this->writeClientSpecToFile($spec); + } catch (Exception $e) { fclose($spec); throw $e; } fclose($spec); } - public function getComposerFilePath($identifier) - { - if ($this->isStream()){ - $composerFilePath = $this->path . "/composer.json" ; + protected function getP4variable($name){ + if (defined('PHP_WINDOWS_VERSION_BUILD')) { + $command = "p4 set"; + $result = $this->executeCommand($command); + $resArray = explode("\n", $result); + foreach ($resArray as $line) { + $fields = explode("=", $line); + if (strcmp($name, $fields[0]) == 0){ + $index = strpos($fields[1], " "); + if ($index === false){ + $value = $fields[1]; + } else { + $value = substr($fields[1], 0, $index); + } + $value = trim($value); + return $value; + } + } } else { - $composerFilePath = $this->path . "/" . $this->p4depot . "/composer.json" ; + $command = 'echo $' . $name; + $result = trim($this->executeCommand($command)); + return $result; } - return $composerFilePath; + } - public function p4Login(IOInterface $io){ + protected function read($pipe, $name){ + if (feof($pipe)) { + return; + } + $line = fgets($pipe); + while ($line != false){ + $line = fgets($pipe); + } + return; + } + + public function windowsLogin($password){ + $descriptorspec = array( + 0 => array("pipe", "r"), + 1 => array("pipe", "w"), + 2 => array("pipe", "a") + ); + $command = $this->generateP4Command(" login -a"); + $process = proc_open($command, $descriptorspec, $pipes); + if (!is_resource($process)){ + return false; + } + fwrite($pipes[0], $password); + fclose($pipes[0]); + + $this->read($pipes[1], "Output"); + $this->read($pipes[2], "Error"); + + fclose($pipes[1]); + fclose($pipes[2]); + + $return_code = proc_close($process); + + return $return_code; + } + + + public function p4Login(IOInterface $io) { $this->queryP4User($io); - if (!$this->isLoggedIn()){ + if (!$this->isLoggedIn()) { $password = $this->queryP4Password($io); - $command = "echo $password | " . $this->generateP4Command("login -a "); - shell_exec($command); + if (defined('PHP_WINDOWS_VERSION_BUILD')) { + $this->windowsLogin($password); + } else { + $command = "echo $password | ".$this->generateP4Command(" login -a", false); + $this->executeCommand($command); + } } } - public static function checkServerExists($url) - { - $result = shell_exec("p4 -p $url info -s"); + public static function checkServerExists($url, ProcessExecutor $process_executor) { + $process = $process_executor ? : new ProcessExecutor; + $result = ""; + $process->execute("p4 -p $url info -s", $result); $index = strpos($result, "error"); - if ($index === false){ - return true; + if ($index === FALSE) { + return TRUE; } - return false; + + return FALSE; } - public function getComposerInformation($identifier) - { + public function getComposerInformation($identifier) { $index = strpos($identifier, "@"); - if ($index === false){ - $composer_json = "$identifier/composer.json"; + if ($index === FALSE) { + $composer_json = "$identifier/composer.json"; + return $this->getComposerInformationFromPath($composer_json); - } else { + } + else { return $this->getComposerInformationFromLabel($identifier, $index); } } - public function getComposerInformationFromPath($composer_json) - { - $command = $this->generateP4Command(" print $composer_json", false); - $result = shell_exec($command); + + public function getComposerInformationFromPath($composer_json) { + $command = $this->generateP4Command(" print $composer_json"); + $result = $this->executeCommand($command); $index = strpos($result, "{"); - if ($index === false){ + if ($index === FALSE) { return ""; } - if ($index >=0){ + if ($index >= 0) { $rawData = substr($result, $index); - $composer_info = json_decode($rawData, true); + $composer_info = json_decode($rawData, TRUE); + return $composer_info; } + return ""; } - public function getComposerInformationFromLabel($identifier, $index) - { + public function getComposerInformationFromLabel($identifier, $index) { $composer_json_path = substr($identifier, 0, $index) . "/composer.json" . substr($identifier, $index); - $command = $this->generateP4Command(" files $composer_json_path", false); - $result = shell_exec($command); + $command = $this->generateP4Command(" files $composer_json_path", FALSE); + $result = $this->executeCommand($command); $index2 = strpos($result, "no such file(s)."); - if ($index2 === false){ + if ($index2 === FALSE) { $index3 = strpos($result, "change"); - if (!($index3 ===false )){ + if (!($index3 === FALSE)) { $phrase = trim(substr($result, $index3)); $fields = explode(" ", $phrase); $id = $fields[1]; $composer_json = substr($identifier, 0, $index) . "/composer.json@" . $id; + return $this->getComposerInformationFromPath($composer_json); } } + return ""; } - public function getBranches() - { + public function getBranches() { $possible_branches = array(); - if (!$this->isStream()){ - $branches[$this->p4branch] = $this->p4stream; - } else { + if (!$this->isStream()) { + $possible_branches[$this->p4branch] = $this->getStream(); + } + else { $command = $this->generateP4Command("streams //$this->p4depot/..."); - $result = shell_exec($command); + $result = ""; + $this->process->execute($command, $result); $resArray = explode("\n", $result); - foreach ($resArray as $line){ + foreach ($resArray as $line) { $resBits = explode(" ", $line); - if (count($resBits) > 4){ - $branch = substr($resBits[4], 1, strlen($resBits[4])-2); + if (count($resBits) > 4) { + $branch = preg_replace("/[^A-Za-z0-9 ]/", '', $resBits[4]); $possible_branches[$branch] = $resBits[1]; } } } $branches = array(); $branches['master'] = $possible_branches[$this->p4branch]; + return $branches; } - public function getTags() - { + public function getTags() { $command = $this->generateP4Command("labels"); - $result = shell_exec($command); + $result = $this->executeCommand($command); $resArray = explode("\n", $result); $tags = array(); - foreach ($resArray as $line){ + foreach ($resArray as $line) { $index = strpos($line, "Label"); - if (!($index===false)){ + if (!($index === FALSE)) { $fields = explode(" ", $line); - $tags[$fields[1]] = $this->getStream()."@" . $fields[1]; + $tags[$fields[1]] = $this->getStream() . "@" . $fields[1]; } } return $tags; } - public function checkStream () - { - $command = $this->generateP4Command("depots"); - $result = shell_exec($command); + public function checkStream() { + $command = $this->generateP4Command("depots", FALSE); + $result = $this->executeCommand($command); $resArray = explode("\n", $result); - foreach ($resArray as $line){ + foreach ($resArray as $line) { $index = strpos($line, "Depot"); - if (!($index===false)){ + if (!($index === FALSE)) { $fields = explode(" ", $line); - if (strcmp($this->p4depot, $fields[1]) === 0){ + if (strcmp($this->p4depot, $fields[1]) === 0) { $this->p4depotType = $fields[3]; + return $this->isStream(); } } } - return false; + + return FALSE; } } \ No newline at end of file diff --git a/tests/Composer/Test/Repository/Vcs/PerforceDriverTest.php b/tests/Composer/Test/Repository/Vcs/PerforceDriverTest.php index 2745887ed..19a374737 100644 --- a/tests/Composer/Test/Repository/Vcs/PerforceDriverTest.php +++ b/tests/Composer/Test/Repository/Vcs/PerforceDriverTest.php @@ -62,21 +62,6 @@ class PerforceDriverTest extends \PHPUnit_Framework_TestCase $this->assertTrue($result); } - public function testGetBranches() - { - $repo_config = array( - 'url' => "perforce.vuhl.root.mrc.local:3710", - 'depot' => "lighthouse" - ); - - $vcs = new PerforceDriver($repo_config, $this->io, $this->config); - $result = $vcs->initialize(); - $this->assertTrue($result); - $branches = $vcs->getBranches(); - //print ("\nBranches are: " . var_export($branches, true)); - $this->assertTrue(strcmp($branches['mainline'], "//lighthouse/mainline") == 0); - } - public function testGetTags() { $repo_config = array( @@ -122,31 +107,5 @@ class PerforceDriverTest extends \PHPUnit_Framework_TestCase $this->assertNull($dist); } - public function testGetRootIdentifier(){ - $repo_config = array( - 'url' => "perforce.vuhl.root.mrc.local:3710", - 'depot' => "lighthouse" - ); - - $vcs = new PerforceDriver($repo_config, $this->io, $this->config); - $result = $vcs->initialize(); - $this->assertTrue($result); - $rootId = $vcs->getRootIdentifier(); - $this->assertEquals("mainline", $rootId); - } - - public function testHasComposerFile(){ - $repo_config = array( - 'url' => "perforce.vuhl.root.mrc.local:3710", - 'depot' => "lighthouse" - ); - - $vcs = new PerforceDriver($repo_config, $this->io, $this->config); - $result = $vcs->initialize(); - $this->assertTrue($result); - $identifier = $vcs->getRootIdentifier(); - $value = $vcs->hasComposerFile($identifier); - $this->assertTrue($value); - } } diff --git a/tests/Composer/Test/Util/RemoteFilesystemTest.php b/tests/Composer/Test/Util/RemoteFilesystemTest.php index eb8ebc07e..f2950a2da 100644 --- a/tests/Composer/Test/Util/RemoteFilesystemTest.php +++ b/tests/Composer/Test/Util/RemoteFilesystemTest.php @@ -148,18 +148,13 @@ class RemoteFilesystemTest extends \PHPUnit_Framework_TestCase public function testCaptureAuthenticationParamsFromUrl() { $io = $this->getMock('Composer\IO\IOInterface'); - $io->expects($this->once()) - ->method('setAuthentication') - ->with($this->equalTo('example.com'), $this->equalTo('user'), $this->equalTo('pass')); + $io + ->expects($this->once()) + ->method('setAuthentication') + ; $fs = new RemoteFilesystem($io); - try { - $fs->getContents('example.com', 'http://user:pass@www.example.com/something'); - } catch (\Exception $e) { - $this->assertInstanceOf('Composer\Downloader\TransportException', $e); - $this->assertEquals(404, $e->getCode()); - $this->assertContains('404 Not Found', $e->getMessage()); - } + $fs->getContents('example.com', 'http://user:pass@www.example.com'); } public function testGetContents() From bab10dd9f8058646f66860f5808552f4d3bfba99 Mon Sep 17 00:00:00 2001 From: matt-whittom Date: Thu, 8 Aug 2013 15:35:21 -0500 Subject: [PATCH 08/26] Added ability to pass p4user and p4password in composer.json --- .../Downloader/PerforceDownloader.php | 20 ++++- .../Repository/Vcs/PerforceDriver.php | 10 ++- src/Composer/Util/Perforce.php | 78 +++++++++++-------- 3 files changed, 72 insertions(+), 36 deletions(-) diff --git a/src/Composer/Downloader/PerforceDownloader.php b/src/Composer/Downloader/PerforceDownloader.php index 8e5f3d01e..325892c32 100644 --- a/src/Composer/Downloader/PerforceDownloader.php +++ b/src/Composer/Downloader/PerforceDownloader.php @@ -28,7 +28,25 @@ class PerforceDownloader extends VcsDownloader $ref = $package->getSourceReference(); $label = $package->getPrettyVersion(); - $perforce = new Perforce("", "", $package->getSourceUrl(), $path); + $repository = $package->getRepository(); + //assume repository is a Perforce Repository + + $reflector = new \ReflectionClass($repository); + $repoConfigProperty = $reflector->getProperty("repoConfig"); + $repoConfigProperty->setAccessible(true); + $repoConfig = $repoConfigProperty->getValue($repository); + + $p4user = ""; + if (isset($repoConfig['p4user'])) { + $p4user = $repoConfig['p4user']; + } + $p4password = ""; + if (isset($repoConfig['p4password'])) { + $p4password = $repoConfig['p4password']; + } + +// print("Perforce Downloader:doDownload - repoConfig:" . var_dump($repoConfig, true) . "\n\n"); + $perforce = new Perforce("", "", $package->getSourceUrl(), $path, null, $p4user, $p4password); $perforce->setStream($ref); $perforce->queryP4User($this->io); $perforce->writeP4ClientSpec(); diff --git a/src/Composer/Repository/Vcs/PerforceDriver.php b/src/Composer/Repository/Vcs/PerforceDriver.php index a1080e5c4..489a0812c 100644 --- a/src/Composer/Repository/Vcs/PerforceDriver.php +++ b/src/Composer/Repository/Vcs/PerforceDriver.php @@ -37,10 +37,18 @@ class PerforceDriver extends VcsDriver { if (isset($this->repoConfig['branch'])) { $this->branch = $this->repoConfig['branch']; } + $p4user = ""; + if (isset($this->repoConfig['p4user'])) { + $p4user = $this->repoConfig['p4user']; + } + $p4password = ""; + if (isset($this->repoConfig['p4password'])) { + $p4password = $this->repoConfig['p4password']; + } $repoDir = $this->config->get('cache-vcs-dir') . "/$this->depot"; if (!isset($this->perforce)) { - $this->perforce = new Perforce($this->depot, $this->branch, $this->getUrl(), $repoDir, $this->process); + $this->perforce = new Perforce($this->depot, $this->branch, $this->getUrl(), $repoDir, $this->process, $p4user, $p4password); } $this->perforce->p4Login($this->io); diff --git a/src/Composer/Util/Perforce.php b/src/Composer/Util/Perforce.php index 6174c5c49..cb46bac81 100644 --- a/src/Composer/Util/Perforce.php +++ b/src/Composer/Util/Perforce.php @@ -17,6 +17,7 @@ class Perforce { protected $path; protected $p4client; protected $p4user; + protected $p4password; protected $p4port; protected $p4stream; protected $p4clientSpec; @@ -24,7 +25,7 @@ class Perforce { protected $p4branch; protected $process; - public function __construct($depot, $branch, $port, $path, ProcessExecutor $process = null) { + public function __construct($depot, $branch, $port, $path, ProcessExecutor $process = null, $p4user = null, $p4password = null) { $this->p4depot = $depot; $this->p4branch = $branch; $this->p4port = $port; @@ -32,6 +33,14 @@ class Perforce { $this->process = $process ? : new ProcessExecutor; $fs = new Filesystem(); $fs->ensureDirectoryExists($path); + if (isset($p4user)){ + $this->p4user = $p4user; + } else { + $this->p4user = $this->getP4variable("P4USER"); + } + if (isset($p4password)){ + $this->p4password = $p4password; + } } protected function getRandomValue() { @@ -55,14 +64,6 @@ class Perforce { return $this->p4client; } - public function getUser() { - if (!isset($this->p4user)) { - $this->p4user = $this->getP4variable("P4USER"); - } - - return $this->p4user; - } - protected function getPath() { return $this->path; } @@ -100,6 +101,10 @@ class Perforce { return $p4clientSpec; } + public function getUser() { + return $this->p4user; + } + public function queryP4User(IOInterface $io) { $this->getUser(); if (strlen($this->p4user) <= 0) { @@ -113,11 +118,41 @@ class Perforce { } } + protected function getP4variable($name){ + if (defined('PHP_WINDOWS_VERSION_BUILD')) { + $command = "p4 set"; + $result = $this->executeCommand($command); + $resArray = explode("\n", $result); + foreach ($resArray as $line) { + $fields = explode("=", $line); + if (strcmp($name, $fields[0]) == 0){ + $index = strpos($fields[1], " "); + if ($index === false){ + $value = $fields[1]; + } else { + $value = substr($fields[1], 0, $index); + } + $value = trim($value); + return $value; + } + } + } else { + $command = 'echo $' . $name; + $result = trim($this->executeCommand($command)); + return $result; + } + + } + protected function queryP4Password(IOInterface $io) { + if (isset($this->p4password)){ + return $this->p4password; + } $password = $this->getP4variable("P4PASSWD"); if (strlen($password) <= 0) { $password = $io->askAndHideAnswer("Enter password for Perforce user " . $this->getUser() . ": "); } + $this->p4password = $password; return $password; } @@ -209,31 +244,6 @@ class Perforce { fclose($spec); } - protected function getP4variable($name){ - if (defined('PHP_WINDOWS_VERSION_BUILD')) { - $command = "p4 set"; - $result = $this->executeCommand($command); - $resArray = explode("\n", $result); - foreach ($resArray as $line) { - $fields = explode("=", $line); - if (strcmp($name, $fields[0]) == 0){ - $index = strpos($fields[1], " "); - if ($index === false){ - $value = $fields[1]; - } else { - $value = substr($fields[1], 0, $index); - } - $value = trim($value); - return $value; - } - } - } else { - $command = 'echo $' . $name; - $result = trim($this->executeCommand($command)); - return $result; - } - - } protected function read($pipe, $name){ if (feof($pipe)) { From 1019c014e58b82d5e211216031a00b1c6aec0425 Mon Sep 17 00:00:00 2001 From: matt-whittom Date: Tue, 13 Aug 2013 10:54:35 -0500 Subject: [PATCH 09/26] using theirs --- composer.json | 3 +- composer.lock | 63 ++++++-- .../Downloader/PerforceDownloader.php | 44 +++--- .../Repository/Vcs/PerforceDriver.php | 26 ++-- src/Composer/Repository/VcsRepository.php | 5 + src/Composer/Util/Perforce.php | 96 +++++++----- .../Repository/Vcs/PerforceDriverTest.php | 138 +++++++++--------- 7 files changed, 221 insertions(+), 154 deletions(-) diff --git a/composer.json b/composer.json index f0d4ee56e..77cc9590f 100644 --- a/composer.json +++ b/composer.json @@ -30,7 +30,8 @@ "symfony/process": "~2.1" }, "require-dev": { - "phpunit/phpunit": "~3.7.10" + "phpunit/phpunit": "~3.7.10", + "mikey179/vfsStream" : "1.2.*" }, "suggest": { "ext-zip": "Enabling the zip extension allows you to unzip archives, and allows gzip compression of all internet traffic", diff --git a/composer.lock b/composer.lock index cae3bad07..5a23476b4 100644 --- a/composer.lock +++ b/composer.lock @@ -3,7 +3,7 @@ "This file locks the dependencies of your project to a known state", "Read more about it at http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file" ], - "hash": "370b764a9317165e8ea7a2e1623e031b", + "hash": "6203fdb419c10ffd84f85611fc9eec61", "packages": [ { "name": "justinrainbow/json-schema", @@ -79,17 +79,17 @@ }, { "name": "symfony/console", - "version": "v2.3.3", + "version": "dev-master", "target-dir": "Symfony/Component/Console", "source": { "type": "git", "url": "https://github.com/symfony/Console.git", - "reference": "v2.3.3" + "reference": "872a494b88fba2f62be85e0bc8441e7946bb6ba6" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/Console/zipball/v2.3.3", - "reference": "v2.3.3", + "url": "https://api.github.com/repos/symfony/Console/zipball/872a494b88fba2f62be85e0bc8441e7946bb6ba6", + "reference": "872a494b88fba2f62be85e0bc8441e7946bb6ba6", "shasum": "" }, "require": { @@ -104,7 +104,7 @@ "type": "library", "extra": { "branch-alias": { - "dev-master": "2.3-dev" + "dev-master": "2.4-dev" } }, "autoload": { @@ -128,7 +128,7 @@ ], "description": "Symfony Console Component", "homepage": "http://symfony.com", - "time": "2013-07-21 12:12:18" + "time": "2013-08-09 06:00:31" }, { "name": "symfony/finder", @@ -179,17 +179,17 @@ }, { "name": "symfony/process", - "version": "v2.3.3", + "version": "dev-master", "target-dir": "Symfony/Component/Process", "source": { "type": "git", "url": "https://github.com/symfony/Process.git", - "reference": "v2.3.3" + "reference": "723fe405fcc878ae75469babcb9507d292797ece" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/Process/zipball/v2.3.3", - "reference": "v2.3.3", + "url": "https://api.github.com/repos/symfony/Process/zipball/723fe405fcc878ae75469babcb9507d292797ece", + "reference": "723fe405fcc878ae75469babcb9507d292797ece", "shasum": "" }, "require": { @@ -198,7 +198,7 @@ "type": "library", "extra": { "branch-alias": { - "dev-master": "2.3-dev" + "dev-master": "2.4-dev" } }, "autoload": { @@ -222,10 +222,40 @@ ], "description": "Symfony Process Component", "homepage": "http://symfony.com", - "time": "2013-08-02 21:51:01" + "time": "2013-08-09 07:03:52" } ], "packages-dev": [ + { + "name": "mikey179/vfsStream", + "version": "v1.2.0", + "source": { + "type": "git", + "url": "https://github.com/mikey179/vfsStream.git", + "reference": "v1.2.0" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/mikey179/vfsStream/zipball/v1.2.0", + "reference": "v1.2.0", + "shasum": "" + }, + "require": { + "php": ">=5.3.0" + }, + "type": "library", + "autoload": { + "psr-0": { + "org\\bovigo\\vfs\\": "src/main/php" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD" + ], + "homepage": "http://vfs.bovigo.org/", + "time": "2013-04-01 10:41:02" + }, { "name": "phpunit/php-code-coverage", "version": "1.2.12", @@ -645,9 +675,10 @@ ], "minimum-stability": "stable", - "stability-flags": [ - - ], + "stability-flags": { + "symfony/console": 20, + "symfony/process": 20 + }, "platform": { "php": ">=5.3.2" }, diff --git a/src/Composer/Downloader/PerforceDownloader.php b/src/Composer/Downloader/PerforceDownloader.php index 325892c32..5ed241d79 100644 --- a/src/Composer/Downloader/PerforceDownloader.php +++ b/src/Composer/Downloader/PerforceDownloader.php @@ -13,6 +13,7 @@ namespace Composer\Downloader; use Composer\Package\PackageInterface; +use Composer\Repository\VcsRepository; use Composer\Util\Perforce; /** @@ -20,6 +21,8 @@ use Composer\Util\Perforce; */ class PerforceDownloader extends VcsDownloader { + protected $perforce; + /** * {@inheritDoc} */ @@ -28,30 +31,29 @@ class PerforceDownloader extends VcsDownloader $ref = $package->getSourceReference(); $label = $package->getPrettyVersion(); + $this->initPerforce($package, $path); + $this->perforce->setStream($ref); + $this->perforce->queryP4User($this->io); + $this->perforce->writeP4ClientSpec(); + $this->perforce->connectClient(); + $this->perforce->syncCodeBase($label); + } + + private function initPerforce($package, $path){ + if (isset($this->perforce)){ + return; + } $repository = $package->getRepository(); - //assume repository is a Perforce Repository + $repoConfig = $this->getRepoConfig($repository); + $this->perforce = new Perforce($repoConfig, $package->getSourceUrl(), $path); + } - $reflector = new \ReflectionClass($repository); - $repoConfigProperty = $reflector->getProperty("repoConfig"); - $repoConfigProperty->setAccessible(true); - $repoConfig = $repoConfigProperty->getValue($repository); + public function injectPerforce($perforce){ + $this->perforce = $perforce; + } - $p4user = ""; - if (isset($repoConfig['p4user'])) { - $p4user = $repoConfig['p4user']; - } - $p4password = ""; - if (isset($repoConfig['p4password'])) { - $p4password = $repoConfig['p4password']; - } - -// print("Perforce Downloader:doDownload - repoConfig:" . var_dump($repoConfig, true) . "\n\n"); - $perforce = new Perforce("", "", $package->getSourceUrl(), $path, null, $p4user, $p4password); - $perforce->setStream($ref); - $perforce->queryP4User($this->io); - $perforce->writeP4ClientSpec(); - $perforce->connectClient(); - $perforce->syncCodeBase($label); + private function getRepoConfig(VcsRepository $repository){ + return $repository->getRepoConfig(); } /** diff --git a/src/Composer/Repository/Vcs/PerforceDriver.php b/src/Composer/Repository/Vcs/PerforceDriver.php index 489a0812c..0f930a52b 100644 --- a/src/Composer/Repository/Vcs/PerforceDriver.php +++ b/src/Composer/Repository/Vcs/PerforceDriver.php @@ -32,25 +32,13 @@ class PerforceDriver extends VcsDriver { * {@inheritDoc} */ public function initialize() { - $this->depot = $this->repoConfig['depot']; + $this->depot = $this->repoConfig['depot']; $this->branch = ""; if (isset($this->repoConfig['branch'])) { $this->branch = $this->repoConfig['branch']; } - $p4user = ""; - if (isset($this->repoConfig['p4user'])) { - $p4user = $this->repoConfig['p4user']; - } - $p4password = ""; - if (isset($this->repoConfig['p4password'])) { - $p4password = $this->repoConfig['p4password']; - } - - $repoDir = $this->config->get('cache-vcs-dir') . "/$this->depot"; - if (!isset($this->perforce)) { - $this->perforce = new Perforce($this->depot, $this->branch, $this->getUrl(), $repoDir, $this->process, $p4user, $p4password); - } + $this->initPerforce(); $this->perforce->p4Login($this->io); $this->perforce->checkStream($this->depot); @@ -60,6 +48,15 @@ class PerforceDriver extends VcsDriver { return TRUE; } + private function initPerforce() { + if (isset($this->perforce)) { + return; + } + + $repoDir = $this->config->get('cache-vcs-dir') . "/$this->depot"; + $this->perforce = new Perforce($this->repoConfig, $this->getUrl(), $repoDir, $this->process); + } + public function injectPerforce(Perforce $perforce) { $this->perforce = $perforce; } @@ -95,6 +92,7 @@ class PerforceDriver extends VcsDriver { */ public function getTags() { $tags = $this->perforce->getTags(); + return $tags; } diff --git a/src/Composer/Repository/VcsRepository.php b/src/Composer/Repository/VcsRepository.php index bb3bacc68..2b876bc22 100644 --- a/src/Composer/Repository/VcsRepository.php +++ b/src/Composer/Repository/VcsRepository.php @@ -59,6 +59,11 @@ class VcsRepository extends ArrayRepository $this->repoConfig = $repoConfig; } + public function getRepoConfig() + { + return $this->repoConfig; + } + public function setLoader(LoaderInterface $loader) { $this->loader = $loader; diff --git a/src/Composer/Util/Perforce.php b/src/Composer/Util/Perforce.php index cb46bac81..5b032b71b 100644 --- a/src/Composer/Util/Perforce.php +++ b/src/Composer/Util/Perforce.php @@ -25,21 +25,27 @@ class Perforce { protected $p4branch; protected $process; - public function __construct($depot, $branch, $port, $path, ProcessExecutor $process = null, $p4user = null, $p4password = null) { - $this->p4depot = $depot; - $this->p4branch = $branch; + public function __construct($repoConfig, $port, $path, ProcessExecutor $process = NULL) { $this->p4port = $port; $this->path = $path; $this->process = $process ? : new ProcessExecutor; $fs = new Filesystem(); $fs->ensureDirectoryExists($path); - if (isset($p4user)){ - $this->p4user = $p4user; - } else { + + if (isset($repoConfig['depot'])) { + $this->p4depot = $repoConfig['depot']; + } + if (isset($repoConfig['branch'])) { + $this->p4branch = $repoConfig['branch']; + } + if (isset($repoConfig['p4user'])) { + $this->p4user = $repoConfig['p4user']; + } + else { $this->p4user = $this->getP4variable("P4USER"); } - if (isset($p4password)){ - $this->p4password = $p4password; + if (isset($repoConfig['p4password'])) { + $this->p4password = $repoConfig['p4password']; } } @@ -47,6 +53,10 @@ class Perforce { return mt_rand(1000, 9999); } + protected function isWindows(){ + return defined('PHP_WINDOWS_VERSION_BUILD'); + } + protected function executeCommand($command) { $result = ""; $this->process->execute($command, $result); @@ -72,6 +82,10 @@ class Perforce { return $this->p4port; } + protected function isStream() { + return (strcmp($this->p4depotType, "stream") === 0); + } + protected function getStream() { if (!isset($this->p4stream)) { if ($this->isStream()) { @@ -107,45 +121,54 @@ class Perforce { public function queryP4User(IOInterface $io) { $this->getUser(); - if (strlen($this->p4user) <= 0) { - $this->p4user = $io->ask("Enter P4 User:"); - if (defined('PHP_WINDOWS_VERSION_BUILD')) { - $command = "p4 set P4USER=$this->p4user"; - } else { - $command = "export P4USER=$this->p4user"; - } - $result = $this->executeCommand($command); + if (strlen($this->p4user) > 0) { + return; } + $this->p4user = $this->getP4variable("P4USER"); + if (strlen($this->p4user) > 0) { + return; + } + $this->p4user = $io->ask("Enter P4 User:"); + if ($this->isWindows()) { + $command = "p4 set P4USER=$this->p4user"; + } + else { + $command = "export P4USER=$this->p4user"; + } + $result = $this->executeCommand($command); } - protected function getP4variable($name){ - if (defined('PHP_WINDOWS_VERSION_BUILD')) { + protected function getP4variable($name) { + if ($this->isWindows()) { $command = "p4 set"; $result = $this->executeCommand($command); $resArray = explode("\n", $result); foreach ($resArray as $line) { $fields = explode("=", $line); - if (strcmp($name, $fields[0]) == 0){ + if (strcmp($name, $fields[0]) == 0) { $index = strpos($fields[1], " "); - if ($index === false){ + if ($index === FALSE) { $value = $fields[1]; - } else { + } + else { $value = substr($fields[1], 0, $index); } $value = trim($value); + return $value; } } - } else { + } + else { $command = 'echo $' . $name; $result = trim($this->executeCommand($command)); + return $result; } - } protected function queryP4Password(IOInterface $io) { - if (isset($this->p4password)){ + if (isset($this->p4password)) { return $this->p4password; } $password = $this->getP4variable("P4PASSWD"); @@ -153,11 +176,8 @@ class Perforce { $password = $io->askAndHideAnswer("Enter password for Perforce user " . $this->getUser() . ": "); } $this->p4password = $password; - return $password; - } - protected function isStream() { - return (strcmp($this->p4depotType, "stream") === 0); + return $password; } protected function generateP4Command($command, $useClient = TRUE) { @@ -179,6 +199,7 @@ class Perforce { if ($index === FALSE) { return FALSE; } + return TRUE; } @@ -245,18 +266,19 @@ class Perforce { } - protected function read($pipe, $name){ + protected function read($pipe, $name) { if (feof($pipe)) { return; } $line = fgets($pipe); - while ($line != false){ + while ($line != FALSE) { $line = fgets($pipe); } + return; } - public function windowsLogin($password){ + public function windowsLogin($password) { $descriptorspec = array( 0 => array("pipe", "r"), 1 => array("pipe", "w"), @@ -264,8 +286,8 @@ class Perforce { ); $command = $this->generateP4Command(" login -a"); $process = proc_open($command, $descriptorspec, $pipes); - if (!is_resource($process)){ - return false; + if (!is_resource($process)) { + return FALSE; } fwrite($pipes[0], $password); fclose($pipes[0]); @@ -286,10 +308,11 @@ class Perforce { $this->queryP4User($io); if (!$this->isLoggedIn()) { $password = $this->queryP4Password($io); - if (defined('PHP_WINDOWS_VERSION_BUILD')) { + if ($this->isWindows()) { $this->windowsLogin($password); - } else { - $command = "echo $password | ".$this->generateP4Command(" login -a", false); + } + else { + $command = "echo $password | " . $this->generateP4Command(" login -a", FALSE); $this->executeCommand($command); } } @@ -392,6 +415,7 @@ class Perforce { $tags[$fields[1]] = $this->getStream() . "@" . $fields[1]; } } + return $tags; } diff --git a/tests/Composer/Test/Repository/Vcs/PerforceDriverTest.php b/tests/Composer/Test/Repository/Vcs/PerforceDriverTest.php index 19a374737..db1485836 100644 --- a/tests/Composer/Test/Repository/Vcs/PerforceDriverTest.php +++ b/tests/Composer/Test/Repository/Vcs/PerforceDriverTest.php @@ -14,97 +14,103 @@ namespace Composer\Test\Repository\Vcs; -#use Composer\Downloader\TransportException; + use Composer\Repository\Vcs\PerforceDriver; use Composer\Util\Filesystem; use Composer\Config; -use Composer\IO\ConsoleIO; -use Symfony\Component\Console\Input\ArrayInput; -use Symfony\Component\Console\Output\ConsoleOutput; -use Symfony\Component\Console\Helper\HelperSet; -class PerforceDriverTest extends \PHPUnit_Framework_TestCase -{ +class PerforceDriverTest extends \PHPUnit_Framework_TestCase { private $config; private $io; + private $process; + private $remoteFileSystem; + private $testPath; - public function setUp() - { + public function setUp() { + $this->testPath = sys_get_temp_dir() . '/composer-test'; $this->config = new Config(); - $this->config->merge(array( - 'config' => array( - 'home' => sys_get_temp_dir() . '/composer-test', - ), - )); - $inputParameters = array(); - $input = new ArrayInput($inputParameters); - $output = new ConsoleOutput(); - $helperSet = new HelperSet(); - $this->io = new ConsoleIO($input, $output, $helperSet); + $this->config->merge( + array( + 'config' => array( + 'home' => $this->testPath, + ), + ) + ); + + $this->io = $this->getMock('Composer\IO\IOInterface'); + $this->process = $this->getMock('Composer\Util\ProcessExecutor'); + $this->remoteFileSystem = $this->getMockBuilder('Composer\Util\RemoteFilesystem')->disableOriginalConstructor()->getMock(); } - public function tearDown() - { + public function tearDown() { $fs = new Filesystem; - $fs->removeDirectory(sys_get_temp_dir() . '/composer-test'); + $fs->removeDirectory($this->testPath); } - public function testPrivateRepository() - { - $repo_config = array( - 'url' => "perforce.vuhl.root.mrc.local:3710", - 'depot' => "lighthouse" - ); + //Test: + //hasComposerFile - $vcs = new PerforceDriver($repo_config, $this->io, $this->config); - $result = $vcs->initialize(); - $this->assertTrue($result); + public function testInitializeCapturesVariablesFromRepoConfig() { + $this->setUp(); + $repo_config = array( + 'url' => 'TEST_PERFORCE_URL', + 'depot' => 'TEST_DEPOT_CONFIG', + 'branch' => 'TEST_BRANCH_CONFIG' + ); + $driver = new TestingPerforceDriver($repo_config, $this->io, $this->config, $this->process, $this->remoteFileSystem); + $arguments = array(array('depot'=>'TEST_DEPOT', 'branch'=>'TEST_BRANCH'), 'port'=>'TEST_PORT', 'path'=>$this->testPath); + $perforce = $this->getMock('Composer\Util\Perforce', null, $arguments); + $driver->injectPerforce($perforce); + $driver->initialize(); + $this->assertEquals("TEST_PERFORCE_URL", $driver->getUrl()); + $this->assertEquals("TEST_DEPOT_CONFIG", $driver->getDepot()); + $this->assertEquals("TEST_BRANCH_CONFIG", $driver->getBranch()); } - public function testGetTags() - { + public function testInitializeLogsInAndConnectsClient() { + $this->setUp(); $repo_config = array( - 'url' => "perforce.vuhl.root.mrc.local:3710", - 'depot' => "lighthouse" + 'url' => 'TEST_PERFORCE_URL', + 'depot' => 'TEST_DEPOT_CONFIG', + 'branch' => 'TEST_BRANCH_CONFIG' ); + $driver = new TestingPerforceDriver($repo_config, $this->io, $this->config, $this->process, $this->remoteFileSystem); + $perforce = $this->getMockBuilder('Composer\Util\Perforce')->disableOriginalConstructor()->getMock(); + $perforce->expects($this->at(0)) + ->method('p4Login') + ->with($this->io); + $perforce->expects($this->at(1)) + ->method('checkStream') + ->with($this->equalTo("TEST_DEPOT_CONFIG")); + $perforce->expects($this->at(2)) + ->method('writeP4ClientSpec'); + $perforce->expects($this->at(3)) + ->method('connectClient'); - $vcs = new PerforceDriver($repo_config, $this->io, $this->config); - $result = $vcs->initialize(); - $this->assertTrue($result); - $tags = $vcs->getTags(); - $this->assertTrue(empty($tags)); + $driver->injectPerforce($perforce); + $driver->initialize(); } - public function testGetSource() - { + public function testHasComposerFile() { + $this->setUp(); $repo_config = array( - 'url' => "perforce.vuhl.root.mrc.local:3710", - 'depot' => "lighthouse" + 'url' => 'TEST_PERFORCE_URL', + 'depot' => 'TEST_DEPOT_CONFIG', + 'branch' => 'TEST_BRANCH_CONFIG' ); - - $vcs = new PerforceDriver($repo_config, $this->io, $this->config); - $result = $vcs->initialize(); + $driver = new TestingPerforceDriver($repo_config, $this->io, $this->config, $this->process, $this->remoteFileSystem); + $arguments = array(array('depot'=>'TEST_DEPOT', 'branch'=>'TEST_BRANCH'), 'port'=>'TEST_PORT', 'path'=>$this->testPath); + $perforce = $this->getMock('Composer\Util\Perforce', array('getComposerInformation'), $arguments); + $perforce->expects($this->at(0)) + ->method('getComposerInformation') + ->with($this->equalTo("//TEST_DEPOT_CONFIG/TEST_IDENTIFIER")) + ->will($this->returnValue("Some json stuff")); + $driver->injectPerforce($perforce); + $driver->initialize(); + $identifier = "TEST_IDENTIFIER"; + $result = $driver->hasComposerFile($identifier); $this->assertTrue($result); - $identifier = $vcs->getRootIdentifier(); - $source = $vcs->getSource($identifier); - $this->assertEquals($source['type'], "perforce"); - $this->assertEquals($source['reference'], $identifier); - } - - public function testGetDist() - { - $repo_config = array( - 'url' => "perforce.vuhl.root.mrc.local:3710", - 'depot' => "lighthouse" - ); - - $vcs = new PerforceDriver($repo_config, $this->io, $this->config); - $result = $vcs->initialize(); - $this->assertTrue($result); - $identifier = $vcs->getRootIdentifier(); - $dist = $vcs->getDist($identifier); - $this->assertNull($dist); } } From 10159576591ddada028915bd8ad17142137f6483 Mon Sep 17 00:00:00 2001 From: matt-whittom Date: Fri, 9 Aug 2013 14:24:58 -0500 Subject: [PATCH 10/26] Added tests and code for tests --- .../Downloader/PerforceDownloaderTest.php | 91 +++ .../Repository/Vcs/TestingPerforceDriver.php | 26 + tests/Composer/Test/Util/PerforceTest.php | 537 ++++++++++++++++++ tests/Composer/Test/Util/TestingPerforce.php | 106 ++++ 4 files changed, 760 insertions(+) create mode 100644 tests/Composer/Test/Downloader/PerforceDownloaderTest.php create mode 100644 tests/Composer/Test/Repository/Vcs/TestingPerforceDriver.php create mode 100644 tests/Composer/Test/Util/PerforceTest.php create mode 100644 tests/Composer/Test/Util/TestingPerforce.php diff --git a/tests/Composer/Test/Downloader/PerforceDownloaderTest.php b/tests/Composer/Test/Downloader/PerforceDownloaderTest.php new file mode 100644 index 000000000..66ed81776 --- /dev/null +++ b/tests/Composer/Test/Downloader/PerforceDownloaderTest.php @@ -0,0 +1,91 @@ +testPath = sys_get_temp_dir() . '/composer-test'; + $this->config = new Config(); + $this->config->merge( + array( + 'config' => array( + 'home' => $this->testPath, + ), + ) + ); + $this->io = $this->getMock('Composer\IO\IOInterface'); + } + + + public function testDoDownloadGetRepoConfig() { + $downloader = new PerforceDownloader($this->io, $this->config); + $package = $this->getMock('Composer\Package\PackageInterface' ); + $repoConfig = array('url'=>'TEST_URL','p4user'=>'TEST_USER'); + $repository = $this->getMock('Composer\Repository\VcsRepository', array('getRepoConfig'), array($repoConfig, $this->io, $this->config)); + $package->expects($this->at(0)) + ->method('getSourceReference') + ->will($this->returnValue("SOURCE_REF")); + $package->expects($this->at(1)) + ->method('getPrettyVersion') + ->will($this->returnValue("100")); + $package->expects($this->at(2)) + ->method('getRepository') + ->will($this->returnValue($repository)); + $repository->expects($this->at(0)) + ->method('getRepoConfig'); + $path = $this->testPath; + $downloader->doDownload($package, $path); + } + + public function testDoDownload() { + $downloader = new PerforceDownloader($this->io, $this->config); + $repoConfig = array("depot"=>"TEST_DEPOT", "branch"=>"TEST_BRANCH", "p4user"=>"TEST_USER"); + $port = "TEST_PORT"; + $path = "TEST_PATH"; + $perforce = $this->getMock('Composer\Util\Perforce', array('setStream', 'queryP4User', 'writeP4ClientSpec', 'connectClient', 'syncCodeBase'), array($repoConfig, $port, $path)); + $ref = "SOURCE_REF"; + $label = "LABEL"; + $perforce->expects($this->at(0)) + ->method('setStream') + ->with($this->equalTo($ref)); + $perforce->expects($this->at(1)) + ->method('queryP4User') + ->with($this->io); + $perforce->expects($this->at(2)) + ->method('writeP4ClientSpec'); + $perforce->expects($this->at(3)) + ->method('connectClient'); + $perforce->expects($this->at(4)) + ->method('syncCodeBase') + ->with($this->equalTo($label)); + $downloader->injectPerforce($perforce); + $package = $this->getMock('Composer\Package\PackageInterface' ); + $package->expects($this->at(0)) + ->method('getSourceReference') + ->will($this->returnValue($ref)); + $package->expects($this->at(1)) + ->method('getPrettyVersion') + ->will($this->returnValue($label)); + $path = $this->testPath; + $downloader->doDownload($package, $path); + + } +} diff --git a/tests/Composer/Test/Repository/Vcs/TestingPerforceDriver.php b/tests/Composer/Test/Repository/Vcs/TestingPerforceDriver.php new file mode 100644 index 000000000..574c69377 --- /dev/null +++ b/tests/Composer/Test/Repository/Vcs/TestingPerforceDriver.php @@ -0,0 +1,26 @@ +depot; + } + public function getBranch(){ + return $this->branch; + } + +} \ No newline at end of file diff --git a/tests/Composer/Test/Util/PerforceTest.php b/tests/Composer/Test/Util/PerforceTest.php new file mode 100644 index 000000000..8a653394f --- /dev/null +++ b/tests/Composer/Test/Util/PerforceTest.php @@ -0,0 +1,537 @@ +processExecutor = $this->getMock('Composer\Util\ProcessExecutor'); + $repoConfig = array("depot"=>"depot", "branch"=>"branch", "p4user"=>"user"); + $this->perforce = new TestingPerforce($repoConfig, "port", "path", $this->processExecutor); + } + + public function testGetClientWithoutStream() { + $client = $this->perforce->testGetClient(); + $expected = "composer_perforce_TEST_depot"; + $this->assertEquals($expected, $client); + } + + public function testGetClientFromStream() { + $this->perforce->setDepotType("stream"); + $client = $this->perforce->testGetClient(); + + $expected = "composer_perforce_TEST_depot_branch"; + $this->assertEquals($expected, $client); + } + + public function testGetStreamWithoutStream() { + $stream = $this->perforce->testGetStream(); + $this->assertEquals("//depot", $stream); + } + + public function testGetStreamWithStream() { + $this->perforce->setDepotType("stream"); + $stream = $this->perforce->testGetStream(); + $this->assertEquals("//depot/branch", $stream); + } + + public function testGetStreamWithoutLabel() { + $stream = $this->perforce->testGetStreamWithoutLabel(); + $this->assertEquals("//depot", $stream); + $this->perforce->setDepotType("stream"); + $stream = $this->perforce->testGetStreamWithoutLabel(); + $this->assertEquals("//depot/branch", $stream); + $this->perforce->setStream("//depot/branching@label"); + $stream = $this->perforce->testGetStreamWithoutLabel(); + $this->assertEquals("//depot/branching", $stream); + } + + public function testGetClientSpec() { + $clientSpec = $this->perforce->testGetClientSpec(); + $expected = "path/composer_perforce_TEST_depot.p4.spec"; + $this->assertEquals($expected, $clientSpec); + } + + public function testGenerateP4Command() { + $command = "do something"; + $p4Command = $this->perforce->testGenerateP4Command($command); + $expected = "p4 -u user -c composer_perforce_TEST_depot -p port do something"; + $this->assertEquals($expected, $p4Command); + } + + public function testQueryP4UserWithUserAlreadySet(){ + $io = $this->getMock('Composer\IO\IOInterface'); + + $this->perforce->setP4User("TEST_USER"); + $this->perforce->queryP4user($io); + $this->assertEquals("TEST_USER", $this->perforce->getUser()); + } + + public function testQueryP4UserWithUserSetInP4VariablesWithWindowsOS(){ + $this->perforce->windows_flag = true; + + $io = $this->getMock('Composer\IO\IOInterface'); + $expectedCommand = "p4 set"; + $this->processExecutor->expects($this->at(0)) + ->method('execute') + ->with($this->equalTo($expectedCommand)) + ->will($this->returnCallback(function($command, &$output) {$output = "P4USER=TEST_P4VARIABLE_USER\n"; return true;})); + + $this->perforce->setP4User(null); + $this->perforce->queryP4user($io); + $this->assertEquals("TEST_P4VARIABLE_USER", $this->perforce->getUser()); + } + + public function testQueryP4UserWithUserSetInP4VariablesNotWindowsOS(){ + $this->perforce->windows_flag = false; + + $io = $this->getMock('Composer\IO\IOInterface'); + $expectedCommand = 'echo $P4USER'; + $this->processExecutor->expects($this->at(0)) + ->method('execute') + ->with($this->equalTo($expectedCommand)) + ->will($this->returnCallback(function($command, &$output) {$output = "TEST_P4VARIABLE_USER\n"; return true;})); + + $this->perforce->setP4User(null); + $this->perforce->queryP4user($io); + $this->assertEquals("TEST_P4VARIABLE_USER", $this->perforce->getUser()); + } + + public function testQueryP4UserQueriesForUser(){ + $io = $this->getMock('Composer\IO\IOInterface'); + $expectedQuestion = "Enter P4 User:"; + $io->expects($this->at(0)) + ->method('ask') + ->with($this->equalTo($expectedQuestion)) + ->will($this->returnValue("TEST_QUERY_USER")); + + $this->perforce->setP4User(null); + $this->perforce->queryP4user($io); + $this->assertEquals("TEST_QUERY_USER", $this->perforce->getUser()); + } + + public function testQueryP4UserStoresResponseToQueryForUserWithWindows(){ + $this->perforce->windows_flag = true; + + $io = $this->getMock('Composer\IO\IOInterface'); + $expectedQuestion = "Enter P4 User:"; + $io->expects($this->at(0)) + ->method('ask') + ->with($this->equalTo($expectedQuestion)) + ->will($this->returnValue("TEST_QUERY_USER")); + $expectedCommand = "p4 set P4USER=TEST_QUERY_USER"; + $this->processExecutor->expects($this->at(1)) + ->method('execute') + ->with($this->equalTo($expectedCommand)) + ->will($this->returnValue(0)); + + $this->perforce->setP4User(null); + $this->perforce->queryP4user($io); + } + + public function testQueryP4UserStoresResponseToQueryForUserWithoutWindows(){ + $this->perforce->windows_flag = false; + + $io = $this->getMock('Composer\IO\IOInterface'); + $expectedQuestion = "Enter P4 User:"; + $io->expects($this->at(0)) + ->method('ask') + ->with($this->equalTo($expectedQuestion)) + ->will($this->returnValue("TEST_QUERY_USER")); + $expectedCommand = "export P4USER=TEST_QUERY_USER"; + $this->processExecutor->expects($this->at(1)) + ->method('execute') + ->with($this->equalTo($expectedCommand)) + ->will($this->returnValue(0)); + + $this->perforce->setP4User(null); + $this->perforce->queryP4user($io); + } + + public function testQueryP4PasswordWithPasswordAlreadySet(){ + $io = $this->getMock('Composer\IO\IOInterface'); + + $this->perforce->setP4Password("TEST_PASSWORD"); + $password = $this->perforce->testQueryP4Password($io); + $this->assertEquals("TEST_PASSWORD", $password); + } + + public function testQueryP4PasswordWithPasswordSetInP4VariablesWithWindowsOS(){ + $this->perforce->windows_flag = true; + + $io = $this->getMock('Composer\IO\IOInterface'); + $expectedCommand = "p4 set"; + $this->processExecutor->expects($this->at(0)) + ->method('execute') + ->with($this->equalTo($expectedCommand)) + ->will($this->returnCallback(function($command, &$output) {$output = "P4PASSWD=TEST_P4VARIABLE_PASSWORD\n"; return true;})); + + $this->perforce->setP4Password(null); + $password = $this->perforce->testQueryP4Password($io); + $this->assertEquals("TEST_P4VARIABLE_PASSWORD", $password); + } + + public function testQueryP4PasswordWithPasswordSetInP4VariablesNotWindowsOS(){ + $this->perforce->windows_flag = false; + + $io = $this->getMock('Composer\IO\IOInterface'); + $expectedCommand = 'echo $P4PASSWD'; + $this->processExecutor->expects($this->at(0)) + ->method('execute') + ->with($this->equalTo($expectedCommand)) + ->will($this->returnCallback(function($command, &$output) {$output = "TEST_P4VARIABLE_PASSWORD\n"; return true;})); + + $this->perforce->setP4Password(null); + $password = $this->perforce->testQueryP4Password($io); + $this->assertEquals("TEST_P4VARIABLE_PASSWORD", $password); + } + + public function testQueryP4PasswordQueriesForPassword(){ + $io = $this->getMock('Composer\IO\IOInterface'); + $expectedQuestion = "Enter password for Perforce user user: "; + $io->expects($this->at(0)) + ->method('askAndHideAnswer') + ->with($this->equalTo($expectedQuestion)) + ->will($this->returnValue("TEST_QUERY_PASSWORD")); + + $this->perforce->setP4Password(null); + $password = $this->perforce->testQueryP4Password($io); + $this->assertEquals("TEST_QUERY_PASSWORD", $password); + } + + public function testWriteP4ClientSpecWithoutStream() { + vfsStreamWrapper::register(); + VfsStreamWrapper::setRoot(new vfsStreamDirectory("path")); + $clientSpec = $this->perforce->testGetClientSpec(); + $this->perforce->writeP4ClientSpec(); + $spec = fopen(vfsStream::url($clientSpec), 'r'); + $expectedArray = $this->getExpectedClientSpec(FALSE); + try { + foreach ($expectedArray as $expected) { + $this->assertStringStartsWith($expected, fgets($spec)); + } + $this->assertFalse(fgets($spec)); + } catch (Exception $e) { + fclose($spec); + throw $e; + } + fclose($spec); + } + + public function testWriteP4ClientSpecWithStream() { + vfsStreamWrapper::register(); + VfsStreamWrapper::setRoot(new vfsStreamDirectory("path")); + $this->perforce->setStream("//depot/branching@label"); + $clientSpec = $this->perforce->testGetClientSpec(); + $this->perforce->writeP4ClientSpec(); + $spec = fopen(vfsStream::url($clientSpec), 'r'); + $expectedArray = $this->getExpectedClientSpec(TRUE); + try { + foreach ($expectedArray as $expected) { + $this->assertStringStartsWith($expected, fgets($spec)); + } + $this->assertFalse(fgets($spec)); + } catch (Exception $e) { + fclose($spec); + throw $e; + } + fclose($spec); + } + + public function testIsLoggedIn() { + $expectedCommand = $this->winCompat("p4 -u user -p port login -s"); + $this->processExecutor->expects($this->at(0)) + ->method('execute') + ->with($this->equalTo($expectedCommand), $this->equalTo(null)) + ->will($this->returnValue(0)); + + $this->perforce->testIsLoggedIn(); + } + + public function testConnectClient() { + $expectedCommand = $this->winCompat("p4 -u user -c composer_perforce_TEST_depot -p port client -i < path/composer_perforce_TEST_depot.p4.spec"); + $this->processExecutor->expects($this->at(0)) + ->method('execute') + ->with($this->equalTo($expectedCommand), $this->equalTo(null)) + ->will($this->returnValue(0)); + + $this->perforce->connectClient(); + } + + public function testGetBranchesWithStream() { + $expectedCommand = $this->winCompat("p4 -u user -c composer_perforce_TEST_depot_branchlabel -p port streams //depot/..."); + $this->processExecutor->expects($this->at(0)) + ->method('execute') + ->with($this->equalTo($expectedCommand)) + ->will($this->returnCallback(function($command, &$output) {$output = "Stream //depot/branch mainline none 'branch'\n"; return true;})); + + $this->perforce->setStream("//depot/branch@label"); + $branches = $this->perforce->getBranches(); + $this->assertEquals("//depot/branch", $branches['master']); + } + + public function testGetBranchesWithoutStream() { + $expectedCommand = $this->winCompat("p4 -u user -p port depots"); + $this->processExecutor->expects($this->at(0)) + ->method('execute') + ->with($this->equalTo($expectedCommand)) + ->will($this->returnCallback(function($command, &$output) {$output = "Depot depot 2013/01/28 local /path/to/depots/depot/... 'depot project'\n"; return true;})); + + $result = $this->perforce->checkStream("depot"); + $this->assertFalse($result); + + $branches = $this->perforce->getBranches(); + $this->assertEquals("//depot", $branches['master']); + } + + public function testGetTagsWithoutStream() { + $expectedCommand = $this->winCompat("p4 -u user -c composer_perforce_TEST_depot -p port labels"); + $this->processExecutor->expects($this->at(0)) + ->method('execute') + ->with($this->equalTo($expectedCommand)) + ->will($this->returnCallback(function($command, &$output) {$output = "Label 0.0.1 2013/07/31 'First Label!'\nLabel 0.0.2 2013/08/01 'Second Label!'\n"; return true;})); + + $tags = $this->perforce->getTags(); + $this->assertEquals("//depot@0.0.1", $tags['0.0.1']); + $this->assertEquals("//depot@0.0.2", $tags['0.0.2']); + } + + public function testGetTagsWithStream() { + $expectedCommand = $this->winCompat("p4 -u user -c composer_perforce_TEST_depot_branch -p port labels"); + $this->processExecutor->expects($this->at(0)) + ->method('execute') + ->with($this->equalTo($expectedCommand)) + ->will($this->returnCallback(function($command, &$output) {$output = "Label 0.0.1 2013/07/31 'First Label!'\nLabel 0.0.2 2013/08/01 'Second Label!'\n"; return true;})); + + $this->perforce->setStream("//depot/branch"); + $tags = $this->perforce->getTags(); + $this->assertEquals("//depot/branch@0.0.1", $tags['0.0.1']); + $this->assertEquals("//depot/branch@0.0.2", $tags['0.0.2']); + } + + public function testCheckStreamWithoutStream() { + $this->perforce->commandReturnValue = "Depot depot 2013/01/28 local /path/to/depots/depot/... 'depot project'"; + $result = $this->perforce->checkStream("depot"); + $this->assertFalse($result); + $this->assertFalse($this->perforce->testIsStream()); + } + + public function testCheckStreamWithStream() { + $line1 = "Depot depot 2013/01/28 branch /path/to/depots/depot/... 'depot project'\n"; + $line2 = "Depot depot 2013/01/28 development /path/to/depots/depot/... 'depot project'\n"; + $this->perforce->commandReturnValue = $line1 . $line2; + $result = $this->perforce->checkStream("depot"); + $this->assertFalse($result); + $this->assertFalse($this->perforce->testIsStream()); + } + + public function testGetComposerInformationWithoutLabelWithoutStream() { + $expectedCommand = $this->winCompat("p4 -u user -c composer_perforce_TEST_depot -p port print //depot/composer.json"); + $this->processExecutor->expects($this->at(0)) + ->method('execute') + ->with($this->equalTo($expectedCommand)) + ->will($this->returnCallback(function($command, &$output) {$output = PerforceTest::getComposerJson(); return true;})); + + $result = $this->perforce->getComposerInformation("//depot"); + $expected = array( + "name" => "test/perforce", + "description" => "Basic project for testing", + "minimum-stability" => "dev", + "autoload" => array("psr-0" => array()) + ); + $this->assertEquals($expected, $result); + } + + public function testGetComposerInformationWithLabelWithoutStream() { + $expectedCommand = $this->winCompat("p4 -u user -p port files //depot/composer.json@0.0.1"); + $this->processExecutor->expects($this->at(0)) + ->method('execute') + ->with($this->equalTo($expectedCommand)) + ->will($this->returnCallback(function($command, &$output) {$output = "//depot/composer.json#1 - branch change 10001 (text)"; return true;})); + + $expectedCommand = $this->winCompat("p4 -u user -c composer_perforce_TEST_depot -p port print //depot/composer.json@10001"); + $this->processExecutor->expects($this->at(1)) + ->method('execute') + ->with($this->equalTo($expectedCommand)) + ->will($this->returnCallback(function($command, &$output) {$output = PerforceTest::getComposerJson(); return true;})); + + $result = $this->perforce->getComposerInformation("//depot@0.0.1"); + + $expected = array( + "name" => "test/perforce", + "description" => "Basic project for testing", + "minimum-stability" => "dev", + "autoload" => array("psr-0" => array()) + ); + $this->assertEquals($expected, $result); + } + + public function testGetComposerInformationWithoutLabelWithStream() { + $expectedCommand = $this->winCompat("p4 -u user -c composer_perforce_TEST_depot_branch -p port print //depot/branch/composer.json"); + $this->processExecutor->expects($this->at(0)) + ->method('execute') + ->with($this->equalTo($expectedCommand)) + ->will($this->returnCallback(function($command, &$output) {$output = PerforceTest::getComposerJson(); return true;})); + + $this->perforce->setStream("//depot/branch"); + $result = $this->perforce->getComposerInformation("//depot/branch"); + + $expected = array( + "name" => "test/perforce", + "description" => "Basic project for testing", + "minimum-stability" => "dev", + "autoload" => array("psr-0" => array()) + ); + $this->assertEquals($expected, $result); + } + + public function testGetComposerInformationWithLabelWithStream() { + $expectedCommand = $this->winCompat("p4 -u user -p port files //depot/branch/composer.json@0.0.1"); + $this->processExecutor->expects($this->at(0)) + ->method('execute') + ->with($this->equalTo($expectedCommand)) + ->will($this->returnCallback(function($command, &$output) {$output = "//depot/composer.json#1 - branch change 10001 (text)"; return true;})); + + $expectedCommand = $this->winCompat("p4 -u user -c composer_perforce_TEST_depot_branch -p port print //depot/branch/composer.json@10001"); + $this->processExecutor->expects($this->at(1)) + ->method('execute') + ->with($this->equalTo($expectedCommand)) + ->will($this->returnCallback(function($command, &$output) {$output = PerforceTest::getComposerJson(); return true;})); + + $this->perforce->setStream("//depot/branch"); + $result = $this->perforce->getComposerInformation("//depot/branch@0.0.1"); + + $expected = array( + "name" => "test/perforce", + "description" => "Basic project for testing", + "minimum-stability" => "dev", + "autoload" => array("psr-0" => array()) + ); + $this->assertEquals($expected, $result); + } + + public function testSyncCodeBaseWithoutStream() { + $expectedCommand = $this->winCompat("p4 -u user -c composer_perforce_TEST_depot -p port sync -f @label"); + $this->processExecutor->expects($this->at(1)) + ->method('execute') + ->with($this->equalTo($expectedCommand), $this->equalTo(null)) + ->will($this->returnValue(0)); + + $this->perforce->syncCodeBase("label"); + } + + public function testSyncCodeBaseWithStream() { + $expectedCommand = $this->winCompat("p4 -u user -c composer_perforce_TEST_depot_branch -p port sync -f @label"); + $this->processExecutor->expects($this->at(1)) + ->method('execute') + ->with($this->equalTo($expectedCommand), $this->equalTo(null)) + ->will($this->returnValue(0)); + + $this->perforce->setStream("//depot/branch"); + $this->perforce->syncCodeBase("label"); + } + + public function testCheckServerExists() { + $processExecutor = $this->getMock('Composer\Util\ProcessExecutor'); + + $expectedCommand = $this->winCompat("p4 -p perforce.does.exist:port info -s"); + $processExecutor->expects($this->at(0)) + ->method('execute') + ->with($this->equalTo($expectedCommand), $this->equalTo(null)) + ->will($this->returnValue(0)); + + $result = $this->perforce->checkServerExists("perforce.does.exist:port", $processExecutor); + $this->assertTrue($result); + } + + public function testCheckServerExistsWithFailure() { + $processExecutor = $this->getMock('Composer\Util\ProcessExecutor'); + + $expectedCommand = $this->winCompat("p4 -p perforce.does.not.exist:port info -s"); + $processExecutor->expects($this->at(0)) + ->method('execute') + ->with($this->equalTo($expectedCommand), $this->equalTo(null)) + ->will($this->returnValue("Perforce client error:")); + + $result = $this->perforce->checkServerExists("perforce.does.not.exist:port", $processExecutor); + $this->assertTrue($result); + } + + public static function getComposerJson() { + $composer_json = array( + '{', + '"name": "test/perforce",', + '"description": "Basic project for testing",', + '"minimum-stability": "dev",', + '"autoload": {', + '"psr-0" : {', + '}', + '}', + '}' + ); + + return implode($composer_json); + } + + private function getExpectedClientSpec($withStream) { + $expectedArray = array( + "Client: composer_perforce_TEST_depot", + "\n", + "Update:", + "\n", + "Access:", + "Owner: user", + "\n", + "Description:", + " Created by user from composer.", + "\n", + "Root: path", + "\n", + "Options: noallwrite noclobber nocompress unlocked modtime rmdir", + "\n", + "SubmitOptions: revertunchanged", + "\n", + "LineEnd: local", + "\n" + ); + if ($withStream) { + $expectedArray[] = "Stream:"; + $expectedArray[] = " //depot/branching"; + } + else { + $expectedArray[] = "View: //depot/... //composer_perforce_TEST_depot/depot/..."; + } + + return $expectedArray; + } + + private function winCompat($cmd) { + if (defined('PHP_WINDOWS_VERSION_BUILD')) { + $cmd = str_replace('cd ', 'cd /D ', $cmd); + $cmd = str_replace('composerPath', getcwd() . '/composerPath', $cmd); + + return strtr($cmd, "'", '"'); + } + + return $cmd; + } + +} + diff --git a/tests/Composer/Test/Util/TestingPerforce.php b/tests/Composer/Test/Util/TestingPerforce.php new file mode 100644 index 000000000..dc21e8c33 --- /dev/null +++ b/tests/Composer/Test/Util/TestingPerforce.php @@ -0,0 +1,106 @@ +windows_flag = false; + } + /* + * Override functions + */ + protected function getRandomValue() { + return "TEST"; + } + protected function isWindows(){ + return $this->windows_flag; + } + +// protected function executeCommand($command) { +// $this->previousCommand = $this->lastCommand; +// $this->lastCommand = $command; +// $result = $this->commandReturnValue; +// $this->commandReturnValue = $this->nextCommandReturnValue; +// $this->nextCommandReturnValue = null; +// return $result; +// } + + public function writeP4ClientSpec() { + $spec = fopen(vfsStream::url($this->getP4ClientSpec()), 'w'); + $this->writeClientSpecToFile($spec); + fclose($spec); + } + + /* + * Test Helper functions + */ + public function setDepotType($depotType) { + $this->p4depotType = $depotType; + $this->p4stream = NULL; + } + + /* + * Functions to expose protected methods for testing: + */ + public function setP4User($p4user){ + $this->p4user = $p4user; + } + public function setP4Password($password){ + $this->p4password = $password; + } + + public function testGetClient() { + return $this->getClient(); + } + + public function testGetStream() { + return $this->getStream(); + } + + public function testGetStreamWithoutLabel() { + return $this->getStreamWithoutLabel(); + } + + public function testGetClientSpec() { + return $this->getP4ClientSpec(); + } + + public function testGenerateP4Command($command, $useClient = TRUE) { + return $this->generateP4Command($command, $useClient); + } + + public function testIsLoggedIn() + { + return $this->isLoggedIn(); + } + + public function testIsStream() + { + return $this->isStream(); + } + + public function testGetP4Variable($name) + { + return $this->testGetP4Variable($name); + } + + public function testQueryP4Password($io) + { + return $this->queryP4Password($io); + } +} \ No newline at end of file From 7fdcdb4c54bb22ac76794ba9e4e59831e4cd80cf Mon Sep 17 00:00:00 2001 From: matt-whittom Date: Mon, 12 Aug 2013 13:28:09 -0500 Subject: [PATCH 11/26] Updated to complete functionality in Downloader to handle updates, returning change logs. --- .../Downloader/PerforceDownloader.php | 15 +++++---- src/Composer/Util/Perforce.php | 31 +++++++++++++++++++ 2 files changed, 40 insertions(+), 6 deletions(-) diff --git a/src/Composer/Downloader/PerforceDownloader.php b/src/Composer/Downloader/PerforceDownloader.php index 5ed241d79..c7ca64ec1 100644 --- a/src/Composer/Downloader/PerforceDownloader.php +++ b/src/Composer/Downloader/PerforceDownloader.php @@ -22,6 +22,7 @@ use Composer\Util\Perforce; class PerforceDownloader extends VcsDownloader { protected $perforce; + protected $perforceInjected = false; /** * {@inheritDoc} @@ -31,6 +32,7 @@ class PerforceDownloader extends VcsDownloader $ref = $package->getSourceReference(); $label = $package->getPrettyVersion(); + $this->io->write(" Cloning ".$ref); $this->initPerforce($package, $path); $this->perforce->setStream($ref); $this->perforce->queryP4User($this->io); @@ -40,7 +42,7 @@ class PerforceDownloader extends VcsDownloader } private function initPerforce($package, $path){ - if (isset($this->perforce)){ + if ($this->perforceInjected){ return; } $repository = $package->getRepository(); @@ -50,6 +52,7 @@ class PerforceDownloader extends VcsDownloader public function injectPerforce($perforce){ $this->perforce = $perforce; + $this->perforceInjected = true; } private function getRepoConfig(VcsRepository $repository){ @@ -62,7 +65,7 @@ class PerforceDownloader extends VcsDownloader public function doUpdate(PackageInterface $initial, PackageInterface $target, $path) { print("PerforceDownloader:doUpdate\n"); - throw new Exception("Unsupported Operation: PerforceDownloader:doUpdate"); + $this->doDownload($target, $path); } /** @@ -70,8 +73,8 @@ class PerforceDownloader extends VcsDownloader */ public function getLocalChanges($path) { - print("PerforceDownloader:getLocalChanges\n"); - throw new Exception("Unsupported Operation: PerforceDownloader:getLocalChanges"); + print ("Perforce driver does not check for local changes before overriding\n"); + return; } @@ -80,8 +83,8 @@ class PerforceDownloader extends VcsDownloader */ protected function getCommitLogs($fromReference, $toReference, $path) { - print("PerforceDownloader:getCommitLogs\n"); - throw new Exception("Unsupported Operation: PerforceDownloader:getCommitLogs"); + $commitLogs = $this->perforce->getCommitLogs($fromReference, $toReference); + return $commitLogs; } } diff --git a/src/Composer/Util/Perforce.php b/src/Composer/Util/Perforce.php index 5b032b71b..7a9e26643 100644 --- a/src/Composer/Util/Perforce.php +++ b/src/Composer/Util/Perforce.php @@ -437,4 +437,35 @@ class Perforce { return FALSE; } + + protected function getChangeList($reference){ + $index = strpos($reference, "@"); + if ($index === false){ + return; + } + $label = substr($reference, $index); + $command = $this->generateP4Command(" changes -m1 $label"); + $changes = $this->executeCommand($command); + if (strpos($changes, "Change") !== 0){ + return; + } + $fields = explode(" ", $changes); + $changeList = $fields[1]; + return $changeList; + } + public function getCommitLogs($fromReference, $toReference){ + $fromChangeList = $this->getChangeList($fromReference); + if ($fromChangeList == null){ + return; + } + $toChangeList = $this->getChangeList($toReference); + if ($toChangeList == null){ + return; + } + $index = strpos($fromReference, "@"); + $main = substr($fromReference, 0, $index) . "/..."; + $command = $this->generateP4Command("filelog $main@$fromChangeList,$toChangeList"); + $result = $this->executeCommand($command); + return $result; + } } \ No newline at end of file From 997b1b5d46aa69f64b9c9ff2a92ab8bf84d03890 Mon Sep 17 00:00:00 2001 From: matt-whittom Date: Tue, 13 Aug 2013 10:04:47 -0500 Subject: [PATCH 12/26] Replaced changes in RemoteFilesystemTest with changes from main branch. --- .../Test/Util/RemoteFilesystemTest.php | 49 ++++++++++--------- 1 file changed, 27 insertions(+), 22 deletions(-) diff --git a/tests/Composer/Test/Util/RemoteFilesystemTest.php b/tests/Composer/Test/Util/RemoteFilesystemTest.php index f2950a2da..39a3bb63d 100644 --- a/tests/Composer/Test/Util/RemoteFilesystemTest.php +++ b/tests/Composer/Test/Util/RemoteFilesystemTest.php @@ -21,9 +21,9 @@ class RemoteFilesystemTest extends \PHPUnit_Framework_TestCase { $io = $this->getMock('Composer\IO\IOInterface'); $io - ->expects($this->once()) - ->method('hasAuthentication') - ->will($this->returnValue(false)) + ->expects($this->once()) + ->method('hasAuthentication') + ->will($this->returnValue(false)) ; $res = $this->callGetOptionsForUrl($io, array('http://example.org', array())); @@ -42,14 +42,14 @@ class RemoteFilesystemTest extends \PHPUnit_Framework_TestCase { $io = $this->getMock('Composer\IO\IOInterface'); $io - ->expects($this->once()) - ->method('hasAuthentication') - ->will($this->returnValue(true)) + ->expects($this->once()) + ->method('hasAuthentication') + ->will($this->returnValue(true)) ; $io - ->expects($this->once()) - ->method('getAuthentication') - ->will($this->returnValue(array('username' => 'login', 'password' => 'password'))) + ->expects($this->once()) + ->method('getAuthentication') + ->will($this->returnValue(array('username' => 'login', 'password' => 'password'))) ; $options = $this->callGetOptionsForUrl($io, array('http://example.org', array())); @@ -67,9 +67,9 @@ class RemoteFilesystemTest extends \PHPUnit_Framework_TestCase { $io = $this->getMock('Composer\IO\IOInterface'); $io - ->expects($this->once()) - ->method('hasAuthentication') - ->will($this->returnValue(true)) + ->expects($this->once()) + ->method('hasAuthentication') + ->will($this->returnValue(true)) ; $streamOptions = array('ssl' => array( @@ -84,9 +84,9 @@ class RemoteFilesystemTest extends \PHPUnit_Framework_TestCase { $io = $this->getMock('Composer\IO\IOInterface'); $io - ->expects($this->once()) - ->method('hasAuthentication') - ->will($this->returnValue(true)) + ->expects($this->once()) + ->method('hasAuthentication') + ->will($this->returnValue(true)) ; $streamOptions = array('http' => array( @@ -118,8 +118,8 @@ class RemoteFilesystemTest extends \PHPUnit_Framework_TestCase { $io = $this->getMock('Composer\IO\IOInterface'); $io - ->expects($this->once()) - ->method('overwrite') + ->expects($this->once()) + ->method('overwrite') ; $fs = new RemoteFilesystem($io); @@ -148,13 +148,18 @@ class RemoteFilesystemTest extends \PHPUnit_Framework_TestCase public function testCaptureAuthenticationParamsFromUrl() { $io = $this->getMock('Composer\IO\IOInterface'); - $io - ->expects($this->once()) + $io->expects($this->once()) ->method('setAuthentication') - ; + ->with($this->equalTo('example.com'), $this->equalTo('user'), $this->equalTo('pass')); $fs = new RemoteFilesystem($io); - $fs->getContents('example.com', 'http://user:pass@www.example.com'); + try { + $fs->getContents('example.com', 'http://user:pass@www.example.com/something'); + } catch (\Exception $e) { + $this->assertInstanceOf('Composer\Downloader\TransportException', $e); + $this->assertEquals(404, $e->getCode()); + $this->assertContains('404 Not Found', $e->getMessage()); + } } public function testGetContents() @@ -197,4 +202,4 @@ class RemoteFilesystemTest extends \PHPUnit_Framework_TestCase $attr->setAccessible(true); $attr->setValue($object, $value); } -} +} \ No newline at end of file From dd81449998f86bc0028bfd7424fa08ee92c62cbd Mon Sep 17 00:00:00 2001 From: matt-whittom Date: Thu, 15 Aug 2013 12:16:15 -0500 Subject: [PATCH 13/26] Updated to remove vfsStreams (no longer needed by tests) Updated Perforce to cleanup, modified VcsRepository to call cleanup on Driver class Updated tests, perforce constructor to make tests cleaner/easier --- composer.json | 3 +- composer.lock | 97 ++++---- .../Downloader/PerforceDownloader.php | 12 +- .../Repository/Vcs/PerforceDriver.php | 40 ++- src/Composer/Repository/Vcs/VcsDriver.php | 8 + .../Repository/Vcs/VcsDriverInterface.php | 6 + src/Composer/Repository/VcsRepository.php | 1 + src/Composer/Util/Perforce.php | 183 ++++++++------ .../Downloader/PerforceDownloaderTest.php | 3 +- .../Repository/Vcs/PerforceDriverTest.php | 15 +- .../Repository/Vcs/TestingPerforceDriver.php | 26 -- tests/Composer/Test/Util/PerforceTest.php | 232 +++++++++--------- tests/Composer/Test/Util/TestingPerforce.php | 106 -------- 13 files changed, 333 insertions(+), 399 deletions(-) delete mode 100644 tests/Composer/Test/Repository/Vcs/TestingPerforceDriver.php delete mode 100644 tests/Composer/Test/Util/TestingPerforce.php diff --git a/composer.json b/composer.json index 77cc9590f..f0d4ee56e 100644 --- a/composer.json +++ b/composer.json @@ -30,8 +30,7 @@ "symfony/process": "~2.1" }, "require-dev": { - "phpunit/phpunit": "~3.7.10", - "mikey179/vfsStream" : "1.2.*" + "phpunit/phpunit": "~3.7.10" }, "suggest": { "ext-zip": "Enabling the zip extension allows you to unzip archives, and allows gzip compression of all internet traffic", diff --git a/composer.lock b/composer.lock index 5a23476b4..b6ff8ec60 100644 --- a/composer.lock +++ b/composer.lock @@ -3,7 +3,7 @@ "This file locks the dependencies of your project to a known state", "Read more about it at http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file" ], - "hash": "6203fdb419c10ffd84f85611fc9eec61", + "hash": "370b764a9317165e8ea7a2e1623e031b", "packages": [ { "name": "justinrainbow/json-schema", @@ -15,7 +15,7 @@ }, "dist": { "type": "zip", - "url": "https://github.com/justinrainbow/json-schema/zipball/v1.1.0", + "url": "https://github.com/justinrainbow/json-schema/archive/v1.1.0.zip", "reference": "v1.1.0", "shasum": "" }, @@ -28,7 +28,37 @@ "JsonSchema": "src/" } }, - "time": "2012-01-02 21:33:17" + "notification-url": "https://packagist.org/downloads/", + "license": [ + "NewBSD" + ], + "authors": [ + { + "name": "Igor Wiedler", + "email": "igor@wiedler.ch", + "homepage": "http://wiedler.ch/igor/" + }, + { + "name": "Bruno Prieto Reis", + "email": "bruno.p.reis@gmail.com" + }, + { + "name": "Justin Rainbow", + "email": "justin.rainbow@gmail.com" + }, + { + "name": "Robert Schönthal", + "email": "seroscho@googlemail.com", + "homepage": "http://digitalkaoz.net" + } + ], + "description": "A library to validate a json schema.", + "homepage": "https://github.com/justinrainbow/json-schema", + "keywords": [ + "json", + "schema" + ], + "time": "2012-01-03 00:33:17" }, { "name": "seld/jsonlint", @@ -79,17 +109,17 @@ }, { "name": "symfony/console", - "version": "dev-master", + "version": "v2.3.3", "target-dir": "Symfony/Component/Console", "source": { "type": "git", "url": "https://github.com/symfony/Console.git", - "reference": "872a494b88fba2f62be85e0bc8441e7946bb6ba6" + "reference": "v2.3.3" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/Console/zipball/872a494b88fba2f62be85e0bc8441e7946bb6ba6", - "reference": "872a494b88fba2f62be85e0bc8441e7946bb6ba6", + "url": "https://api.github.com/repos/symfony/Console/zipball/v2.3.3", + "reference": "v2.3.3", "shasum": "" }, "require": { @@ -104,7 +134,7 @@ "type": "library", "extra": { "branch-alias": { - "dev-master": "2.4-dev" + "dev-master": "2.3-dev" } }, "autoload": { @@ -128,7 +158,7 @@ ], "description": "Symfony Console Component", "homepage": "http://symfony.com", - "time": "2013-08-09 06:00:31" + "time": "2013-07-21 12:12:18" }, { "name": "symfony/finder", @@ -179,17 +209,17 @@ }, { "name": "symfony/process", - "version": "dev-master", + "version": "v2.3.3", "target-dir": "Symfony/Component/Process", "source": { "type": "git", "url": "https://github.com/symfony/Process.git", - "reference": "723fe405fcc878ae75469babcb9507d292797ece" + "reference": "v2.3.3" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/Process/zipball/723fe405fcc878ae75469babcb9507d292797ece", - "reference": "723fe405fcc878ae75469babcb9507d292797ece", + "url": "https://api.github.com/repos/symfony/Process/zipball/v2.3.3", + "reference": "v2.3.3", "shasum": "" }, "require": { @@ -198,7 +228,7 @@ "type": "library", "extra": { "branch-alias": { - "dev-master": "2.4-dev" + "dev-master": "2.3-dev" } }, "autoload": { @@ -222,40 +252,10 @@ ], "description": "Symfony Process Component", "homepage": "http://symfony.com", - "time": "2013-08-09 07:03:52" + "time": "2013-08-02 21:51:01" } ], "packages-dev": [ - { - "name": "mikey179/vfsStream", - "version": "v1.2.0", - "source": { - "type": "git", - "url": "https://github.com/mikey179/vfsStream.git", - "reference": "v1.2.0" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/mikey179/vfsStream/zipball/v1.2.0", - "reference": "v1.2.0", - "shasum": "" - }, - "require": { - "php": ">=5.3.0" - }, - "type": "library", - "autoload": { - "psr-0": { - "org\\bovigo\\vfs\\": "src/main/php" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "BSD" - ], - "homepage": "http://vfs.bovigo.org/", - "time": "2013-04-01 10:41:02" - }, { "name": "phpunit/php-code-coverage", "version": "1.2.12", @@ -675,10 +675,9 @@ ], "minimum-stability": "stable", - "stability-flags": { - "symfony/console": 20, - "symfony/process": 20 - }, + "stability-flags": [ + + ], "platform": { "php": ">=5.3.2" }, diff --git a/src/Composer/Downloader/PerforceDownloader.php b/src/Composer/Downloader/PerforceDownloader.php index c7ca64ec1..5cede4ff6 100644 --- a/src/Composer/Downloader/PerforceDownloader.php +++ b/src/Composer/Downloader/PerforceDownloader.php @@ -6,10 +6,14 @@ * (c) Nils Adermann * Jordi Boggiano * + * Contributor: Matt Whittom + * Date: 7/17/13 + * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ + namespace Composer\Downloader; use Composer\Package\PackageInterface; @@ -17,7 +21,7 @@ use Composer\Repository\VcsRepository; use Composer\Util\Perforce; /** - * @author Jordi Boggiano + * @author Matt Whittom */ class PerforceDownloader extends VcsDownloader { @@ -39,6 +43,7 @@ class PerforceDownloader extends VcsDownloader $this->perforce->writeP4ClientSpec(); $this->perforce->connectClient(); $this->perforce->syncCodeBase($label); + $this->perforce->cleanupClientSpec(); } private function initPerforce($package, $path){ @@ -47,7 +52,7 @@ class PerforceDownloader extends VcsDownloader } $repository = $package->getRepository(); $repoConfig = $this->getRepoConfig($repository); - $this->perforce = new Perforce($repoConfig, $package->getSourceUrl(), $path); + $this->perforce = Perforce::createPerforce($repoConfig, $package->getSourceUrl(), $path); } public function injectPerforce($perforce){ @@ -64,14 +69,13 @@ class PerforceDownloader extends VcsDownloader */ public function doUpdate(PackageInterface $initial, PackageInterface $target, $path) { - print("PerforceDownloader:doUpdate\n"); $this->doDownload($target, $path); } /** * {@inheritDoc} */ - public function getLocalChanges($path) + public function getLocalChanges(PackageInterface $package, $path) { print ("Perforce driver does not check for local changes before overriding\n"); return; diff --git a/src/Composer/Repository/Vcs/PerforceDriver.php b/src/Composer/Repository/Vcs/PerforceDriver.php index 0f930a52b..7068ebb3e 100644 --- a/src/Composer/Repository/Vcs/PerforceDriver.php +++ b/src/Composer/Repository/Vcs/PerforceDriver.php @@ -6,13 +6,14 @@ * (c) Nils Adermann * Jordi Boggiano * - * Contributor: matt-whittom + * Contributor: Matt Whittom * Date: 7/17/13 * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ + namespace Composer\Repository\Vcs; use Composer\IO\IOInterface; @@ -21,12 +22,14 @@ use Composer\Util\Filesystem; use Composer\Util\Perforce; /** - * @author matt-whittom <> + * @author Matt Whittom */ class PerforceDriver extends VcsDriver { protected $depot; protected $branch; protected $perforce; + protected $composer_info; + protected $composer_info_identifier; /** * {@inheritDoc} @@ -54,7 +57,7 @@ class PerforceDriver extends VcsDriver { } $repoDir = $this->config->get('cache-vcs-dir') . "/$this->depot"; - $this->perforce = new Perforce($this->repoConfig, $this->getUrl(), $repoDir, $this->process); + $this->perforce = Perforce::createPerforce($this->repoConfig, $this->getUrl(), $repoDir, $this->process); } public function injectPerforce(Perforce $perforce) { @@ -66,6 +69,12 @@ class PerforceDriver extends VcsDriver { * {@inheritDoc} */ public function getComposerInformation($identifier) { + if (isset($this->composer_info_identifier)){ + if (strcmp($identifier, $this->composer_info_identifier) === 0 ) + { + return $this->composer_info; + } + } $composer_info = $this->perforce->getComposerInformation($identifier); return $composer_info; @@ -127,9 +136,12 @@ class PerforceDriver extends VcsDriver { * {@inheritDoc} */ public function hasComposerFile($identifier) { - $composer_info = $this->perforce->getComposerInformation("//$this->depot/$identifier"); - $result = strlen(trim($composer_info)) > 0; - + $this->composer_info = $this->perforce->getComposerInformation("//$this->depot/$identifier"); + $this->composer_info_identifier = $identifier; + $result = false; + if (isset($this->composer_info)){ + $result = count($this->composer_info) > 0; + } return $result; } @@ -146,4 +158,20 @@ class PerforceDriver extends VcsDriver { public static function supports(IOInterface $io, $url, $deep = FALSE) { return Perforce::checkServerExists($url, new ProcessExecutor); } + + /** + * {@inheritDoc} + */ + public function cleanup(){ + $this->perforce->cleanupClientSpec(); + $this->perforce = null; + } + + public function getDepot(){ + return $this->depot; + } + + public function getBranch(){ + return $this->branch; + } } diff --git a/src/Composer/Repository/Vcs/VcsDriver.php b/src/Composer/Repository/Vcs/VcsDriver.php index f6d428802..1a42e0927 100644 --- a/src/Composer/Repository/Vcs/VcsDriver.php +++ b/src/Composer/Repository/Vcs/VcsDriver.php @@ -105,4 +105,12 @@ abstract class VcsDriver implements VcsDriverInterface { return (bool) preg_match('{^(file://|/|[a-z]:[\\\\/])}i', $url); } + + /** + * {@inheritDoc} + */ + public function cleanup() + { + return; + } } diff --git a/src/Composer/Repository/Vcs/VcsDriverInterface.php b/src/Composer/Repository/Vcs/VcsDriverInterface.php index 44486f007..b29841b68 100644 --- a/src/Composer/Repository/Vcs/VcsDriverInterface.php +++ b/src/Composer/Repository/Vcs/VcsDriverInterface.php @@ -81,6 +81,12 @@ interface VcsDriverInterface */ public function hasComposerFile($identifier); + /** + * Performs any cleanup necessary as the driver is not longer needed + * + */ + public function cleanup(); + /** * Checks if this driver can handle a given url * diff --git a/src/Composer/Repository/VcsRepository.php b/src/Composer/Repository/VcsRepository.php index 2b876bc22..701db33bb 100644 --- a/src/Composer/Repository/VcsRepository.php +++ b/src/Composer/Repository/VcsRepository.php @@ -253,6 +253,7 @@ class VcsRepository extends ArrayRepository continue; } } + $driver->cleanup(); if (!$verbose) { $this->io->overwrite('', false); diff --git a/src/Composer/Util/Perforce.php b/src/Composer/Util/Perforce.php index 7a9e26643..6acee208f 100644 --- a/src/Composer/Util/Perforce.php +++ b/src/Composer/Util/Perforce.php @@ -1,60 +1,91 @@ + * Jordi Boggiano + * + * Contributor: Matt Whittom + * Date: 7/17/13 + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. */ + namespace Composer\Util; use Composer\IO\IOInterface; - +/** + * @author Matt Whittom + */ class Perforce { protected $path; - protected $p4client; - protected $p4user; - protected $p4password; - protected $p4port; - protected $p4stream; - protected $p4clientSpec; - protected $p4depotType; - protected $p4branch; + protected $p4Depot; + protected $p4Client; + protected $p4User; + protected $p4Password; + protected $p4Port; + protected $p4Stream; + protected $p4ClientSpec; + protected $p4DepotType; + protected $p4Branch; protected $process; + protected $unique_perforce_client_name; + protected $windowsFlag; - public function __construct($repoConfig, $port, $path, ProcessExecutor $process = NULL) { - $this->p4port = $port; + + public static function createPerforce($repoConfig, $port, $path, ProcessExecutor $process = NULL) { + if (!isset($process)){ + $process = new ProcessExecutor; + } + $isWindows = defined('PHP_WINDOWS_VERSION_BUILD'); + if (isset($repoConfig['unique_perforce_client_name'])){ + $unique_perforce_client_name = $repoConfig['unique_perforce_client_name']; + } else { + $unique_perforce_client_name = gethostname() . "_" . time(); + $repoConfig['unique_perforce_client_name'] = $unique_perforce_client_name; + } + + $perforce = new Perforce($repoConfig, $port, $path, $process, $isWindows, $unique_perforce_client_name); + return $perforce; + } + + public function __construct($repoConfig, $port, $path, ProcessExecutor $process, $isWindows, $unique_perforce_client_name) { + $this->windowsFlag = $isWindows; + $this->unique_perforce_client_name = $unique_perforce_client_name; + $this->p4Port = $port; $this->path = $path; - $this->process = $process ? : new ProcessExecutor; $fs = new Filesystem(); $fs->ensureDirectoryExists($path); + $this->process = $process; if (isset($repoConfig['depot'])) { - $this->p4depot = $repoConfig['depot']; + $this->p4Depot = $repoConfig['depot']; } if (isset($repoConfig['branch'])) { - $this->p4branch = $repoConfig['branch']; + $this->p4Branch = $repoConfig['branch']; } if (isset($repoConfig['p4user'])) { - $this->p4user = $repoConfig['p4user']; + $this->p4User = $repoConfig['p4user']; } else { - $this->p4user = $this->getP4variable("P4USER"); + $this->p4User = $this->getP4variable("P4USER"); } if (isset($repoConfig['p4password'])) { - $this->p4password = $repoConfig['p4password']; + $this->p4Password = $repoConfig['p4password']; } } - protected function getRandomValue() { - return mt_rand(1000, 9999); - } - - protected function isWindows(){ - return defined('PHP_WINDOWS_VERSION_BUILD'); + public function cleanupClientSpec(){ + $client = $this->getClient(); + $command = "p4 client -d $client"; + $this->executeCommand($command); + $clientSpec = $this->getP4ClientSpec(); + $fileSystem = new FileSystem($this->process); + $fileSystem->remove($clientSpec); } protected function executeCommand($command) { @@ -64,14 +95,13 @@ class Perforce { return $result; } - protected function getClient() { - if (!isset($this->p4client)) { - $random_value = $this->getRandomValue(); + public function getClient() { + if (!isset($this->p4Client)) { $clean_stream_name = str_replace("@", "", str_replace("/", "_", str_replace("//", "", $this->getStream()))); - $this->p4client = "composer_perforce_" . $random_value . "_" . $clean_stream_name; + $this->p4Client = "composer_perforce_" . $this->unique_perforce_client_name . "_" . $clean_stream_name; } - return $this->p4client; + return $this->p4Client; } protected function getPath() { @@ -79,28 +109,31 @@ class Perforce { } protected function getPort() { - return $this->p4port; + return $this->p4Port; } - protected function isStream() { - return (strcmp($this->p4depotType, "stream") === 0); + public function setStream($stream) { + $this->p4Stream = $stream; + $this->p4DepotType = "stream"; } - protected function getStream() { - if (!isset($this->p4stream)) { + public function isStream() { + return (strcmp($this->p4DepotType, "stream") === 0); + } + + public function getStream() { + if (!isset($this->p4Stream)) { if ($this->isStream()) { - $this->p4stream = "//$this->p4depot/$this->p4branch"; + $this->p4Stream = "//$this->p4Depot/$this->p4Branch"; } else { - $this->p4stream = "//$this->p4depot"; + $this->p4Stream = "//$this->p4Depot"; } } - - return $this->p4stream; + return $this->p4Stream; } - protected function getStreamWithoutLabel() { - $stream = $this->getStream(); + public function getStreamWithoutLabel($stream) { $index = strpos($stream, "@"); if ($index === FALSE) { return $stream; @@ -109,37 +142,37 @@ class Perforce { return substr($stream, 0, $index); } - protected function getP4ClientSpec() { + public function getP4ClientSpec() { $p4clientSpec = $this->path . "/" . $this->getClient() . ".p4.spec"; return $p4clientSpec; } public function getUser() { - return $this->p4user; + return $this->p4User; } public function queryP4User(IOInterface $io) { $this->getUser(); - if (strlen($this->p4user) > 0) { + if (strlen($this->p4User) > 0) { return; } - $this->p4user = $this->getP4variable("P4USER"); - if (strlen($this->p4user) > 0) { + $this->p4User = $this->getP4variable("P4USER"); + if (strlen($this->p4User) > 0) { return; } - $this->p4user = $io->ask("Enter P4 User:"); - if ($this->isWindows()) { - $command = "p4 set P4USER=$this->p4user"; + $this->p4User = $io->ask("Enter P4 User:"); + if ($this->windowsFlag) { + $command = "p4 set P4USER=$this->p4User"; } else { - $command = "export P4USER=$this->p4user"; + $command = "export P4USER=$this->p4User"; } $result = $this->executeCommand($command); } protected function getP4variable($name) { - if ($this->isWindows()) { + if ($this->windowsFlag) { $command = "p4 set"; $result = $this->executeCommand($command); $resArray = explode("\n", $result); @@ -167,20 +200,20 @@ class Perforce { } } - protected function queryP4Password(IOInterface $io) { - if (isset($this->p4password)) { - return $this->p4password; + public function queryP4Password(IOInterface $io) { + if (isset($this->p4Password)) { + return $this->p4Password; } $password = $this->getP4variable("P4PASSWD"); if (strlen($password) <= 0) { $password = $io->askAndHideAnswer("Enter password for Perforce user " . $this->getUser() . ": "); } - $this->p4password = $password; + $this->p4Password = $password; return $password; } - protected function generateP4Command($command, $useClient = TRUE) { + public function generateP4Command($command, $useClient = TRUE) { $p4Command = "p4 "; $p4Command = $p4Command . "-u " . $this->getUser() . " "; if ($useClient) { @@ -192,22 +225,16 @@ class Perforce { return $p4Command; } - protected function isLoggedIn() { + public function isLoggedIn() { $command = $this->generateP4Command("login -s", FALSE); $result = trim($this->executeCommand($command)); $index = strpos($result, $this->getUser()); if ($index === FALSE) { return FALSE; } - return TRUE; } - public function setStream($stream) { - $this->p4stream = $stream; - $this->p4depotType = "stream"; - } - public function connectClient() { $p4CreateClientCommand = $this->generateP4Command("client -i < " . $this->getP4ClientSpec()); $this->executeCommand($p4CreateClientCommand); @@ -230,7 +257,7 @@ class Perforce { chdir($prevDir); } - protected function writeClientSpecToFile($spec) { + public function writeClientSpecToFile($spec) { fwrite($spec, "Client: " . $this->getClient() . "\n\n"); fwrite($spec, "Update: " . date("Y/m/d H:i:s") . "\n\n"); fwrite($spec, "Access: " . date("Y/m/d H:i:s") . "\n"); @@ -243,7 +270,7 @@ class Perforce { fwrite($spec, "LineEnd: local\n\n"); if ($this->isStream()) { fwrite($spec, "Stream:\n"); - fwrite($spec, " " . $this->getStreamWithoutLabel() . "\n"); + fwrite($spec, " " . $this->getStreamWithoutLabel($this->p4Stream) . "\n"); } else { fwrite( @@ -255,7 +282,8 @@ class Perforce { } public function writeP4ClientSpec() { - $spec = fopen($this->getP4ClientSpec(), 'w'); + $clientSpec = $this->getP4ClientSpec(); + $spec = fopen($clientSpec, 'w'); try { $this->writeClientSpecToFile($spec); } catch (Exception $e) { @@ -308,7 +336,7 @@ class Perforce { $this->queryP4User($io); if (!$this->isLoggedIn()) { $password = $this->queryP4Password($io); - if ($this->isWindows()) { + if ($this->windowsFlag) { $this->windowsLogin($password); } else { @@ -382,12 +410,11 @@ class Perforce { public function getBranches() { $possible_branches = array(); if (!$this->isStream()) { - $possible_branches[$this->p4branch] = $this->getStream(); + $possible_branches[$this->p4Branch] = $this->getStream(); } else { - $command = $this->generateP4Command("streams //$this->p4depot/..."); - $result = ""; - $this->process->execute($command, $result); + $command = $this->generateP4Command("streams //$this->p4Depot/..."); + $result = $this->executeCommand($command); $resArray = explode("\n", $result); foreach ($resArray as $line) { $resBits = explode(" ", $line); @@ -398,7 +425,7 @@ class Perforce { } } $branches = array(); - $branches['master'] = $possible_branches[$this->p4branch]; + $branches['master'] = $possible_branches[$this->p4Branch]; return $branches; } @@ -427,8 +454,8 @@ class Perforce { $index = strpos($line, "Depot"); if (!($index === FALSE)) { $fields = explode(" ", $line); - if (strcmp($this->p4depot, $fields[1]) === 0) { - $this->p4depotType = $fields[3]; + if (strcmp($this->p4Depot, $fields[1]) === 0) { + $this->p4DepotType = $fields[3]; return $this->isStream(); } diff --git a/tests/Composer/Test/Downloader/PerforceDownloaderTest.php b/tests/Composer/Test/Downloader/PerforceDownloaderTest.php index 66ed81776..634cc5f18 100644 --- a/tests/Composer/Test/Downloader/PerforceDownloaderTest.php +++ b/tests/Composer/Test/Downloader/PerforceDownloaderTest.php @@ -60,7 +60,8 @@ class PerforceDownloaderTest extends \PHPUnit_Framework_TestCase { $repoConfig = array("depot"=>"TEST_DEPOT", "branch"=>"TEST_BRANCH", "p4user"=>"TEST_USER"); $port = "TEST_PORT"; $path = "TEST_PATH"; - $perforce = $this->getMock('Composer\Util\Perforce', array('setStream', 'queryP4User', 'writeP4ClientSpec', 'connectClient', 'syncCodeBase'), array($repoConfig, $port, $path)); + $process = $this->getmock('Composer\Util\ProcessExecutor'); + $perforce = $this->getMock('Composer\Util\Perforce', array('setStream', 'queryP4User', 'writeP4ClientSpec', 'connectClient', 'syncCodeBase'), array($repoConfig, $port, $path, $process, true, "TEST")); $ref = "SOURCE_REF"; $label = "LABEL"; $perforce->expects($this->at(0)) diff --git a/tests/Composer/Test/Repository/Vcs/PerforceDriverTest.php b/tests/Composer/Test/Repository/Vcs/PerforceDriverTest.php index db1485836..ed14b077e 100644 --- a/tests/Composer/Test/Repository/Vcs/PerforceDriverTest.php +++ b/tests/Composer/Test/Repository/Vcs/PerforceDriverTest.php @@ -48,9 +48,6 @@ class PerforceDriverTest extends \PHPUnit_Framework_TestCase { $fs->removeDirectory($this->testPath); } - //Test: - //hasComposerFile - public function testInitializeCapturesVariablesFromRepoConfig() { $this->setUp(); $repo_config = array( @@ -58,8 +55,9 @@ class PerforceDriverTest extends \PHPUnit_Framework_TestCase { 'depot' => 'TEST_DEPOT_CONFIG', 'branch' => 'TEST_BRANCH_CONFIG' ); - $driver = new TestingPerforceDriver($repo_config, $this->io, $this->config, $this->process, $this->remoteFileSystem); - $arguments = array(array('depot'=>'TEST_DEPOT', 'branch'=>'TEST_BRANCH'), 'port'=>'TEST_PORT', 'path'=>$this->testPath); + $driver = new PerforceDriver($repo_config, $this->io, $this->config, $this->process, $this->remoteFileSystem); + $process = $this->getMock('Composer\Util\ProcessExecutor'); + $arguments = array(array('depot'=>'TEST_DEPOT', 'branch'=>'TEST_BRANCH'), 'port'=>'TEST_PORT', 'path'=>$this->testPath, $process, true, "TEST"); $perforce = $this->getMock('Composer\Util\Perforce', null, $arguments); $driver->injectPerforce($perforce); $driver->initialize(); @@ -75,7 +73,7 @@ class PerforceDriverTest extends \PHPUnit_Framework_TestCase { 'depot' => 'TEST_DEPOT_CONFIG', 'branch' => 'TEST_BRANCH_CONFIG' ); - $driver = new TestingPerforceDriver($repo_config, $this->io, $this->config, $this->process, $this->remoteFileSystem); + $driver = new PerforceDriver($repo_config, $this->io, $this->config, $this->process, $this->remoteFileSystem); $perforce = $this->getMockBuilder('Composer\Util\Perforce')->disableOriginalConstructor()->getMock(); $perforce->expects($this->at(0)) ->method('p4Login') @@ -99,8 +97,9 @@ class PerforceDriverTest extends \PHPUnit_Framework_TestCase { 'depot' => 'TEST_DEPOT_CONFIG', 'branch' => 'TEST_BRANCH_CONFIG' ); - $driver = new TestingPerforceDriver($repo_config, $this->io, $this->config, $this->process, $this->remoteFileSystem); - $arguments = array(array('depot'=>'TEST_DEPOT', 'branch'=>'TEST_BRANCH'), 'port'=>'TEST_PORT', 'path'=>$this->testPath); + $driver = new PerforceDriver($repo_config, $this->io, $this->config, $this->process, $this->remoteFileSystem); + $process = $this->getMock('Composer\Util\ProcessExecutor'); + $arguments = array(array('depot'=>'TEST_DEPOT', 'branch'=>'TEST_BRANCH'), 'port'=>'TEST_PORT', 'path'=>$this->testPath, $process, true, "TEST"); $perforce = $this->getMock('Composer\Util\Perforce', array('getComposerInformation'), $arguments); $perforce->expects($this->at(0)) ->method('getComposerInformation') diff --git a/tests/Composer/Test/Repository/Vcs/TestingPerforceDriver.php b/tests/Composer/Test/Repository/Vcs/TestingPerforceDriver.php deleted file mode 100644 index 574c69377..000000000 --- a/tests/Composer/Test/Repository/Vcs/TestingPerforceDriver.php +++ /dev/null @@ -1,26 +0,0 @@ -depot; - } - public function getBranch(){ - return $this->branch; - } - -} \ No newline at end of file diff --git a/tests/Composer/Test/Util/PerforceTest.php b/tests/Composer/Test/Util/PerforceTest.php index 8a653394f..62df75f69 100644 --- a/tests/Composer/Test/Util/PerforceTest.php +++ b/tests/Composer/Test/Util/PerforceTest.php @@ -1,21 +1,26 @@ + * Jordi Boggiano + * + * Contributor: Matt Whittom + * Date: 7/17/13 + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. */ + namespace Composer\Test\Util; -use Composer\Test\Util\TestingPerforce; +use Composer\Util\Perforce; use Composer\Util\ProcessExecutor; -use org\bovigo\vfs\vfsStreamWrapper; -use org\bovigo\vfs\vfsStreamDirectory; -use org\bovigo\vfs\vfsStream; - +/** + * @author Matt Whittom + */ class PerforceTest extends \PHPUnit_Framework_TestCase { protected $perforce; @@ -24,54 +29,59 @@ class PerforceTest extends \PHPUnit_Framework_TestCase { public function setUp() { $this->processExecutor = $this->getMock('Composer\Util\ProcessExecutor'); $repoConfig = array("depot"=>"depot", "branch"=>"branch", "p4user"=>"user"); - $this->perforce = new TestingPerforce($repoConfig, "port", "path", $this->processExecutor); + $this->perforce = new Perforce($repoConfig, "port", "path", $this->processExecutor, true, "TEST"); } public function testGetClientWithoutStream() { - $client = $this->perforce->testGetClient(); + $client = $this->perforce->getClient(); + $hostname = gethostname(); + $timestamp = time(); + $expected = "composer_perforce_TEST_depot"; $this->assertEquals($expected, $client); } public function testGetClientFromStream() { - $this->perforce->setDepotType("stream"); - $client = $this->perforce->testGetClient(); + $this->setPerforceToStream(); + + $client = $this->perforce->getClient(); $expected = "composer_perforce_TEST_depot_branch"; $this->assertEquals($expected, $client); } public function testGetStreamWithoutStream() { - $stream = $this->perforce->testGetStream(); + $stream = $this->perforce->getStream(); $this->assertEquals("//depot", $stream); } public function testGetStreamWithStream() { - $this->perforce->setDepotType("stream"); - $stream = $this->perforce->testGetStream(); + $this->setPerforceToStream(); + + $stream = $this->perforce->getStream(); $this->assertEquals("//depot/branch", $stream); } - public function testGetStreamWithoutLabel() { - $stream = $this->perforce->testGetStreamWithoutLabel(); - $this->assertEquals("//depot", $stream); - $this->perforce->setDepotType("stream"); - $stream = $this->perforce->testGetStreamWithoutLabel(); + + public function testGetStreamWithoutLabelWithStreamWithoutLabel(){ + $stream = $this->perforce->getStreamWithoutLabel("//depot/branch"); $this->assertEquals("//depot/branch", $stream); - $this->perforce->setStream("//depot/branching@label"); - $stream = $this->perforce->testGetStreamWithoutLabel(); + } + + public function testGetStreamWithoutLabelWithStreamWithLabel(){ + $stream = $this->perforce->getStreamWithoutLabel("//depot/branching@label"); $this->assertEquals("//depot/branching", $stream); } public function testGetClientSpec() { - $clientSpec = $this->perforce->testGetClientSpec(); + $clientSpec = $this->perforce->getP4ClientSpec(); $expected = "path/composer_perforce_TEST_depot.p4.spec"; $this->assertEquals($expected, $clientSpec); } public function testGenerateP4Command() { $command = "do something"; - $p4Command = $this->perforce->testGenerateP4Command($command); + $p4Command = $this->perforce->generateP4Command($command); $expected = "p4 -u user -c composer_perforce_TEST_depot -p port do something"; $this->assertEquals($expected, $p4Command); } @@ -79,13 +89,16 @@ class PerforceTest extends \PHPUnit_Framework_TestCase { public function testQueryP4UserWithUserAlreadySet(){ $io = $this->getMock('Composer\IO\IOInterface'); - $this->perforce->setP4User("TEST_USER"); + $repoConfig = array("depot"=>"depot", "branch"=>"branch", "p4user"=>"TEST_USER"); + $this->perforce = new Perforce($repoConfig, "port", "path", $this->processExecutor, true, "TEST"); + $this->perforce->queryP4user($io); $this->assertEquals("TEST_USER", $this->perforce->getUser()); } public function testQueryP4UserWithUserSetInP4VariablesWithWindowsOS(){ - $this->perforce->windows_flag = true; + $repoConfig = array("depot"=>"depot", "branch"=>"branch"); + $this->perforce = new Perforce($repoConfig, "port", "path", $this->processExecutor, true, "TEST"); $io = $this->getMock('Composer\IO\IOInterface'); $expectedCommand = "p4 set"; @@ -94,13 +107,13 @@ class PerforceTest extends \PHPUnit_Framework_TestCase { ->with($this->equalTo($expectedCommand)) ->will($this->returnCallback(function($command, &$output) {$output = "P4USER=TEST_P4VARIABLE_USER\n"; return true;})); - $this->perforce->setP4User(null); $this->perforce->queryP4user($io); $this->assertEquals("TEST_P4VARIABLE_USER", $this->perforce->getUser()); } public function testQueryP4UserWithUserSetInP4VariablesNotWindowsOS(){ - $this->perforce->windows_flag = false; + $repoConfig = array("depot"=>"depot", "branch"=>"branch"); + $this->perforce = new Perforce($repoConfig, "port", "path", $this->processExecutor, false, "TEST"); $io = $this->getMock('Composer\IO\IOInterface'); $expectedCommand = 'echo $P4USER'; @@ -109,12 +122,13 @@ class PerforceTest extends \PHPUnit_Framework_TestCase { ->with($this->equalTo($expectedCommand)) ->will($this->returnCallback(function($command, &$output) {$output = "TEST_P4VARIABLE_USER\n"; return true;})); - $this->perforce->setP4User(null); $this->perforce->queryP4user($io); $this->assertEquals("TEST_P4VARIABLE_USER", $this->perforce->getUser()); } public function testQueryP4UserQueriesForUser(){ + $repoConfig = array("depot"=>"depot", "branch"=>"branch"); + $this->perforce = new Perforce($repoConfig, "port", "path", $this->processExecutor, false, "TEST"); $io = $this->getMock('Composer\IO\IOInterface'); $expectedQuestion = "Enter P4 User:"; $io->expects($this->at(0)) @@ -122,13 +136,13 @@ class PerforceTest extends \PHPUnit_Framework_TestCase { ->with($this->equalTo($expectedQuestion)) ->will($this->returnValue("TEST_QUERY_USER")); - $this->perforce->setP4User(null); $this->perforce->queryP4user($io); $this->assertEquals("TEST_QUERY_USER", $this->perforce->getUser()); } public function testQueryP4UserStoresResponseToQueryForUserWithWindows(){ - $this->perforce->windows_flag = true; + $repoConfig = array("depot"=>"depot", "branch"=>"branch"); + $this->perforce = new Perforce($repoConfig, "port", "path", $this->processExecutor, true, "TEST"); $io = $this->getMock('Composer\IO\IOInterface'); $expectedQuestion = "Enter P4 User:"; @@ -142,12 +156,12 @@ class PerforceTest extends \PHPUnit_Framework_TestCase { ->with($this->equalTo($expectedCommand)) ->will($this->returnValue(0)); - $this->perforce->setP4User(null); $this->perforce->queryP4user($io); } public function testQueryP4UserStoresResponseToQueryForUserWithoutWindows(){ - $this->perforce->windows_flag = false; + $repoConfig = array("depot"=>"depot", "branch"=>"branch"); + $this->perforce = new Perforce($repoConfig, "port", "path", $this->processExecutor, false, "TEST"); $io = $this->getMock('Composer\IO\IOInterface'); $expectedQuestion = "Enter P4 User:"; @@ -161,35 +175,34 @@ class PerforceTest extends \PHPUnit_Framework_TestCase { ->with($this->equalTo($expectedCommand)) ->will($this->returnValue(0)); - $this->perforce->setP4User(null); $this->perforce->queryP4user($io); } public function testQueryP4PasswordWithPasswordAlreadySet(){ + $repoConfig = array("depot"=>"depot", "branch"=>"branch", "p4user"=>"user", "p4password"=>"TEST_PASSWORD"); + $this->perforce = new Perforce($repoConfig, "port", "path", $this->processExecutor, false, "TEST"); $io = $this->getMock('Composer\IO\IOInterface'); - $this->perforce->setP4Password("TEST_PASSWORD"); - $password = $this->perforce->testQueryP4Password($io); + $password = $this->perforce->queryP4Password($io); $this->assertEquals("TEST_PASSWORD", $password); } public function testQueryP4PasswordWithPasswordSetInP4VariablesWithWindowsOS(){ - $this->perforce->windows_flag = true; - $io = $this->getMock('Composer\IO\IOInterface'); + $expectedCommand = "p4 set"; $this->processExecutor->expects($this->at(0)) ->method('execute') ->with($this->equalTo($expectedCommand)) ->will($this->returnCallback(function($command, &$output) {$output = "P4PASSWD=TEST_P4VARIABLE_PASSWORD\n"; return true;})); - $this->perforce->setP4Password(null); - $password = $this->perforce->testQueryP4Password($io); + $password = $this->perforce->queryP4Password($io); $this->assertEquals("TEST_P4VARIABLE_PASSWORD", $password); } public function testQueryP4PasswordWithPasswordSetInP4VariablesNotWindowsOS(){ - $this->perforce->windows_flag = false; + $repoConfig = array("depot"=>"depot", "branch"=>"branch", "p4user"=>"user"); + $this->perforce = new Perforce($repoConfig, "port", "path", $this->processExecutor, false, "TEST"); $io = $this->getMock('Composer\IO\IOInterface'); $expectedCommand = 'echo $P4PASSWD'; @@ -198,8 +211,7 @@ class PerforceTest extends \PHPUnit_Framework_TestCase { ->with($this->equalTo($expectedCommand)) ->will($this->returnCallback(function($command, &$output) {$output = "TEST_P4VARIABLE_PASSWORD\n"; return true;})); - $this->perforce->setP4Password(null); - $password = $this->perforce->testQueryP4Password($io); + $password = $this->perforce->queryP4Password($io); $this->assertEquals("TEST_P4VARIABLE_PASSWORD", $password); } @@ -211,62 +223,60 @@ class PerforceTest extends \PHPUnit_Framework_TestCase { ->with($this->equalTo($expectedQuestion)) ->will($this->returnValue("TEST_QUERY_PASSWORD")); - $this->perforce->setP4Password(null); - $password = $this->perforce->testQueryP4Password($io); + $password = $this->perforce->queryP4Password($io); $this->assertEquals("TEST_QUERY_PASSWORD", $password); } public function testWriteP4ClientSpecWithoutStream() { - vfsStreamWrapper::register(); - VfsStreamWrapper::setRoot(new vfsStreamDirectory("path")); - $clientSpec = $this->perforce->testGetClientSpec(); - $this->perforce->writeP4ClientSpec(); - $spec = fopen(vfsStream::url($clientSpec), 'r'); + $stream = fopen("php://memory", 'w+'); + $this->perforce->writeClientSpecToFile($stream); + + rewind($stream); $expectedArray = $this->getExpectedClientSpec(FALSE); try { foreach ($expectedArray as $expected) { - $this->assertStringStartsWith($expected, fgets($spec)); + $this->assertStringStartsWith($expected, fgets($stream)); } - $this->assertFalse(fgets($spec)); + $this->assertFalse(fgets($stream)); } catch (Exception $e) { - fclose($spec); + fclose($stream); throw $e; } - fclose($spec); + fclose($stream); } public function testWriteP4ClientSpecWithStream() { - vfsStreamWrapper::register(); - VfsStreamWrapper::setRoot(new vfsStreamDirectory("path")); - $this->perforce->setStream("//depot/branching@label"); - $clientSpec = $this->perforce->testGetClientSpec(); - $this->perforce->writeP4ClientSpec(); - $spec = fopen(vfsStream::url($clientSpec), 'r'); + $this->setPerforceToStream(); + $stream = fopen("php://memory", 'w+'); + + $this->perforce->writeClientSpecToFile($stream); + rewind($stream); + $expectedArray = $this->getExpectedClientSpec(TRUE); try { foreach ($expectedArray as $expected) { - $this->assertStringStartsWith($expected, fgets($spec)); + $this->assertStringStartsWith($expected, fgets($stream)); } - $this->assertFalse(fgets($spec)); + $this->assertFalse(fgets($stream)); } catch (Exception $e) { - fclose($spec); + fclose($stream); throw $e; } - fclose($spec); + fclose($stream); } public function testIsLoggedIn() { - $expectedCommand = $this->winCompat("p4 -u user -p port login -s"); + $expectedCommand = "p4 -u user -p port login -s"; $this->processExecutor->expects($this->at(0)) ->method('execute') ->with($this->equalTo($expectedCommand), $this->equalTo(null)) ->will($this->returnValue(0)); - $this->perforce->testIsLoggedIn(); + $this->perforce->isLoggedIn(); } public function testConnectClient() { - $expectedCommand = $this->winCompat("p4 -u user -c composer_perforce_TEST_depot -p port client -i < path/composer_perforce_TEST_depot.p4.spec"); + $expectedCommand = "p4 -u user -c composer_perforce_TEST_depot -p port client -i < path/composer_perforce_TEST_depot.p4.spec"; $this->processExecutor->expects($this->at(0)) ->method('execute') ->with($this->equalTo($expectedCommand), $this->equalTo(null)) @@ -276,33 +286,25 @@ class PerforceTest extends \PHPUnit_Framework_TestCase { } public function testGetBranchesWithStream() { - $expectedCommand = $this->winCompat("p4 -u user -c composer_perforce_TEST_depot_branchlabel -p port streams //depot/..."); + $this->setPerforceToStream(); + + $expectedCommand = "p4 -u user -c composer_perforce_TEST_depot_branch -p port streams //depot/..."; $this->processExecutor->expects($this->at(0)) ->method('execute') ->with($this->equalTo($expectedCommand)) ->will($this->returnCallback(function($command, &$output) {$output = "Stream //depot/branch mainline none 'branch'\n"; return true;})); - $this->perforce->setStream("//depot/branch@label"); $branches = $this->perforce->getBranches(); $this->assertEquals("//depot/branch", $branches['master']); } public function testGetBranchesWithoutStream() { - $expectedCommand = $this->winCompat("p4 -u user -p port depots"); - $this->processExecutor->expects($this->at(0)) - ->method('execute') - ->with($this->equalTo($expectedCommand)) - ->will($this->returnCallback(function($command, &$output) {$output = "Depot depot 2013/01/28 local /path/to/depots/depot/... 'depot project'\n"; return true;})); - - $result = $this->perforce->checkStream("depot"); - $this->assertFalse($result); - $branches = $this->perforce->getBranches(); $this->assertEquals("//depot", $branches['master']); } public function testGetTagsWithoutStream() { - $expectedCommand = $this->winCompat("p4 -u user -c composer_perforce_TEST_depot -p port labels"); + $expectedCommand = "p4 -u user -c composer_perforce_TEST_depot -p port labels"; $this->processExecutor->expects($this->at(0)) ->method('execute') ->with($this->equalTo($expectedCommand)) @@ -314,36 +316,35 @@ class PerforceTest extends \PHPUnit_Framework_TestCase { } public function testGetTagsWithStream() { - $expectedCommand = $this->winCompat("p4 -u user -c composer_perforce_TEST_depot_branch -p port labels"); + $this->setPerforceToStream(); + + $expectedCommand = "p4 -u user -c composer_perforce_TEST_depot_branch -p port labels"; $this->processExecutor->expects($this->at(0)) ->method('execute') ->with($this->equalTo($expectedCommand)) ->will($this->returnCallback(function($command, &$output) {$output = "Label 0.0.1 2013/07/31 'First Label!'\nLabel 0.0.2 2013/08/01 'Second Label!'\n"; return true;})); - $this->perforce->setStream("//depot/branch"); $tags = $this->perforce->getTags(); $this->assertEquals("//depot/branch@0.0.1", $tags['0.0.1']); $this->assertEquals("//depot/branch@0.0.2", $tags['0.0.2']); } public function testCheckStreamWithoutStream() { - $this->perforce->commandReturnValue = "Depot depot 2013/01/28 local /path/to/depots/depot/... 'depot project'"; $result = $this->perforce->checkStream("depot"); $this->assertFalse($result); - $this->assertFalse($this->perforce->testIsStream()); + $this->assertFalse($this->perforce->isStream()); } public function testCheckStreamWithStream() { - $line1 = "Depot depot 2013/01/28 branch /path/to/depots/depot/... 'depot project'\n"; - $line2 = "Depot depot 2013/01/28 development /path/to/depots/depot/... 'depot project'\n"; - $this->perforce->commandReturnValue = $line1 . $line2; + $this->processExecutor->expects($this->any())->method('execute') + ->will($this->returnCallback(function($command, &$output) {$output = "Depot depot 2013/06/25 stream /p4/1/depots/depot/... 'Created by Me'"; return true;})); $result = $this->perforce->checkStream("depot"); - $this->assertFalse($result); - $this->assertFalse($this->perforce->testIsStream()); + $this->assertTrue($result); + $this->assertTrue($this->perforce->isStream()); } public function testGetComposerInformationWithoutLabelWithoutStream() { - $expectedCommand = $this->winCompat("p4 -u user -c composer_perforce_TEST_depot -p port print //depot/composer.json"); + $expectedCommand = "p4 -u user -c composer_perforce_TEST_depot -p port print //depot/composer.json"; $this->processExecutor->expects($this->at(0)) ->method('execute') ->with($this->equalTo($expectedCommand)) @@ -360,13 +361,13 @@ class PerforceTest extends \PHPUnit_Framework_TestCase { } public function testGetComposerInformationWithLabelWithoutStream() { - $expectedCommand = $this->winCompat("p4 -u user -p port files //depot/composer.json@0.0.1"); + $expectedCommand = "p4 -u user -p port files //depot/composer.json@0.0.1"; $this->processExecutor->expects($this->at(0)) ->method('execute') ->with($this->equalTo($expectedCommand)) ->will($this->returnCallback(function($command, &$output) {$output = "//depot/composer.json#1 - branch change 10001 (text)"; return true;})); - $expectedCommand = $this->winCompat("p4 -u user -c composer_perforce_TEST_depot -p port print //depot/composer.json@10001"); + $expectedCommand = "p4 -u user -c composer_perforce_TEST_depot -p port print //depot/composer.json@10001"; $this->processExecutor->expects($this->at(1)) ->method('execute') ->with($this->equalTo($expectedCommand)) @@ -384,13 +385,14 @@ class PerforceTest extends \PHPUnit_Framework_TestCase { } public function testGetComposerInformationWithoutLabelWithStream() { - $expectedCommand = $this->winCompat("p4 -u user -c composer_perforce_TEST_depot_branch -p port print //depot/branch/composer.json"); + $this->setPerforceToStream(); + + $expectedCommand = "p4 -u user -c composer_perforce_TEST_depot_branch -p port print //depot/branch/composer.json"; $this->processExecutor->expects($this->at(0)) ->method('execute') ->with($this->equalTo($expectedCommand)) ->will($this->returnCallback(function($command, &$output) {$output = PerforceTest::getComposerJson(); return true;})); - $this->perforce->setStream("//depot/branch"); $result = $this->perforce->getComposerInformation("//depot/branch"); $expected = array( @@ -403,19 +405,19 @@ class PerforceTest extends \PHPUnit_Framework_TestCase { } public function testGetComposerInformationWithLabelWithStream() { - $expectedCommand = $this->winCompat("p4 -u user -p port files //depot/branch/composer.json@0.0.1"); + $this->setPerforceToStream(); + $expectedCommand = "p4 -u user -p port files //depot/branch/composer.json@0.0.1"; $this->processExecutor->expects($this->at(0)) ->method('execute') ->with($this->equalTo($expectedCommand)) ->will($this->returnCallback(function($command, &$output) {$output = "//depot/composer.json#1 - branch change 10001 (text)"; return true;})); - $expectedCommand = $this->winCompat("p4 -u user -c composer_perforce_TEST_depot_branch -p port print //depot/branch/composer.json@10001"); + $expectedCommand = "p4 -u user -c composer_perforce_TEST_depot_branch -p port print //depot/branch/composer.json@10001"; $this->processExecutor->expects($this->at(1)) ->method('execute') ->with($this->equalTo($expectedCommand)) ->will($this->returnCallback(function($command, &$output) {$output = PerforceTest::getComposerJson(); return true;})); - $this->perforce->setStream("//depot/branch"); $result = $this->perforce->getComposerInformation("//depot/branch@0.0.1"); $expected = array( @@ -428,7 +430,7 @@ class PerforceTest extends \PHPUnit_Framework_TestCase { } public function testSyncCodeBaseWithoutStream() { - $expectedCommand = $this->winCompat("p4 -u user -c composer_perforce_TEST_depot -p port sync -f @label"); + $expectedCommand = "p4 -u user -c composer_perforce_TEST_depot -p port sync -f @label"; $this->processExecutor->expects($this->at(1)) ->method('execute') ->with($this->equalTo($expectedCommand), $this->equalTo(null)) @@ -438,20 +440,20 @@ class PerforceTest extends \PHPUnit_Framework_TestCase { } public function testSyncCodeBaseWithStream() { - $expectedCommand = $this->winCompat("p4 -u user -c composer_perforce_TEST_depot_branch -p port sync -f @label"); - $this->processExecutor->expects($this->at(1)) - ->method('execute') - ->with($this->equalTo($expectedCommand), $this->equalTo(null)) - ->will($this->returnValue(0)); + $this->setPerforceToStream(); + $expectedCommand = "p4 -u user -c composer_perforce_TEST_depot_branch -p port sync -f @label"; + $this->processExecutor->expects($this->at(1)) + ->method('execute') + ->with($this->equalTo($expectedCommand)) + ->will($this->returnValue(0)); - $this->perforce->setStream("//depot/branch"); $this->perforce->syncCodeBase("label"); } public function testCheckServerExists() { $processExecutor = $this->getMock('Composer\Util\ProcessExecutor'); - $expectedCommand = $this->winCompat("p4 -p perforce.does.exist:port info -s"); + $expectedCommand = "p4 -p perforce.does.exist:port info -s"; $processExecutor->expects($this->at(0)) ->method('execute') ->with($this->equalTo($expectedCommand), $this->equalTo(null)) @@ -464,7 +466,7 @@ class PerforceTest extends \PHPUnit_Framework_TestCase { public function testCheckServerExistsWithFailure() { $processExecutor = $this->getMock('Composer\Util\ProcessExecutor'); - $expectedCommand = $this->winCompat("p4 -p perforce.does.not.exist:port info -s"); + $expectedCommand = "p4 -p perforce.does.not.exist:port info -s"; $processExecutor->expects($this->at(0)) ->method('execute') ->with($this->equalTo($expectedCommand), $this->equalTo(null)) @@ -513,7 +515,7 @@ class PerforceTest extends \PHPUnit_Framework_TestCase { ); if ($withStream) { $expectedArray[] = "Stream:"; - $expectedArray[] = " //depot/branching"; + $expectedArray[] = " //depot/branch"; } else { $expectedArray[] = "View: //depot/... //composer_perforce_TEST_depot/depot/..."; @@ -522,16 +524,8 @@ class PerforceTest extends \PHPUnit_Framework_TestCase { return $expectedArray; } - private function winCompat($cmd) { - if (defined('PHP_WINDOWS_VERSION_BUILD')) { - $cmd = str_replace('cd ', 'cd /D ', $cmd); - $cmd = str_replace('composerPath', getcwd() . '/composerPath', $cmd); - - return strtr($cmd, "'", '"'); - } - - return $cmd; + private function setPerforceToStream(){ + $this->perforce->setStream("//depot/branch"); } - } diff --git a/tests/Composer/Test/Util/TestingPerforce.php b/tests/Composer/Test/Util/TestingPerforce.php deleted file mode 100644 index dc21e8c33..000000000 --- a/tests/Composer/Test/Util/TestingPerforce.php +++ /dev/null @@ -1,106 +0,0 @@ -windows_flag = false; - } - /* - * Override functions - */ - protected function getRandomValue() { - return "TEST"; - } - protected function isWindows(){ - return $this->windows_flag; - } - -// protected function executeCommand($command) { -// $this->previousCommand = $this->lastCommand; -// $this->lastCommand = $command; -// $result = $this->commandReturnValue; -// $this->commandReturnValue = $this->nextCommandReturnValue; -// $this->nextCommandReturnValue = null; -// return $result; -// } - - public function writeP4ClientSpec() { - $spec = fopen(vfsStream::url($this->getP4ClientSpec()), 'w'); - $this->writeClientSpecToFile($spec); - fclose($spec); - } - - /* - * Test Helper functions - */ - public function setDepotType($depotType) { - $this->p4depotType = $depotType; - $this->p4stream = NULL; - } - - /* - * Functions to expose protected methods for testing: - */ - public function setP4User($p4user){ - $this->p4user = $p4user; - } - public function setP4Password($password){ - $this->p4password = $password; - } - - public function testGetClient() { - return $this->getClient(); - } - - public function testGetStream() { - return $this->getStream(); - } - - public function testGetStreamWithoutLabel() { - return $this->getStreamWithoutLabel(); - } - - public function testGetClientSpec() { - return $this->getP4ClientSpec(); - } - - public function testGenerateP4Command($command, $useClient = TRUE) { - return $this->generateP4Command($command, $useClient); - } - - public function testIsLoggedIn() - { - return $this->isLoggedIn(); - } - - public function testIsStream() - { - return $this->isStream(); - } - - public function testGetP4Variable($name) - { - return $this->testGetP4Variable($name); - } - - public function testQueryP4Password($io) - { - return $this->queryP4Password($io); - } -} \ No newline at end of file From 8379985166be59f0b23a7c7d8a7ba2ee079bf3dd Mon Sep 17 00:00:00 2001 From: matt-whittom Date: Thu, 15 Aug 2013 14:45:42 -0500 Subject: [PATCH 14/26] Updated/Standardized header comments --- .../Downloader/PerforceDownloaderTest.php | 22 +++++++++++++------ .../Repository/Vcs/PerforceDriverTest.php | 4 +++- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/tests/Composer/Test/Downloader/PerforceDownloaderTest.php b/tests/Composer/Test/Downloader/PerforceDownloaderTest.php index 634cc5f18..527ef9f6d 100644 --- a/tests/Composer/Test/Downloader/PerforceDownloaderTest.php +++ b/tests/Composer/Test/Downloader/PerforceDownloaderTest.php @@ -1,19 +1,27 @@ + * Jordi Boggiano + * + * Contributor: Matt Whittom + * Date: 7/17/13 + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. */ + namespace Composer\Test\Downloader; use Composer\Downloader\PerforceDownloader; use Composer\Config; use Composer\Repository\VcsRepository; - +/** + * @author Matt Whittom + */ class PerforceDownloaderTest extends \PHPUnit_Framework_TestCase { private $io; diff --git a/tests/Composer/Test/Repository/Vcs/PerforceDriverTest.php b/tests/Composer/Test/Repository/Vcs/PerforceDriverTest.php index ed14b077e..ef0fc694a 100644 --- a/tests/Composer/Test/Repository/Vcs/PerforceDriverTest.php +++ b/tests/Composer/Test/Repository/Vcs/PerforceDriverTest.php @@ -19,7 +19,9 @@ use Composer\Repository\Vcs\PerforceDriver; use Composer\Util\Filesystem; use Composer\Config; - +/** + * @author Matt Whittom + */ class PerforceDriverTest extends \PHPUnit_Framework_TestCase { private $config; private $io; From f3722a46ed8af5858d6bc6fa11caba8f62a050e4 Mon Sep 17 00:00:00 2001 From: matt-whittom Date: Fri, 16 Aug 2013 09:37:11 -0500 Subject: [PATCH 15/26] Updated comments --- .../Downloader/PerforceDownloader.php | 17 +++--- .../Repository/Vcs/PerforceDriver.php | 55 ++++++++++++------- 2 files changed, 42 insertions(+), 30 deletions(-) diff --git a/src/Composer/Downloader/PerforceDownloader.php b/src/Composer/Downloader/PerforceDownloader.php index 5cede4ff6..e68315668 100644 --- a/src/Composer/Downloader/PerforceDownloader.php +++ b/src/Composer/Downloader/PerforceDownloader.php @@ -51,19 +51,10 @@ class PerforceDownloader extends VcsDownloader return; } $repository = $package->getRepository(); - $repoConfig = $this->getRepoConfig($repository); + $repoConfig = $repository->getRepoConfig(); $this->perforce = Perforce::createPerforce($repoConfig, $package->getSourceUrl(), $path); } - public function injectPerforce($perforce){ - $this->perforce = $perforce; - $this->perforceInjected = true; - } - - private function getRepoConfig(VcsRepository $repository){ - return $repository->getRepoConfig(); - } - /** * {@inheritDoc} */ @@ -91,4 +82,10 @@ class PerforceDownloader extends VcsDownloader return $commitLogs; } + public function injectPerforce($perforce){ + $this->perforce = $perforce; + $this->perforceInjected = true; + } + + } diff --git a/src/Composer/Repository/Vcs/PerforceDriver.php b/src/Composer/Repository/Vcs/PerforceDriver.php index 7068ebb3e..4918cca5c 100644 --- a/src/Composer/Repository/Vcs/PerforceDriver.php +++ b/src/Composer/Repository/Vcs/PerforceDriver.php @@ -34,7 +34,8 @@ class PerforceDriver extends VcsDriver { /** * {@inheritDoc} */ - public function initialize() { + public function initialize() + { $this->depot = $this->repoConfig['depot']; $this->branch = ""; if (isset($this->repoConfig['branch'])) { @@ -51,7 +52,8 @@ class PerforceDriver extends VcsDriver { return TRUE; } - private function initPerforce() { + private function initPerforce() + { if (isset($this->perforce)) { return; } @@ -60,15 +62,11 @@ class PerforceDriver extends VcsDriver { $this->perforce = Perforce::createPerforce($this->repoConfig, $this->getUrl(), $repoDir, $this->process); } - public function injectPerforce(Perforce $perforce) { - $this->perforce = $perforce; - } - - /** * {@inheritDoc} */ - public function getComposerInformation($identifier) { + public function getComposerInformation($identifier) + { if (isset($this->composer_info_identifier)){ if (strcmp($identifier, $this->composer_info_identifier) === 0 ) { @@ -83,14 +81,16 @@ class PerforceDriver extends VcsDriver { /** * {@inheritDoc} */ - public function getRootIdentifier() { + public function getRootIdentifier() + { return $this->branch; } /** * {@inheritDoc} */ - public function getBranches() { + public function getBranches() + { $branches = $this->perforce->getBranches(); return $branches; @@ -99,7 +99,8 @@ class PerforceDriver extends VcsDriver { /** * {@inheritDoc} */ - public function getTags() { + public function getTags() + { $tags = $this->perforce->getTags(); return $tags; @@ -108,14 +109,16 @@ class PerforceDriver extends VcsDriver { /** * {@inheritDoc} */ - public function getDist($identifier) { + public function getDist($identifier) + { return NULL; } /** * {@inheritDoc} */ - public function getSource($identifier) { + public function getSource($identifier) + { $source = array( 'type' => 'perforce', 'url' => $this->repoConfig['url'], @@ -128,14 +131,16 @@ class PerforceDriver extends VcsDriver { /** * {@inheritDoc} */ - public function getUrl() { + public function getUrl() + { return $this->url; } /** * {@inheritDoc} */ - public function hasComposerFile($identifier) { + public function hasComposerFile($identifier) + { $this->composer_info = $this->perforce->getComposerInformation("//$this->depot/$identifier"); $this->composer_info_identifier = $identifier; $result = false; @@ -148,30 +153,40 @@ class PerforceDriver extends VcsDriver { /** * {@inheritDoc} */ - public function getContents($url) { + public function getContents($url) + { return FALSE; } /** * {@inheritDoc} */ - public static function supports(IOInterface $io, $url, $deep = FALSE) { + public static function supports(IOInterface $io, $url, $deep = FALSE) + { return Perforce::checkServerExists($url, new ProcessExecutor); } /** * {@inheritDoc} */ - public function cleanup(){ + public function cleanup() + { $this->perforce->cleanupClientSpec(); $this->perforce = null; } - public function getDepot(){ + public function getDepot() + { return $this->depot; } - public function getBranch(){ + public function getBranch() + { return $this->branch; } + + public function injectPerforce(Perforce $perforce) + { + $this->perforce = $perforce; + } } From a543e8bc8f69b80f224518b13b525b470ab6eff8 Mon Sep 17 00:00:00 2001 From: matt-whittom Date: Fri, 16 Aug 2013 10:43:52 -0500 Subject: [PATCH 16/26] Replaced composer.lock with version from Master --- composer.lock | 36 +++--------------------------------- 1 file changed, 3 insertions(+), 33 deletions(-) diff --git a/composer.lock b/composer.lock index b6ff8ec60..0086e1a4c 100644 --- a/composer.lock +++ b/composer.lock @@ -15,7 +15,7 @@ }, "dist": { "type": "zip", - "url": "https://github.com/justinrainbow/json-schema/archive/v1.1.0.zip", + "url": "https://github.com/justinrainbow/json-schema/zipball/v1.1.0", "reference": "v1.1.0", "shasum": "" }, @@ -28,37 +28,7 @@ "JsonSchema": "src/" } }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "NewBSD" - ], - "authors": [ - { - "name": "Igor Wiedler", - "email": "igor@wiedler.ch", - "homepage": "http://wiedler.ch/igor/" - }, - { - "name": "Bruno Prieto Reis", - "email": "bruno.p.reis@gmail.com" - }, - { - "name": "Justin Rainbow", - "email": "justin.rainbow@gmail.com" - }, - { - "name": "Robert Schönthal", - "email": "seroscho@googlemail.com", - "homepage": "http://digitalkaoz.net" - } - ], - "description": "A library to validate a json schema.", - "homepage": "https://github.com/justinrainbow/json-schema", - "keywords": [ - "json", - "schema" - ], - "time": "2012-01-03 00:33:17" + "time": "2012-01-02 21:33:17" }, { "name": "seld/jsonlint", @@ -684,4 +654,4 @@ "platform-dev": [ ] -} +} \ No newline at end of file From f737e49aae56666d86803058dd07069a836f3e41 Mon Sep 17 00:00:00 2001 From: matt-whittom Date: Thu, 22 Aug 2013 09:49:22 -0500 Subject: [PATCH 17/26] Fixed issue with downloader assuming repository would be VcsRepository --- .../Downloader/PerforceDownloader.php | 13 ++++-- .../Repository/Vcs/PerforceDriver.php | 9 ++-- src/Composer/Util/Perforce.php | 45 ++++++++++++++----- tests/Composer/Test/Util/PerforceTest.php | 4 +- 4 files changed, 50 insertions(+), 21 deletions(-) diff --git a/src/Composer/Downloader/PerforceDownloader.php b/src/Composer/Downloader/PerforceDownloader.php index e68315668..1a7cee65c 100644 --- a/src/Composer/Downloader/PerforceDownloader.php +++ b/src/Composer/Downloader/PerforceDownloader.php @@ -37,7 +37,7 @@ class PerforceDownloader extends VcsDownloader $label = $package->getPrettyVersion(); $this->io->write(" Cloning ".$ref); - $this->initPerforce($package, $path); + $this->initPerforce($package, $path, $ref); $this->perforce->setStream($ref); $this->perforce->queryP4User($this->io); $this->perforce->writeP4ClientSpec(); @@ -46,15 +46,22 @@ class PerforceDownloader extends VcsDownloader $this->perforce->cleanupClientSpec(); } - private function initPerforce($package, $path){ + private function initPerforce($package, $path, $ref){ if ($this->perforceInjected){ return; } $repository = $package->getRepository(); - $repoConfig = $repository->getRepoConfig(); + $repoConfig = null; + if ($repository instanceof VcsRepository){ + $repoConfig = $this->getRepoConfig($repository); + } $this->perforce = Perforce::createPerforce($repoConfig, $package->getSourceUrl(), $path); } + private function getRepoConfig(VcsRepository $repository){ + return $repository->getRepoConfig(); + } + /** * {@inheritDoc} */ diff --git a/src/Composer/Repository/Vcs/PerforceDriver.php b/src/Composer/Repository/Vcs/PerforceDriver.php index 4918cca5c..35f1389f7 100644 --- a/src/Composer/Repository/Vcs/PerforceDriver.php +++ b/src/Composer/Repository/Vcs/PerforceDriver.php @@ -42,7 +42,7 @@ class PerforceDriver extends VcsDriver { $this->branch = $this->repoConfig['branch']; } - $this->initPerforce(); + $this->initPerforce($this->repoConfig); $this->perforce->p4Login($this->io); $this->perforce->checkStream($this->depot); @@ -52,14 +52,14 @@ class PerforceDriver extends VcsDriver { return TRUE; } - private function initPerforce() + private function initPerforce($repoConfig) { if (isset($this->perforce)) { return; } $repoDir = $this->config->get('cache-vcs-dir') . "/$this->depot"; - $this->perforce = Perforce::createPerforce($this->repoConfig, $this->getUrl(), $repoDir, $this->process); + $this->perforce = Perforce::createPerforce($repoConfig, $this->getUrl(), $repoDir, $this->process); } /** @@ -122,7 +122,8 @@ class PerforceDriver extends VcsDriver { $source = array( 'type' => 'perforce', 'url' => $this->repoConfig['url'], - 'reference' => $identifier + 'reference' => $identifier, + 'p4user' => $this->perforce->getUser() ); return $source; diff --git a/src/Composer/Util/Perforce.php b/src/Composer/Util/Perforce.php index 6acee208f..99c640cf1 100644 --- a/src/Composer/Util/Perforce.php +++ b/src/Composer/Util/Perforce.php @@ -42,30 +42,34 @@ class Perforce { $process = new ProcessExecutor; } $isWindows = defined('PHP_WINDOWS_VERSION_BUILD'); - if (isset($repoConfig['unique_perforce_client_name'])){ - $unique_perforce_client_name = $repoConfig['unique_perforce_client_name']; - } else { - $unique_perforce_client_name = gethostname() . "_" . time(); - $repoConfig['unique_perforce_client_name'] = $unique_perforce_client_name; - } - $perforce = new Perforce($repoConfig, $port, $path, $process, $isWindows, $unique_perforce_client_name); + $perforce = new Perforce($repoConfig, $port, $path, $process, $isWindows); return $perforce; } - public function __construct($repoConfig, $port, $path, ProcessExecutor $process, $isWindows, $unique_perforce_client_name) { + public function __construct($repoConfig, $port, $path, ProcessExecutor $process, $isWindows) { $this->windowsFlag = $isWindows; - $this->unique_perforce_client_name = $unique_perforce_client_name; $this->p4Port = $port; $this->path = $path; $fs = new Filesystem(); $fs->ensureDirectoryExists($path); $this->process = $process; + $this->initialize($repoConfig); + } - if (isset($repoConfig['depot'])) { + public function initialize($repoConfig){ + $this->unique_perforce_client_name = $this->generateUniquePerforceClientName(); + if (!isset ($repoConfig)){ + return; + } + if (isset($repoConfig['unique_perforce_client_name'])){ + $this->unique_perforce_client_name = $repoConfig['unique_perforce_client_name']; + } + + if (isset($repoConfig['depot'])){ $this->p4Depot = $repoConfig['depot']; } - if (isset($repoConfig['branch'])) { + if (isset($repoConfig['branch'])){ $this->p4Branch = $repoConfig['branch']; } if (isset($repoConfig['p4user'])) { @@ -79,6 +83,19 @@ class Perforce { } } + public function initializeDepotAndBranch($depot, $branch){ + if (isset($depot)) { + $this->p4Depot = $depot; + } + if (isset($branch)) { + $this->p4Branch = $branch; + } + } + + public function generateUniquePerforceClientName(){ + return gethostname() . "_" . time(); + } + public function cleanupClientSpec(){ $client = $this->getClient(); $command = "p4 client -d $client"; @@ -114,7 +131,11 @@ class Perforce { public function setStream($stream) { $this->p4Stream = $stream; - $this->p4DepotType = "stream"; + $index = strrpos($stream, "/"); + //Stream format is //depot/stream, while non-streaming depot is //depot + if ($index > 2){ + $this->p4DepotType = "stream"; + } } public function isStream() { diff --git a/tests/Composer/Test/Util/PerforceTest.php b/tests/Composer/Test/Util/PerforceTest.php index 62df75f69..e4e0e36b3 100644 --- a/tests/Composer/Test/Util/PerforceTest.php +++ b/tests/Composer/Test/Util/PerforceTest.php @@ -28,8 +28,8 @@ class PerforceTest extends \PHPUnit_Framework_TestCase { public function setUp() { $this->processExecutor = $this->getMock('Composer\Util\ProcessExecutor'); - $repoConfig = array("depot"=>"depot", "branch"=>"branch", "p4user"=>"user"); - $this->perforce = new Perforce($repoConfig, "port", "path", $this->processExecutor, true, "TEST"); + $repoConfig = array("depot"=>"depot", "branch"=>"branch", "p4user"=>"user", "unique_perforce_client_name" => "TEST"); + $this->perforce = new Perforce($repoConfig, "port", "path", $this->processExecutor, true); } public function testGetClientWithoutStream() { From 774a55befd402c061c8e77f38b6bb6c35d36e6f9 Mon Sep 17 00:00:00 2001 From: mwhittom Date: Wed, 4 Sep 2013 09:24:49 -0500 Subject: [PATCH 18/26] Updated to match psr-2 spec, fixed header comments --- .../Downloader/PerforceDownloader.php | 22 +- .../Repository/Vcs/PerforceDriver.php | 23 +- src/Composer/Util/Perforce.php | 220 ++++++----- .../Downloader/PerforceDownloaderTest.php | 74 ++-- .../Repository/Vcs/PerforceDriverTest.php | 73 ++-- tests/Composer/Test/Util/PerforceTest.php | 345 +++++++++++++----- 6 files changed, 476 insertions(+), 281 deletions(-) diff --git a/src/Composer/Downloader/PerforceDownloader.php b/src/Composer/Downloader/PerforceDownloader.php index 1a7cee65c..9d3e31e06 100644 --- a/src/Composer/Downloader/PerforceDownloader.php +++ b/src/Composer/Downloader/PerforceDownloader.php @@ -6,9 +6,6 @@ * (c) Nils Adermann * Jordi Boggiano * - * Contributor: Matt Whittom - * Date: 7/17/13 - * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ @@ -36,7 +33,7 @@ class PerforceDownloader extends VcsDownloader $ref = $package->getSourceReference(); $label = $package->getPrettyVersion(); - $this->io->write(" Cloning ".$ref); + $this->io->write(" Cloning " . $ref); $this->initPerforce($package, $path, $ref); $this->perforce->setStream($ref); $this->perforce->queryP4User($this->io); @@ -46,19 +43,21 @@ class PerforceDownloader extends VcsDownloader $this->perforce->cleanupClientSpec(); } - private function initPerforce($package, $path, $ref){ - if ($this->perforceInjected){ + private function initPerforce($package, $path, $ref) + { + if ($this->perforceInjected) { return; } $repository = $package->getRepository(); $repoConfig = null; - if ($repository instanceof VcsRepository){ + if ($repository instanceof VcsRepository) { $repoConfig = $this->getRepoConfig($repository); } $this->perforce = Perforce::createPerforce($repoConfig, $package->getSourceUrl(), $path); } - private function getRepoConfig(VcsRepository $repository){ + private function getRepoConfig(VcsRepository $repository) + { return $repository->getRepoConfig(); } @@ -75,7 +74,7 @@ class PerforceDownloader extends VcsDownloader */ public function getLocalChanges(PackageInterface $package, $path) { - print ("Perforce driver does not check for local changes before overriding\n"); + $this->io->write("Perforce driver does not check for local changes before overriding", true); return; } @@ -89,10 +88,9 @@ class PerforceDownloader extends VcsDownloader return $commitLogs; } - public function injectPerforce($perforce){ + public function injectPerforce($perforce) + { $this->perforce = $perforce; $this->perforceInjected = true; } - - } diff --git a/src/Composer/Repository/Vcs/PerforceDriver.php b/src/Composer/Repository/Vcs/PerforceDriver.php index 35f1389f7..cc04fea2f 100644 --- a/src/Composer/Repository/Vcs/PerforceDriver.php +++ b/src/Composer/Repository/Vcs/PerforceDriver.php @@ -6,9 +6,6 @@ * (c) Nils Adermann * Jordi Boggiano * - * Contributor: Matt Whittom - * Date: 7/17/13 - * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ @@ -24,7 +21,8 @@ use Composer\Util\Perforce; /** * @author Matt Whittom */ -class PerforceDriver extends VcsDriver { +class PerforceDriver extends VcsDriver +{ protected $depot; protected $branch; protected $perforce; @@ -49,7 +47,7 @@ class PerforceDriver extends VcsDriver { $this->perforce->writeP4ClientSpec(); $this->perforce->connectClient(); - return TRUE; + return true; } private function initPerforce($repoConfig) @@ -67,9 +65,8 @@ class PerforceDriver extends VcsDriver { */ public function getComposerInformation($identifier) { - if (isset($this->composer_info_identifier)){ - if (strcmp($identifier, $this->composer_info_identifier) === 0 ) - { + if (isset($this->composer_info_identifier)) { + if (strcmp($identifier, $this->composer_info_identifier) === 0) { return $this->composer_info; } } @@ -111,7 +108,7 @@ class PerforceDriver extends VcsDriver { */ public function getDist($identifier) { - return NULL; + return null; } /** @@ -123,7 +120,7 @@ class PerforceDriver extends VcsDriver { 'type' => 'perforce', 'url' => $this->repoConfig['url'], 'reference' => $identifier, - 'p4user' => $this->perforce->getUser() + 'p4user' => $this->perforce->getUser() ); return $source; @@ -145,7 +142,7 @@ class PerforceDriver extends VcsDriver { $this->composer_info = $this->perforce->getComposerInformation("//$this->depot/$identifier"); $this->composer_info_identifier = $identifier; $result = false; - if (isset($this->composer_info)){ + if (isset($this->composer_info)) { $result = count($this->composer_info) > 0; } return $result; @@ -156,13 +153,13 @@ class PerforceDriver extends VcsDriver { */ public function getContents($url) { - return FALSE; + return false; } /** * {@inheritDoc} */ - public static function supports(IOInterface $io, $url, $deep = FALSE) + public static function supports(IOInterface $io, $url, $deep = false) { return Perforce::checkServerExists($url, new ProcessExecutor); } diff --git a/src/Composer/Util/Perforce.php b/src/Composer/Util/Perforce.php index 99c640cf1..d22d3a13c 100644 --- a/src/Composer/Util/Perforce.php +++ b/src/Composer/Util/Perforce.php @@ -5,9 +5,6 @@ * (c) Nils Adermann * Jordi Boggiano * - * Contributor: Matt Whittom - * Date: 7/17/13 - * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ @@ -20,7 +17,8 @@ use Composer\IO\IOInterface; /** * @author Matt Whittom */ -class Perforce { +class Perforce +{ protected $path; protected $p4Depot; @@ -37,8 +35,9 @@ class Perforce { protected $windowsFlag; - public static function createPerforce($repoConfig, $port, $path, ProcessExecutor $process = NULL) { - if (!isset($process)){ + public static function createPerforce($repoConfig, $port, $path, ProcessExecutor $process = null) + { + if (!isset($process)) { $process = new ProcessExecutor; } $isWindows = defined('PHP_WINDOWS_VERSION_BUILD'); @@ -47,7 +46,8 @@ class Perforce { return $perforce; } - public function __construct($repoConfig, $port, $path, ProcessExecutor $process, $isWindows) { + public function __construct($repoConfig, $port, $path, ProcessExecutor $process, $isWindows) + { $this->windowsFlag = $isWindows; $this->p4Port = $port; $this->path = $path; @@ -57,25 +57,25 @@ class Perforce { $this->initialize($repoConfig); } - public function initialize($repoConfig){ + public function initialize($repoConfig) + { $this->unique_perforce_client_name = $this->generateUniquePerforceClientName(); - if (!isset ($repoConfig)){ + if (!isset ($repoConfig)) { return; } - if (isset($repoConfig['unique_perforce_client_name'])){ + if (isset($repoConfig['unique_perforce_client_name'])) { $this->unique_perforce_client_name = $repoConfig['unique_perforce_client_name']; } - if (isset($repoConfig['depot'])){ + if (isset($repoConfig['depot'])) { $this->p4Depot = $repoConfig['depot']; } - if (isset($repoConfig['branch'])){ + if (isset($repoConfig['branch'])) { $this->p4Branch = $repoConfig['branch']; } if (isset($repoConfig['p4user'])) { $this->p4User = $repoConfig['p4user']; - } - else { + } else { $this->p4User = $this->getP4variable("P4USER"); } if (isset($repoConfig['p4password'])) { @@ -83,7 +83,8 @@ class Perforce { } } - public function initializeDepotAndBranch($depot, $branch){ + public function initializeDepotAndBranch($depot, $branch) + { if (isset($depot)) { $this->p4Depot = $depot; } @@ -92,11 +93,13 @@ class Perforce { } } - public function generateUniquePerforceClientName(){ - return gethostname() . "_" . time(); + public function generateUniquePerforceClientName() + { + return gethostname() . "_" . time(); } - public function cleanupClientSpec(){ + public function cleanupClientSpec() + { $client = $this->getClient(); $command = "p4 client -d $client"; $this->executeCommand($command); @@ -105,14 +108,16 @@ class Perforce { $fileSystem->remove($clientSpec); } - protected function executeCommand($command) { + protected function executeCommand($command) + { $result = ""; $this->process->execute($command, $result); return $result; } - public function getClient() { + public function getClient() + { if (!isset($this->p4Client)) { $clean_stream_name = str_replace("@", "", str_replace("/", "_", str_replace("//", "", $this->getStream()))); $this->p4Client = "composer_perforce_" . $this->unique_perforce_client_name . "_" . $clean_stream_name; @@ -121,59 +126,67 @@ class Perforce { return $this->p4Client; } - protected function getPath() { + protected function getPath() + { return $this->path; } - protected function getPort() { + protected function getPort() + { return $this->p4Port; } - public function setStream($stream) { + public function setStream($stream) + { $this->p4Stream = $stream; $index = strrpos($stream, "/"); //Stream format is //depot/stream, while non-streaming depot is //depot - if ($index > 2){ + if ($index > 2) { $this->p4DepotType = "stream"; } } - public function isStream() { + public function isStream() + { return (strcmp($this->p4DepotType, "stream") === 0); } - public function getStream() { + public function getStream() + { if (!isset($this->p4Stream)) { if ($this->isStream()) { $this->p4Stream = "//$this->p4Depot/$this->p4Branch"; - } - else { + } else { $this->p4Stream = "//$this->p4Depot"; } } return $this->p4Stream; } - public function getStreamWithoutLabel($stream) { + public function getStreamWithoutLabel($stream) + { $index = strpos($stream, "@"); - if ($index === FALSE) { + if ($index === false) { return $stream; } return substr($stream, 0, $index); } - public function getP4ClientSpec() { + public function getP4ClientSpec() + { $p4clientSpec = $this->path . "/" . $this->getClient() . ".p4.spec"; return $p4clientSpec; } - public function getUser() { + public function getUser() + { return $this->p4User; } - public function queryP4User(IOInterface $io) { + public function queryP4User(IOInterface $io) + { $this->getUser(); if (strlen($this->p4User) > 0) { return; @@ -185,14 +198,14 @@ class Perforce { $this->p4User = $io->ask("Enter P4 User:"); if ($this->windowsFlag) { $command = "p4 set P4USER=$this->p4User"; - } - else { + } else { $command = "export P4USER=$this->p4User"; } $result = $this->executeCommand($command); } - protected function getP4variable($name) { + protected function getP4variable($name) + { if ($this->windowsFlag) { $command = "p4 set"; $result = $this->executeCommand($command); @@ -201,10 +214,9 @@ class Perforce { $fields = explode("=", $line); if (strcmp($name, $fields[0]) == 0) { $index = strpos($fields[1], " "); - if ($index === FALSE) { + if ($index === false) { $value = $fields[1]; - } - else { + } else { $value = substr($fields[1], 0, $index); } $value = trim($value); @@ -212,8 +224,7 @@ class Perforce { return $value; } } - } - else { + } else { $command = 'echo $' . $name; $result = trim($this->executeCommand($command)); @@ -221,7 +232,8 @@ class Perforce { } } - public function queryP4Password(IOInterface $io) { + public function queryP4Password(IOInterface $io) + { if (isset($this->p4Password)) { return $this->p4Password; } @@ -234,7 +246,8 @@ class Perforce { return $password; } - public function generateP4Command($command, $useClient = TRUE) { + public function generateP4Command($command, $useClient = true) + { $p4Command = "p4 "; $p4Command = $p4Command . "-u " . $this->getUser() . " "; if ($useClient) { @@ -246,22 +259,25 @@ class Perforce { return $p4Command; } - public function isLoggedIn() { - $command = $this->generateP4Command("login -s", FALSE); + public function isLoggedIn() + { + $command = $this->generateP4Command("login -s", false); $result = trim($this->executeCommand($command)); $index = strpos($result, $this->getUser()); - if ($index === FALSE) { - return FALSE; + if ($index === false) { + return false; } - return TRUE; + return true; } - public function connectClient() { + public function connectClient() + { $p4CreateClientCommand = $this->generateP4Command("client -i < " . $this->getP4ClientSpec()); $this->executeCommand($p4CreateClientCommand); } - public function syncCodeBase($label) { + public function syncCodeBase($label) + { $prevDir = getcwd(); chdir($this->path); @@ -278,7 +294,8 @@ class Perforce { chdir($prevDir); } - public function writeClientSpecToFile($spec) { + public function writeClientSpecToFile($spec) + { fwrite($spec, "Client: " . $this->getClient() . "\n\n"); fwrite($spec, "Update: " . date("Y/m/d H:i:s") . "\n\n"); fwrite($spec, "Access: " . date("Y/m/d H:i:s") . "\n"); @@ -292,17 +309,20 @@ class Perforce { if ($this->isStream()) { fwrite($spec, "Stream:\n"); fwrite($spec, " " . $this->getStreamWithoutLabel($this->p4Stream) . "\n"); - } - else { + } else { fwrite( - $spec, "View: " . $this->getStream() . "/... //" . $this->getClient() . "/" . str_replace( - "//", "", $this->getStream() - ) . "/... \n" + $spec, + "View: " . $this->getStream() . "/... //" . $this->getClient() . "/" . str_replace( + "//", + "", + $this->getStream() + ) . "/... \n" ); } } - public function writeP4ClientSpec() { + public function writeP4ClientSpec() + { $clientSpec = $this->getP4ClientSpec(); $spec = fopen($clientSpec, 'w'); try { @@ -315,19 +335,21 @@ class Perforce { } - protected function read($pipe, $name) { + protected function read($pipe, $name) + { if (feof($pipe)) { return; } $line = fgets($pipe); - while ($line != FALSE) { + while ($line != false) { $line = fgets($pipe); } return; } - public function windowsLogin($password) { + public function windowsLogin($password) + { $descriptorspec = array( 0 => array("pipe", "r"), 1 => array("pipe", "w"), @@ -336,7 +358,7 @@ class Perforce { $command = $this->generateP4Command(" login -a"); $process = proc_open($command, $descriptorspec, $pipes); if (!is_resource($process)) { - return FALSE; + return false; } fwrite($pipes[0], $password); fclose($pipes[0]); @@ -353,54 +375,56 @@ class Perforce { } - public function p4Login(IOInterface $io) { + public function p4Login(IOInterface $io) + { $this->queryP4User($io); if (!$this->isLoggedIn()) { $password = $this->queryP4Password($io); if ($this->windowsFlag) { $this->windowsLogin($password); - } - else { - $command = "echo $password | " . $this->generateP4Command(" login -a", FALSE); + } else { + $command = "echo $password | " . $this->generateP4Command(" login -a", false); $this->executeCommand($command); } } } - public static function checkServerExists($url, ProcessExecutor $process_executor) { + public static function checkServerExists($url, ProcessExecutor $process_executor) + { $process = $process_executor ? : new ProcessExecutor; $result = ""; $process->execute("p4 -p $url info -s", $result); $index = strpos($result, "error"); - if ($index === FALSE) { - return TRUE; + if ($index === false) { + return true; } - return FALSE; + return false; } - public function getComposerInformation($identifier) { + public function getComposerInformation($identifier) + { $index = strpos($identifier, "@"); - if ($index === FALSE) { + if ($index === false) { $composer_json = "$identifier/composer.json"; return $this->getComposerInformationFromPath($composer_json); - } - else { + } else { return $this->getComposerInformationFromLabel($identifier, $index); } } - public function getComposerInformationFromPath($composer_json) { + public function getComposerInformationFromPath($composer_json) + { $command = $this->generateP4Command(" print $composer_json"); $result = $this->executeCommand($command); $index = strpos($result, "{"); - if ($index === FALSE) { + if ($index === false) { return ""; } if ($index >= 0) { $rawData = substr($result, $index); - $composer_info = json_decode($rawData, TRUE); + $composer_info = json_decode($rawData, true); return $composer_info; } @@ -408,14 +432,15 @@ class Perforce { return ""; } - public function getComposerInformationFromLabel($identifier, $index) { + public function getComposerInformationFromLabel($identifier, $index) + { $composer_json_path = substr($identifier, 0, $index) . "/composer.json" . substr($identifier, $index); - $command = $this->generateP4Command(" files $composer_json_path", FALSE); + $command = $this->generateP4Command(" files $composer_json_path", false); $result = $this->executeCommand($command); $index2 = strpos($result, "no such file(s)."); - if ($index2 === FALSE) { + if ($index2 === false) { $index3 = strpos($result, "change"); - if (!($index3 === FALSE)) { + if (!($index3 === false)) { $phrase = trim(substr($result, $index3)); $fields = explode(" ", $phrase); $id = $fields[1]; @@ -428,12 +453,12 @@ class Perforce { return ""; } - public function getBranches() { + public function getBranches() + { $possible_branches = array(); if (!$this->isStream()) { $possible_branches[$this->p4Branch] = $this->getStream(); - } - else { + } else { $command = $this->generateP4Command("streams //$this->p4Depot/..."); $result = $this->executeCommand($command); $resArray = explode("\n", $result); @@ -451,14 +476,15 @@ class Perforce { return $branches; } - public function getTags() { + public function getTags() + { $command = $this->generateP4Command("labels"); $result = $this->executeCommand($command); $resArray = explode("\n", $result); $tags = array(); foreach ($resArray as $line) { $index = strpos($line, "Label"); - if (!($index === FALSE)) { + if (!($index === false)) { $fields = explode(" ", $line); $tags[$fields[1]] = $this->getStream() . "@" . $fields[1]; } @@ -467,13 +493,14 @@ class Perforce { return $tags; } - public function checkStream() { - $command = $this->generateP4Command("depots", FALSE); + public function checkStream() + { + $command = $this->generateP4Command("depots", false); $result = $this->executeCommand($command); $resArray = explode("\n", $result); foreach ($resArray as $line) { $index = strpos($line, "Depot"); - if (!($index === FALSE)) { + if (!($index === false)) { $fields = explode(" ", $line); if (strcmp($this->p4Depot, $fields[1]) === 0) { $this->p4DepotType = $fields[3]; @@ -483,31 +510,34 @@ class Perforce { } } - return FALSE; + return false; } - protected function getChangeList($reference){ + protected function getChangeList($reference) + { $index = strpos($reference, "@"); - if ($index === false){ + if ($index === false) { return; } $label = substr($reference, $index); $command = $this->generateP4Command(" changes -m1 $label"); $changes = $this->executeCommand($command); - if (strpos($changes, "Change") !== 0){ + if (strpos($changes, "Change") !== 0) { return; } $fields = explode(" ", $changes); $changeList = $fields[1]; return $changeList; } - public function getCommitLogs($fromReference, $toReference){ + + public function getCommitLogs($fromReference, $toReference) + { $fromChangeList = $this->getChangeList($fromReference); - if ($fromChangeList == null){ + if ($fromChangeList == null) { return; } $toChangeList = $this->getChangeList($toReference); - if ($toChangeList == null){ + if ($toChangeList == null) { return; } $index = strpos($fromReference, "@"); diff --git a/tests/Composer/Test/Downloader/PerforceDownloaderTest.php b/tests/Composer/Test/Downloader/PerforceDownloaderTest.php index 527ef9f6d..557507090 100644 --- a/tests/Composer/Test/Downloader/PerforceDownloaderTest.php +++ b/tests/Composer/Test/Downloader/PerforceDownloaderTest.php @@ -5,9 +5,6 @@ * (c) Nils Adermann * Jordi Boggiano * - * Contributor: Matt Whittom - * Date: 7/17/13 - * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ @@ -22,14 +19,16 @@ use Composer\Repository\VcsRepository; /** * @author Matt Whittom */ -class PerforceDownloaderTest extends \PHPUnit_Framework_TestCase { +class PerforceDownloaderTest extends \PHPUnit_Framework_TestCase +{ private $io; private $config; private $testPath; public static $repository; - function setUp() { + function setUp() + { $this->testPath = sys_get_temp_dir() . '/composer-test'; $this->config = new Config(); $this->config->merge( @@ -43,58 +42,67 @@ class PerforceDownloaderTest extends \PHPUnit_Framework_TestCase { } - public function testDoDownloadGetRepoConfig() { + public function testDoDownloadGetRepoConfig() + { $downloader = new PerforceDownloader($this->io, $this->config); - $package = $this->getMock('Composer\Package\PackageInterface' ); - $repoConfig = array('url'=>'TEST_URL','p4user'=>'TEST_USER'); - $repository = $this->getMock('Composer\Repository\VcsRepository', array('getRepoConfig'), array($repoConfig, $this->io, $this->config)); + $package = $this->getMock('Composer\Package\PackageInterface'); + $repoConfig = array('url' => 'TEST_URL', 'p4user' => 'TEST_USER'); + $repository = $this->getMock( + 'Composer\Repository\VcsRepository', + array('getRepoConfig'), + array($repoConfig, $this->io, $this->config) + ); $package->expects($this->at(0)) - ->method('getSourceReference') - ->will($this->returnValue("SOURCE_REF")); + ->method('getSourceReference') + ->will($this->returnValue("SOURCE_REF")); $package->expects($this->at(1)) - ->method('getPrettyVersion') - ->will($this->returnValue("100")); + ->method('getPrettyVersion') + ->will($this->returnValue("100")); $package->expects($this->at(2)) - ->method('getRepository') - ->will($this->returnValue($repository)); + ->method('getRepository') + ->will($this->returnValue($repository)); $repository->expects($this->at(0)) - ->method('getRepoConfig'); + ->method('getRepoConfig'); $path = $this->testPath; $downloader->doDownload($package, $path); } - public function testDoDownload() { + public function testDoDownload() + { $downloader = new PerforceDownloader($this->io, $this->config); - $repoConfig = array("depot"=>"TEST_DEPOT", "branch"=>"TEST_BRANCH", "p4user"=>"TEST_USER"); + $repoConfig = array("depot" => "TEST_DEPOT", "branch" => "TEST_BRANCH", "p4user" => "TEST_USER"); $port = "TEST_PORT"; $path = "TEST_PATH"; $process = $this->getmock('Composer\Util\ProcessExecutor'); - $perforce = $this->getMock('Composer\Util\Perforce', array('setStream', 'queryP4User', 'writeP4ClientSpec', 'connectClient', 'syncCodeBase'), array($repoConfig, $port, $path, $process, true, "TEST")); + $perforce = $this->getMock( + 'Composer\Util\Perforce', + array('setStream', 'queryP4User', 'writeP4ClientSpec', 'connectClient', 'syncCodeBase'), + array($repoConfig, $port, $path, $process, true, "TEST") + ); $ref = "SOURCE_REF"; $label = "LABEL"; $perforce->expects($this->at(0)) - ->method('setStream') - ->with($this->equalTo($ref)); + ->method('setStream') + ->with($this->equalTo($ref)); $perforce->expects($this->at(1)) - ->method('queryP4User') - ->with($this->io); + ->method('queryP4User') + ->with($this->io); $perforce->expects($this->at(2)) - ->method('writeP4ClientSpec'); + ->method('writeP4ClientSpec'); $perforce->expects($this->at(3)) - ->method('connectClient'); + ->method('connectClient'); $perforce->expects($this->at(4)) - ->method('syncCodeBase') - ->with($this->equalTo($label)); + ->method('syncCodeBase') + ->with($this->equalTo($label)); $downloader->injectPerforce($perforce); - $package = $this->getMock('Composer\Package\PackageInterface' ); + $package = $this->getMock('Composer\Package\PackageInterface'); $package->expects($this->at(0)) - ->method('getSourceReference') - ->will($this->returnValue($ref)); + ->method('getSourceReference') + ->will($this->returnValue($ref)); $package->expects($this->at(1)) - ->method('getPrettyVersion') - ->will($this->returnValue($label)); + ->method('getPrettyVersion') + ->will($this->returnValue($label)); $path = $this->testPath; $downloader->doDownload($package, $path); - } } diff --git a/tests/Composer/Test/Repository/Vcs/PerforceDriverTest.php b/tests/Composer/Test/Repository/Vcs/PerforceDriverTest.php index ef0fc694a..77c41a0e1 100644 --- a/tests/Composer/Test/Repository/Vcs/PerforceDriverTest.php +++ b/tests/Composer/Test/Repository/Vcs/PerforceDriverTest.php @@ -5,9 +5,6 @@ * (c) Nils Adermann * Jordi Boggiano * - * Contributor: matt-whittom - * Date: 7/17/13 - * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ @@ -22,14 +19,16 @@ use Composer\Config; /** * @author Matt Whittom */ -class PerforceDriverTest extends \PHPUnit_Framework_TestCase { +class PerforceDriverTest extends \PHPUnit_Framework_TestCase +{ private $config; private $io; private $process; private $remoteFileSystem; private $testPath; - public function setUp() { + public function setUp() + { $this->testPath = sys_get_temp_dir() . '/composer-test'; $this->config = new Config(); $this->config->merge( @@ -42,24 +41,34 @@ class PerforceDriverTest extends \PHPUnit_Framework_TestCase { $this->io = $this->getMock('Composer\IO\IOInterface'); $this->process = $this->getMock('Composer\Util\ProcessExecutor'); - $this->remoteFileSystem = $this->getMockBuilder('Composer\Util\RemoteFilesystem')->disableOriginalConstructor()->getMock(); + $this->remoteFileSystem = $this->getMockBuilder('Composer\Util\RemoteFilesystem')->disableOriginalConstructor() + ->getMock(); } - public function tearDown() { + public function tearDown() + { $fs = new Filesystem; $fs->removeDirectory($this->testPath); } - public function testInitializeCapturesVariablesFromRepoConfig() { + public function testInitializeCapturesVariablesFromRepoConfig() + { $this->setUp(); $repo_config = array( - 'url' => 'TEST_PERFORCE_URL', - 'depot' => 'TEST_DEPOT_CONFIG', + 'url' => 'TEST_PERFORCE_URL', + 'depot' => 'TEST_DEPOT_CONFIG', 'branch' => 'TEST_BRANCH_CONFIG' ); $driver = new PerforceDriver($repo_config, $this->io, $this->config, $this->process, $this->remoteFileSystem); $process = $this->getMock('Composer\Util\ProcessExecutor'); - $arguments = array(array('depot'=>'TEST_DEPOT', 'branch'=>'TEST_BRANCH'), 'port'=>'TEST_PORT', 'path'=>$this->testPath, $process, true, "TEST"); + $arguments = array( + array('depot' => 'TEST_DEPOT', 'branch' => 'TEST_BRANCH'), + 'port' => 'TEST_PORT', + 'path' => $this->testPath, + $process, + true, + "TEST" + ); $perforce = $this->getMock('Composer\Util\Perforce', null, $arguments); $driver->injectPerforce($perforce); $driver->initialize(); @@ -68,51 +77,59 @@ class PerforceDriverTest extends \PHPUnit_Framework_TestCase { $this->assertEquals("TEST_BRANCH_CONFIG", $driver->getBranch()); } - public function testInitializeLogsInAndConnectsClient() { + public function testInitializeLogsInAndConnectsClient() + { $this->setUp(); $repo_config = array( - 'url' => 'TEST_PERFORCE_URL', - 'depot' => 'TEST_DEPOT_CONFIG', + 'url' => 'TEST_PERFORCE_URL', + 'depot' => 'TEST_DEPOT_CONFIG', 'branch' => 'TEST_BRANCH_CONFIG' ); $driver = new PerforceDriver($repo_config, $this->io, $this->config, $this->process, $this->remoteFileSystem); $perforce = $this->getMockBuilder('Composer\Util\Perforce')->disableOriginalConstructor()->getMock(); $perforce->expects($this->at(0)) - ->method('p4Login') - ->with($this->io); + ->method('p4Login') + ->with($this->io); $perforce->expects($this->at(1)) - ->method('checkStream') - ->with($this->equalTo("TEST_DEPOT_CONFIG")); + ->method('checkStream') + ->with($this->equalTo("TEST_DEPOT_CONFIG")); $perforce->expects($this->at(2)) - ->method('writeP4ClientSpec'); + ->method('writeP4ClientSpec'); $perforce->expects($this->at(3)) - ->method('connectClient'); + ->method('connectClient'); $driver->injectPerforce($perforce); $driver->initialize(); } - public function testHasComposerFile() { + public function testHasComposerFile() + { $this->setUp(); $repo_config = array( - 'url' => 'TEST_PERFORCE_URL', - 'depot' => 'TEST_DEPOT_CONFIG', + 'url' => 'TEST_PERFORCE_URL', + 'depot' => 'TEST_DEPOT_CONFIG', 'branch' => 'TEST_BRANCH_CONFIG' ); $driver = new PerforceDriver($repo_config, $this->io, $this->config, $this->process, $this->remoteFileSystem); $process = $this->getMock('Composer\Util\ProcessExecutor'); - $arguments = array(array('depot'=>'TEST_DEPOT', 'branch'=>'TEST_BRANCH'), 'port'=>'TEST_PORT', 'path'=>$this->testPath, $process, true, "TEST"); + $arguments = array( + array('depot' => 'TEST_DEPOT', 'branch' => 'TEST_BRANCH'), + 'port' => 'TEST_PORT', + 'path' => $this->testPath, + $process, + true, + "TEST" + ); $perforce = $this->getMock('Composer\Util\Perforce', array('getComposerInformation'), $arguments); $perforce->expects($this->at(0)) - ->method('getComposerInformation') - ->with($this->equalTo("//TEST_DEPOT_CONFIG/TEST_IDENTIFIER")) - ->will($this->returnValue("Some json stuff")); + ->method('getComposerInformation') + ->with($this->equalTo("//TEST_DEPOT_CONFIG/TEST_IDENTIFIER")) + ->will($this->returnValue("Some json stuff")); $driver->injectPerforce($perforce); $driver->initialize(); $identifier = "TEST_IDENTIFIER"; $result = $driver->hasComposerFile($identifier); $this->assertTrue($result); } - } diff --git a/tests/Composer/Test/Util/PerforceTest.php b/tests/Composer/Test/Util/PerforceTest.php index e4e0e36b3..0cca37c2e 100644 --- a/tests/Composer/Test/Util/PerforceTest.php +++ b/tests/Composer/Test/Util/PerforceTest.php @@ -5,9 +5,6 @@ * (c) Nils Adermann * Jordi Boggiano * - * Contributor: Matt Whittom - * Date: 7/17/13 - * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ @@ -21,18 +18,26 @@ use Composer\Util\ProcessExecutor; /** * @author Matt Whittom */ -class PerforceTest extends \PHPUnit_Framework_TestCase { +class PerforceTest extends \PHPUnit_Framework_TestCase +{ protected $perforce; protected $processExecutor; - public function setUp() { + public function setUp() + { $this->processExecutor = $this->getMock('Composer\Util\ProcessExecutor'); - $repoConfig = array("depot"=>"depot", "branch"=>"branch", "p4user"=>"user", "unique_perforce_client_name" => "TEST"); + $repoConfig = array( + "depot" => "depot", + "branch" => "branch", + "p4user" => "user", + "unique_perforce_client_name" => "TEST" + ); $this->perforce = new Perforce($repoConfig, "port", "path", $this->processExecutor, true); } - public function testGetClientWithoutStream() { + public function testGetClientWithoutStream() + { $client = $this->perforce->getClient(); $hostname = gethostname(); $timestamp = time(); @@ -41,7 +46,8 @@ class PerforceTest extends \PHPUnit_Framework_TestCase { $this->assertEquals($expected, $client); } - public function testGetClientFromStream() { + public function testGetClientFromStream() + { $this->setPerforceToStream(); $client = $this->perforce->getClient(); @@ -50,12 +56,14 @@ class PerforceTest extends \PHPUnit_Framework_TestCase { $this->assertEquals($expected, $client); } - public function testGetStreamWithoutStream() { + public function testGetStreamWithoutStream() + { $stream = $this->perforce->getStream(); $this->assertEquals("//depot", $stream); } - public function testGetStreamWithStream() { + public function testGetStreamWithStream() + { $this->setPerforceToStream(); $stream = $this->perforce->getStream(); @@ -63,56 +71,70 @@ class PerforceTest extends \PHPUnit_Framework_TestCase { } - public function testGetStreamWithoutLabelWithStreamWithoutLabel(){ + public function testGetStreamWithoutLabelWithStreamWithoutLabel() + { $stream = $this->perforce->getStreamWithoutLabel("//depot/branch"); $this->assertEquals("//depot/branch", $stream); } - public function testGetStreamWithoutLabelWithStreamWithLabel(){ + public function testGetStreamWithoutLabelWithStreamWithLabel() + { $stream = $this->perforce->getStreamWithoutLabel("//depot/branching@label"); $this->assertEquals("//depot/branching", $stream); } - public function testGetClientSpec() { + public function testGetClientSpec() + { $clientSpec = $this->perforce->getP4ClientSpec(); $expected = "path/composer_perforce_TEST_depot.p4.spec"; $this->assertEquals($expected, $clientSpec); } - public function testGenerateP4Command() { + public function testGenerateP4Command() + { $command = "do something"; $p4Command = $this->perforce->generateP4Command($command); $expected = "p4 -u user -c composer_perforce_TEST_depot -p port do something"; $this->assertEquals($expected, $p4Command); } - public function testQueryP4UserWithUserAlreadySet(){ + public function testQueryP4UserWithUserAlreadySet() + { $io = $this->getMock('Composer\IO\IOInterface'); - $repoConfig = array("depot"=>"depot", "branch"=>"branch", "p4user"=>"TEST_USER"); + $repoConfig = array("depot" => "depot", "branch" => "branch", "p4user" => "TEST_USER"); $this->perforce = new Perforce($repoConfig, "port", "path", $this->processExecutor, true, "TEST"); $this->perforce->queryP4user($io); $this->assertEquals("TEST_USER", $this->perforce->getUser()); } - public function testQueryP4UserWithUserSetInP4VariablesWithWindowsOS(){ - $repoConfig = array("depot"=>"depot", "branch"=>"branch"); + public function testQueryP4UserWithUserSetInP4VariablesWithWindowsOS() + { + $repoConfig = array("depot" => "depot", "branch" => "branch"); $this->perforce = new Perforce($repoConfig, "port", "path", $this->processExecutor, true, "TEST"); $io = $this->getMock('Composer\IO\IOInterface'); $expectedCommand = "p4 set"; $this->processExecutor->expects($this->at(0)) - ->method('execute') - ->with($this->equalTo($expectedCommand)) - ->will($this->returnCallback(function($command, &$output) {$output = "P4USER=TEST_P4VARIABLE_USER\n"; return true;})); + ->method('execute') + ->with($this->equalTo($expectedCommand)) + ->will( + $this->returnCallback( + function ($command, &$output) { + $output = "P4USER=TEST_P4VARIABLE_USER\n"; + return true; + } + ) + ); $this->perforce->queryP4user($io); $this->assertEquals("TEST_P4VARIABLE_USER", $this->perforce->getUser()); } - public function testQueryP4UserWithUserSetInP4VariablesNotWindowsOS(){ - $repoConfig = array("depot"=>"depot", "branch"=>"branch"); + public function testQueryP4UserWithUserSetInP4VariablesNotWindowsOS() + { + $repoConfig = array("depot" => "depot", "branch" => "branch"); $this->perforce = new Perforce($repoConfig, "port", "path", $this->processExecutor, false, "TEST"); $io = $this->getMock('Composer\IO\IOInterface'); @@ -120,47 +142,57 @@ class PerforceTest extends \PHPUnit_Framework_TestCase { $this->processExecutor->expects($this->at(0)) ->method('execute') ->with($this->equalTo($expectedCommand)) - ->will($this->returnCallback(function($command, &$output) {$output = "TEST_P4VARIABLE_USER\n"; return true;})); + ->will( + $this->returnCallback( + function ($command, &$output) { + $output = "TEST_P4VARIABLE_USER\n"; + return true; + } + ) + ); $this->perforce->queryP4user($io); $this->assertEquals("TEST_P4VARIABLE_USER", $this->perforce->getUser()); } - public function testQueryP4UserQueriesForUser(){ - $repoConfig = array("depot"=>"depot", "branch"=>"branch"); + public function testQueryP4UserQueriesForUser() + { + $repoConfig = array("depot" => "depot", "branch" => "branch"); $this->perforce = new Perforce($repoConfig, "port", "path", $this->processExecutor, false, "TEST"); $io = $this->getMock('Composer\IO\IOInterface'); $expectedQuestion = "Enter P4 User:"; $io->expects($this->at(0)) - ->method('ask') - ->with($this->equalTo($expectedQuestion)) - ->will($this->returnValue("TEST_QUERY_USER")); + ->method('ask') + ->with($this->equalTo($expectedQuestion)) + ->will($this->returnValue("TEST_QUERY_USER")); $this->perforce->queryP4user($io); $this->assertEquals("TEST_QUERY_USER", $this->perforce->getUser()); } - public function testQueryP4UserStoresResponseToQueryForUserWithWindows(){ - $repoConfig = array("depot"=>"depot", "branch"=>"branch"); + public function testQueryP4UserStoresResponseToQueryForUserWithWindows() + { + $repoConfig = array("depot" => "depot", "branch" => "branch"); $this->perforce = new Perforce($repoConfig, "port", "path", $this->processExecutor, true, "TEST"); $io = $this->getMock('Composer\IO\IOInterface'); $expectedQuestion = "Enter P4 User:"; $io->expects($this->at(0)) - ->method('ask') - ->with($this->equalTo($expectedQuestion)) - ->will($this->returnValue("TEST_QUERY_USER")); + ->method('ask') + ->with($this->equalTo($expectedQuestion)) + ->will($this->returnValue("TEST_QUERY_USER")); $expectedCommand = "p4 set P4USER=TEST_QUERY_USER"; $this->processExecutor->expects($this->at(1)) - ->method('execute') - ->with($this->equalTo($expectedCommand)) - ->will($this->returnValue(0)); + ->method('execute') + ->with($this->equalTo($expectedCommand)) + ->will($this->returnValue(0)); $this->perforce->queryP4user($io); } - public function testQueryP4UserStoresResponseToQueryForUserWithoutWindows(){ - $repoConfig = array("depot"=>"depot", "branch"=>"branch"); + public function testQueryP4UserStoresResponseToQueryForUserWithoutWindows() + { + $repoConfig = array("depot" => "depot", "branch" => "branch"); $this->perforce = new Perforce($repoConfig, "port", "path", $this->processExecutor, false, "TEST"); $io = $this->getMock('Composer\IO\IOInterface'); @@ -178,8 +210,14 @@ class PerforceTest extends \PHPUnit_Framework_TestCase { $this->perforce->queryP4user($io); } - public function testQueryP4PasswordWithPasswordAlreadySet(){ - $repoConfig = array("depot"=>"depot", "branch"=>"branch", "p4user"=>"user", "p4password"=>"TEST_PASSWORD"); + public function testQueryP4PasswordWithPasswordAlreadySet() + { + $repoConfig = array( + "depot" => "depot", + "branch" => "branch", + "p4user" => "user", + "p4password" => "TEST_PASSWORD" + ); $this->perforce = new Perforce($repoConfig, "port", "path", $this->processExecutor, false, "TEST"); $io = $this->getMock('Composer\IO\IOInterface'); @@ -187,21 +225,30 @@ class PerforceTest extends \PHPUnit_Framework_TestCase { $this->assertEquals("TEST_PASSWORD", $password); } - public function testQueryP4PasswordWithPasswordSetInP4VariablesWithWindowsOS(){ + public function testQueryP4PasswordWithPasswordSetInP4VariablesWithWindowsOS() + { $io = $this->getMock('Composer\IO\IOInterface'); $expectedCommand = "p4 set"; $this->processExecutor->expects($this->at(0)) - ->method('execute') - ->with($this->equalTo($expectedCommand)) - ->will($this->returnCallback(function($command, &$output) {$output = "P4PASSWD=TEST_P4VARIABLE_PASSWORD\n"; return true;})); + ->method('execute') + ->with($this->equalTo($expectedCommand)) + ->will( + $this->returnCallback( + function ($command, &$output) { + $output = "P4PASSWD=TEST_P4VARIABLE_PASSWORD\n"; + return true; + } + ) + ); $password = $this->perforce->queryP4Password($io); $this->assertEquals("TEST_P4VARIABLE_PASSWORD", $password); } - public function testQueryP4PasswordWithPasswordSetInP4VariablesNotWindowsOS(){ - $repoConfig = array("depot"=>"depot", "branch"=>"branch", "p4user"=>"user"); + public function testQueryP4PasswordWithPasswordSetInP4VariablesNotWindowsOS() + { + $repoConfig = array("depot" => "depot", "branch" => "branch", "p4user" => "user"); $this->perforce = new Perforce($repoConfig, "port", "path", $this->processExecutor, false, "TEST"); $io = $this->getMock('Composer\IO\IOInterface'); @@ -209,30 +256,39 @@ class PerforceTest extends \PHPUnit_Framework_TestCase { $this->processExecutor->expects($this->at(0)) ->method('execute') ->with($this->equalTo($expectedCommand)) - ->will($this->returnCallback(function($command, &$output) {$output = "TEST_P4VARIABLE_PASSWORD\n"; return true;})); + ->will( + $this->returnCallback( + function ($command, &$output) { + $output = "TEST_P4VARIABLE_PASSWORD\n"; + return true; + } + ) + ); $password = $this->perforce->queryP4Password($io); $this->assertEquals("TEST_P4VARIABLE_PASSWORD", $password); } - public function testQueryP4PasswordQueriesForPassword(){ + public function testQueryP4PasswordQueriesForPassword() + { $io = $this->getMock('Composer\IO\IOInterface'); $expectedQuestion = "Enter password for Perforce user user: "; $io->expects($this->at(0)) - ->method('askAndHideAnswer') - ->with($this->equalTo($expectedQuestion)) - ->will($this->returnValue("TEST_QUERY_PASSWORD")); + ->method('askAndHideAnswer') + ->with($this->equalTo($expectedQuestion)) + ->will($this->returnValue("TEST_QUERY_PASSWORD")); $password = $this->perforce->queryP4Password($io); $this->assertEquals("TEST_QUERY_PASSWORD", $password); } - public function testWriteP4ClientSpecWithoutStream() { + public function testWriteP4ClientSpecWithoutStream() + { $stream = fopen("php://memory", 'w+'); $this->perforce->writeClientSpecToFile($stream); rewind($stream); - $expectedArray = $this->getExpectedClientSpec(FALSE); + $expectedArray = $this->getExpectedClientSpec(false); try { foreach ($expectedArray as $expected) { $this->assertStringStartsWith($expected, fgets($stream)); @@ -245,14 +301,15 @@ class PerforceTest extends \PHPUnit_Framework_TestCase { fclose($stream); } - public function testWriteP4ClientSpecWithStream() { + public function testWriteP4ClientSpecWithStream() + { $this->setPerforceToStream(); $stream = fopen("php://memory", 'w+'); $this->perforce->writeClientSpecToFile($stream); rewind($stream); - $expectedArray = $this->getExpectedClientSpec(TRUE); + $expectedArray = $this->getExpectedClientSpec(true); try { foreach ($expectedArray as $expected) { $this->assertStringStartsWith($expected, fgets($stream)); @@ -265,17 +322,19 @@ class PerforceTest extends \PHPUnit_Framework_TestCase { fclose($stream); } - public function testIsLoggedIn() { + public function testIsLoggedIn() + { $expectedCommand = "p4 -u user -p port login -s"; $this->processExecutor->expects($this->at(0)) - ->method('execute') - ->with($this->equalTo($expectedCommand), $this->equalTo(null)) - ->will($this->returnValue(0)); + ->method('execute') + ->with($this->equalTo($expectedCommand), $this->equalTo(null)) + ->will($this->returnValue(0)); $this->perforce->isLoggedIn(); } - public function testConnectClient() { + public function testConnectClient() + { $expectedCommand = "p4 -u user -c composer_perforce_TEST_depot -p port client -i < path/composer_perforce_TEST_depot.p4.spec"; $this->processExecutor->expects($this->at(0)) ->method('execute') @@ -285,70 +344,112 @@ class PerforceTest extends \PHPUnit_Framework_TestCase { $this->perforce->connectClient(); } - public function testGetBranchesWithStream() { + public function testGetBranchesWithStream() + { $this->setPerforceToStream(); $expectedCommand = "p4 -u user -c composer_perforce_TEST_depot_branch -p port streams //depot/..."; $this->processExecutor->expects($this->at(0)) ->method('execute') ->with($this->equalTo($expectedCommand)) - ->will($this->returnCallback(function($command, &$output) {$output = "Stream //depot/branch mainline none 'branch'\n"; return true;})); + ->will( + $this->returnCallback( + function ($command, &$output) { + $output = "Stream //depot/branch mainline none 'branch'\n"; + return true; + } + ) + ); $branches = $this->perforce->getBranches(); $this->assertEquals("//depot/branch", $branches['master']); } - public function testGetBranchesWithoutStream() { + public function testGetBranchesWithoutStream() + { $branches = $this->perforce->getBranches(); $this->assertEquals("//depot", $branches['master']); } - public function testGetTagsWithoutStream() { + public function testGetTagsWithoutStream() + { $expectedCommand = "p4 -u user -c composer_perforce_TEST_depot -p port labels"; $this->processExecutor->expects($this->at(0)) ->method('execute') ->with($this->equalTo($expectedCommand)) - ->will($this->returnCallback(function($command, &$output) {$output = "Label 0.0.1 2013/07/31 'First Label!'\nLabel 0.0.2 2013/08/01 'Second Label!'\n"; return true;})); + ->will( + $this->returnCallback( + function ($command, &$output) { + $output = "Label 0.0.1 2013/07/31 'First Label!'\nLabel 0.0.2 2013/08/01 'Second Label!'\n"; + return true; + } + ) + ); $tags = $this->perforce->getTags(); $this->assertEquals("//depot@0.0.1", $tags['0.0.1']); $this->assertEquals("//depot@0.0.2", $tags['0.0.2']); } - public function testGetTagsWithStream() { + public function testGetTagsWithStream() + { $this->setPerforceToStream(); $expectedCommand = "p4 -u user -c composer_perforce_TEST_depot_branch -p port labels"; $this->processExecutor->expects($this->at(0)) ->method('execute') ->with($this->equalTo($expectedCommand)) - ->will($this->returnCallback(function($command, &$output) {$output = "Label 0.0.1 2013/07/31 'First Label!'\nLabel 0.0.2 2013/08/01 'Second Label!'\n"; return true;})); + ->will( + $this->returnCallback( + function ($command, &$output) { + $output = "Label 0.0.1 2013/07/31 'First Label!'\nLabel 0.0.2 2013/08/01 'Second Label!'\n"; + return true; + } + ) + ); $tags = $this->perforce->getTags(); $this->assertEquals("//depot/branch@0.0.1", $tags['0.0.1']); $this->assertEquals("//depot/branch@0.0.2", $tags['0.0.2']); } - public function testCheckStreamWithoutStream() { + public function testCheckStreamWithoutStream() + { $result = $this->perforce->checkStream("depot"); $this->assertFalse($result); $this->assertFalse($this->perforce->isStream()); } - public function testCheckStreamWithStream() { + public function testCheckStreamWithStream() + { $this->processExecutor->expects($this->any())->method('execute') - ->will($this->returnCallback(function($command, &$output) {$output = "Depot depot 2013/06/25 stream /p4/1/depots/depot/... 'Created by Me'"; return true;})); + ->will( + $this->returnCallback( + function ($command, &$output) { + $output = "Depot depot 2013/06/25 stream /p4/1/depots/depot/... 'Created by Me'"; + return true; + } + ) + ); $result = $this->perforce->checkStream("depot"); $this->assertTrue($result); $this->assertTrue($this->perforce->isStream()); } - public function testGetComposerInformationWithoutLabelWithoutStream() { + public function testGetComposerInformationWithoutLabelWithoutStream() + { $expectedCommand = "p4 -u user -c composer_perforce_TEST_depot -p port print //depot/composer.json"; $this->processExecutor->expects($this->at(0)) ->method('execute') ->with($this->equalTo($expectedCommand)) - ->will($this->returnCallback(function($command, &$output) {$output = PerforceTest::getComposerJson(); return true;})); + ->will( + $this->returnCallback( + function ($command, &$output) { + $output = PerforceTest::getComposerJson(); + return true; + } + ) + ); $result = $this->perforce->getComposerInformation("//depot"); $expected = array( @@ -360,18 +461,33 @@ class PerforceTest extends \PHPUnit_Framework_TestCase { $this->assertEquals($expected, $result); } - public function testGetComposerInformationWithLabelWithoutStream() { + public function testGetComposerInformationWithLabelWithoutStream() + { $expectedCommand = "p4 -u user -p port files //depot/composer.json@0.0.1"; $this->processExecutor->expects($this->at(0)) ->method('execute') ->with($this->equalTo($expectedCommand)) - ->will($this->returnCallback(function($command, &$output) {$output = "//depot/composer.json#1 - branch change 10001 (text)"; return true;})); + ->will( + $this->returnCallback( + function ($command, &$output) { + $output = "//depot/composer.json#1 - branch change 10001 (text)"; + return true; + } + ) + ); $expectedCommand = "p4 -u user -c composer_perforce_TEST_depot -p port print //depot/composer.json@10001"; $this->processExecutor->expects($this->at(1)) ->method('execute') ->with($this->equalTo($expectedCommand)) - ->will($this->returnCallback(function($command, &$output) {$output = PerforceTest::getComposerJson(); return true;})); + ->will( + $this->returnCallback( + function ($command, &$output) { + $output = PerforceTest::getComposerJson(); + return true; + } + ) + ); $result = $this->perforce->getComposerInformation("//depot@0.0.1"); @@ -384,14 +500,22 @@ class PerforceTest extends \PHPUnit_Framework_TestCase { $this->assertEquals($expected, $result); } - public function testGetComposerInformationWithoutLabelWithStream() { + public function testGetComposerInformationWithoutLabelWithStream() + { $this->setPerforceToStream(); $expectedCommand = "p4 -u user -c composer_perforce_TEST_depot_branch -p port print //depot/branch/composer.json"; $this->processExecutor->expects($this->at(0)) ->method('execute') ->with($this->equalTo($expectedCommand)) - ->will($this->returnCallback(function($command, &$output) {$output = PerforceTest::getComposerJson(); return true;})); + ->will( + $this->returnCallback( + function ($command, &$output) { + $output = PerforceTest::getComposerJson(); + return true; + } + ) + ); $result = $this->perforce->getComposerInformation("//depot/branch"); @@ -404,19 +528,34 @@ class PerforceTest extends \PHPUnit_Framework_TestCase { $this->assertEquals($expected, $result); } - public function testGetComposerInformationWithLabelWithStream() { + public function testGetComposerInformationWithLabelWithStream() + { $this->setPerforceToStream(); $expectedCommand = "p4 -u user -p port files //depot/branch/composer.json@0.0.1"; $this->processExecutor->expects($this->at(0)) ->method('execute') ->with($this->equalTo($expectedCommand)) - ->will($this->returnCallback(function($command, &$output) {$output = "//depot/composer.json#1 - branch change 10001 (text)"; return true;})); + ->will( + $this->returnCallback( + function ($command, &$output) { + $output = "//depot/composer.json#1 - branch change 10001 (text)"; + return true; + } + ) + ); $expectedCommand = "p4 -u user -c composer_perforce_TEST_depot_branch -p port print //depot/branch/composer.json@10001"; $this->processExecutor->expects($this->at(1)) ->method('execute') ->with($this->equalTo($expectedCommand)) - ->will($this->returnCallback(function($command, &$output) {$output = PerforceTest::getComposerJson(); return true;})); + ->will( + $this->returnCallback( + function ($command, &$output) { + $output = PerforceTest::getComposerJson(); + return true; + } + ) + ); $result = $this->perforce->getComposerInformation("//depot/branch@0.0.1"); @@ -429,7 +568,8 @@ class PerforceTest extends \PHPUnit_Framework_TestCase { $this->assertEquals($expected, $result); } - public function testSyncCodeBaseWithoutStream() { + public function testSyncCodeBaseWithoutStream() + { $expectedCommand = "p4 -u user -c composer_perforce_TEST_depot -p port sync -f @label"; $this->processExecutor->expects($this->at(1)) ->method('execute') @@ -439,44 +579,48 @@ class PerforceTest extends \PHPUnit_Framework_TestCase { $this->perforce->syncCodeBase("label"); } - public function testSyncCodeBaseWithStream() { + public function testSyncCodeBaseWithStream() + { $this->setPerforceToStream(); $expectedCommand = "p4 -u user -c composer_perforce_TEST_depot_branch -p port sync -f @label"; - $this->processExecutor->expects($this->at(1)) - ->method('execute') - ->with($this->equalTo($expectedCommand)) - ->will($this->returnValue(0)); + $this->processExecutor->expects($this->at(1)) + ->method('execute') + ->with($this->equalTo($expectedCommand)) + ->will($this->returnValue(0)); $this->perforce->syncCodeBase("label"); } - public function testCheckServerExists() { + public function testCheckServerExists() + { $processExecutor = $this->getMock('Composer\Util\ProcessExecutor'); $expectedCommand = "p4 -p perforce.does.exist:port info -s"; $processExecutor->expects($this->at(0)) - ->method('execute') - ->with($this->equalTo($expectedCommand), $this->equalTo(null)) - ->will($this->returnValue(0)); + ->method('execute') + ->with($this->equalTo($expectedCommand), $this->equalTo(null)) + ->will($this->returnValue(0)); $result = $this->perforce->checkServerExists("perforce.does.exist:port", $processExecutor); $this->assertTrue($result); } - public function testCheckServerExistsWithFailure() { + public function testCheckServerExistsWithFailure() + { $processExecutor = $this->getMock('Composer\Util\ProcessExecutor'); $expectedCommand = "p4 -p perforce.does.not.exist:port info -s"; $processExecutor->expects($this->at(0)) - ->method('execute') - ->with($this->equalTo($expectedCommand), $this->equalTo(null)) - ->will($this->returnValue("Perforce client error:")); + ->method('execute') + ->with($this->equalTo($expectedCommand), $this->equalTo(null)) + ->will($this->returnValue("Perforce client error:")); $result = $this->perforce->checkServerExists("perforce.does.not.exist:port", $processExecutor); $this->assertTrue($result); } - public static function getComposerJson() { + public static function getComposerJson() + { $composer_json = array( '{', '"name": "test/perforce",', @@ -492,7 +636,8 @@ class PerforceTest extends \PHPUnit_Framework_TestCase { return implode($composer_json); } - private function getExpectedClientSpec($withStream) { + private function getExpectedClientSpec($withStream) + { $expectedArray = array( "Client: composer_perforce_TEST_depot", "\n", @@ -516,15 +661,15 @@ class PerforceTest extends \PHPUnit_Framework_TestCase { if ($withStream) { $expectedArray[] = "Stream:"; $expectedArray[] = " //depot/branch"; - } - else { + } else { $expectedArray[] = "View: //depot/... //composer_perforce_TEST_depot/depot/..."; } return $expectedArray; } - private function setPerforceToStream(){ + private function setPerforceToStream() + { $this->perforce->setStream("//depot/branch"); } } From d77bd493011665dbbd85dc8ab544462ff67118ad Mon Sep 17 00:00:00 2001 From: mwhittom Date: Wed, 4 Sep 2013 09:30:23 -0500 Subject: [PATCH 19/26] Replaced RemoteFileSystemTest with version from main branch --- .../Test/Util/RemoteFilesystemTest.php | 40 +++++++++---------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/tests/Composer/Test/Util/RemoteFilesystemTest.php b/tests/Composer/Test/Util/RemoteFilesystemTest.php index 39a3bb63d..eb8ebc07e 100644 --- a/tests/Composer/Test/Util/RemoteFilesystemTest.php +++ b/tests/Composer/Test/Util/RemoteFilesystemTest.php @@ -21,9 +21,9 @@ class RemoteFilesystemTest extends \PHPUnit_Framework_TestCase { $io = $this->getMock('Composer\IO\IOInterface'); $io - ->expects($this->once()) - ->method('hasAuthentication') - ->will($this->returnValue(false)) + ->expects($this->once()) + ->method('hasAuthentication') + ->will($this->returnValue(false)) ; $res = $this->callGetOptionsForUrl($io, array('http://example.org', array())); @@ -42,14 +42,14 @@ class RemoteFilesystemTest extends \PHPUnit_Framework_TestCase { $io = $this->getMock('Composer\IO\IOInterface'); $io - ->expects($this->once()) - ->method('hasAuthentication') - ->will($this->returnValue(true)) + ->expects($this->once()) + ->method('hasAuthentication') + ->will($this->returnValue(true)) ; $io - ->expects($this->once()) - ->method('getAuthentication') - ->will($this->returnValue(array('username' => 'login', 'password' => 'password'))) + ->expects($this->once()) + ->method('getAuthentication') + ->will($this->returnValue(array('username' => 'login', 'password' => 'password'))) ; $options = $this->callGetOptionsForUrl($io, array('http://example.org', array())); @@ -67,9 +67,9 @@ class RemoteFilesystemTest extends \PHPUnit_Framework_TestCase { $io = $this->getMock('Composer\IO\IOInterface'); $io - ->expects($this->once()) - ->method('hasAuthentication') - ->will($this->returnValue(true)) + ->expects($this->once()) + ->method('hasAuthentication') + ->will($this->returnValue(true)) ; $streamOptions = array('ssl' => array( @@ -84,9 +84,9 @@ class RemoteFilesystemTest extends \PHPUnit_Framework_TestCase { $io = $this->getMock('Composer\IO\IOInterface'); $io - ->expects($this->once()) - ->method('hasAuthentication') - ->will($this->returnValue(true)) + ->expects($this->once()) + ->method('hasAuthentication') + ->will($this->returnValue(true)) ; $streamOptions = array('http' => array( @@ -118,8 +118,8 @@ class RemoteFilesystemTest extends \PHPUnit_Framework_TestCase { $io = $this->getMock('Composer\IO\IOInterface'); $io - ->expects($this->once()) - ->method('overwrite') + ->expects($this->once()) + ->method('overwrite') ; $fs = new RemoteFilesystem($io); @@ -149,8 +149,8 @@ class RemoteFilesystemTest extends \PHPUnit_Framework_TestCase { $io = $this->getMock('Composer\IO\IOInterface'); $io->expects($this->once()) - ->method('setAuthentication') - ->with($this->equalTo('example.com'), $this->equalTo('user'), $this->equalTo('pass')); + ->method('setAuthentication') + ->with($this->equalTo('example.com'), $this->equalTo('user'), $this->equalTo('pass')); $fs = new RemoteFilesystem($io); try { @@ -202,4 +202,4 @@ class RemoteFilesystemTest extends \PHPUnit_Framework_TestCase $attr->setAccessible(true); $attr->setValue($object, $value); } -} \ No newline at end of file +} From 07d46aa3c3fb0d91aedd709a0dd739aa7c43f9be Mon Sep 17 00:00:00 2001 From: mwhittom Date: Fri, 6 Sep 2013 12:14:22 -0500 Subject: [PATCH 20/26] Fixed spacing issues for PSR-2 --- src/Composer/Util/Perforce.php | 2 +- .../Downloader/PerforceDownloaderTest.php | 2 +- .../Repository/Vcs/PerforceDriverTest.php | 1 - tests/Composer/Test/Util/PerforceTest.php | 197 +++++++++--------- 4 files changed, 100 insertions(+), 102 deletions(-) diff --git a/src/Composer/Util/Perforce.php b/src/Composer/Util/Perforce.php index d22d3a13c..c61a1472e 100644 --- a/src/Composer/Util/Perforce.php +++ b/src/Composer/Util/Perforce.php @@ -546,4 +546,4 @@ class Perforce $result = $this->executeCommand($command); return $result; } -} \ No newline at end of file +} diff --git a/tests/Composer/Test/Downloader/PerforceDownloaderTest.php b/tests/Composer/Test/Downloader/PerforceDownloaderTest.php index 557507090..ad9ba2c9b 100644 --- a/tests/Composer/Test/Downloader/PerforceDownloaderTest.php +++ b/tests/Composer/Test/Downloader/PerforceDownloaderTest.php @@ -27,7 +27,7 @@ class PerforceDownloaderTest extends \PHPUnit_Framework_TestCase private $testPath; public static $repository; - function setUp() + private function setUp() { $this->testPath = sys_get_temp_dir() . '/composer-test'; $this->config = new Config(); diff --git a/tests/Composer/Test/Repository/Vcs/PerforceDriverTest.php b/tests/Composer/Test/Repository/Vcs/PerforceDriverTest.php index 77c41a0e1..83647aac6 100644 --- a/tests/Composer/Test/Repository/Vcs/PerforceDriverTest.php +++ b/tests/Composer/Test/Repository/Vcs/PerforceDriverTest.php @@ -132,4 +132,3 @@ class PerforceDriverTest extends \PHPUnit_Framework_TestCase $this->assertTrue($result); } } - diff --git a/tests/Composer/Test/Util/PerforceTest.php b/tests/Composer/Test/Util/PerforceTest.php index 0cca37c2e..912523a31 100644 --- a/tests/Composer/Test/Util/PerforceTest.php +++ b/tests/Composer/Test/Util/PerforceTest.php @@ -120,13 +120,13 @@ class PerforceTest extends \PHPUnit_Framework_TestCase ->method('execute') ->with($this->equalTo($expectedCommand)) ->will( - $this->returnCallback( - function ($command, &$output) { - $output = "P4USER=TEST_P4VARIABLE_USER\n"; - return true; - } - ) - ); + $this->returnCallback( + function ($command, &$output) { + $output = "P4USER=TEST_P4VARIABLE_USER\n"; + return true; + } + ) + ); $this->perforce->queryP4user($io); $this->assertEquals("TEST_P4VARIABLE_USER", $this->perforce->getUser()); @@ -143,13 +143,13 @@ class PerforceTest extends \PHPUnit_Framework_TestCase ->method('execute') ->with($this->equalTo($expectedCommand)) ->will( - $this->returnCallback( - function ($command, &$output) { - $output = "TEST_P4VARIABLE_USER\n"; - return true; - } - ) - ); + $this->returnCallback( + function ($command, &$output) { + $output = "TEST_P4VARIABLE_USER\n"; + return true; + } + ) + ); $this->perforce->queryP4user($io); $this->assertEquals("TEST_P4VARIABLE_USER", $this->perforce->getUser()); @@ -234,13 +234,13 @@ class PerforceTest extends \PHPUnit_Framework_TestCase ->method('execute') ->with($this->equalTo($expectedCommand)) ->will( - $this->returnCallback( - function ($command, &$output) { - $output = "P4PASSWD=TEST_P4VARIABLE_PASSWORD\n"; - return true; - } - ) - ); + $this->returnCallback( + function ($command, &$output) { + $output = "P4PASSWD=TEST_P4VARIABLE_PASSWORD\n"; + return true; + } + ) + ); $password = $this->perforce->queryP4Password($io); $this->assertEquals("TEST_P4VARIABLE_PASSWORD", $password); @@ -257,13 +257,13 @@ class PerforceTest extends \PHPUnit_Framework_TestCase ->method('execute') ->with($this->equalTo($expectedCommand)) ->will( - $this->returnCallback( - function ($command, &$output) { - $output = "TEST_P4VARIABLE_PASSWORD\n"; - return true; - } - ) - ); + $this->returnCallback( + function ($command, &$output) { + $output = "TEST_P4VARIABLE_PASSWORD\n"; + return true; + } + ) + ); $password = $this->perforce->queryP4Password($io); $this->assertEquals("TEST_P4VARIABLE_PASSWORD", $password); @@ -353,13 +353,13 @@ class PerforceTest extends \PHPUnit_Framework_TestCase ->method('execute') ->with($this->equalTo($expectedCommand)) ->will( - $this->returnCallback( - function ($command, &$output) { - $output = "Stream //depot/branch mainline none 'branch'\n"; - return true; - } - ) - ); + $this->returnCallback( + function ($command, &$output) { + $output = "Stream //depot/branch mainline none 'branch'\n"; + return true; + } + ) + ); $branches = $this->perforce->getBranches(); $this->assertEquals("//depot/branch", $branches['master']); @@ -378,13 +378,13 @@ class PerforceTest extends \PHPUnit_Framework_TestCase ->method('execute') ->with($this->equalTo($expectedCommand)) ->will( - $this->returnCallback( - function ($command, &$output) { - $output = "Label 0.0.1 2013/07/31 'First Label!'\nLabel 0.0.2 2013/08/01 'Second Label!'\n"; - return true; - } - ) - ); + $this->returnCallback( + function ($command, &$output) { + $output = "Label 0.0.1 2013/07/31 'First Label!'\nLabel 0.0.2 2013/08/01 'Second Label!'\n"; + return true; + } + ) + ); $tags = $this->perforce->getTags(); $this->assertEquals("//depot@0.0.1", $tags['0.0.1']); @@ -400,13 +400,13 @@ class PerforceTest extends \PHPUnit_Framework_TestCase ->method('execute') ->with($this->equalTo($expectedCommand)) ->will( - $this->returnCallback( - function ($command, &$output) { - $output = "Label 0.0.1 2013/07/31 'First Label!'\nLabel 0.0.2 2013/08/01 'Second Label!'\n"; - return true; - } - ) - ); + $this->returnCallback( + function ($command, &$output) { + $output = "Label 0.0.1 2013/07/31 'First Label!'\nLabel 0.0.2 2013/08/01 'Second Label!'\n"; + return true; + } + ) + ); $tags = $this->perforce->getTags(); $this->assertEquals("//depot/branch@0.0.1", $tags['0.0.1']); @@ -424,13 +424,13 @@ class PerforceTest extends \PHPUnit_Framework_TestCase { $this->processExecutor->expects($this->any())->method('execute') ->will( - $this->returnCallback( - function ($command, &$output) { - $output = "Depot depot 2013/06/25 stream /p4/1/depots/depot/... 'Created by Me'"; - return true; - } - ) - ); + $this->returnCallback( + function ($command, &$output) { + $output = "Depot depot 2013/06/25 stream /p4/1/depots/depot/... 'Created by Me'"; + return true; + } + ) + ); $result = $this->perforce->checkStream("depot"); $this->assertTrue($result); $this->assertTrue($this->perforce->isStream()); @@ -443,13 +443,13 @@ class PerforceTest extends \PHPUnit_Framework_TestCase ->method('execute') ->with($this->equalTo($expectedCommand)) ->will( - $this->returnCallback( - function ($command, &$output) { - $output = PerforceTest::getComposerJson(); - return true; - } - ) - ); + $this->returnCallback( + function ($command, &$output) { + $output = PerforceTest::getComposerJson(); + return true; + } + ) + ); $result = $this->perforce->getComposerInformation("//depot"); $expected = array( @@ -468,26 +468,26 @@ class PerforceTest extends \PHPUnit_Framework_TestCase ->method('execute') ->with($this->equalTo($expectedCommand)) ->will( - $this->returnCallback( - function ($command, &$output) { - $output = "//depot/composer.json#1 - branch change 10001 (text)"; - return true; - } - ) - ); + $this->returnCallback( + function ($command, &$output) { + $output = "//depot/composer.json#1 - branch change 10001 (text)"; + return true; + } + ) + ); $expectedCommand = "p4 -u user -c composer_perforce_TEST_depot -p port print //depot/composer.json@10001"; $this->processExecutor->expects($this->at(1)) ->method('execute') ->with($this->equalTo($expectedCommand)) ->will( - $this->returnCallback( - function ($command, &$output) { - $output = PerforceTest::getComposerJson(); - return true; - } - ) - ); + $this->returnCallback( + function ($command, &$output) { + $output = PerforceTest::getComposerJson(); + return true; + } + ) + ); $result = $this->perforce->getComposerInformation("//depot@0.0.1"); @@ -509,13 +509,13 @@ class PerforceTest extends \PHPUnit_Framework_TestCase ->method('execute') ->with($this->equalTo($expectedCommand)) ->will( - $this->returnCallback( - function ($command, &$output) { - $output = PerforceTest::getComposerJson(); - return true; - } - ) - ); + $this->returnCallback( + function ($command, &$output) { + $output = PerforceTest::getComposerJson(); + return true; + } + ) + ); $result = $this->perforce->getComposerInformation("//depot/branch"); @@ -536,26 +536,26 @@ class PerforceTest extends \PHPUnit_Framework_TestCase ->method('execute') ->with($this->equalTo($expectedCommand)) ->will( - $this->returnCallback( - function ($command, &$output) { - $output = "//depot/composer.json#1 - branch change 10001 (text)"; - return true; - } - ) - ); + $this->returnCallback( + function ($command, &$output) { + $output = "//depot/composer.json#1 - branch change 10001 (text)"; + return true; + } + ) + ); $expectedCommand = "p4 -u user -c composer_perforce_TEST_depot_branch -p port print //depot/branch/composer.json@10001"; $this->processExecutor->expects($this->at(1)) ->method('execute') ->with($this->equalTo($expectedCommand)) ->will( - $this->returnCallback( - function ($command, &$output) { - $output = PerforceTest::getComposerJson(); - return true; - } - ) - ); + $this->returnCallback( + function ($command, &$output) { + $output = PerforceTest::getComposerJson(); + return true; + } + ) + ); $result = $this->perforce->getComposerInformation("//depot/branch@0.0.1"); @@ -673,4 +673,3 @@ class PerforceTest extends \PHPUnit_Framework_TestCase $this->perforce->setStream("//depot/branch"); } } - From f7d9f3d8b4ace923685e445eccd20a0fbb82b67e Mon Sep 17 00:00:00 2001 From: mwhittom Date: Fri, 6 Sep 2013 12:31:04 -0500 Subject: [PATCH 21/26] Fixed Test method setUp to be protected instead of private. --- tests/Composer/Test/Downloader/PerforceDownloaderTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Composer/Test/Downloader/PerforceDownloaderTest.php b/tests/Composer/Test/Downloader/PerforceDownloaderTest.php index ad9ba2c9b..46eec7284 100644 --- a/tests/Composer/Test/Downloader/PerforceDownloaderTest.php +++ b/tests/Composer/Test/Downloader/PerforceDownloaderTest.php @@ -27,7 +27,7 @@ class PerforceDownloaderTest extends \PHPUnit_Framework_TestCase private $testPath; public static $repository; - private function setUp() + protected function setUp() { $this->testPath = sys_get_temp_dir() . '/composer-test'; $this->config = new Config(); From 8207518e04676766a3155757054ea3dca1ca27b7 Mon Sep 17 00:00:00 2001 From: mwhittom Date: Mon, 9 Sep 2013 12:45:50 -0500 Subject: [PATCH 22/26] Updated to clean up code and rename variables to camelcase --- .../Repository/Vcs/PerforceDriver.php | 18 +++--- src/Composer/Util/Perforce.php | 57 ++++++++----------- .../Archiver/ArchivableFilesFinderTest.php | 1 - .../Repository/Vcs/PerforceDriverTest.php | 12 ++-- 4 files changed, 40 insertions(+), 48 deletions(-) diff --git a/src/Composer/Repository/Vcs/PerforceDriver.php b/src/Composer/Repository/Vcs/PerforceDriver.php index cc04fea2f..b6e94c597 100644 --- a/src/Composer/Repository/Vcs/PerforceDriver.php +++ b/src/Composer/Repository/Vcs/PerforceDriver.php @@ -26,8 +26,8 @@ class PerforceDriver extends VcsDriver protected $depot; protected $branch; protected $perforce; - protected $composer_info; - protected $composer_info_identifier; + protected $composerInfo; + protected $composerInfoIdentifier; /** * {@inheritDoc} @@ -65,9 +65,9 @@ class PerforceDriver extends VcsDriver */ public function getComposerInformation($identifier) { - if (isset($this->composer_info_identifier)) { - if (strcmp($identifier, $this->composer_info_identifier) === 0) { - return $this->composer_info; + if (isset($this->composerInfoIdentifier)) { + if (strcmp($identifier, $this->composerInfoIdentifier) === 0) { + return $this->composerInfo; } } $composer_info = $this->perforce->getComposerInformation($identifier); @@ -139,11 +139,11 @@ class PerforceDriver extends VcsDriver */ public function hasComposerFile($identifier) { - $this->composer_info = $this->perforce->getComposerInformation("//$this->depot/$identifier"); - $this->composer_info_identifier = $identifier; + $this->composerInfo = $this->perforce->getComposerInformation("//$this->depot/$identifier"); + $this->composerInfoIdentifier = $identifier; $result = false; - if (isset($this->composer_info)) { - $result = count($this->composer_info) > 0; + if (isset($this->composerInfo)) { + $result = count($this->composerInfo) > 0; } return $result; } diff --git a/src/Composer/Util/Perforce.php b/src/Composer/Util/Perforce.php index c61a1472e..d5d940e8f 100644 --- a/src/Composer/Util/Perforce.php +++ b/src/Composer/Util/Perforce.php @@ -31,7 +31,7 @@ class Perforce protected $p4DepotType; protected $p4Branch; protected $process; - protected $unique_perforce_client_name; + protected $uniquePerforceClientName; protected $windowsFlag; @@ -59,12 +59,12 @@ class Perforce public function initialize($repoConfig) { - $this->unique_perforce_client_name = $this->generateUniquePerforceClientName(); - if (!isset ($repoConfig)) { + $this->uniquePerforceClientName = $this->generateUniquePerforceClientName(); + if (null == $repoConfig) { return; } if (isset($repoConfig['unique_perforce_client_name'])) { - $this->unique_perforce_client_name = $repoConfig['unique_perforce_client_name']; + $this->uniquePerforceClientName = $repoConfig['unique_perforce_client_name']; } if (isset($repoConfig['depot'])) { @@ -119,8 +119,8 @@ class Perforce public function getClient() { if (!isset($this->p4Client)) { - $clean_stream_name = str_replace("@", "", str_replace("/", "_", str_replace("//", "", $this->getStream()))); - $this->p4Client = "composer_perforce_" . $this->unique_perforce_client_name . "_" . $clean_stream_name; + $cleanStreamName = str_replace("@", "", str_replace("/", "_", str_replace("//", "", $this->getStream()))); + $this->p4Client = "composer_perforce_" . $this->uniquePerforceClientName . "_" . $cleanStreamName; } return $this->p4Client; @@ -327,7 +327,7 @@ class Perforce $spec = fopen($clientSpec, 'w'); try { $this->writeClientSpecToFile($spec); - } catch (Exception $e) { + } catch (\Exception $e) { fclose($spec); throw $e; } @@ -369,9 +369,9 @@ class Perforce fclose($pipes[1]); fclose($pipes[2]); - $return_code = proc_close($process); + $returnCode = proc_close($process); - return $return_code; + return $returnCode; } @@ -389,34 +389,27 @@ class Perforce } } - public static function checkServerExists($url, ProcessExecutor $process_executor) + public static function checkServerExists($url, ProcessExecutor $processExecutor) { - $process = $process_executor ? : new ProcessExecutor; $result = ""; - $process->execute("p4 -p $url info -s", $result); - $index = strpos($result, "error"); - if ($index === false) { - return true; - } - - return false; + $processExecutor->execute("p4 -p $url info -s", $result); + return false === strpos($result, "error"); } public function getComposerInformation($identifier) { $index = strpos($identifier, "@"); if ($index === false) { - $composer_json = "$identifier/composer.json"; + $composerJson = "$identifier/composer.json"; - return $this->getComposerInformationFromPath($composer_json); - } else { - return $this->getComposerInformationFromLabel($identifier, $index); + return $this->getComposerInformationFromPath($composerJson); } + return $this->getComposerInformationFromLabel($identifier, $index); } - public function getComposerInformationFromPath($composer_json) + public function getComposerInformationFromPath($composerJson) { - $command = $this->generateP4Command(" print $composer_json"); + $command = $this->generateP4Command(" print $composerJson"); $result = $this->executeCommand($command); $index = strpos($result, "{"); if ($index === false) { @@ -434,8 +427,8 @@ class Perforce public function getComposerInformationFromLabel($identifier, $index) { - $composer_json_path = substr($identifier, 0, $index) . "/composer.json" . substr($identifier, $index); - $command = $this->generateP4Command(" files $composer_json_path", false); + $composerJsonPath = substr($identifier, 0, $index) . "/composer.json" . substr($identifier, $index); + $command = $this->generateP4Command(" files $composerJsonPath", false); $result = $this->executeCommand($command); $index2 = strpos($result, "no such file(s)."); if ($index2 === false) { @@ -444,9 +437,9 @@ class Perforce $phrase = trim(substr($result, $index3)); $fields = explode(" ", $phrase); $id = $fields[1]; - $composer_json = substr($identifier, 0, $index) . "/composer.json@" . $id; + $composerJson = substr($identifier, 0, $index) . "/composer.json@" . $id; - return $this->getComposerInformationFromPath($composer_json); + return $this->getComposerInformationFromPath($composerJson); } } @@ -455,9 +448,9 @@ class Perforce public function getBranches() { - $possible_branches = array(); + $possibleBranches = array(); if (!$this->isStream()) { - $possible_branches[$this->p4Branch] = $this->getStream(); + $possibleBranches[$this->p4Branch] = $this->getStream(); } else { $command = $this->generateP4Command("streams //$this->p4Depot/..."); $result = $this->executeCommand($command); @@ -466,12 +459,12 @@ class Perforce $resBits = explode(" ", $line); if (count($resBits) > 4) { $branch = preg_replace("/[^A-Za-z0-9 ]/", '', $resBits[4]); - $possible_branches[$branch] = $resBits[1]; + $possibleBranches[$branch] = $resBits[1]; } } } $branches = array(); - $branches['master'] = $possible_branches[$this->p4Branch]; + $branches['master'] = $possibleBranches[$this->p4Branch]; return $branches; } diff --git a/tests/Composer/Test/Package/Archiver/ArchivableFilesFinderTest.php b/tests/Composer/Test/Package/Archiver/ArchivableFilesFinderTest.php index 2623007d8..536f2128c 100644 --- a/tests/Composer/Test/Package/Archiver/ArchivableFilesFinderTest.php +++ b/tests/Composer/Test/Package/Archiver/ArchivableFilesFinderTest.php @@ -146,7 +146,6 @@ class ArchivableFilesFinderTest extends \PHPUnit_Framework_TestCase public function testHgExcludes() { - $this->markTestSkipped('Mercurial test does not work.'); // Ensure that Mercurial is available for testing. if (!$this->isProcessAvailable('hg')) { return $this->markTestSkipped('Mercurial is not available.'); diff --git a/tests/Composer/Test/Repository/Vcs/PerforceDriverTest.php b/tests/Composer/Test/Repository/Vcs/PerforceDriverTest.php index 83647aac6..706320bff 100644 --- a/tests/Composer/Test/Repository/Vcs/PerforceDriverTest.php +++ b/tests/Composer/Test/Repository/Vcs/PerforceDriverTest.php @@ -54,12 +54,12 @@ class PerforceDriverTest extends \PHPUnit_Framework_TestCase public function testInitializeCapturesVariablesFromRepoConfig() { $this->setUp(); - $repo_config = array( + $repoConfig = array( 'url' => 'TEST_PERFORCE_URL', 'depot' => 'TEST_DEPOT_CONFIG', 'branch' => 'TEST_BRANCH_CONFIG' ); - $driver = new PerforceDriver($repo_config, $this->io, $this->config, $this->process, $this->remoteFileSystem); + $driver = new PerforceDriver($repoConfig, $this->io, $this->config, $this->process, $this->remoteFileSystem); $process = $this->getMock('Composer\Util\ProcessExecutor'); $arguments = array( array('depot' => 'TEST_DEPOT', 'branch' => 'TEST_BRANCH'), @@ -80,12 +80,12 @@ class PerforceDriverTest extends \PHPUnit_Framework_TestCase public function testInitializeLogsInAndConnectsClient() { $this->setUp(); - $repo_config = array( + $repoConfig = array( 'url' => 'TEST_PERFORCE_URL', 'depot' => 'TEST_DEPOT_CONFIG', 'branch' => 'TEST_BRANCH_CONFIG' ); - $driver = new PerforceDriver($repo_config, $this->io, $this->config, $this->process, $this->remoteFileSystem); + $driver = new PerforceDriver($repoConfig, $this->io, $this->config, $this->process, $this->remoteFileSystem); $perforce = $this->getMockBuilder('Composer\Util\Perforce')->disableOriginalConstructor()->getMock(); $perforce->expects($this->at(0)) ->method('p4Login') @@ -105,12 +105,12 @@ class PerforceDriverTest extends \PHPUnit_Framework_TestCase public function testHasComposerFile() { $this->setUp(); - $repo_config = array( + $repoConfig = array( 'url' => 'TEST_PERFORCE_URL', 'depot' => 'TEST_DEPOT_CONFIG', 'branch' => 'TEST_BRANCH_CONFIG' ); - $driver = new PerforceDriver($repo_config, $this->io, $this->config, $this->process, $this->remoteFileSystem); + $driver = new PerforceDriver($repoConfig, $this->io, $this->config, $this->process, $this->remoteFileSystem); $process = $this->getMock('Composer\Util\ProcessExecutor'); $arguments = array( array('depot' => 'TEST_DEPOT', 'branch' => 'TEST_BRANCH'), From 2e737ac43989603722601c86f00489c3ad9bdcde Mon Sep 17 00:00:00 2001 From: mwhittom Date: Mon, 9 Sep 2013 14:48:24 -0500 Subject: [PATCH 23/26] updated to replace double-quotes with single quotes --- .../Downloader/PerforceDownloader.php | 4 +- .../Repository/Vcs/PerforceDriver.php | 6 +- src/Composer/Util/Perforce.php | 178 ++++++------ .../Downloader/PerforceDownloaderTest.php | 16 +- .../Repository/Vcs/PerforceDriverTest.php | 18 +- tests/Composer/Test/Util/PerforceTest.php | 273 +++++++++--------- 6 files changed, 248 insertions(+), 247 deletions(-) diff --git a/src/Composer/Downloader/PerforceDownloader.php b/src/Composer/Downloader/PerforceDownloader.php index 9d3e31e06..ad94d1ad8 100644 --- a/src/Composer/Downloader/PerforceDownloader.php +++ b/src/Composer/Downloader/PerforceDownloader.php @@ -33,7 +33,7 @@ class PerforceDownloader extends VcsDownloader $ref = $package->getSourceReference(); $label = $package->getPrettyVersion(); - $this->io->write(" Cloning " . $ref); + $this->io->write(' Cloning ' . $ref); $this->initPerforce($package, $path, $ref); $this->perforce->setStream($ref); $this->perforce->queryP4User($this->io); @@ -74,7 +74,7 @@ class PerforceDownloader extends VcsDownloader */ public function getLocalChanges(PackageInterface $package, $path) { - $this->io->write("Perforce driver does not check for local changes before overriding", true); + $this->io->write('Perforce driver does not check for local changes before overriding', true); return; } diff --git a/src/Composer/Repository/Vcs/PerforceDriver.php b/src/Composer/Repository/Vcs/PerforceDriver.php index b6e94c597..03acd0a21 100644 --- a/src/Composer/Repository/Vcs/PerforceDriver.php +++ b/src/Composer/Repository/Vcs/PerforceDriver.php @@ -35,7 +35,7 @@ class PerforceDriver extends VcsDriver public function initialize() { $this->depot = $this->repoConfig['depot']; - $this->branch = ""; + $this->branch = ''; if (isset($this->repoConfig['branch'])) { $this->branch = $this->repoConfig['branch']; } @@ -56,7 +56,7 @@ class PerforceDriver extends VcsDriver return; } - $repoDir = $this->config->get('cache-vcs-dir') . "/$this->depot"; + $repoDir = $this->config->get('cache-vcs-dir') . '/' . $this->depot; $this->perforce = Perforce::createPerforce($repoConfig, $this->getUrl(), $repoDir, $this->process); } @@ -139,7 +139,7 @@ class PerforceDriver extends VcsDriver */ public function hasComposerFile($identifier) { - $this->composerInfo = $this->perforce->getComposerInformation("//$this->depot/$identifier"); + $this->composerInfo = $this->perforce->getComposerInformation('//' . $this->depot . '/' . $identifier); $this->composerInfoIdentifier = $identifier; $result = false; if (isset($this->composerInfo)) { diff --git a/src/Composer/Util/Perforce.php b/src/Composer/Util/Perforce.php index d5d940e8f..eedc204c9 100644 --- a/src/Composer/Util/Perforce.php +++ b/src/Composer/Util/Perforce.php @@ -76,7 +76,7 @@ class Perforce if (isset($repoConfig['p4user'])) { $this->p4User = $repoConfig['p4user']; } else { - $this->p4User = $this->getP4variable("P4USER"); + $this->p4User = $this->getP4variable('P4USER'); } if (isset($repoConfig['p4password'])) { $this->p4Password = $repoConfig['p4password']; @@ -101,7 +101,7 @@ class Perforce public function cleanupClientSpec() { $client = $this->getClient(); - $command = "p4 client -d $client"; + $command = 'p4 client -d $client'; $this->executeCommand($command); $clientSpec = $this->getP4ClientSpec(); $fileSystem = new FileSystem($this->process); @@ -119,8 +119,8 @@ class Perforce public function getClient() { if (!isset($this->p4Client)) { - $cleanStreamName = str_replace("@", "", str_replace("/", "_", str_replace("//", "", $this->getStream()))); - $this->p4Client = "composer_perforce_" . $this->uniquePerforceClientName . "_" . $cleanStreamName; + $cleanStreamName = str_replace('@', '', str_replace('/', '_', str_replace('//', '', $this->getStream()))); + $this->p4Client = 'composer_perforce_' . $this->uniquePerforceClientName . '_' . $cleanStreamName; } return $this->p4Client; @@ -139,25 +139,25 @@ class Perforce public function setStream($stream) { $this->p4Stream = $stream; - $index = strrpos($stream, "/"); + $index = strrpos($stream, '/'); //Stream format is //depot/stream, while non-streaming depot is //depot if ($index > 2) { - $this->p4DepotType = "stream"; + $this->p4DepotType = 'stream'; } } public function isStream() { - return (strcmp($this->p4DepotType, "stream") === 0); + return (strcmp($this->p4DepotType, 'stream') === 0); } public function getStream() { if (!isset($this->p4Stream)) { if ($this->isStream()) { - $this->p4Stream = "//$this->p4Depot/$this->p4Branch"; + $this->p4Stream = '//' . $this->p4Depot . '/' . $this->p4Branch; } else { - $this->p4Stream = "//$this->p4Depot"; + $this->p4Stream = '//' . $this->p4Depot; } } return $this->p4Stream; @@ -165,7 +165,7 @@ class Perforce public function getStreamWithoutLabel($stream) { - $index = strpos($stream, "@"); + $index = strpos($stream, '@'); if ($index === false) { return $stream; } @@ -175,7 +175,7 @@ class Perforce public function getP4ClientSpec() { - $p4clientSpec = $this->path . "/" . $this->getClient() . ".p4.spec"; + $p4clientSpec = $this->path . '/' . $this->getClient() . '.p4.spec'; return $p4clientSpec; } @@ -191,15 +191,15 @@ class Perforce if (strlen($this->p4User) > 0) { return; } - $this->p4User = $this->getP4variable("P4USER"); + $this->p4User = $this->getP4variable('P4USER'); if (strlen($this->p4User) > 0) { return; } - $this->p4User = $io->ask("Enter P4 User:"); + $this->p4User = $io->ask('Enter P4 User:'); if ($this->windowsFlag) { - $command = "p4 set P4USER=$this->p4User"; + $command = 'p4 set P4USER=' . $this->p4User; } else { - $command = "export P4USER=$this->p4User"; + $command = 'export P4USER=' . $this->p4User; } $result = $this->executeCommand($command); } @@ -207,13 +207,13 @@ class Perforce protected function getP4variable($name) { if ($this->windowsFlag) { - $command = "p4 set"; + $command = 'p4 set'; $result = $this->executeCommand($command); - $resArray = explode("\n", $result); + $resArray = explode(PHP_EOL, $result); foreach ($resArray as $line) { - $fields = explode("=", $line); + $fields = explode('=', $line); if (strcmp($name, $fields[0]) == 0) { - $index = strpos($fields[1], " "); + $index = strpos($fields[1], ' '); if ($index === false) { $value = $fields[1]; } else { @@ -237,9 +237,9 @@ class Perforce if (isset($this->p4Password)) { return $this->p4Password; } - $password = $this->getP4variable("P4PASSWD"); + $password = $this->getP4variable('P4PASSWD'); if (strlen($password) <= 0) { - $password = $io->askAndHideAnswer("Enter password for Perforce user " . $this->getUser() . ": "); + $password = $io->askAndHideAnswer('Enter password for Perforce user ' . $this->getUser() . ': '); } $this->p4Password = $password; @@ -248,12 +248,12 @@ class Perforce public function generateP4Command($command, $useClient = true) { - $p4Command = "p4 "; - $p4Command = $p4Command . "-u " . $this->getUser() . " "; + $p4Command = 'p4 '; + $p4Command = $p4Command . '-u ' . $this->getUser() . ' '; if ($useClient) { - $p4Command = $p4Command . "-c " . $this->getClient() . " "; + $p4Command = $p4Command . '-c ' . $this->getClient() . ' '; } - $p4Command = $p4Command . "-p " . $this->getPort() . " "; + $p4Command = $p4Command . '-p ' . $this->getPort() . ' '; $p4Command = $p4Command . $command; return $p4Command; @@ -261,7 +261,7 @@ class Perforce public function isLoggedIn() { - $command = $this->generateP4Command("login -s", false); + $command = $this->generateP4Command('login -s', false); $result = trim($this->executeCommand($command)); $index = strpos($result, $this->getUser()); if ($index === false) { @@ -272,7 +272,7 @@ class Perforce public function connectClient() { - $p4CreateClientCommand = $this->generateP4Command("client -i < " . $this->getP4ClientSpec()); + $p4CreateClientCommand = $this->generateP4Command('client -i < ' . $this->getP4ClientSpec()); $this->executeCommand($p4CreateClientCommand); } @@ -281,12 +281,12 @@ class Perforce $prevDir = getcwd(); chdir($this->path); - $this->executeCommand("pwd"); + $this->executeCommand('pwd'); - $p4SyncCommand = $this->generateP4Command("sync -f "); + $p4SyncCommand = $this->generateP4Command('sync -f '); if (isset($label)) { - if (strcmp($label, "dev-master") != 0) { - $p4SyncCommand = $p4SyncCommand . "@" . $label; + if (strcmp($label, 'dev-master') != 0) { + $p4SyncCommand = $p4SyncCommand . '@' . $label; } } $this->executeCommand($p4SyncCommand); @@ -296,27 +296,27 @@ class Perforce public function writeClientSpecToFile($spec) { - fwrite($spec, "Client: " . $this->getClient() . "\n\n"); - fwrite($spec, "Update: " . date("Y/m/d H:i:s") . "\n\n"); - fwrite($spec, "Access: " . date("Y/m/d H:i:s") . "\n"); - fwrite($spec, "Owner: " . $this->getUser() . "\n\n"); - fwrite($spec, "Description:\n"); - fwrite($spec, " Created by " . $this->getUser() . " from composer.\n\n"); - fwrite($spec, "Root: " . $this->getPath() . "\n\n"); - fwrite($spec, "Options: noallwrite noclobber nocompress unlocked modtime rmdir\n\n"); - fwrite($spec, "SubmitOptions: revertunchanged\n\n"); - fwrite($spec, "LineEnd: local\n\n"); + fwrite($spec, 'Client: ' . $this->getClient() . PHP_EOL . PHP_EOL); + fwrite($spec, 'Update: ' . date('Y/m/d H:i:s') . PHP_EOL . PHP_EOL); + fwrite($spec, 'Access: ' . date('Y/m/d H:i:s') . PHP_EOL ); + fwrite($spec, 'Owner: ' . $this->getUser() . PHP_EOL . PHP_EOL); + fwrite($spec, 'Description:' . PHP_EOL); + fwrite($spec, ' Created by ' . $this->getUser() . ' from composer.' . PHP_EOL . PHP_EOL); + fwrite($spec, 'Root: ' . $this->getPath() . PHP_EOL . PHP_EOL); + fwrite($spec, 'Options: noallwrite noclobber nocompress unlocked modtime rmdir' . PHP_EOL . PHP_EOL); + fwrite($spec, 'SubmitOptions: revertunchanged' . PHP_EOL . PHP_EOL); + fwrite($spec, 'LineEnd: local' . PHP_EOL . PHP_EOL); if ($this->isStream()) { - fwrite($spec, "Stream:\n"); - fwrite($spec, " " . $this->getStreamWithoutLabel($this->p4Stream) . "\n"); + fwrite($spec, 'Stream:' . PHP_EOL); + fwrite($spec, ' ' . $this->getStreamWithoutLabel($this->p4Stream) . PHP_EOL); } else { fwrite( $spec, - "View: " . $this->getStream() . "/... //" . $this->getClient() . "/" . str_replace( - "//", - "", + 'View: ' . $this->getStream() . '/... //' . $this->getClient() . '/' . str_replace( + '//', + '', $this->getStream() - ) . "/... \n" + ) . '/... ' . PHP_EOL ); } } @@ -351,11 +351,11 @@ class Perforce public function windowsLogin($password) { $descriptorspec = array( - 0 => array("pipe", "r"), - 1 => array("pipe", "w"), - 2 => array("pipe", "a") + 0 => array('pipe', 'r'), + 1 => array('pipe', 'w'), + 2 => array('pipe', 'a') ); - $command = $this->generateP4Command(" login -a"); + $command = $this->generateP4Command(' login -a'); $process = proc_open($command, $descriptorspec, $pipes); if (!is_resource($process)) { return false; @@ -363,8 +363,8 @@ class Perforce fwrite($pipes[0], $password); fclose($pipes[0]); - $this->read($pipes[1], "Output"); - $this->read($pipes[2], "Error"); + $this->read($pipes[1], 'Output'); + $this->read($pipes[2], 'Error'); fclose($pipes[1]); fclose($pipes[2]); @@ -383,7 +383,7 @@ class Perforce if ($this->windowsFlag) { $this->windowsLogin($password); } else { - $command = "echo $password | " . $this->generateP4Command(" login -a", false); + $command = 'echo ' . $password . ' | ' . $this->generateP4Command(' login -a', false); $this->executeCommand($command); } } @@ -391,16 +391,16 @@ class Perforce public static function checkServerExists($url, ProcessExecutor $processExecutor) { - $result = ""; - $processExecutor->execute("p4 -p $url info -s", $result); - return false === strpos($result, "error"); + $result = ''; + $processExecutor->execute('p4 -p ' . $url . ' info -s', $result); + return false === strpos($result, 'error'); } public function getComposerInformation($identifier) { - $index = strpos($identifier, "@"); + $index = strpos($identifier, '@'); if ($index === false) { - $composerJson = "$identifier/composer.json"; + $composerJson = $identifier. '/composer.json'; return $this->getComposerInformationFromPath($composerJson); } @@ -409,11 +409,11 @@ class Perforce public function getComposerInformationFromPath($composerJson) { - $command = $this->generateP4Command(" print $composerJson"); + $command = $this->generateP4Command(' print ' . $composerJson); $result = $this->executeCommand($command); - $index = strpos($result, "{"); + $index = strpos($result, '{'); if ($index === false) { - return ""; + return ''; } if ($index >= 0) { $rawData = substr($result, $index); @@ -422,22 +422,22 @@ class Perforce return $composer_info; } - return ""; + return ''; } public function getComposerInformationFromLabel($identifier, $index) { - $composerJsonPath = substr($identifier, 0, $index) . "/composer.json" . substr($identifier, $index); - $command = $this->generateP4Command(" files $composerJsonPath", false); + $composerJsonPath = substr($identifier, 0, $index) . '/composer.json' . substr($identifier, $index); + $command = $this->generateP4Command(' files ' . $composerJsonPath, false); $result = $this->executeCommand($command); - $index2 = strpos($result, "no such file(s)."); + $index2 = strpos($result, 'no such file(s).'); if ($index2 === false) { - $index3 = strpos($result, "change"); + $index3 = strpos($result, 'change'); if (!($index3 === false)) { $phrase = trim(substr($result, $index3)); - $fields = explode(" ", $phrase); + $fields = explode(' ', $phrase); $id = $fields[1]; - $composerJson = substr($identifier, 0, $index) . "/composer.json@" . $id; + $composerJson = substr($identifier, 0, $index) . '/composer.json@' . $id; return $this->getComposerInformationFromPath($composerJson); } @@ -452,13 +452,13 @@ class Perforce if (!$this->isStream()) { $possibleBranches[$this->p4Branch] = $this->getStream(); } else { - $command = $this->generateP4Command("streams //$this->p4Depot/..."); + $command = $this->generateP4Command('streams //' . $this->p4Depot . '/...'); $result = $this->executeCommand($command); - $resArray = explode("\n", $result); + $resArray = explode(PHP_EOL, $result); foreach ($resArray as $line) { - $resBits = explode(" ", $line); + $resBits = explode(' ', $line); if (count($resBits) > 4) { - $branch = preg_replace("/[^A-Za-z0-9 ]/", '', $resBits[4]); + $branch = preg_replace('/[^A-Za-z0-9 ]/', '', $resBits[4]); $possibleBranches[$branch] = $resBits[1]; } } @@ -471,15 +471,15 @@ class Perforce public function getTags() { - $command = $this->generateP4Command("labels"); + $command = $this->generateP4Command('labels'); $result = $this->executeCommand($command); - $resArray = explode("\n", $result); + $resArray = explode(PHP_EOL, $result); $tags = array(); foreach ($resArray as $line) { - $index = strpos($line, "Label"); + $index = strpos($line, 'Label'); if (!($index === false)) { - $fields = explode(" ", $line); - $tags[$fields[1]] = $this->getStream() . "@" . $fields[1]; + $fields = explode(' ', $line); + $tags[$fields[1]] = $this->getStream() . '@' . $fields[1]; } } @@ -488,13 +488,13 @@ class Perforce public function checkStream() { - $command = $this->generateP4Command("depots", false); + $command = $this->generateP4Command('depots', false); $result = $this->executeCommand($command); - $resArray = explode("\n", $result); + $resArray = explode(PHP_EOL, $result); foreach ($resArray as $line) { - $index = strpos($line, "Depot"); + $index = strpos($line, 'Depot'); if (!($index === false)) { - $fields = explode(" ", $line); + $fields = explode(' ', $line); if (strcmp($this->p4Depot, $fields[1]) === 0) { $this->p4DepotType = $fields[3]; @@ -508,17 +508,17 @@ class Perforce protected function getChangeList($reference) { - $index = strpos($reference, "@"); + $index = strpos($reference, '@'); if ($index === false) { return; } $label = substr($reference, $index); - $command = $this->generateP4Command(" changes -m1 $label"); + $command = $this->generateP4Command(' changes -m1 ' . $label); $changes = $this->executeCommand($command); - if (strpos($changes, "Change") !== 0) { + if (strpos($changes, 'Change') !== 0) { return; } - $fields = explode(" ", $changes); + $fields = explode(' ', $changes); $changeList = $fields[1]; return $changeList; } @@ -533,9 +533,9 @@ class Perforce if ($toChangeList == null) { return; } - $index = strpos($fromReference, "@"); - $main = substr($fromReference, 0, $index) . "/..."; - $command = $this->generateP4Command("filelog $main@$fromChangeList,$toChangeList"); + $index = strpos($fromReference, '@'); + $main = substr($fromReference, 0, $index) . '/...'; + $command = $this->generateP4Command('filelog ' . $main . '@' . $fromChangeList. ',' . $toChangeList); $result = $this->executeCommand($command); return $result; } diff --git a/tests/Composer/Test/Downloader/PerforceDownloaderTest.php b/tests/Composer/Test/Downloader/PerforceDownloaderTest.php index 46eec7284..b247cfbf3 100644 --- a/tests/Composer/Test/Downloader/PerforceDownloaderTest.php +++ b/tests/Composer/Test/Downloader/PerforceDownloaderTest.php @@ -54,10 +54,10 @@ class PerforceDownloaderTest extends \PHPUnit_Framework_TestCase ); $package->expects($this->at(0)) ->method('getSourceReference') - ->will($this->returnValue("SOURCE_REF")); + ->will($this->returnValue('SOURCE_REF')); $package->expects($this->at(1)) ->method('getPrettyVersion') - ->will($this->returnValue("100")); + ->will($this->returnValue('100')); $package->expects($this->at(2)) ->method('getRepository') ->will($this->returnValue($repository)); @@ -70,17 +70,17 @@ class PerforceDownloaderTest extends \PHPUnit_Framework_TestCase public function testDoDownload() { $downloader = new PerforceDownloader($this->io, $this->config); - $repoConfig = array("depot" => "TEST_DEPOT", "branch" => "TEST_BRANCH", "p4user" => "TEST_USER"); - $port = "TEST_PORT"; - $path = "TEST_PATH"; + $repoConfig = array('depot' => 'TEST_DEPOT', 'branch' => 'TEST_BRANCH', 'p4user' => 'TEST_USER'); + $port = 'TEST_PORT'; + $path = 'TEST_PATH'; $process = $this->getmock('Composer\Util\ProcessExecutor'); $perforce = $this->getMock( 'Composer\Util\Perforce', array('setStream', 'queryP4User', 'writeP4ClientSpec', 'connectClient', 'syncCodeBase'), - array($repoConfig, $port, $path, $process, true, "TEST") + array($repoConfig, $port, $path, $process, true, 'TEST') ); - $ref = "SOURCE_REF"; - $label = "LABEL"; + $ref = 'SOURCE_REF'; + $label = 'LABEL'; $perforce->expects($this->at(0)) ->method('setStream') ->with($this->equalTo($ref)); diff --git a/tests/Composer/Test/Repository/Vcs/PerforceDriverTest.php b/tests/Composer/Test/Repository/Vcs/PerforceDriverTest.php index 706320bff..5b0bc43f9 100644 --- a/tests/Composer/Test/Repository/Vcs/PerforceDriverTest.php +++ b/tests/Composer/Test/Repository/Vcs/PerforceDriverTest.php @@ -67,14 +67,14 @@ class PerforceDriverTest extends \PHPUnit_Framework_TestCase 'path' => $this->testPath, $process, true, - "TEST" + 'TEST' ); $perforce = $this->getMock('Composer\Util\Perforce', null, $arguments); $driver->injectPerforce($perforce); $driver->initialize(); - $this->assertEquals("TEST_PERFORCE_URL", $driver->getUrl()); - $this->assertEquals("TEST_DEPOT_CONFIG", $driver->getDepot()); - $this->assertEquals("TEST_BRANCH_CONFIG", $driver->getBranch()); + $this->assertEquals('TEST_PERFORCE_URL', $driver->getUrl()); + $this->assertEquals('TEST_DEPOT_CONFIG', $driver->getDepot()); + $this->assertEquals('TEST_BRANCH_CONFIG', $driver->getBranch()); } public function testInitializeLogsInAndConnectsClient() @@ -92,7 +92,7 @@ class PerforceDriverTest extends \PHPUnit_Framework_TestCase ->with($this->io); $perforce->expects($this->at(1)) ->method('checkStream') - ->with($this->equalTo("TEST_DEPOT_CONFIG")); + ->with($this->equalTo('TEST_DEPOT_CONFIG')); $perforce->expects($this->at(2)) ->method('writeP4ClientSpec'); $perforce->expects($this->at(3)) @@ -118,16 +118,16 @@ class PerforceDriverTest extends \PHPUnit_Framework_TestCase 'path' => $this->testPath, $process, true, - "TEST" + 'TEST' ); $perforce = $this->getMock('Composer\Util\Perforce', array('getComposerInformation'), $arguments); $perforce->expects($this->at(0)) ->method('getComposerInformation') - ->with($this->equalTo("//TEST_DEPOT_CONFIG/TEST_IDENTIFIER")) - ->will($this->returnValue("Some json stuff")); + ->with($this->equalTo('//TEST_DEPOT_CONFIG/TEST_IDENTIFIER')) + ->will($this->returnValue('Some json stuff')); $driver->injectPerforce($perforce); $driver->initialize(); - $identifier = "TEST_IDENTIFIER"; + $identifier = 'TEST_IDENTIFIER'; $result = $driver->hasComposerFile($identifier); $this->assertTrue($result); } diff --git a/tests/Composer/Test/Util/PerforceTest.php b/tests/Composer/Test/Util/PerforceTest.php index 912523a31..73b06bc6e 100644 --- a/tests/Composer/Test/Util/PerforceTest.php +++ b/tests/Composer/Test/Util/PerforceTest.php @@ -28,12 +28,12 @@ class PerforceTest extends \PHPUnit_Framework_TestCase { $this->processExecutor = $this->getMock('Composer\Util\ProcessExecutor'); $repoConfig = array( - "depot" => "depot", - "branch" => "branch", - "p4user" => "user", - "unique_perforce_client_name" => "TEST" + 'depot' => 'depot', + 'branch' => 'branch', + 'p4user' => 'user', + 'unique_perforce_client_name' => 'TEST' ); - $this->perforce = new Perforce($repoConfig, "port", "path", $this->processExecutor, true); + $this->perforce = new Perforce($repoConfig, 'port', 'path', $this->processExecutor, true); } public function testGetClientWithoutStream() @@ -42,7 +42,7 @@ class PerforceTest extends \PHPUnit_Framework_TestCase $hostname = gethostname(); $timestamp = time(); - $expected = "composer_perforce_TEST_depot"; + $expected = 'composer_perforce_TEST_depot'; $this->assertEquals($expected, $client); } @@ -52,7 +52,7 @@ class PerforceTest extends \PHPUnit_Framework_TestCase $client = $this->perforce->getClient(); - $expected = "composer_perforce_TEST_depot_branch"; + $expected = 'composer_perforce_TEST_depot_branch'; $this->assertEquals($expected, $client); } @@ -67,34 +67,34 @@ class PerforceTest extends \PHPUnit_Framework_TestCase $this->setPerforceToStream(); $stream = $this->perforce->getStream(); - $this->assertEquals("//depot/branch", $stream); + $this->assertEquals('//depot/branch', $stream); } public function testGetStreamWithoutLabelWithStreamWithoutLabel() { - $stream = $this->perforce->getStreamWithoutLabel("//depot/branch"); - $this->assertEquals("//depot/branch", $stream); + $stream = $this->perforce->getStreamWithoutLabel('//depot/branch'); + $this->assertEquals('//depot/branch', $stream); } public function testGetStreamWithoutLabelWithStreamWithLabel() { - $stream = $this->perforce->getStreamWithoutLabel("//depot/branching@label"); - $this->assertEquals("//depot/branching", $stream); + $stream = $this->perforce->getStreamWithoutLabel('//depot/branching@label'); + $this->assertEquals('//depot/branching', $stream); } public function testGetClientSpec() { $clientSpec = $this->perforce->getP4ClientSpec(); - $expected = "path/composer_perforce_TEST_depot.p4.spec"; + $expected = 'path/composer_perforce_TEST_depot.p4.spec'; $this->assertEquals($expected, $clientSpec); } public function testGenerateP4Command() { - $command = "do something"; + $command = 'do something'; $p4Command = $this->perforce->generateP4Command($command); - $expected = "p4 -u user -c composer_perforce_TEST_depot -p port do something"; + $expected = 'p4 -u user -c composer_perforce_TEST_depot -p port do something'; $this->assertEquals($expected, $p4Command); } @@ -102,40 +102,40 @@ class PerforceTest extends \PHPUnit_Framework_TestCase { $io = $this->getMock('Composer\IO\IOInterface'); - $repoConfig = array("depot" => "depot", "branch" => "branch", "p4user" => "TEST_USER"); - $this->perforce = new Perforce($repoConfig, "port", "path", $this->processExecutor, true, "TEST"); + $repoConfig = array('depot' => 'depot', 'branch' => 'branch', 'p4user' => 'TEST_USER'); + $this->perforce = new Perforce($repoConfig, 'port', 'path', $this->processExecutor, true, 'TEST'); $this->perforce->queryP4user($io); - $this->assertEquals("TEST_USER", $this->perforce->getUser()); + $this->assertEquals('TEST_USER', $this->perforce->getUser()); } public function testQueryP4UserWithUserSetInP4VariablesWithWindowsOS() { - $repoConfig = array("depot" => "depot", "branch" => "branch"); - $this->perforce = new Perforce($repoConfig, "port", "path", $this->processExecutor, true, "TEST"); + $repoConfig = array('depot' => 'depot', 'branch' => 'branch'); + $this->perforce = new Perforce($repoConfig, 'port', 'path', $this->processExecutor, true, 'TEST'); $io = $this->getMock('Composer\IO\IOInterface'); - $expectedCommand = "p4 set"; + $expectedCommand = 'p4 set'; $this->processExecutor->expects($this->at(0)) ->method('execute') ->with($this->equalTo($expectedCommand)) ->will( $this->returnCallback( function ($command, &$output) { - $output = "P4USER=TEST_P4VARIABLE_USER\n"; + $output = 'P4USER=TEST_P4VARIABLE_USER' . PHP_EOL ; return true; } ) ); $this->perforce->queryP4user($io); - $this->assertEquals("TEST_P4VARIABLE_USER", $this->perforce->getUser()); + $this->assertEquals('TEST_P4VARIABLE_USER', $this->perforce->getUser()); } public function testQueryP4UserWithUserSetInP4VariablesNotWindowsOS() { - $repoConfig = array("depot" => "depot", "branch" => "branch"); - $this->perforce = new Perforce($repoConfig, "port", "path", $this->processExecutor, false, "TEST"); + $repoConfig = array('depot' => 'depot', 'branch' => 'branch'); + $this->perforce = new Perforce($repoConfig, 'port', 'path', $this->processExecutor, false, 'TEST'); $io = $this->getMock('Composer\IO\IOInterface'); $expectedCommand = 'echo $P4USER'; @@ -145,43 +145,43 @@ class PerforceTest extends \PHPUnit_Framework_TestCase ->will( $this->returnCallback( function ($command, &$output) { - $output = "TEST_P4VARIABLE_USER\n"; + $output = 'TEST_P4VARIABLE_USER' . PHP_EOL; return true; } ) ); $this->perforce->queryP4user($io); - $this->assertEquals("TEST_P4VARIABLE_USER", $this->perforce->getUser()); + $this->assertEquals('TEST_P4VARIABLE_USER', $this->perforce->getUser()); } public function testQueryP4UserQueriesForUser() { - $repoConfig = array("depot" => "depot", "branch" => "branch"); - $this->perforce = new Perforce($repoConfig, "port", "path", $this->processExecutor, false, "TEST"); + $repoConfig = array('depot' => 'depot', 'branch' => 'branch'); + $this->perforce = new Perforce($repoConfig, 'port', 'path', $this->processExecutor, false, 'TEST'); $io = $this->getMock('Composer\IO\IOInterface'); - $expectedQuestion = "Enter P4 User:"; + $expectedQuestion = 'Enter P4 User:'; $io->expects($this->at(0)) ->method('ask') ->with($this->equalTo($expectedQuestion)) - ->will($this->returnValue("TEST_QUERY_USER")); + ->will($this->returnValue('TEST_QUERY_USER')); $this->perforce->queryP4user($io); - $this->assertEquals("TEST_QUERY_USER", $this->perforce->getUser()); + $this->assertEquals('TEST_QUERY_USER', $this->perforce->getUser()); } public function testQueryP4UserStoresResponseToQueryForUserWithWindows() { - $repoConfig = array("depot" => "depot", "branch" => "branch"); - $this->perforce = new Perforce($repoConfig, "port", "path", $this->processExecutor, true, "TEST"); + $repoConfig = array('depot' => 'depot', 'branch' => 'branch'); + $this->perforce = new Perforce($repoConfig, 'port', 'path', $this->processExecutor, true, 'TEST'); $io = $this->getMock('Composer\IO\IOInterface'); - $expectedQuestion = "Enter P4 User:"; + $expectedQuestion = 'Enter P4 User:'; $io->expects($this->at(0)) ->method('ask') ->with($this->equalTo($expectedQuestion)) - ->will($this->returnValue("TEST_QUERY_USER")); - $expectedCommand = "p4 set P4USER=TEST_QUERY_USER"; + ->will($this->returnValue('TEST_QUERY_USER')); + $expectedCommand = 'p4 set P4USER=TEST_QUERY_USER'; $this->processExecutor->expects($this->at(1)) ->method('execute') ->with($this->equalTo($expectedCommand)) @@ -192,16 +192,16 @@ class PerforceTest extends \PHPUnit_Framework_TestCase public function testQueryP4UserStoresResponseToQueryForUserWithoutWindows() { - $repoConfig = array("depot" => "depot", "branch" => "branch"); - $this->perforce = new Perforce($repoConfig, "port", "path", $this->processExecutor, false, "TEST"); + $repoConfig = array('depot' => 'depot', 'branch' => 'branch'); + $this->perforce = new Perforce($repoConfig, 'port', 'path', $this->processExecutor, false, 'TEST'); $io = $this->getMock('Composer\IO\IOInterface'); - $expectedQuestion = "Enter P4 User:"; + $expectedQuestion = 'Enter P4 User:'; $io->expects($this->at(0)) ->method('ask') ->with($this->equalTo($expectedQuestion)) - ->will($this->returnValue("TEST_QUERY_USER")); - $expectedCommand = "export P4USER=TEST_QUERY_USER"; + ->will($this->returnValue('TEST_QUERY_USER')); + $expectedCommand = 'export P4USER=TEST_QUERY_USER'; $this->processExecutor->expects($this->at(1)) ->method('execute') ->with($this->equalTo($expectedCommand)) @@ -213,43 +213,43 @@ class PerforceTest extends \PHPUnit_Framework_TestCase public function testQueryP4PasswordWithPasswordAlreadySet() { $repoConfig = array( - "depot" => "depot", - "branch" => "branch", - "p4user" => "user", - "p4password" => "TEST_PASSWORD" + 'depot' => 'depot', + 'branch' => 'branch', + 'p4user' => 'user', + 'p4password' => 'TEST_PASSWORD' ); - $this->perforce = new Perforce($repoConfig, "port", "path", $this->processExecutor, false, "TEST"); + $this->perforce = new Perforce($repoConfig, 'port', 'path', $this->processExecutor, false, 'TEST'); $io = $this->getMock('Composer\IO\IOInterface'); $password = $this->perforce->queryP4Password($io); - $this->assertEquals("TEST_PASSWORD", $password); + $this->assertEquals('TEST_PASSWORD', $password); } public function testQueryP4PasswordWithPasswordSetInP4VariablesWithWindowsOS() { $io = $this->getMock('Composer\IO\IOInterface'); - $expectedCommand = "p4 set"; + $expectedCommand = 'p4 set'; $this->processExecutor->expects($this->at(0)) ->method('execute') ->with($this->equalTo($expectedCommand)) ->will( $this->returnCallback( function ($command, &$output) { - $output = "P4PASSWD=TEST_P4VARIABLE_PASSWORD\n"; + $output = 'P4PASSWD=TEST_P4VARIABLE_PASSWORD' . PHP_EOL; return true; } ) ); $password = $this->perforce->queryP4Password($io); - $this->assertEquals("TEST_P4VARIABLE_PASSWORD", $password); + $this->assertEquals('TEST_P4VARIABLE_PASSWORD', $password); } public function testQueryP4PasswordWithPasswordSetInP4VariablesNotWindowsOS() { - $repoConfig = array("depot" => "depot", "branch" => "branch", "p4user" => "user"); - $this->perforce = new Perforce($repoConfig, "port", "path", $this->processExecutor, false, "TEST"); + $repoConfig = array('depot' => 'depot', 'branch' => 'branch', 'p4user' => 'user'); + $this->perforce = new Perforce($repoConfig, 'port', 'path', $this->processExecutor, false, 'TEST'); $io = $this->getMock('Composer\IO\IOInterface'); $expectedCommand = 'echo $P4PASSWD'; @@ -259,35 +259,36 @@ class PerforceTest extends \PHPUnit_Framework_TestCase ->will( $this->returnCallback( function ($command, &$output) { - $output = "TEST_P4VARIABLE_PASSWORD\n"; + $output = 'TEST_P4VARIABLE_PASSWORD' . PHP_EOL; return true; } ) ); $password = $this->perforce->queryP4Password($io); - $this->assertEquals("TEST_P4VARIABLE_PASSWORD", $password); + $this->assertEquals('TEST_P4VARIABLE_PASSWORD', $password); } public function testQueryP4PasswordQueriesForPassword() { $io = $this->getMock('Composer\IO\IOInterface'); - $expectedQuestion = "Enter password for Perforce user user: "; + $expectedQuestion = 'Enter password for Perforce user user: '; $io->expects($this->at(0)) ->method('askAndHideAnswer') ->with($this->equalTo($expectedQuestion)) - ->will($this->returnValue("TEST_QUERY_PASSWORD")); + ->will($this->returnValue('TEST_QUERY_PASSWORD')); $password = $this->perforce->queryP4Password($io); - $this->assertEquals("TEST_QUERY_PASSWORD", $password); + $this->assertEquals('TEST_QUERY_PASSWORD', $password); } public function testWriteP4ClientSpecWithoutStream() { - $stream = fopen("php://memory", 'w+'); + $stream = fopen('php://memory', 'w+'); $this->perforce->writeClientSpecToFile($stream); rewind($stream); + $expectedArray = $this->getExpectedClientSpec(false); try { foreach ($expectedArray as $expected) { @@ -304,7 +305,7 @@ class PerforceTest extends \PHPUnit_Framework_TestCase public function testWriteP4ClientSpecWithStream() { $this->setPerforceToStream(); - $stream = fopen("php://memory", 'w+'); + $stream = fopen('php://memory', 'w+'); $this->perforce->writeClientSpecToFile($stream); rewind($stream); @@ -324,7 +325,7 @@ class PerforceTest extends \PHPUnit_Framework_TestCase public function testIsLoggedIn() { - $expectedCommand = "p4 -u user -p port login -s"; + $expectedCommand = 'p4 -u user -p port login -s'; $this->processExecutor->expects($this->at(0)) ->method('execute') ->with($this->equalTo($expectedCommand), $this->equalTo(null)) @@ -335,7 +336,7 @@ class PerforceTest extends \PHPUnit_Framework_TestCase public function testConnectClient() { - $expectedCommand = "p4 -u user -c composer_perforce_TEST_depot -p port client -i < path/composer_perforce_TEST_depot.p4.spec"; + $expectedCommand = 'p4 -u user -c composer_perforce_TEST_depot -p port client -i < path/composer_perforce_TEST_depot.p4.spec'; $this->processExecutor->expects($this->at(0)) ->method('execute') ->with($this->equalTo($expectedCommand), $this->equalTo(null)) @@ -348,74 +349,74 @@ class PerforceTest extends \PHPUnit_Framework_TestCase { $this->setPerforceToStream(); - $expectedCommand = "p4 -u user -c composer_perforce_TEST_depot_branch -p port streams //depot/..."; + $expectedCommand = 'p4 -u user -c composer_perforce_TEST_depot_branch -p port streams //depot/...'; $this->processExecutor->expects($this->at(0)) ->method('execute') ->with($this->equalTo($expectedCommand)) ->will( $this->returnCallback( function ($command, &$output) { - $output = "Stream //depot/branch mainline none 'branch'\n"; + $output = 'Stream //depot/branch mainline none \'branch\'' . PHP_EOL; return true; } ) ); $branches = $this->perforce->getBranches(); - $this->assertEquals("//depot/branch", $branches['master']); + $this->assertEquals('//depot/branch', $branches['master']); } public function testGetBranchesWithoutStream() { $branches = $this->perforce->getBranches(); - $this->assertEquals("//depot", $branches['master']); + $this->assertEquals('//depot', $branches['master']); } public function testGetTagsWithoutStream() { - $expectedCommand = "p4 -u user -c composer_perforce_TEST_depot -p port labels"; + $expectedCommand = 'p4 -u user -c composer_perforce_TEST_depot -p port labels'; $this->processExecutor->expects($this->at(0)) ->method('execute') ->with($this->equalTo($expectedCommand)) ->will( $this->returnCallback( function ($command, &$output) { - $output = "Label 0.0.1 2013/07/31 'First Label!'\nLabel 0.0.2 2013/08/01 'Second Label!'\n"; + $output = 'Label 0.0.1 2013/07/31 \'First Label!\'' . PHP_EOL . 'Label 0.0.2 2013/08/01 \'Second Label!\'' . PHP_EOL; return true; } ) ); $tags = $this->perforce->getTags(); - $this->assertEquals("//depot@0.0.1", $tags['0.0.1']); - $this->assertEquals("//depot@0.0.2", $tags['0.0.2']); + $this->assertEquals('//depot@0.0.1', $tags['0.0.1']); + $this->assertEquals('//depot@0.0.2', $tags['0.0.2']); } public function testGetTagsWithStream() { $this->setPerforceToStream(); - $expectedCommand = "p4 -u user -c composer_perforce_TEST_depot_branch -p port labels"; + $expectedCommand = 'p4 -u user -c composer_perforce_TEST_depot_branch -p port labels'; $this->processExecutor->expects($this->at(0)) ->method('execute') ->with($this->equalTo($expectedCommand)) ->will( $this->returnCallback( function ($command, &$output) { - $output = "Label 0.0.1 2013/07/31 'First Label!'\nLabel 0.0.2 2013/08/01 'Second Label!'\n"; + $output = 'Label 0.0.1 2013/07/31 \'First Label!\'' . PHP_EOL . 'Label 0.0.2 2013/08/01 \'Second Label!\'' . PHP_EOL; return true; } ) ); $tags = $this->perforce->getTags(); - $this->assertEquals("//depot/branch@0.0.1", $tags['0.0.1']); - $this->assertEquals("//depot/branch@0.0.2", $tags['0.0.2']); + $this->assertEquals('//depot/branch@0.0.1', $tags['0.0.1']); + $this->assertEquals('//depot/branch@0.0.2', $tags['0.0.2']); } public function testCheckStreamWithoutStream() { - $result = $this->perforce->checkStream("depot"); + $result = $this->perforce->checkStream('depot'); $this->assertFalse($result); $this->assertFalse($this->perforce->isStream()); } @@ -426,19 +427,19 @@ class PerforceTest extends \PHPUnit_Framework_TestCase ->will( $this->returnCallback( function ($command, &$output) { - $output = "Depot depot 2013/06/25 stream /p4/1/depots/depot/... 'Created by Me'"; + $output = 'Depot depot 2013/06/25 stream /p4/1/depots/depot/... \'Created by Me\''; return true; } ) ); - $result = $this->perforce->checkStream("depot"); + $result = $this->perforce->checkStream('depot'); $this->assertTrue($result); $this->assertTrue($this->perforce->isStream()); } public function testGetComposerInformationWithoutLabelWithoutStream() { - $expectedCommand = "p4 -u user -c composer_perforce_TEST_depot -p port print //depot/composer.json"; + $expectedCommand = 'p4 -u user -c composer_perforce_TEST_depot -p port print //depot/composer.json'; $this->processExecutor->expects($this->at(0)) ->method('execute') ->with($this->equalTo($expectedCommand)) @@ -451,32 +452,32 @@ class PerforceTest extends \PHPUnit_Framework_TestCase ) ); - $result = $this->perforce->getComposerInformation("//depot"); + $result = $this->perforce->getComposerInformation('//depot'); $expected = array( - "name" => "test/perforce", - "description" => "Basic project for testing", - "minimum-stability" => "dev", - "autoload" => array("psr-0" => array()) + 'name' => 'test/perforce', + 'description' => 'Basic project for testing', + 'minimum-stability' => 'dev', + 'autoload' => array('psr-0' => array()) ); $this->assertEquals($expected, $result); } public function testGetComposerInformationWithLabelWithoutStream() { - $expectedCommand = "p4 -u user -p port files //depot/composer.json@0.0.1"; + $expectedCommand = 'p4 -u user -p port files //depot/composer.json@0.0.1'; $this->processExecutor->expects($this->at(0)) ->method('execute') ->with($this->equalTo($expectedCommand)) ->will( $this->returnCallback( function ($command, &$output) { - $output = "//depot/composer.json#1 - branch change 10001 (text)"; + $output = '//depot/composer.json#1 - branch change 10001 (text)'; return true; } ) ); - $expectedCommand = "p4 -u user -c composer_perforce_TEST_depot -p port print //depot/composer.json@10001"; + $expectedCommand = 'p4 -u user -c composer_perforce_TEST_depot -p port print //depot/composer.json@10001'; $this->processExecutor->expects($this->at(1)) ->method('execute') ->with($this->equalTo($expectedCommand)) @@ -489,13 +490,13 @@ class PerforceTest extends \PHPUnit_Framework_TestCase ) ); - $result = $this->perforce->getComposerInformation("//depot@0.0.1"); + $result = $this->perforce->getComposerInformation('//depot@0.0.1'); $expected = array( - "name" => "test/perforce", - "description" => "Basic project for testing", - "minimum-stability" => "dev", - "autoload" => array("psr-0" => array()) + 'name' => 'test/perforce', + 'description' => 'Basic project for testing', + 'minimum-stability' => 'dev', + 'autoload' => array('psr-0' => array()) ); $this->assertEquals($expected, $result); } @@ -504,7 +505,7 @@ class PerforceTest extends \PHPUnit_Framework_TestCase { $this->setPerforceToStream(); - $expectedCommand = "p4 -u user -c composer_perforce_TEST_depot_branch -p port print //depot/branch/composer.json"; + $expectedCommand = 'p4 -u user -c composer_perforce_TEST_depot_branch -p port print //depot/branch/composer.json'; $this->processExecutor->expects($this->at(0)) ->method('execute') ->with($this->equalTo($expectedCommand)) @@ -517,13 +518,13 @@ class PerforceTest extends \PHPUnit_Framework_TestCase ) ); - $result = $this->perforce->getComposerInformation("//depot/branch"); + $result = $this->perforce->getComposerInformation('//depot/branch'); $expected = array( - "name" => "test/perforce", - "description" => "Basic project for testing", - "minimum-stability" => "dev", - "autoload" => array("psr-0" => array()) + 'name' => 'test/perforce', + 'description' => 'Basic project for testing', + 'minimum-stability' => 'dev', + 'autoload' => array('psr-0' => array()) ); $this->assertEquals($expected, $result); } @@ -531,20 +532,20 @@ class PerforceTest extends \PHPUnit_Framework_TestCase public function testGetComposerInformationWithLabelWithStream() { $this->setPerforceToStream(); - $expectedCommand = "p4 -u user -p port files //depot/branch/composer.json@0.0.1"; + $expectedCommand = 'p4 -u user -p port files //depot/branch/composer.json@0.0.1'; $this->processExecutor->expects($this->at(0)) ->method('execute') ->with($this->equalTo($expectedCommand)) ->will( $this->returnCallback( function ($command, &$output) { - $output = "//depot/composer.json#1 - branch change 10001 (text)"; + $output = '//depot/composer.json#1 - branch change 10001 (text)'; return true; } ) ); - $expectedCommand = "p4 -u user -c composer_perforce_TEST_depot_branch -p port print //depot/branch/composer.json@10001"; + $expectedCommand = 'p4 -u user -c composer_perforce_TEST_depot_branch -p port print //depot/branch/composer.json@10001'; $this->processExecutor->expects($this->at(1)) ->method('execute') ->with($this->equalTo($expectedCommand)) @@ -557,51 +558,51 @@ class PerforceTest extends \PHPUnit_Framework_TestCase ) ); - $result = $this->perforce->getComposerInformation("//depot/branch@0.0.1"); + $result = $this->perforce->getComposerInformation('//depot/branch@0.0.1'); $expected = array( - "name" => "test/perforce", - "description" => "Basic project for testing", - "minimum-stability" => "dev", - "autoload" => array("psr-0" => array()) + 'name' => 'test/perforce', + 'description' => 'Basic project for testing', + 'minimum-stability' => 'dev', + 'autoload' => array('psr-0' => array()) ); $this->assertEquals($expected, $result); } public function testSyncCodeBaseWithoutStream() { - $expectedCommand = "p4 -u user -c composer_perforce_TEST_depot -p port sync -f @label"; + $expectedCommand = 'p4 -u user -c composer_perforce_TEST_depot -p port sync -f @label'; $this->processExecutor->expects($this->at(1)) ->method('execute') ->with($this->equalTo($expectedCommand), $this->equalTo(null)) ->will($this->returnValue(0)); - $this->perforce->syncCodeBase("label"); + $this->perforce->syncCodeBase('label'); } public function testSyncCodeBaseWithStream() { $this->setPerforceToStream(); - $expectedCommand = "p4 -u user -c composer_perforce_TEST_depot_branch -p port sync -f @label"; + $expectedCommand = 'p4 -u user -c composer_perforce_TEST_depot_branch -p port sync -f @label'; $this->processExecutor->expects($this->at(1)) ->method('execute') ->with($this->equalTo($expectedCommand)) ->will($this->returnValue(0)); - $this->perforce->syncCodeBase("label"); + $this->perforce->syncCodeBase('label'); } public function testCheckServerExists() { $processExecutor = $this->getMock('Composer\Util\ProcessExecutor'); - $expectedCommand = "p4 -p perforce.does.exist:port info -s"; + $expectedCommand = 'p4 -p perforce.does.exist:port info -s'; $processExecutor->expects($this->at(0)) ->method('execute') ->with($this->equalTo($expectedCommand), $this->equalTo(null)) ->will($this->returnValue(0)); - $result = $this->perforce->checkServerExists("perforce.does.exist:port", $processExecutor); + $result = $this->perforce->checkServerExists('perforce.does.exist:port', $processExecutor); $this->assertTrue($result); } @@ -609,13 +610,13 @@ class PerforceTest extends \PHPUnit_Framework_TestCase { $processExecutor = $this->getMock('Composer\Util\ProcessExecutor'); - $expectedCommand = "p4 -p perforce.does.not.exist:port info -s"; + $expectedCommand = 'p4 -p perforce.does.not.exist:port info -s'; $processExecutor->expects($this->at(0)) ->method('execute') ->with($this->equalTo($expectedCommand), $this->equalTo(null)) - ->will($this->returnValue("Perforce client error:")); + ->will($this->returnValue('Perforce client error:')); - $result = $this->perforce->checkServerExists("perforce.does.not.exist:port", $processExecutor); + $result = $this->perforce->checkServerExists('perforce.does.not.exist:port', $processExecutor); $this->assertTrue($result); } @@ -639,30 +640,30 @@ class PerforceTest extends \PHPUnit_Framework_TestCase private function getExpectedClientSpec($withStream) { $expectedArray = array( - "Client: composer_perforce_TEST_depot", - "\n", - "Update:", - "\n", - "Access:", - "Owner: user", - "\n", - "Description:", - " Created by user from composer.", - "\n", - "Root: path", - "\n", - "Options: noallwrite noclobber nocompress unlocked modtime rmdir", - "\n", - "SubmitOptions: revertunchanged", - "\n", - "LineEnd: local", - "\n" + 'Client: composer_perforce_TEST_depot', + PHP_EOL, + 'Update:', + PHP_EOL, + 'Access:', + 'Owner: user', + PHP_EOL, + 'Description:', + ' Created by user from composer.', + PHP_EOL, + 'Root: path', + PHP_EOL, + 'Options: noallwrite noclobber nocompress unlocked modtime rmdir', + PHP_EOL, + 'SubmitOptions: revertunchanged', + PHP_EOL, + 'LineEnd: local', + PHP_EOL ); if ($withStream) { - $expectedArray[] = "Stream:"; - $expectedArray[] = " //depot/branch"; + $expectedArray[] = 'Stream:'; + $expectedArray[] = ' //depot/branch'; } else { - $expectedArray[] = "View: //depot/... //composer_perforce_TEST_depot/depot/..."; + $expectedArray[] = 'View: //depot/... //composer_perforce_TEST_depot/depot/...'; } return $expectedArray; @@ -670,6 +671,6 @@ class PerforceTest extends \PHPUnit_Framework_TestCase private function setPerforceToStream() { - $this->perforce->setStream("//depot/branch"); + $this->perforce->setStream('//depot/branch'); } } From 114f6c9b6b2c7014e45be2ae57cc4581f13c2bfb Mon Sep 17 00:00:00 2001 From: mwhittom Date: Mon, 9 Sep 2013 15:36:16 -0500 Subject: [PATCH 24/26] Replaced proc_open in windowsLogin method with call to Symfony Process Component --- src/Composer/Util/Perforce.php | 24 +++--------------------- 1 file changed, 3 insertions(+), 21 deletions(-) diff --git a/src/Composer/Util/Perforce.php b/src/Composer/Util/Perforce.php index eedc204c9..1ae0316df 100644 --- a/src/Composer/Util/Perforce.php +++ b/src/Composer/Util/Perforce.php @@ -13,6 +13,7 @@ namespace Composer\Util; use Composer\IO\IOInterface; +use Symfony\Component\Process\Process; /** * @author Matt Whittom @@ -350,28 +351,9 @@ class Perforce public function windowsLogin($password) { - $descriptorspec = array( - 0 => array('pipe', 'r'), - 1 => array('pipe', 'w'), - 2 => array('pipe', 'a') - ); $command = $this->generateP4Command(' login -a'); - $process = proc_open($command, $descriptorspec, $pipes); - if (!is_resource($process)) { - return false; - } - fwrite($pipes[0], $password); - fclose($pipes[0]); - - $this->read($pipes[1], 'Output'); - $this->read($pipes[2], 'Error'); - - fclose($pipes[1]); - fclose($pipes[2]); - - $returnCode = proc_close($process); - - return $returnCode; + $process = new Process($command, null, null, $password); + return $process->run(); } From 1b68f9151e439e43c42db3c82e5136b0817f7db5 Mon Sep 17 00:00:00 2001 From: mwhittom Date: Tue, 10 Sep 2013 08:08:31 -0500 Subject: [PATCH 25/26] removed excess space at end of function call --- src/Composer/Util/Perforce.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Composer/Util/Perforce.php b/src/Composer/Util/Perforce.php index 1ae0316df..7ac147b49 100644 --- a/src/Composer/Util/Perforce.php +++ b/src/Composer/Util/Perforce.php @@ -299,7 +299,7 @@ class Perforce { fwrite($spec, 'Client: ' . $this->getClient() . PHP_EOL . PHP_EOL); fwrite($spec, 'Update: ' . date('Y/m/d H:i:s') . PHP_EOL . PHP_EOL); - fwrite($spec, 'Access: ' . date('Y/m/d H:i:s') . PHP_EOL ); + fwrite($spec, 'Access: ' . date('Y/m/d H:i:s') . PHP_EOL); fwrite($spec, 'Owner: ' . $this->getUser() . PHP_EOL . PHP_EOL); fwrite($spec, 'Description:' . PHP_EOL); fwrite($spec, ' Created by ' . $this->getUser() . ' from composer.' . PHP_EOL . PHP_EOL); From 8a319a719b1e8e3bb812ee8797fc6887bac27a05 Mon Sep 17 00:00:00 2001 From: mwhittom Date: Fri, 13 Sep 2013 15:01:00 -0500 Subject: [PATCH 26/26] Fixed issue with non-streaming perforce depots, also fixed issue where PerforceDownloader was not logging in --- src/Composer/Downloader/PerforceDownloader.php | 2 +- src/Composer/Util/Perforce.php | 10 ++-------- tests/Composer/Test/Util/PerforceTest.php | 2 +- 3 files changed, 4 insertions(+), 10 deletions(-) diff --git a/src/Composer/Downloader/PerforceDownloader.php b/src/Composer/Downloader/PerforceDownloader.php index ad94d1ad8..6ddea153f 100644 --- a/src/Composer/Downloader/PerforceDownloader.php +++ b/src/Composer/Downloader/PerforceDownloader.php @@ -36,7 +36,7 @@ class PerforceDownloader extends VcsDownloader $this->io->write(' Cloning ' . $ref); $this->initPerforce($package, $path, $ref); $this->perforce->setStream($ref); - $this->perforce->queryP4User($this->io); + $this->perforce->p4Login($this->io); $this->perforce->writeP4ClientSpec(); $this->perforce->connectClient(); $this->perforce->syncCodeBase($label); diff --git a/src/Composer/Util/Perforce.php b/src/Composer/Util/Perforce.php index 7ac147b49..2fb0e4bea 100644 --- a/src/Composer/Util/Perforce.php +++ b/src/Composer/Util/Perforce.php @@ -282,15 +282,13 @@ class Perforce $prevDir = getcwd(); chdir($this->path); - $this->executeCommand('pwd'); - $p4SyncCommand = $this->generateP4Command('sync -f '); if (isset($label)) { if (strcmp($label, 'dev-master') != 0) { $p4SyncCommand = $p4SyncCommand . '@' . $label; } } - $this->executeCommand($p4SyncCommand); + $result = $this->executeCommand($p4SyncCommand); chdir($prevDir); } @@ -313,11 +311,7 @@ class Perforce } else { fwrite( $spec, - 'View: ' . $this->getStream() . '/... //' . $this->getClient() . '/' . str_replace( - '//', - '', - $this->getStream() - ) . '/... ' . PHP_EOL + 'View: ' . $this->getStream() . '/... //' . $this->getClient() . '/... ' . PHP_EOL ); } } diff --git a/tests/Composer/Test/Util/PerforceTest.php b/tests/Composer/Test/Util/PerforceTest.php index 73b06bc6e..6bde3d797 100644 --- a/tests/Composer/Test/Util/PerforceTest.php +++ b/tests/Composer/Test/Util/PerforceTest.php @@ -663,7 +663,7 @@ class PerforceTest extends \PHPUnit_Framework_TestCase $expectedArray[] = 'Stream:'; $expectedArray[] = ' //depot/branch'; } else { - $expectedArray[] = 'View: //depot/... //composer_perforce_TEST_depot/depot/...'; + $expectedArray[] = 'View: //depot/... //composer_perforce_TEST_depot/...'; } return $expectedArray;