MediaWiki REL1_33
GitInfoTest.php
Go to the documentation of this file.
1<?php
6
7 private static $tempDir;
8
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' );
14 }
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" );
32 }
33
34 public static function tearDownAfterClass() {
35 if ( self::$tempDir ) {
36 wfRecursiveRemoveDir( self::$tempDir );
37 }
38 }
39
40 protected function setUp() {
41 parent::setUp();
42 $this->setMwGlobals( 'wgGitInfoCacheDirectory', __DIR__ . '/../data/gitinfo' );
43 }
44
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() );
54 }
55
56 public function testValidJsonData() {
57 global $IP;
58
59 $this->assertValidGitInfo( new GitInfo( "$IP/testValidJsonData" ) );
60 $this->assertValidGitInfo( new GitInfo( __DIR__ . "/../data/gitinfo/extension" ) );
61 }
62
63 public function testMissingJsonData() {
64 $dir = $GLOBALS['IP'] . '/testMissingJsonData';
65 $fixture = new GitInfo( $dir );
66
67 $this->assertFalse( $fixture->cacheIsComplete() );
68
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() );
74
75 // After calling all the outputs, the cache should be complete
76 $this->assertTrue( $fixture->cacheIsComplete() );
77 }
78
79 public function testReadingHead() {
80 $dir = self::$tempDir . '/gitrepo/1';
81 $fixture = new GitInfo( $dir );
82
83 $this->assertEquals( 'refs/heads/master', $fixture->getHead() );
84 $this->assertEquals( '0123456789012345678901234567890123abcdef', $fixture->getHeadSHA1() );
85 }
86
87 public function testIndirection() {
88 $dir = self::$tempDir . '/gitrepo/2';
89 $fixture = new GitInfo( $dir );
90
91 $this->assertEquals( 'refs/heads/master', $fixture->getHead() );
92 $this->assertEquals( '0123456789012345678901234567890123abcdef', $fixture->getHeadSHA1() );
93 }
94
95 public function testIndirection2() {
96 $dir = self::$tempDir . '/gitrepo/3';
97 $fixture = new GitInfo( $dir );
98
99 $this->assertEquals( 'refs/heads/master', $fixture->getHead() );
100 $this->assertEquals( '0123456789012345678901234567890123abcdef', $fixture->getHeadSHA1() );
101 }
102
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 );
107
108 $this->assertEquals( 'refs/heads/master', $fixture->getHead() );
109 $this->assertEquals( 'abcdef6789012345678901234567890123456789', $fixture->getHeadSHA1() );
110 }
111}
and that you know you can do these things To protect your we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights These restrictions translate to certain responsibilities for you if you distribute copies of the or if you modify it For if you distribute copies of such a whether gratis or for a you must give the recipients all the rights that you have You must make sure that receive or can get the source code And you must show them these terms so they know their rights We protect your rights with two and(2) offer you this license which gives you legal permission to copy
$GLOBALS['IP']
wfTempDir()
Tries to get the system directory for temporary files.
wfRandomString( $length=32)
Get a random string containing a number of pseudo-random hex characters.
wfRecursiveRemoveDir( $dir)
Remove a directory and all its content.
GitInfo.
static setUpBeforeClass()
assertValidGitInfo(GitInfo $gitInfo)
static $tempDir
static tearDownAfterClass()
getHead()
Get the HEAD of the repo (without any opening "ref: ")
Definition GitInfo.php:167
getHeadCommitDate()
Get the commit date of HEAD entry of the git code repository.
Definition GitInfo.php:223
cacheIsComplete()
Check to see if the current cache is fully populated.
Definition GitInfo.php:344
getHeadViewUrl()
Get an URL to a web viewer link to the HEAD revision.
Definition GitInfo.php:279
getHeadSHA1()
Get the SHA1 for the current HEAD of the repo.
Definition GitInfo.php:191
getCurrentBranch()
Get the name of the current branch, or HEAD if not found.
Definition GitInfo.php:261
setMwGlobals( $pairs, $value=null)
Sets a global, maintaining a stashed version of the previous global to be restored in tearDown.
$IP
Definition update.php:3