Skip to content

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

@znbailey

Description

@znbailey

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions