5 class GitInfoTest
extends MediaWikiTestCase
{
7 public static function setUpBeforeClass() {
8 mkdir( __DIR__
. '/../data/gitrepo' );
9 mkdir( __DIR__
. '/../data/gitrepo/1' );
10 mkdir( __DIR__
. '/../data/gitrepo/2' );
11 mkdir( __DIR__
. '/../data/gitrepo/3' );
12 mkdir( __DIR__
. '/../data/gitrepo/1/.git' );
13 mkdir( __DIR__
. '/../data/gitrepo/1/.git/refs' );
14 mkdir( __DIR__
. '/../data/gitrepo/1/.git/refs/heads' );
15 file_put_contents( __DIR__
. '/../data/gitrepo/1/.git/HEAD',
16 "ref: refs/heads/master\n" );
17 file_put_contents( __DIR__
. '/../data/gitrepo/1/.git/refs/heads/master',
18 "0123456789012345678901234567890123abcdef\n" );
19 file_put_contents( __DIR__
. '/../data/gitrepo/1/.git/packed-refs',
20 "abcdef6789012345678901234567890123456789 refs/heads/master\n" );
21 file_put_contents( __DIR__
. '/../data/gitrepo/2/.git',
22 "gitdir: ../1/.git\n" );
23 file_put_contents( __DIR__
. '/../data/gitrepo/3/.git',
24 'gitdir: ' . __DIR__
. "/../data/gitrepo/1/.git\n" );
27 public static function tearDownAfterClass() {
28 wfRecursiveRemoveDir( __DIR__
. '/../data/gitrepo' );
31 protected function setUp() {
33 $this->setMwGlobals( 'wgGitInfoCacheDirectory', __DIR__
. '/../data/gitinfo' );
36 protected function assertValidGitInfo( GitInfo
$gitInfo ) {
37 $this->assertTrue( $gitInfo->cacheIsComplete() );
38 $this->assertEquals( 'refs/heads/master', $gitInfo->getHead() );
39 $this->assertEquals( '0123456789abcdef0123456789abcdef01234567',
40 $gitInfo->getHeadSHA1() );
41 $this->assertEquals( '1070884800', $gitInfo->getHeadCommitDate() );
42 $this->assertEquals( 'master', $gitInfo->getCurrentBranch() );
43 $this->assertContains( '0123456789abcdef0123456789abcdef01234567',
44 $gitInfo->getHeadViewUrl() );
47 public function testValidJsonData() {
50 $this->assertValidGitInfo( new GitInfo( "$IP/testValidJsonData" ) );
51 $this->assertValidGitInfo( new GitInfo( __DIR__
. "/../data/gitinfo/extension" ) );
54 public function testMissingJsonData() {
55 $dir = $GLOBALS['IP'] . '/testMissingJsonData';
56 $fixture = new GitInfo( $dir );
58 $this->assertFalse( $fixture->cacheIsComplete() );
60 $this->assertEquals( false, $fixture->getHead() );
61 $this->assertEquals( false, $fixture->getHeadSHA1() );
62 $this->assertEquals( false, $fixture->getHeadCommitDate() );
63 $this->assertEquals( false, $fixture->getCurrentBranch() );
64 $this->assertEquals( false, $fixture->getHeadViewUrl() );
66 // After calling all the outputs, the cache should be complete
67 $this->assertTrue( $fixture->cacheIsComplete() );
70 public function testReadingHead() {
71 $dir = __DIR__
. '/../data/gitrepo/1';
72 $fixture = new GitInfo( $dir );
74 $this->assertEquals( 'refs/heads/master', $fixture->getHead() );
75 $this->assertEquals( '0123456789012345678901234567890123abcdef', $fixture->getHeadSHA1() );
78 public function testIndirection() {
79 $dir = __DIR__
. '/../data/gitrepo/2';
80 $fixture = new GitInfo( $dir );
82 $this->assertEquals( 'refs/heads/master', $fixture->getHead() );
83 $this->assertEquals( '0123456789012345678901234567890123abcdef', $fixture->getHeadSHA1() );
86 public function testIndirection2() {
87 $dir = __DIR__
. '/../data/gitrepo/3';
88 $fixture = new GitInfo( $dir );
90 $this->assertEquals( 'refs/heads/master', $fixture->getHead() );
91 $this->assertEquals( '0123456789012345678901234567890123abcdef', $fixture->getHeadSHA1() );
94 public function testReadingPackedRefs() {
95 $dir = __DIR__
. '/../data/gitrepo/1';
96 unlink( __DIR__
. '/../data/gitrepo/1/.git/refs/heads/master' );
97 $fixture = new GitInfo( $dir );
99 $this->assertEquals( 'refs/heads/master', $fixture->getHead() );
100 $this->assertEquals( 'abcdef6789012345678901234567890123456789', $fixture->getHeadSHA1() );