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

feat: improve captainhook validation error message #101

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/CaptainHook/ValidateConventionalCommit.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,18 +74,18 @@ public function execute(
$parser = new Parser($this->findConfiguration(new ArrayInput([]), new Output($io), $options));
$parser->parse($message->getContent());
} catch (ConventionalException $exception) {
$this->writeErrorMessage($io);
$this->writeErrorMessage($io, $exception);

throw new ActionFailed('Validation failed.');
}
}

private function writeErrorMessage(IO $io): void
private function writeErrorMessage(IO $io, ConventionalException $exception): void
{
$console = $this->styleFactory->factory(new ArrayInput([]), new Output($io));

$console->error([
'Invalid Commit Message',
'Invalid Commit Message: ' . $exception->getMessage(),
'The commit message is not properly formatted according to the '
. 'Conventional Commits specification. For more details, see '
. 'https://www.conventionalcommits.org/en/v1.0.0/',
Expand Down
18 changes: 16 additions & 2 deletions tests/CaptainHook/ValidateConventionalCommitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
use Ramsey\CaptainHook\Output;
use Ramsey\CaptainHook\ValidateConventionalCommit;
use Ramsey\ConventionalCommits\Console\SymfonyStyleFactory;
use Ramsey\ConventionalCommits\Exception\ConventionalException;
use Ramsey\ConventionalCommits\Parser;
use Ramsey\Test\TestCase;
use SebastianFeldmann\Git\CommitMessage;
use SebastianFeldmann\Git\Repository;
Expand Down Expand Up @@ -93,13 +95,15 @@ function (string $value) use (&$output): void {
'getOptions->getAll' => ['config' => []],
]);

$rawCommitMessage = 'not a valid commit message';

/** @var CommitMessage & MockInterface $commitMessage */
$commitMessage = $this->mockery(CommitMessage::class);
$commitMessage
->expects()
->getContent()
->once()
->andReturn('not a valid commit message');
->andReturn($rawCommitMessage);

/** @var Repository & MockInterface $repository */
$repository = $this->mockery(Repository::class);
Expand All @@ -109,10 +113,20 @@ function (string $value) use (&$output): void {
->andReturn($commitMessage);

$console = $this->mockery(SymfonyStyle::class);

$invalidCommitMessage = '';

try {
$parser = new Parser();
$parser->parse($rawCommitMessage);
} catch (ConventionalException $exception) {
$invalidCommitMessage = $exception->getMessage();
}

$console
->expects('error')
->with([
'Invalid Commit Message',
'Invalid Commit Message: ' . $invalidCommitMessage,
'The commit message is not properly formatted according to the '
. 'Conventional Commits specification. For more details, see '
. 'https://www.conventionalcommits.org/en/v1.0.0/',
Expand Down