Skip to content

fix: support LF line endings for @pest-mutate-ignore on Windows - #44

Open
webdevsamran wants to merge 2 commits into
pestphp:5.xfrom
webdevsamran:fix/windows-lf-mutate-ignore
Open

webdevsamran wants to merge 2 commits into
pestphp:5.xfrom
webdevsamran:fix/windows-lf-mutate-ignore

Conversation

@webdevsamran

Copy link
Copy Markdown

Fixes pestphp/pest#1928

Problem

On Windows, MutationGenerator::generate() parses @pest-mutate-ignore annotations by splitting file contents on PHP_EOL:

foreach (explode(PHP_EOL, $contents) as $lineNumber => $line) {

On Windows, PHP_EOL evaluates to "\r\n". When inspecting source files that use Unix LF (\n) line endings (such as repositories with eol=lf in .gitattributes), explode() does not split the file. The entire file is treated as a single line, causing any ignore comment anywhere in the file to be attributed to line 1 ($lineNumber + 1 = 1). Consequently, mutants on their actual lines are not suppressed and fail mutation testing runs.

Solution

Replace explode(PHP_EOL, $contents) with preg_split('/\R/', $contents) to handle all OS newline formats (\n, \r\n, \r) consistently across platforms.

Verification

  • Added a regression test in tests/Unit/MutationGeneratorTest.php asserting that @pest-mutate-ignore correctly ignores mutators on lines beyond line 1 when files use LF line endings.

…indows

Signed-off-by: Samran Asif <samranwebdev2000@gmail.com>

@MrPunyapal MrPunyapal left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Could you change $a > $b to $a >= $b in the new LF test? GreaterOrEqualToGreater only targets >=, so the current test passes even with the fix removed.

I checked on Windows: with >=, the test fails before the fix and passes afterward. The source fix itself looks good.

…oGreater

Signed-off-by: Samran Asif <samranwebdev2000@gmail.com>
@webdevsamran

Copy link
Copy Markdown
Author

Hi @MrPunyapal, thank you for catching that!

Updated the test in commit 823e766 to use $a >= instead of $a > so that GreaterOrEqualToGreater actually targets the mutation candidate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: [mutate] Windows: @pest-mutate-ignore comments land on line 1 in files with LF line endings

2 participants