sebastianbergmann/diff

Regression in unified diff output of identical strings between 2.x and 3.x

znbailey opened this issue · 2 comments

Hi,

While working on upgrading some dependencies, we have been testing an upgrade of this package from 2.0.1 to 3.0.2, and came across the following behavior which appears to be a regression. Can you please investigate and confirm whether this is intended or not?

Expected Behavior / Behavior in 2.0.1
Diffing two identical one-line strings, such as 'these strings contain no differences', and using the UnifiedDiffOutputBuilder returns an empty string, i.e. a string with length 0 ("").

Observed Behavior in 3.0.2
Diffing two identical one-line strings returns the string "\n", i.e. a one-byte length string with the newline character in it.

Reproduction
Using composer to manage the dependency, the following test harness file can enable you to quickly test between different versions.

<?php
require_once('vendor/autoload.php');

use SebastianBergmann\Diff\Differ;
use SebastianBergmann\Diff\Output\UnifiedDiffOutputBuilder;

$a = 'something that is the same';
$b = 'something that is the same';

$differ = new Differ(new UnifiedDiffOutputBuilder(''));
$diff = $differ->diff($a, $b);

echo "The diff is:\n";
echo serialize($diff);

Running the above script with composer pulling in sebastian/diff version 2.0.1 outputs:

The diff is:
s:0:""

Whereas running the script with sebastian/diff 3.0.2 outputs:

The diff is:
s:1:"
"

We are using this library to detect differences and treat any non-zero length output as a "difference". I suspect other consumers of this library likely do the same.

Note also that using the command-line diff tool on two identical files on linux outputs a zero-length response as well, so the expected behavior is consistent with built-in tools on common platforms.

Thank you @sebastianbergmann for merging my fix. I'm curious what the schedule is like for a new release to be made, so we can pull this in via our composer? We have a PHPUnit 6.5 -> 8.5 upgrade waiting on this :)