5 class GitInfoTest
extends MediaWikiTestCase
{
7 private static $tempDir;
9 public static function setUpBeforeClass() {
10 self
::$tempDir = wfTempDir() . '/mw-phpunit-' . wfRandomString( 8 );
11 if ( !mkdir( self
::$tempDir ) ) {
12 self
::$tempDir = null;
13 throw new Exception( 'Unable to create temporary directory' );
15 mkdir( self
::$tempDir . '/gitrepo' );
16 mkdir( self
::$tempDir . '/gitrepo/1' );
17 mkdir( self
::$tempDir . '/gitrepo/2' );
18 mkdir( self
::$tempDir . '/gitrepo/3' );
19 mkdir( self
::$tempDir . '/gitrepo/1/.git' );
20 mkdir( self
::$tempDir . '/gitrepo/1/.git/refs' );
21 mkdir( self
::$tempDir . '/gitrepo/1/.git/refs/heads' );
22 file_put_contents( self
::$tempDir . '/gitrepo/1/.git/HEAD',
23 "ref: refs/heads/master\n" );
24 file_put_contents( self
::$tempDir . '/gitrepo/1/.git/refs/heads/master',
25 "0123456789012345678901234567890123abcdef\n" );
26 file_put_contents( self
::$tempDir . '/gitrepo/1/.git/packed-refs',
27 "abcdef6789012345678901234567890123456789 refs/heads/master\n" );
28 file_put_contents( self
::$tempDir . '/gitrepo/2/.git',
29 "gitdir: ../1/.git\n" );
30 file_put_contents( self
::$tempDir . '/gitrepo/3/.git',
31 'gitdir: ' . self
::$tempDir . "/gitrepo/1/.git\n" );
34 public static function tearDownAfterClass() {
35 if ( self
::$tempDir ) {
36 wfRecursiveRemoveDir( self
::$tempDir );
40 protected function setUp() {
42 $this->setMwGlobals( 'wgGitInfoCacheDirectory', __DIR__
. '/../data/gitinfo' );
45 protected function assertValidGitInfo( GitInfo
$gitInfo ) {
46 $this->assertTrue( $gitInfo->cacheIsComplete() );
47 $this->assertEquals( 'refs/heads/master', $gitInfo->getHead() );
48 $this->assertEquals( '0123456789abcdef0123456789abcdef01234567',
49 $gitInfo->getHeadSHA1() );
50 $this->assertEquals( '1070884800', $gitInfo->getHeadCommitDate() );
51 $this->assertEquals( 'master', $gitInfo->getCurrentBranch() );
52 $this->assertContains( '0123456789abcdef0123456789abcdef01234567',
53 $gitInfo->getHeadViewUrl() );
56 public function testValidJsonData() {
59 $this->assertValidGitInfo( new GitInfo( "$IP/testValidJsonData" ) );
60 $this->assertValidGitInfo( new GitInfo( __DIR__
. "/../data/gitinfo/extension" ) );
63 public function testMissingJsonData() {
64 $dir = $GLOBALS['IP'] . '/testMissingJsonData';
65 $fixture = new GitInfo( $dir );
67 $this->assertFalse( $fixture->cacheIsComplete() );
69 $this->assertEquals( false, $fixture->getHead() );
70 $this->assertEquals( false, $fixture->getHeadSHA1() );
71 $this->assertEquals( false, $fixture->getHeadCommitDate() );
72 $this->assertEquals( false, $fixture->getCurrentBranch() );
73 $this->assertEquals( false, $fixture->getHeadViewUrl() );
75 // After calling all the outputs, the cache should be complete
76 $this->assertTrue( $fixture->cacheIsComplete() );
79 public function testReadingHead() {
80 $dir = self
::$tempDir . '/gitrepo/1';
81 $fixture = new GitInfo( $dir );
83 $this->assertEquals( 'refs/heads/master', $fixture->getHead() );
84 $this->assertEquals( '0123456789012345678901234567890123abcdef', $fixture->getHeadSHA1() );
87 public function testIndirection() {
88 $dir = self
::$tempDir . '/gitrepo/2';
89 $fixture = new GitInfo( $dir );
91 $this->assertEquals( 'refs/heads/master', $fixture->getHead() );
92 $this->assertEquals( '0123456789012345678901234567890123abcdef', $fixture->getHeadSHA1() );
95 public function testIndirection2() {
96 $dir = self
::$tempDir . '/gitrepo/3';
97 $fixture = new GitInfo( $dir );
99 $this->assertEquals( 'refs/heads/master', $fixture->getHead() );
100 $this->assertEquals( '0123456789012345678901234567890123abcdef', $fixture->getHeadSHA1() );
103 public function testReadingPackedRefs() {
104 $dir = self
::$tempDir . '/gitrepo/1';
105 unlink( self
::$tempDir . '/gitrepo/1/.git/refs/heads/master' );
106 $fixture = new GitInfo( $dir );
108 $this->assertEquals( 'refs/heads/master', $fixture->getHead() );
109 $this->assertEquals( 'abcdef6789012345678901234567890123456789', $fixture->getHeadSHA1() );