From a5ed38dc54af849098b4137bf0a04ca2f18b5e6d Mon Sep 17 00:00:00 2001 From: Kunal Mehta Date: Mon, 4 Jan 2016 09:39:09 -0800 Subject: [PATCH] GitInfo: Allow cache to be in the extension directory itself For ExtensionDistributor to provide git metadata, we need to be able to store the cache file inside the extension directory itself. The GitInfo class will now first check if the $wgGitInfoCacheDirectory is populated, otherwise it will fallback to "$extensionDir/gitinfo.json". Bug: T122769 Change-Id: Ib3457589ca6899925ae4610cfcdae22af8eaaaeb --- includes/GitInfo.php | 10 +++++--- .../data/gitinfo/extension/gitinfo.json | 7 ++++++ tests/phpunit/includes/GitInfoTest.php | 25 +++++++++++-------- 3 files changed, 29 insertions(+), 13 deletions(-) create mode 100644 tests/phpunit/data/gitinfo/extension/gitinfo.json diff --git a/includes/GitInfo.php b/includes/GitInfo.php index 7f05bb0fd7..14f3cc14af 100644 --- a/includes/GitInfo.php +++ b/includes/GitInfo.php @@ -96,7 +96,7 @@ class GitInfo { * * @param string $repoDir The root directory of the repo where .git can be found * @return string Path to GitInfo cache file in $wgGitInfoCacheDirectory or - * null if $wgGitInfoCacheDirectory is false (cache disabled). + * fallback in the extension directory itself * @since 1.24 */ protected static function getCacheFilePath( $repoDir ) { @@ -119,9 +119,13 @@ class GitInfo { // a filename $repoName = strtr( $repoName, DIRECTORY_SEPARATOR, '-' ); $fileName = 'info' . $repoName . '.json'; - return "{$wgGitInfoCacheDirectory}/{$fileName}"; + $cachePath = "{$wgGitInfoCacheDirectory}/{$fileName}"; + if ( is_readable( $cachePath ) ) { + return $cachePath; + } } - return null; + + return "$repoDir/gitinfo.json"; } /** diff --git a/tests/phpunit/data/gitinfo/extension/gitinfo.json b/tests/phpunit/data/gitinfo/extension/gitinfo.json new file mode 100644 index 0000000000..8cf21bda7b --- /dev/null +++ b/tests/phpunit/data/gitinfo/extension/gitinfo.json @@ -0,0 +1,7 @@ +{ + "head": "refs/heads/master", + "headSHA1": "0123456789abcdef0123456789abcdef01234567", + "headCommitDate": "1070884800", + "branch": "master", + "remoteURL": "https://gerrit.wikimedia.org/r/mediawiki/core" +} diff --git a/tests/phpunit/includes/GitInfoTest.php b/tests/phpunit/includes/GitInfoTest.php index c3539d0e7d..9f4a01c9d8 100644 --- a/tests/phpunit/includes/GitInfoTest.php +++ b/tests/phpunit/includes/GitInfoTest.php @@ -9,18 +9,23 @@ class GitInfoTest extends MediaWikiTestCase { $this->setMwGlobals( 'wgGitInfoCacheDirectory', __DIR__ . '/../data/gitinfo' ); } - public function testValidJsonData() { - $dir = $GLOBALS['IP'] . DIRECTORY_SEPARATOR . 'testValidJsonData'; - $fixture = new GitInfo( $dir ); - - $this->assertTrue( $fixture->cacheIsComplete() ); - $this->assertEquals( 'refs/heads/master', $fixture->getHead() ); + protected function assertValidGitInfo( GitInfo $gitInfo ) { + $this->assertTrue( $gitInfo->cacheIsComplete() ); + $this->assertEquals( 'refs/heads/master', $gitInfo->getHead() ); $this->assertEquals( '0123456789abcdef0123456789abcdef01234567', - $fixture->getHeadSHA1() ); - $this->assertEquals( '1070884800', $fixture->getHeadCommitDate() ); - $this->assertEquals( 'master', $fixture->getCurrentBranch() ); + $gitInfo->getHeadSHA1() ); + $this->assertEquals( '1070884800', $gitInfo->getHeadCommitDate() ); + $this->assertEquals( 'master', $gitInfo->getCurrentBranch() ); $this->assertContains( '0123456789abcdef0123456789abcdef01234567', - $fixture->getHeadViewUrl() ); + $gitInfo->getHeadViewUrl() ); + + } + + public function testValidJsonData() { + global $IP; + + $this->assertValidGitInfo( new GitInfo( "$IP/testValidJsonData") ); + $this->assertValidGitInfo( new GitInfo( __DIR__ . "/../data/gitinfo/extension" ) ); } public function testMissingJsonData() { -- 2.20.1