Skip to content

Commit

Permalink
Merge 4d2feff into 9e39f11
Browse files Browse the repository at this point in the history
  • Loading branch information
keradus committed Oct 5, 2020
2 parents 9e39f11 + 4d2feff commit f83889e
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 11 deletions.
9 changes: 8 additions & 1 deletion src/Bundle/CoverallsBundle/Collector/GitInfoCollector.php
Expand Up @@ -73,8 +73,15 @@ protected function collectBranch()
$branchesResult = $this->command->getBranches();

foreach ($branchesResult as $result) {
if ($result === '* (no branch)') {
// Case detected on Travis PUSH hook for tags, can be reporduced by following command:
// $ git clone --depth=1 --branch=v2.4.0 https://github.com/php-coveralls/php-coveralls.git php-coveralls && cd php-coveralls && git branch
// * (no branch)
return '(no branch)';
}

if (strpos($result, '* ') === 0) {
preg_match('~^\* (?:\(HEAD detached at )?([\w/]+)\)?~', $result, $matches);
preg_match('/^\* (?:\(HEAD detached at )?([\w\/\-]+)\)?/', $result, $matches);

return $matches[1];
}
Expand Down
37 changes: 27 additions & 10 deletions tests/Bundle/CoverallsBundle/Collector/GitInfoCollectorTest.php
Expand Up @@ -21,8 +21,8 @@ class GitInfoCollectorTest extends ProjectTestCase
*/
private $getBranchesValue = [
' master',
'* branch1',
' branch2',
'* branch-1',
' branch-2',
];

/**
Expand Down Expand Up @@ -72,6 +72,7 @@ public function shouldCollect()

$this->assertInstanceOf(Git::class, $git);
$this->assertGit($git);
$this->assertSame('branch-1', $git->getBranch());
}

/**
Expand All @@ -88,25 +89,45 @@ public function shouldCollectDetachedRef()

$git = $object->collect();

$this->assertInstanceOf(Git::class, $git);
$this->assertGit($git);
$this->assertSame('pull/1/merge', $git->getBranch());
}

/**
* @test
*/
public function shouldCollectNoBranch()
{
$gitCommand = $this->createGitCommandStubWith(
['* (no branch)'],
$this->getHeadCommitValue,
$this->getRemotesValue
);
$object = new GitInfoCollector($gitCommand);

$git = $object->collect();

$this->assertInstanceOf(Git::class, $git);
$this->assertGit($git);
$this->assertSame('(no branch)', $git->getBranch());
}

// collectBranch() exception

/**
* @test
*/
public function throwRuntimeExceptionIfCurrentBranchNotFound()
{
$this->expectException(\RuntimeException::class);

$getBranchesValue = [
' master',
];
$gitCommand = $this->createGitCommandStubCalledBranches($getBranchesValue);

$object = new GitInfoCollector($gitCommand);

$this->expectException(\RuntimeException::class);
$object->collect();
}

Expand All @@ -117,13 +138,12 @@ public function throwRuntimeExceptionIfCurrentBranchNotFound()
*/
public function throwRuntimeExceptionIfHeadCommitIsInvalid()
{
$this->expectException(\RuntimeException::class);

$getHeadCommitValue = [];
$gitCommand = $this->createGitCommandStubCalledHeadCommit($this->getBranchesValue, $getHeadCommitValue);

$object = new GitInfoCollector($gitCommand);

$this->expectException(\RuntimeException::class);
$object->collect();
}

Expand All @@ -134,13 +154,12 @@ public function throwRuntimeExceptionIfHeadCommitIsInvalid()
*/
public function throwRuntimeExceptionIfRemoteIsInvalid()
{
$this->expectException(\RuntimeException::class);

$getRemotesValue = [];
$gitCommand = $this->createGitCommandStubWith($this->getBranchesValue, $this->getHeadCommitValue, $getRemotesValue);

$object = new GitInfoCollector($gitCommand);

$this->expectException(\RuntimeException::class);
$object->collect();
}

Expand Down Expand Up @@ -253,8 +272,6 @@ protected function setUpGitCommandStubWithGetRemotesNeverCalled($stub)

protected function assertGit(Git $git)
{
$this->assertSame('branch1', $git->getBranch());

$commit = $git->getHead();

$this->assertInstanceOf(Commit::class, $commit);
Expand Down

0 comments on commit f83889e

Please sign in to comment.