Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/Utils/PatchParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace ShipMonk\CoverageGuard\Utils;

use Composer\InstalledVersions;
use LogicException;
use SebastianBergmann\Diff\Line;
use SebastianBergmann\Diff\Parser as DiffParser;
use ShipMonk\CoverageGuard\Config;
Expand All @@ -12,6 +13,7 @@
use function file_exists;
use function file_get_contents;
use function is_file;
use function preg_replace;
use function str_ends_with;
use function str_starts_with;
use function substr;
Expand Down Expand Up @@ -63,6 +65,13 @@ public function getPatchChangedLines(
throw new ErrorException("Failed to read patch file: {$patchFile}");
}

// sebastian/diff parses "\ No newline at end of file" as an unchanged line, which shifts numbers of the following lines
$patchContent = preg_replace('~^\\\\.*\R?~m', '', $patchContent);

if ($patchContent === null) {
throw new LogicException('Failed to remove no-newline markers from patch file');
}

$diffs = (new DiffParser())->parse($patchContent);
$changes = [];

Expand Down
31 changes: 31 additions & 0 deletions tests/Utils/PatchParserTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php declare(strict_types = 1);

namespace ShipMonk\CoverageGuard\Utils;

use PHPUnit\Framework\TestCase;
use ShipMonk\CoverageGuard\Config;
use ShipMonk\CoverageGuard\Printer;
use ShipMonk\CoverageGuard\StreamTestTrait;
use function range;
use function realpath;

final class PatchParserTest extends TestCase
{

use StreamTestTrait;

public function testIgnoresNoNewlineAtEndOfFileMarker(): void
{
$config = new Config();
$config->setGitRoot(__DIR__ . '/../../');

$patchParser = new PatchParser(__DIR__, new Printer($this->createStream(), noColor: true));
$changes = $patchParser->getPatchChangedLines(__DIR__ . '/../_fixtures/PatchParser/no-newline-at-eof.patch', $config);

$changedFile = realpath(__DIR__ . '/../_fixtures/PatchParser/NoNewlineAtEof.php');
self::assertNotFalse($changedFile);

self::assertSame([$changedFile => range(11, 16)], $changes);
}

}
16 changes: 16 additions & 0 deletions tests/_fixtures/PatchParser/NoNewlineAtEof.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php declare(strict_types = 1);

class NoNewlineAtEof
{

public function first(): int
{
return 1;
}

public function second(): int
{
return 2;
}

}
16 changes: 16 additions & 0 deletions tests/_fixtures/PatchParser/no-newline-at-eof.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
diff --git a/tests/_fixtures/PatchParser/NoNewlineAtEof.php b/tests/_fixtures/PatchParser/NoNewlineAtEof.php
index 88952c9..f5afafd 100644
--- a/tests/_fixtures/PatchParser/NoNewlineAtEof.php
+++ b/tests/_fixtures/PatchParser/NoNewlineAtEof.php
@@ -8,4 +8,9 @@ class NoNewlineAtEof
return 1;
}

-}
\ No newline at end of file
+ public function second(): int
+ {
+ return 2;
+ }
+
+}
Loading