| 1 |
<?php |
| 2 |
|
| 3 |
use MediaWiki\MainConfigNames; |
| 4 |
use MediaWiki\Utils\GitInfo; |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
class GitInfoTest extends MediaWikiIntegrationTestCase { |
| 10 |
|
| 11 |
|
| 12 |
private static $tempDir; |
| 13 |
|
| 14 |
public static function setUpBeforeClass(): void { |
| 15 |
parent::setUpBeforeClass(); |
| 16 |
|
| 17 |
self::$tempDir = wfTempDir() . '/mw-phpunit-' . wfRandomString( 8 ); |
| 18 |
if ( !mkdir( self::$tempDir ) ) { |
| 19 |
self::$tempDir = null; |
| 20 |
self::fail( 'Unable to create temporary directory' ); |
| 21 |
} |
| 22 |
mkdir( self::$tempDir . '/gitrepo' ); |
| 23 |
mkdir( self::$tempDir . '/gitrepo/1' ); |
| 24 |
mkdir( self::$tempDir . '/gitrepo/2' ); |
| 25 |
mkdir( self::$tempDir . '/gitrepo/3' ); |
| 26 |
mkdir( self::$tempDir . '/gitrepo/1/.git' ); |
| 27 |
mkdir( self::$tempDir . '/gitrepo/1/.git/refs' ); |
| 28 |
mkdir( self::$tempDir . '/gitrepo/1/.git/refs/heads' ); |
| 29 |
file_put_contents( self::$tempDir . '/gitrepo/1/.git/HEAD', |
| 30 |
"ref: refs/heads/master\n" ); |
| 31 |
file_put_contents( self::$tempDir . '/gitrepo/1/.git/refs/heads/master', |
| 32 |
"0123456789012345678901234567890123abcdef\n" ); |
| 33 |
file_put_contents( self::$tempDir . '/gitrepo/1/.git/packed-refs', |
| 34 |
"abcdef6789012345678901234567890123456789 refs/heads/master\n" ); |
| 35 |
file_put_contents( self::$tempDir . '/gitrepo/2/.git', |
| 36 |
"gitdir: ../1/.git\n" ); |
| 37 |
file_put_contents( self::$tempDir . '/gitrepo/3/.git', |
| 38 |
'gitdir: ' . self::$tempDir . "/gitrepo/1/.git\n" ); |
| 39 |
} |
| 40 |
|
| 41 |
public static function tearDownAfterClass(): void { |
| 42 |
if ( self::$tempDir ) { |
| 43 |
wfRecursiveRemoveDir( self::$tempDir ); |
| 44 |
} |
| 45 |
parent::tearDownAfterClass(); |
| 46 |
} |
| 47 |
|
| 48 |
protected function setUp(): void { |
| 49 |
parent::setUp(); |
| 50 |
$this->overrideConfigValue( MainConfigNames::GitInfoCacheDirectory, __DIR__ . '/../../data/gitinfo' ); |
| 51 |
} |
| 52 |
|
| 53 |
protected function assertValidGitInfo( GitInfo $gitInfo ) { |
| 54 |
$this->assertTrue( $gitInfo->cacheIsComplete() ); |
| 55 |
$this->assertEquals( 'refs/heads/master', $gitInfo->getHead() ); |
| 56 |
$this->assertSame( '0123456789abcdef0123456789abcdef01234567', |
| 57 |
$gitInfo->getHeadSHA1() ); |
| 58 |
$this->assertSame( '1070884800', $gitInfo->getHeadCommitDate() ); |
| 59 |
$this->assertEquals( 'master', $gitInfo->getCurrentBranch() ); |
| 60 |
$this->assertStringContainsString( '0123456789abcdef0123456789abcdef01234567', |
| 61 |
$gitInfo->getHeadViewUrl() ); |
| 62 |
} |
| 63 |
|
| 64 |
public function testValidJsonData() { |
| 65 |
global $IP; |
| 66 |
|
| 67 |
$this->assertValidGitInfo( new GitInfo( "$IP/testValidJsonData" ) ); |
| 68 |
$this->assertValidGitInfo( new GitInfo( __DIR__ . "/../../data/gitinfo/extension" ) ); |
| 69 |
} |
| 70 |
|
| 71 |
public function testMissingJsonData() { |
| 72 |
$dir = $GLOBALS['IP'] . '/testMissingJsonData'; |
| 73 |
$fixture = new GitInfo( $dir ); |
| 74 |
|
| 75 |
$this->assertFalse( $fixture->cacheIsComplete() ); |
| 76 |
|
| 77 |
$this->assertFalse( $fixture->getHead() ); |
| 78 |
$this->assertFalse( $fixture->getHeadSHA1() ); |
| 79 |
$this->assertFalse( $fixture->getHeadCommitDate() ); |
| 80 |
$this->assertFalse( $fixture->getCurrentBranch() ); |
| 81 |
$this->assertFalse( $fixture->getHeadViewUrl() ); |
| 82 |
|
| 83 |
|
| 84 |
$this->assertTrue( $fixture->cacheIsComplete() ); |
| 85 |
} |
| 86 |
|
| 87 |
public function testReadingHead() { |
| 88 |
$dir = self::$tempDir . '/gitrepo/1'; |
| 89 |
$fixture = new GitInfo( $dir ); |
| 90 |
|
| 91 |
$this->assertEquals( 'refs/heads/master', $fixture->getHead() ); |
| 92 |
$this->assertSame( '0123456789012345678901234567890123abcdef', $fixture->getHeadSHA1() ); |
| 93 |
} |
| 94 |
|
| 95 |
public function testIndirection() { |
| 96 |
$dir = self::$tempDir . '/gitrepo/2'; |
| 97 |
$fixture = new GitInfo( $dir ); |
| 98 |
|
| 99 |
$this->assertEquals( 'refs/heads/master', $fixture->getHead() ); |
| 100 |
$this->assertSame( '0123456789012345678901234567890123abcdef', $fixture->getHeadSHA1() ); |
| 101 |
} |
| 102 |
|
| 103 |
public function testIndirection2() { |
| 104 |
$dir = self::$tempDir . '/gitrepo/3'; |
| 105 |
$fixture = new GitInfo( $dir ); |
| 106 |
|
| 107 |
$this->assertEquals( 'refs/heads/master', $fixture->getHead() ); |
| 108 |
$this->assertSame( '0123456789012345678901234567890123abcdef', $fixture->getHeadSHA1() ); |
| 109 |
} |
| 110 |
|
| 111 |
public function testReadingPackedRefs() { |
| 112 |
$dir = self::$tempDir . '/gitrepo/1'; |
| 113 |
unlink( self::$tempDir . '/gitrepo/1/.git/refs/heads/master' ); |
| 114 |
$fixture = new GitInfo( $dir ); |
| 115 |
|
| 116 |
$this->assertEquals( 'refs/heads/master', $fixture->getHead() ); |
| 117 |
$this->assertEquals( 'abcdef6789012345678901234567890123456789', $fixture->getHeadSHA1() ); |
| 118 |
} |
| 119 |
} |