8000 [Logs] Highlight inline differences in CLI by Slamdunk · Pull Request #1974 · infection/infection · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

[Logs] Highlight inline differences in CLI #1974

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

Merged
merged 5 commits into from
Jun 3, 2024
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
5 changes: 5 additions & 0 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="5.11.0@c9b192ab8400fdaf04b2b13d110575adc879aa90">
<file src="src/Differ/DiffColorizer.php">
<InvalidArrayOffset>
<code>$lines[$prevIndex]</code>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know why PSalm doesn't get the Assert::greaterThan($index, 0);

</InvalidArrayOffset>
</file>
<file src="src/Mutator/Extensions/BCMath.php">
<ImpureFunctionCall>
<code><![CDATA[$this->converters[$name->toLowerString()]($node)]]></code>
Expand Down
6 changes: 4 additions & 2 deletions src/Console/OutputFormatterStyleConfigurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,10 @@

private static function configureDiffStyle(OutputFormatterInterface $formatter): void
{
$formatter->setStyle('diff-add', new OutputFormatterStyle('green'));
$formatter->setStyle('diff-del', new OutputFormatterStyle('red'));
$formatter->setStyle('diff-add', new OutputFormatterStyle('green', null, ['bold']));

Check warning on line 74 in src/Console/OutputFormatterStyleConfigurator.php

View workflow job for this annotation

GitHub Actions / Mutation Testing Code Review Annotations 8.1

Escaped Mutant for Mutator "ArrayItemRemoval": @@ @@ } private static function configureDiffStyle(OutputFormatterInterface $formatter): void { - $formatter->setStyle('diff-add', new OutputFormatterStyle('green', null, ['bold'])); + $formatter->setStyle('diff-add', new OutputFormatterStyle('green', null, [])); $formatter->setStyle('diff-add-inline', new OutputFormatterStyle('green', null, ['bold', 'reverse'])); $formatter->setStyle('diff-del', new OutputFormatterStyle('red', null, ['bold'])); $formatter->setStyle('diff-del-inline', new OutputFormatterStyle('red', null, ['bold', 'reverse']));
$formatter->setStyle('diff-add-inline', new OutputFormatterStyle('green', null, ['bold', 'reverse']));

Check warning on line 75 in src/Console/OutputFormatterStyleConfigurator.php

View workflow job for this annotation

GitHub Actions / Mutation Testing Code Review Annotations 8.1

Escaped Mutant for Mutator "ArrayItemRemoval": @@ @@ private static function configureDiffStyle(OutputFormatterInterface $formatter): void { $formatter->setStyle('diff-add', new OutputFormatterStyle('green', null, ['bold'])); - $formatter->setStyle('diff-add-inline', new OutputFormatterStyle('green', null, ['bold', 'reverse'])); + $formatter->setStyle('diff-add-inline', new OutputFormatterStyle('green', null, ['reverse'])); $formatter->setStyle('diff-del', new OutputFormatterStyle('red', null, ['bold'])); $formatter->setStyle('diff-del-inline', new OutputFormatterStyle('red', null, ['bold', 'reverse'])); }
$formatter->setStyle('diff-del', new OutputFormatterStyle('red', null, ['bold']));

Check warning on line 76 in src/Console/OutputFormatterStyleConfigurator.php

View workflow job for this annotation

GitHub Actions / Mutation Testing Code Review Annotations 8.1

Escaped Mutant for Mutator "ArrayItemRemoval": @@ @@ { $formatter->setStyle('diff-add', new OutputFormatterStyle('green', null, ['bold'])); $formatter->setStyle('diff-add-inline', new OutputFormatterStyle('green', null, ['bold', 'reverse'])); - $formatter->setStyle('diff-del', new OutputFormatterStyle('red', null, ['bold'])); + $formatter->setStyle('diff-del', new OutputFormatterStyle('red', null, [])); $formatter->setStyle('diff-del-inline', new OutputFormatterStyle('red', null, ['bold', 'reverse'])); } private static function configureMutationScoreStyle(OutputFormatterInterface $formatter): void
$formatter->setStyle('diff-del-inline', new OutputFormatterStyle('red', null, ['bold', 'reverse']));

Check warning on line 77 in src/Console/OutputFormatterStyleConfigurator.php

View workflow job for this annotation

GitHub Actions / Mutation Testing Code Review Annotations 8.1

Escaped Mutant for Mutator "ArrayItemRemoval": @@ @@ $formatter->setStyle('diff-add', new OutputFormatterStyle('green', null, ['bold'])); $formatter->setStyle('diff-add-inline', new OutputFormatterStyle('green', null, ['bold', 'reverse'])); $formatter->setStyle('diff-del', new OutputFormatterStyle('red', null, ['bold'])); - $formatter->setStyle('diff-del-inline', new OutputFormatterStyle('red', null, ['bold', 'reverse'])); + $formatter->setStyle('diff-del-inline', new OutputFormatterStyle('red', null, ['reverse'])); } private static function configureMutationScoreStyle(OutputFormatterInterface $formatter): void {
}

private static function configureMutationScoreStyle(OutputFormatterInterface $formatter): void
Expand Down
2 changes: 1 addition & 1 deletion src/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ public static function create(): self
$container->getPrinter(),
$container->getMutantCodeFactory(),
),
Differ::class => static fn (): Differ => new Differ(new BaseDiffer(new UnifiedDiffOutputBuilder())),
Differ::class => static fn (): Differ => new Differ(new BaseDiffer(new UnifiedDiffOutputBuilder(''))),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I hope you are ok removing the "--- Original\n+++ New\n" header, which only adds visual clutter to the output

SyncEventDispatcher::class => static fn (): SyncEventDispatcher => new SyncEventDispatcher(),
ParallelProcessRunner::class => static fn (self $container): ParallelProcessRunner => new ParallelProcessRunner($container->getConfiguration()->getThreadCount()),
DryProcessRunner::class => static fn (): DryProcessRunner => new DryProcessRunner(),
Expand Down
66 changes: 53 additions & 13 deletions src/Differ/DiffColorizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,17 @@

namespace Infection\Differ;

use function array_map;
use function explode;
use function implode;
use function mb_strlen;
use function mb_strpos;
use function mb_strrpos;
use function mb_substr;
use function sprintf;
use function str_starts_with;
use function substr;
use function substr_replace;
use Webmozart\Assert\Assert;

/**
* @internal
Expand All @@ -49,21 +55,55 @@
{
public function colorize(string $diff): string
{
$lines = array_map(
static function (string $line): string {
if (str_starts_with($line, '-')) {
return sprintf('<diff-del>%s</diff-del>', $line);
}
$lines = explode("\n", $diff);

if (str_starts_with($line, '+')) {
return sprintf('<diff-add>%s</diff-add>', $line);
}
foreach ($lines as $index => $line) {
if (!str_starts_with($line, '+')) {
continue;
}

return $line;
},
explode("\n", $diff),
);
Assert::greaterThan($index, 0);

$prevIndex = $index - 1;
$prevLine = $lines[$prevIndex];

Assert::same($prevLine[0], '-');

$lines[$prevIndex] = sprintf('<diff-del>-%s</diff-del>',
$this->inlineDiff(substr($prevLine, 1), substr($line, 1), '<diff-del-inline>', '</diff-del-inline>'),
);
$lines[$index] = sprintf('<diff-add>+%s</diff-add>',
$this->inlineDiff(substr($line, 1), substr($prevLine, 1), '<diff-add-inline>', '</diff-add-inline>'),
);
}

return sprintf('<code>%s%s</code>', "\n", implode("\n", $lines));
}

private function inlineDiff(string $previousLine, string $nextLine, string $leftAddition, string $rightAddition): string
{
$previousLineLength = mb_strlen($previousLine);

Check warning on line 85 in src/Differ/DiffColorizer.php

View workflow job for this annotation

GitHub Actions / Mutation Testing Code Review Annotations 8.1

Escaped Mutant for Mutator "MBString": @@ @@ } private function inlineDiff(string $previousLine, string $nextLine, string $leftAddition, string $rightAddition): string { - $previousLineLength = mb_strlen($previousLine); + $previousLineLength = strlen($previousLine); $nextLineLength = mb_strlen($nextLine); $start = $previousLineLength; while ($start !== 0 && mb_strpos($nextLine, mb_substr($previousLine, 0, $start)) !== 0) {
$nextLineLength = mb_strlen($nextLine);

Check warning on line 86 in src/Differ/DiffColorizer.php

View workflow job for this annotation

GitHub Actions / Mutation Testing Code Review Annotations 8.1

Escaped Mutant for Mutator "MBString": @@ @@ private function inlineDiff(string $previousLine, string $nextLine, string $leftAddition, string $rightAddition): string { $previousLineLength = mb_strlen($previousLine); - $nextLineLength = mb_strlen($nextLine); + $nextLineLength = strlen($nextLine); $start = $previousLineLength; while ($start !== 0 && mb_strpos($nextLine, mb_substr($previousLine, 0, $start)) !== 0) { --$start;

$start = $previousLineLength;

while ($start !== 0 && mb_strpos($nextLine, mb_substr($previousLine, 0, $start)) !== 0) {

Check warning on line 90 in src/Differ/DiffColorizer.php

View workflow job for this annotation

GitHub Actions / Mutation Testing Code Review Annotations 8.1

Escaped Mutant for Mutator "DecrementInteger": @@ @@ $previousLineLength = mb_strlen($previousLine); $nextLineLength = mb_strlen($nextLine); $start = $previousLineLength; - while ($start !== 0 && mb_strpos($nextLine, mb_substr($previousLine, 0, $start)) !== 0) { + while ($start !== -1 && mb_strpos($nextLine, mb_substr($previousLine, 0, $start)) !== 0) { --$start; } $end = $start;

Check warning on line 90 in src/Differ/DiffColorizer.php

View workflow job for this annotation

GitHub Actions / Mutation Testing Code Review Annotations 8.1

Escaped Mutant for Mutator "MBString": @@ @@ $previousLineLength = mb_strlen($previousLine); $nextLineLength = mb_strlen($nextLine); $start = $previousLineLength; - while ($start !== 0 && mb_strpos($nextLine, mb_substr($previousLine, 0, $start)) !== 0) { + while ($start !== 0 && mb_strpos($nextLine, substr($previousLine, 0, $start)) !== 0) { --$start; } $end = $start;

Check warning on line 90 in src/Differ/DiffColorizer.php

View workflow job for this annotation

GitHub Actions / Mutation Testing Code Review Annotations 8.1

Escaped Mutant for Mutator "MBString": @@ @@ $previousLineLength = mb_strlen($previousLine); $nextLineLength = mb_strlen($nextLine); $start = $previousLineLength; - while ($start !== 0 && mb_strpos($nextLine, mb_substr($previousLine, 0, $start)) !== 0) { + while ($start !== 0 && strpos($nextLine, mb_substr($previousLine, 0, $start)) !== 0) { --$start; } $end = $start;
--$start;
}

$end = $start;

while ($end < $previousLineLength && mb_strrpos($nextLine, $t = mb_substr($previousLine, $end), $start) !== ($nextLineLength - mb_strlen($t))) {

Check warning on line 96 in src/Differ/DiffColorizer.php

View workflow job for this annotation

GitHub Actions / Mutation Testing Code Review Annotations 8.1

Escaped Mutant for Mutator "LessThan": @@ @@ --$start; } $end = $start; - while ($end < $previousLineLength && mb_strrpos($nextLine, $t = mb_substr($previousLine, $end), $start) !== $nextLineLength - mb_strlen($t)) { + while ($end <= $previousLineLength && mb_strrpos($nextLine, $t = mb_substr($previousLine, $end), $start) !== $nextLineLength - mb_strlen($t)) { ++$end; } $return = $previousLine;
++$end;
}

$return = $previousLine;

if ($start < $end) {
$return = substr_replace($return, $rightAddition, $end, 0);
$return = substr_replace($return, $leftAddition, $start, 0);
}

return $return;
}
}
4 changes: 1 addition & 3 deletions src/Logger/Html/StrykerHtmlReportBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ final class StrykerHtmlReportBuilder
];

private const PLUS_LENGTH = 1;
private const DIFF_HEADERS_LINES_COUNT = 3;
private const DIFF_HEADERS_LINES_COUNT = 1;

public function __construct(private readonly MetricsCalculator $metricsCalculator, private readonly ResultsCollector $resultsCollector)
{
Expand Down Expand Up @@ -276,8 +276,6 @@ private function retrieveReplacementFromDiff(string $diff): string
static fn (string $line): string => isset($line[0]) ? substr($line, self::PLUS_LENGTH) : $line,
array_filter(
/*
--- Original
+++ New
@@ @@
*/
array_slice($lines, self::DIFF_HEADERS_LINES_COUNT),
Expand Down
F438
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function test_it_adds_styles(): void
{
$formatter = $this->createMock(OutputFormatterInterface::class);
$formatter
->expects($this->exactly(14))
->expects($this->exactly(16))
->method('setStyle')
;

Expand Down
113 changes: 91 additions & 22 deletions tests/phpunit/Differ/DiffColorizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,36 +37,105 @@

use Infection\Differ\DiffColorizer;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;

#[CoversClass(DiffColorizer::class)]
final class DiffColorizerTest extends TestCase
{
public function test_id_adds_colours_to_a_given_diff(): void
/**
* @param non-empty-string $originalDiff
* @param non-empty-string $expected
*/
#[DataProvider('provideDiffs')]
public function test_id_adds_colours_to_a_given_diff(string $originalDiff, string $expected): void
{
$originalDiff = <<<'CODE'
--- Original
+++ New
@@ @@
function ($a) {
- return $a < 0;
+ return $a <= 0;
}
CODE;

$expected = <<<'CODE'
<code>
<diff-del>--- Original</diff-del>
<diff-add>+++ New</diff-add>
@@ @@
function ($a) {
<diff-del>- return $a < 0;</diff-del>
<diff-add>+ return $a <= 0;</diff-add>
}</code>
CODE;

$actual = (new DiffColorizer())->colorize($originalDiff);

$this->assertSame($expected, $actual);
}

/**
* @return array<non-empty-string, list<non-empty-string>>
*/
public static function provideDiffs(): array
{
return [
'full-deletion' => [
<<<'CODE'
function ($a) {
- exit();
+
}
CODE,
<<<'CODE'
<code>
function ($a) {
<diff-del>-<diff-del-inline> exit();</diff-del-inline></diff-del>
<diff-add>+</diff-add>
}</code>
CODE,
],
'full-addition' => [
<<<'CODE'
function ($a) {
-
+ exit();
}
CODE,
<<<'CODE'
<code>
function ($a) {
<diff-del>-</diff-del>
<diff-add>+<diff-add-inline> exit();</diff-add-inline></diff-add>
}</code>
CODE,
],
'partial-deletion' => [
<<<'CODE'
function ($a) {
- return 'foo' . 'bar';
+ return 'foo';
}
CODE,
<<<'CODE'
<code>
function ($a) {
<diff-del>- return 'foo'<diff-del-inline> . 'bar'</diff-del-inline>;</diff-del>
<diff-add>+ return 'foo';</diff-add>
}</code>
CODE,
],
'partial-addition' => [
<<<'CODE'
function ($a) {
- return 'foo';
+ return 'foo' . 'bar';
}
CODE,
<<<'CODE'
<code>
function ($a) {
<diff-del>- return 'foo';</diff-del>
<diff-add>+ return 'foo'<diff-add-inline> . 'bar'</diff-add-inline>;</diff-add>
}</code>
CODE,
],
'deletion-and-addition' => [
<<<'CODE'
function ($a, $b) {
- return $a && $b;
+ return $a || $b;
}
CODE,
<<<'CODE'
<code>
function ($a, $b) {
<diff-del>- return $a <diff-del-inline>&&</diff-del-inline> $b;</diff-del>
<diff-add>+ return $a <diff-add-inline>||</diff-add-inline> $b;</diff-add>
}</code>
CODE,
],
];
}
}
12 changes: 0 additions & 12 deletions tests/phpunit/Logger/Html/StrykerHtmlReportBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -289,8 +289,6 @@ private static function initHtmlReportCollector(Collector $collector): void
self::createMutantExecutionResult(
DetectionStatus::KILLED,
<<<'DIFF'
--- Original
+++ New
@@ @@
use function array_fill_keys;
final class ForHtmlReport
Expand Down Expand Up @@ -320,8 +318,6 @@ final class ForHtmlReport
self::createMutantExecutionResult(
DetectionStatus::ESCAPED,
<<<'DIFF'
--- Original
+++ New
@@ @@
{
public function add(int $a, int $b) : int
Expand All @@ -348,8 +344,6 @@ public function add(int $a, int $b) : int
self::createMutantExecutionResult(
DetectionStatus::ESCAPED,
<<<'DIFF'
--- Original
+++ New
@@ @@
public function add(int $a, int $b) : int
{
Expand All @@ -376,8 +370,6 @@ public function add(int $a, int $b) : int
self::createMutantExecutionResult(
DetectionStatus::ESCAPED,
<<<'DIFF'
--- Original
+++ New
@@ @@
default:
break;
Expand Down Expand Up @@ -406,8 +398,6 @@ public function add(int $a, int $b) : int
self::createMutantExecutionResult(
DetectionStatus::KILLED,
<<<'DIFF'
--- Original
+++ New
@@ @@
use function array_fill_keys;
final class ForHtmlReport2
Expand All @@ -434,8 +424,6 @@ final class ForHtmlReport2
self::createMutantExecutionResult(
DetectionStatus::KILLED,
<<<'DIFF'
--- Original
+++ New
@@ @@
use function array_fill_keys;
final class ForHtmlReport2
Expand Down
Loading
0