MercurialArchiverTest.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. /*
  3. * This file is part of Composer.
  4. *
  5. * (c) Nils Adermann <naderman@naderman.de>
  6. * Jordi Boggiano <j.boggiano@seld.be>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Composer\Test\Package\Archiver;
  12. use Composer\Package\Archiver\MercurialArchiver;
  13. use Composer\Package\Package;
  14. /**
  15. * @author Matthieu Moquet <matthieu@moquet.net>
  16. * @author Till Klampaeckel <till@php.net>
  17. */
  18. class MercurialArchiverTest extends ArchiverTest
  19. {
  20. public function testZipArchive()
  21. {
  22. $this->setupMercurialRepo();
  23. $package = $this->setupMercurialPackage();
  24. $target = sys_get_temp_dir().'/composer_archiver_test.zip';
  25. // Test archive
  26. $archiver = new MercurialArchiver();
  27. $archiver->setFormat('zip');
  28. $archiver->setSourceRef('default');
  29. $archiver->archive($package->getSourceUrl(), $target);
  30. $this->assertFileExists($target);
  31. unlink($target);
  32. $this->removeMercurialRepo();
  33. }
  34. public function testTarArchive()
  35. {
  36. $this->setupMercurialRepo();
  37. $package = $this->setupMercurialPackage();
  38. $target = sys_get_temp_dir().'/composer_archiver_test.tar';
  39. // Test archive
  40. $archiver = new MercurialArchiver();
  41. $archiver->setFormat('tar');
  42. $archiver->setSourceRef('default');
  43. $archiver->archive($package->getSourceUrl(), $target);
  44. $this->assertFileExists($target);
  45. unlink($target);
  46. $this->removeMercurialRepo();
  47. }
  48. /**
  49. * Create local git repository to run tests against!
  50. */
  51. protected function setupMercurialRepo()
  52. {
  53. $this->filesystem->removeDirectory($this->testDir);
  54. $this->filesystem->ensureDirectoryExists($this->testDir);
  55. $currentWorkDir = getcwd();
  56. chdir($this->testDir);
  57. $result = $this->process->execute('hg init -q');
  58. if ($result > 0) {
  59. throw new \RuntimeException('Could not init: '.$this->process->getErrorOutput());
  60. }
  61. $result = file_put_contents('b', 'a');
  62. if (false === $result) {
  63. throw new \RuntimeException('Could not save file.');
  64. }
  65. $result = $this->process->execute('hg add b && hg commit -m "commit b" --config ui.username=test -q');
  66. if ($result > 0) {
  67. throw new \RuntimeException('Could not commit: '.$this->process->getErrorOutput());
  68. }
  69. chdir($currentWorkDir);
  70. }
  71. protected function removeMercurialRepo()
  72. {
  73. $this->filesystem->removeDirectory($this->testDir);
  74. }
  75. protected function setupMercurialPackage()
  76. {
  77. $package = new Package('archivertest/archivertest', 'master', 'master');
  78. $package->setSourceUrl(realpath($this->testDir));
  79. $package->setSourceReference('default');
  80. $package->setSourceType('hg');
  81. return $package;
  82. }
  83. }