GitInfoTest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. /**
  3. * @covers GitInfo
  4. */
  5. class GitInfoTest extends MediaWikiTestCase {
  6. private static $tempDir;
  7. public static function setUpBeforeClass() {
  8. self::$tempDir = wfTempDir() . '/mw-phpunit-' . wfRandomString( 8 );
  9. if ( !mkdir( self::$tempDir ) ) {
  10. self::$tempDir = null;
  11. throw new Exception( 'Unable to create temporary directory' );
  12. }
  13. mkdir( self::$tempDir . '/gitrepo' );
  14. mkdir( self::$tempDir . '/gitrepo/1' );
  15. mkdir( self::$tempDir . '/gitrepo/2' );
  16. mkdir( self::$tempDir . '/gitrepo/3' );
  17. mkdir( self::$tempDir . '/gitrepo/1/.git' );
  18. mkdir( self::$tempDir . '/gitrepo/1/.git/refs' );
  19. mkdir( self::$tempDir . '/gitrepo/1/.git/refs/heads' );
  20. file_put_contents( self::$tempDir . '/gitrepo/1/.git/HEAD',
  21. "ref: refs/heads/master\n" );
  22. file_put_contents( self::$tempDir . '/gitrepo/1/.git/refs/heads/master',
  23. "0123456789012345678901234567890123abcdef\n" );
  24. file_put_contents( self::$tempDir . '/gitrepo/1/.git/packed-refs',
  25. "abcdef6789012345678901234567890123456789 refs/heads/master\n" );
  26. file_put_contents( self::$tempDir . '/gitrepo/2/.git',
  27. "gitdir: ../1/.git\n" );
  28. file_put_contents( self::$tempDir . '/gitrepo/3/.git',
  29. 'gitdir: ' . self::$tempDir . "/gitrepo/1/.git\n" );
  30. }
  31. public static function tearDownAfterClass() {
  32. if ( self::$tempDir ) {
  33. wfRecursiveRemoveDir( self::$tempDir );
  34. }
  35. }
  36. protected function setUp() {
  37. parent::setUp();
  38. $this->setMwGlobals( 'wgGitInfoCacheDirectory', __DIR__ . '/../data/gitinfo' );
  39. }
  40. protected function assertValidGitInfo( GitInfo $gitInfo ) {
  41. $this->assertTrue( $gitInfo->cacheIsComplete() );
  42. $this->assertEquals( 'refs/heads/master', $gitInfo->getHead() );
  43. $this->assertEquals( '0123456789abcdef0123456789abcdef01234567',
  44. $gitInfo->getHeadSHA1() );
  45. $this->assertEquals( '1070884800', $gitInfo->getHeadCommitDate() );
  46. $this->assertEquals( 'master', $gitInfo->getCurrentBranch() );
  47. $this->assertContains( '0123456789abcdef0123456789abcdef01234567',
  48. $gitInfo->getHeadViewUrl() );
  49. }
  50. public function testValidJsonData() {
  51. global $IP;
  52. $this->assertValidGitInfo( new GitInfo( "$IP/testValidJsonData" ) );
  53. $this->assertValidGitInfo( new GitInfo( __DIR__ . "/../data/gitinfo/extension" ) );
  54. }
  55. public function testMissingJsonData() {
  56. $dir = $GLOBALS['IP'] . '/testMissingJsonData';
  57. $fixture = new GitInfo( $dir );
  58. $this->assertFalse( $fixture->cacheIsComplete() );
  59. $this->assertEquals( false, $fixture->getHead() );
  60. $this->assertEquals( false, $fixture->getHeadSHA1() );
  61. $this->assertEquals( false, $fixture->getHeadCommitDate() );
  62. $this->assertEquals( false, $fixture->getCurrentBranch() );
  63. $this->assertEquals( false, $fixture->getHeadViewUrl() );
  64. // After calling all the outputs, the cache should be complete
  65. $this->assertTrue( $fixture->cacheIsComplete() );
  66. }
  67. public function testReadingHead() {
  68. $dir = self::$tempDir . '/gitrepo/1';
  69. $fixture = new GitInfo( $dir );
  70. $this->assertEquals( 'refs/heads/master', $fixture->getHead() );
  71. $this->assertEquals( '0123456789012345678901234567890123abcdef', $fixture->getHeadSHA1() );
  72. }
  73. public function testIndirection() {
  74. $dir = self::$tempDir . '/gitrepo/2';
  75. $fixture = new GitInfo( $dir );
  76. $this->assertEquals( 'refs/heads/master', $fixture->getHead() );
  77. $this->assertEquals( '0123456789012345678901234567890123abcdef', $fixture->getHeadSHA1() );
  78. }
  79. public function testIndirection2() {
  80. $dir = self::$tempDir . '/gitrepo/3';
  81. $fixture = new GitInfo( $dir );
  82. $this->assertEquals( 'refs/heads/master', $fixture->getHead() );
  83. $this->assertEquals( '0123456789012345678901234567890123abcdef', $fixture->getHeadSHA1() );
  84. }
  85. public function testReadingPackedRefs() {
  86. $dir = self::$tempDir . '/gitrepo/1';
  87. unlink( self::$tempDir . '/gitrepo/1/.git/refs/heads/master' );
  88. $fixture = new GitInfo( $dir );
  89. $this->assertEquals( 'refs/heads/master', $fixture->getHead() );
  90. $this->assertEquals( 'abcdef6789012345678901234567890123456789', $fixture->getHeadSHA1() );
  91. }
  92. }