Skip to content

Conversation

fredden
Copy link
Member

@fredden fredden commented Aug 8, 2025

Description

When running phpcbf with a PHP file as input (eg, phpcbf < test.php), the exit code was not reported correctly. This pull request fixes that bug. For more details, see #1082.

Suggested changelog entry

Fixed exit code when phpcbf is processing STDIN

Related issues/external references

Fixes #1082

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
    • This change is only breaking for integrators, not for external standards or end-users.
  • Documentation improvement

PR checklist

  • I have checked there is no other PR open for the same change.
  • I have read the Contribution Guidelines.
  • I grant the project the right to include and distribute the code under the BSD-3-Clause license (and I have the right to grant these rights).
  • I have added tests to cover my changes.
  • I have verified that the code complies with the projects coding standards.
  • [Required for new sniffs] I have added XML documentation for the sniff.
  • I have opened a sister-PR in the documentation repository to update the Wiki.

@fredden
Copy link
Member Author

fredden commented Aug 8, 2025

@jrfnl how would you like to cover this change with tests?

@jrfnl
Copy link
Member

jrfnl commented Aug 8, 2025

how would you like to cover this change with tests?

I imagine it may be difficult to cover this change via the PHPUnit setup as it would require mocking STDIN. How about adding some tests to the bashunit E2E testsuite ?

@jrfnl jrfnl added this to the 4.0.0 milestone Aug 8, 2025
@jrfnl
Copy link
Member

jrfnl commented Aug 16, 2025

@fredden Just checking - will you have time over the next few days to add a few bashunit tests for STDIN handling ? Or would you like me to add something ?

@fredden
Copy link
Member Author

fredden commented Aug 17, 2025

I'm happy to update this, but I'm away at the moment. Earliest that I can pick this up again would be Wednesday/Thursday this week. I do have a bash script that I used for local development which will be trivial to convert into the format needed for bashunit.

If you want to land this before Friday, feel free to add some tests.

@fredden fredden force-pushed the issue-1082/exit-code-when-fixing-stdin branch from 4fed094 to 7bfebcc Compare August 21, 2025 23:09
@fredden fredden requested a review from jrfnl August 21, 2025 23:14
Copy link
Member

@jrfnl jrfnl left a comment

Choose a reason for hiding this comment

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

@fredden Thanks for adding the tests! I've had a play with this and left some feedback inline. Not necessarily blocking. We could decide to merge this now and iterate on organizing the bashunit tests in a future PR.


namespace PHP_CodeSniffer\Tests\EndToEnd\Fixtures;

# The brace on the following line should be on a line by itself. This can be fixed with phpcbf.
Copy link
Member

Choose a reason for hiding this comment

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

Quick question: any particular reason to use #-style inline comments instead of //-style inline comments in the test fixtures ? Or is this accidental because of bash using #-style comments ?

Copy link
Member Author

Choose a reason for hiding this comment

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

This is probably because Terraform prefers # over // and I'm writing more Terraform than PHP these days. I imagine that because I was also writing shell script, that the hash seemed more natural.
This should be trivial to change. If we end up modifying this pull request, I will change these lines.

Comment on lines +47 to +54
function test_phpcbf_exit_code_clean() {
bin/phpcbf --suffix=.fixed --standard=tests/EndToEnd/Fixtures/endtoend.xml.dist tests/EndToEnd/Fixtures/ClassOneWithoutStyleError.inc
assert_exit_code 0

# Same result via STDIN
bin/phpcbf --suffix=.fixed --standard=tests/EndToEnd/Fixtures/endtoend.xml.dist < tests/EndToEnd/Fixtures/ClassOneWithoutStyleError.inc
assert_exit_code 0
}
Copy link
Member

Choose a reason for hiding this comment

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

I've just been having a play with this and I believe it would be prudent to split each of these test methods into two separate test methods:

  1. The first (normal) command + check the exit code.
  2. The command with STDIN + check the exit code.

Reason being, is that if either of the assertions would fail, the resulting error message would be the same, so we have no way of knowing which of the two assertions failed.

To demonstrate, this would be the output from the new tests if the fix was removed:
image
... which doesn't give any clue as to whether the exit code setting is broken completely or only for STDIN.

I imagine, we could even consider moving these tests into separate test scripts too (phpcs_exitcodes_test.sh and phpcbf_exitcodes_test.sh).

Copy link
Member Author

Choose a reason for hiding this comment

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

If you're open to having additional files in this directory, then I'd like to put all these exit code tests in a single file of their own.
I'll get that sorted in the next few days.

Copy link
Member

Choose a reason for hiding this comment

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

I see no reason to limit the tests in that directory to just the current files, so, yes please ♥️

I think the original tests were split into two files because phpcbf needs a tear_down() method, while phpcs does not, but running that tear_down() after a phpcs run shouldn't do any harm, so one file for these tests could work.

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

Successfully merging this pull request may close these issues.

2 participants