Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix support of branch name with hyphen and "(no branch)" #298

Merged
merged 3 commits into from
Oct 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/Bundle/CoverallsBundle/Collector/GitInfoCollector.php
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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