Skip to content

Commit

Permalink
test: normalize line endings for tests running on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
ramsey committed Aug 12, 2020
1 parent 084292f commit 43d45c2
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 7 deletions.
4 changes: 3 additions & 1 deletion src/ConventionalCommits/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class Parser
. "(?'type'(?P>noun))"
. "(?:\((?'scope'(?P>noun))\))?(?'bc'!)?: "
. "(?'desc'[[:print:]]+)"
. "(?:(?:\\n|\\r|\\r\\n){2}"
. "(?:(?:\n{2}|\r{2}|(?:\r\n){2})"
. "(?'body'.*?(?=(?P>tokenPrefix)|\$))?"
. "(?:(?=(?P>tokenPrefix))(?'footer'.*))?)?\$/ius";

Expand All @@ -59,6 +59,8 @@ class Parser
*/
public function parse(string $commitMessage): Message
{
$commitMessage = trim($commitMessage);

if (!preg_match(self::COMMIT_PATTERN, $commitMessage, $matches)) {
throw new InvalidCommitMessage(
'Could not find a valid Conventional Commits message',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
use Symfony\Component\Console\Output\NullOutput;
use Symfony\Component\Console\Style\SymfonyStyle;

use const PHP_EOL;

class PrepareCommandTest extends RamseyTestCase
{
public function testCommandName(): void
Expand Down Expand Up @@ -61,6 +63,9 @@ public function testRun(): void

EOD;

// Fix line endings in case running tests on Windows.
$expectedMessage = preg_replace('/(?<!\r)\n/', PHP_EOL, $expectedMessage);

$input = new StringInput('');
$output = new NullOutput();

Expand Down Expand Up @@ -163,6 +168,9 @@ public function testRunWithMinimalResponses(): void

EOD;

// Fix line endings in case running tests on Windows.
$expectedMessage = preg_replace('/(?<!\r)\n/', PHP_EOL, $expectedMessage);

$input = new StringInput('');
$output = new NullOutput();

Expand Down
11 changes: 5 additions & 6 deletions tests/ConventionalCommits/Message/BodyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,15 @@ public function testBody(): void

public function getExpectedBody(): string
{
$body = $this->getRawBodyForTest();

// Replace line feeds with PHP_EOL, in case tests run on platforms with
// different line endings.
return str_replace("\n", PHP_EOL, $body);
return $this->getRawBodyForTest();
}

/**
* phpcs:disable
*/
public function getRawBodyForTest(): string
{
return <<<'EOD'
$body = <<<'EOD'
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce pharetra quis massa ut elementum. Pellentesque dapibus ultricies pharetra. In in finibus massa. Donec cursus nec ligula nec eleifend. Ut eleifend ut nisl sit amet laoreet. Mauris tristique nibh urna, vestibulum dictum ante tincidunt a. Curabitur ultricies dignissim neque eget rutrum. Maecenas nulla diam, dictum et pellentesque tristique, vestibulum ut dolor. Nam ac lobortis turpis. Nulla faucibus eu ex tempus tempus. Mauris tincidunt pellentesque velit quis ullamcorper. Nam massa mauris, porta vel varius id, sollicitudin vitae dolor. Aliquam at mi sem. Sed rhoncus urna sapien, vel vehicula urna sagittis eget.
``` php
Expand All @@ -45,5 +41,8 @@ function loremIpsum(string $ipsum): string
Morbi tincidunt pulvinar sem. https://example.com/this-is-a-really-long-url-that-should-not-break-across-lines-when-wrapping In posuere eros et venenatis mattis. Nullam tincidunt vehicula ullamcorper. Vivamus posuere eros eget condimentum posuere. Sed vel magna quis neque facilisis aliquet eu sed nisi. Nam nec arcu mi. Fusce laoreet mi sed mi egestas, sed dignissim nibh ultricies. Maecenas viverra tellus eros, nec ullamcorper magna cursus quis. In a nulla et nisi sagittis vehicula.
EOD;

// Fix line endings in case running tests on Windows.
return (string) preg_replace('/(?<!\r)\n/', PHP_EOL, $body);
}
}
10 changes: 10 additions & 0 deletions tests/ConventionalCommits/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use Ramsey\ConventionalCommits\Parser;
use Ramsey\Test\RamseyTestCase;

use const PHP_EOL;

class ParserTest extends RamseyTestCase
{
/**
Expand Down Expand Up @@ -57,6 +59,10 @@ public function testParserAccuratelyParsesCommitMessages(
$rawMessage = (string) file_get_contents($rawMessageFile);
$expectedMessage = (string) file_get_contents($expectedMessageFile);

// Fix line endings in case running tests on Windows.
$rawMessage = (string) preg_replace('/(?<!\r)\n/', PHP_EOL, $rawMessage);
$expectedMessage = (string) preg_replace('/(?<!\r)\n/', PHP_EOL, $expectedMessage);

$parser = new Parser();
$commit = $parser->parse($rawMessage);

Expand Down Expand Up @@ -87,6 +93,10 @@ public function provideInvalidCommitMessage(): array
public function testInvalidCommitMessageThrowsException(string $invalidMessageFile): void
{
$invalidMessage = (string) file_get_contents($invalidMessageFile);

// Fix line endings in case running tests on Windows.
$invalidMessage = (string) preg_replace('/(?<!\r)\n/', PHP_EOL, $invalidMessage);

$parser = new Parser();

$this->expectException(InvalidCommitMessage::class);
Expand Down

0 comments on commit 43d45c2

Please sign in to comment.