Tests: Improve Windows compatibility#200
Conversation
|
Hello! 👋 Thanks for opening this pull request! Please check out our contributing guidelines. We appreciate you taking the initiative to contribute to this project. Contributing isn't limited to just code. We encourage you to contribute in the way that best fits your abilities, by writing tutorials, giving a demo at your local meetup, helping other users with their support questions, or revising our documentation. Here are some useful Composer commands to get you started:
To run a single Behat test, you can use the following command: # Run all tests in a single file
composer behat features/some-feature.feature
# Run only a specific scenario (where 123 is the line number of the "Scenario:" title)
composer behat features/some-feature.feature:123You can find a list of all available Behat steps in our handbook. |
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
There was a problem hiding this comment.
Code Review
This pull request updates Shell.php to prioritize the COLUMNS environment variable for terminal width detection and includes test updates to handle Windows-specific behavior and line-ending normalization. Feedback highlights a bug in the environment variable restoration logic within Test_Shell.php, where setting WP_CLI_TEST_IS_WINDOWS to an empty string on Windows incorrectly causes is_windows() to return false, potentially breaking subsequent tests.
| if ( false === $env_is_windows ) { | ||
| if ( strtoupper( substr( PHP_OS, 0, 3 ) ) === 'WIN' ) { | ||
| putenv( 'WP_CLI_TEST_IS_WINDOWS=' ); | ||
| } else { | ||
| putenv( 'WP_CLI_TEST_IS_WINDOWS' ); | ||
| } | ||
| } else { | ||
| putenv( "WP_CLI_TEST_IS_WINDOWS=$env_is_windows" ); | ||
| } |
There was a problem hiding this comment.
This change to restore the WP_CLI_TEST_IS_WINDOWS environment variable introduces a problem for tests running on Windows.
On Windows, putenv('WP_CLI_TEST_IS_WINDOWS=') sets the variable to an empty string. Consequently, is_windows() will return false because (bool)'' is false. This breaks test isolation, as subsequent tests will incorrectly detect the OS as non-Windows.
The underlying issue is in is_windows(), which doesn't differentiate an unset variable from one set to an empty string. The ideal fix is to make is_windows() more robust, for instance:
if (false !== $test_is_windows && '' !== $test_is_windows) {
return (bool) $test_is_windows;
}Since modifying is_windows() might be out of scope for this file, this restoration logic needs to be re-evaluated to avoid breaking other tests.
There was a problem hiding this comment.
Pull request overview
This PR updates CLI-related tests and shell utilities to behave consistently on Windows, addressing environment-variable handling and line-ending differences.
Changes:
- Normalize CRLF vs LF when asserting ASCII table output written to a temp file.
- Adjust table rendering tests to account for Windows behavior when evaluating wrapped multibyte output.
- Improve
Shell::columns()andShell::is_windows()behavior aroundCOLUMNSandWP_CLI_TEST_IS_WINDOWS.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
tests/Test_Table_Ascii.php |
Normalizes line endings before comparing expected vs actual output. |
tests/Test_Table.php |
Adds Windows-specific expectations for multibyte-width wrapping assertions. |
tests/Test_Shell.php |
Restores WP_CLI_TEST_IS_WINDOWS in a Windows-safe way (empty vs unset). |
lib/cli/Shell.php |
Prefers COLUMNS before exec()-based detection; treats empty test override as “not set”. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
No description provided.