Skip to content

Commit 4730c6d

Browse files
authored
Normalize line endings in assertions (#334)
* Add tests to verify line ending normalizations * now add the actual normalization * fix unrelated windows test while at it
1 parent 530a012 commit 4730c6d

4 files changed

Lines changed: 128 additions & 23 deletions

File tree

‎features/behat-steps.feature‎

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -695,3 +695,64 @@ Feature: Test that WP-CLI Behat steps work as expected
695695
| user_login | user_email |
696696
| admin | admin@example.com |
697697
| user2 | user2@example.com |
698+
699+
Scenario: Test line endings normalization for command output and files
700+
Given an empty directory
701+
702+
# 1. Test STDOUT / STDERR assertions with CRLF
703+
When I run `php -r "echo 'line1' . \"\r\n\" . 'line2' . \"\r\n\";"`
704+
Then STDOUT should be:
705+
"""
706+
line1
707+
line2
708+
"""
709+
And STDOUT should contain:
710+
"""
711+
line1
712+
line2
713+
"""
714+
And STDOUT should match /line1\nline2/
715+
716+
# 2. Test JSON assertions with CRLF
717+
When I run `php -r "echo json_encode(['foo' => \"bar\r\nbaz\r\n\"]) . PHP_EOL;"`
718+
Then STDOUT should be JSON containing:
719+
"""
720+
{"foo":"bar\nbaz\n"}
721+
"""
722+
723+
# 3. Test JSON array assertions with CRLF
724+
When I run `php -r "echo json_encode([\"foo\r\nbar\", \"baz\r\nqux\"]) . PHP_EOL;"`
725+
Then STDOUT should be a JSON array containing:
726+
"""
727+
["foo\nbar", "baz\nqux"]
728+
"""
729+
730+
# 4. Test Table assertions with CRLF
731+
When I run `php -r "echo 'name' . \"\t\" . 'value' . \"\r\n\" . 'foo' . \"\t\" . 'bar' . \"\r\n\";"`
732+
Then STDOUT should be a table containing rows:
733+
| name | value |
734+
| foo | bar |
735+
736+
# 5. Test CSV assertions with CRLF
737+
When I run `php -r "echo 'name,value' . \"\r\n\" . 'foo,bar' . \"\r\n\";"`
738+
Then STDOUT should be CSV containing:
739+
| name | value |
740+
| foo | bar |
741+
742+
# 6. Test YAML assertions with CRLF
743+
When I run `php -r "echo 'foo: bar' . \"\r\n\" . 'baz: qux' . \"\r\n\";"`
744+
Then STDOUT should be YAML containing:
745+
"""
746+
foo: bar
747+
baz: qux
748+
"""
749+
750+
# 7. Test File assertions with CRLF
751+
When I run `php -r "file_put_contents('crlf.txt', 'crlf1' . \"\r\n\" . 'crlf2' . \"\r\n\");"`
752+
Then the crlf.txt file should contain:
753+
"""
754+
crlf1
755+
crlf2
756+
"""
757+
And the contents of the crlf.txt file should match /crlf1\ncrlf2/
758+

‎features/http-mocking.feature‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ Feature: HTTP request mocking
130130
Mocked file contents on disk!
131131
"""
132132

133-
When I try `wp eval 'WP_CLI\Utils\http_request("GET", "https://example.com/mocked-file.txt", null, [], ["filename" => "downloaded.txt"]);' --skip-wordpress`
133+
When I try `wp eval "WP_CLI\Utils\http_request('GET', 'https://example.com/mocked-file.txt', null, [], ['filename' => 'downloaded.txt']);" --skip-wordpress`
134134
Then the return code should be 0
135135
And the downloaded.txt file should contain:
136136
"""

‎src/Context/Support.php‎

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,9 @@ protected function assert_not_numeric( $actual ): void {
9595
* @throws Exception
9696
*/
9797
protected function check_string( $output, $expected, $action, $message = false, $strictly = false ): void {
98+
$output = $this->normalize_line_endings( $output );
99+
$expected = $this->normalize_line_endings( $expected );
100+
98101
// Strip ANSI color codes before comparing strings.
99102
if ( ! $strictly ) {
100103
$output = preg_replace( '/\e[[][A-Za-z0-9];?[0-9]*m?/', '', $output );
@@ -236,8 +239,8 @@ protected function compare_contents( $expected, $actual ) {
236239
* the contents of 'array' does not include 3
237240
*/
238241
protected function check_that_json_string_contains_json_string( $actual_json, $expected_json ) {
239-
$actual_value = json_decode( $actual_json );
240-
$expected_value = json_decode( $expected_json );
242+
$actual_value = $this->normalize_line_endings_in_data( json_decode( $actual_json ) );
243+
$expected_value = $this->normalize_line_endings_in_data( json_decode( $expected_json ) );
241244

242245
if ( ! $actual_value ) {
243246
return false;
@@ -311,8 +314,8 @@ static function ( $str ) {
311314
* @return bool whether or not $actual_yaml contains $expected_json
312315
*/
313316
protected function check_that_yaml_string_contains_yaml_string( $actual_yaml, $expected_yaml ) {
314-
$actual_value = Spyc::YAMLLoad( $actual_yaml );
315-
$expected_value = Spyc::YAMLLoad( $expected_yaml );
317+
$actual_value = $this->normalize_line_endings_in_data( Spyc::YAMLLoad( $actual_yaml ) );
318+
$expected_value = $this->normalize_line_endings_in_data( Spyc::YAMLLoad( $expected_yaml ) );
316319

317320
if ( ! $actual_value ) {
318321
return false;
@@ -336,4 +339,40 @@ protected function generate_diff( string $expected, string $actual ): string {
336339
$differ = new Differ( $builder );
337340
return $differ->diff( $expected, $actual );
338341
}
342+
343+
/**
344+
* Normalize line endings of a string to Unix-style LF (\n).
345+
*
346+
* @param string $str The string to normalize.
347+
* @return string The normalized string.
348+
*/
349+
protected function normalize_line_endings( string $str ): string {
350+
return str_replace( "\r\n", "\n", $str );
351+
}
352+
353+
/**
354+
* Recursively normalize line endings in a data structure (array, object, or string).
355+
*
356+
* @param mixed $data The data structure to normalize.
357+
* @return mixed The normalized data structure.
358+
*/
359+
protected function normalize_line_endings_in_data( $data ) {
360+
if ( is_string( $data ) ) {
361+
return $this->normalize_line_endings( $data );
362+
}
363+
364+
if ( is_array( $data ) ) {
365+
return array_map( [ $this, 'normalize_line_endings_in_data' ], $data );
366+
}
367+
368+
if ( is_object( $data ) ) {
369+
$normalized = new \stdClass();
370+
foreach ( get_object_vars( $data ) as $key => $value ) {
371+
$normalized->$key = $this->normalize_line_endings_in_data( $value );
372+
}
373+
return $normalized;
374+
}
375+
376+
return $data;
377+
}
339378
}

‎src/Context/ThenStepDefinitions.php‎

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,9 @@ public function then_stdout_stderr_should_contain( $stream, $strictly, $action,
9595
public function then_stdout_stderr_should_be_a_number( $stream ): void {
9696

9797
$stream = strtolower( $stream );
98+
$output = $this->normalize_line_endings( $this->result->$stream );
9899

99-
$this->assert_numeric( trim( $this->result->$stream, "\n" ) );
100+
$this->assert_numeric( trim( $output, "\n" ) );
100101
}
101102

102103
/**
@@ -118,8 +119,9 @@ public function then_stdout_stderr_should_be_a_number( $stream ): void {
118119
public function then_stdout_stderr_should_not_be_a_number( $stream ): void {
119120

120121
$stream = strtolower( $stream );
122+
$output = $this->normalize_line_endings( $this->result->$stream );
121123

122-
$this->assert_not_numeric( trim( $this->result->$stream, "\n" ) );
124+
$this->assert_not_numeric( trim( $output, "\n" ) );
123125
}
124126

125127
/**
@@ -140,7 +142,7 @@ public function then_stdout_stderr_should_not_be_a_number( $stream ): void {
140142
* @Then /^STDOUT should be a table containing rows:$/
141143
*/
142144
public function then_stdout_should_be_a_table_containing_rows( TableNode $expected ): void {
143-
$output = $this->result->stdout;
145+
$output = $this->normalize_line_endings( $this->result->stdout );
144146
$actual_rows = explode( "\n", rtrim( $output, "\n" ) );
145147

146148
$expected_rows = array();
@@ -176,7 +178,7 @@ public function then_stdout_should_be_a_table_containing_rows( TableNode $expect
176178
* @Then /^STDOUT should end with a table containing rows:$/
177179
*/
178180
public function then_stdout_should_end_with_a_table_containing_rows( TableNode $expected ): void {
179-
$output = $this->result->stdout;
181+
$output = $this->normalize_line_endings( $this->result->stdout );
180182
$actual_rows = explode( "\n", rtrim( $output, "\n" ) );
181183

182184
$expected_rows = array();
@@ -212,8 +214,8 @@ public function then_stdout_should_end_with_a_table_containing_rows( TableNode $
212214
* @Then /^STDOUT should be JSON containing:$/
213215
*/
214216
public function then_stdout_should_be_json_containing( PyStringNode $expected ): void {
215-
$output = $this->result->stdout;
216-
$expected = $this->replace_variables( (string) $expected );
217+
$output = $this->normalize_line_endings( $this->result->stdout );
218+
$expected = $this->normalize_line_endings( $this->replace_variables( (string) $expected ) );
217219

218220
if ( ! $this->check_that_json_string_contains_json_string( $output, $expected ) ) {
219221
$message = (string) $this->result;
@@ -253,11 +255,11 @@ public function then_stdout_should_be_json_containing( PyStringNode $expected ):
253255
* @Then /^STDOUT should be a JSON array containing:$/
254256
*/
255257
public function then_stdout_should_be_a_json_array_containing( PyStringNode $expected ): void {
256-
$output = $this->result->stdout;
257-
$expected = $this->replace_variables( (string) $expected );
258+
$output = $this->normalize_line_endings( $this->result->stdout );
259+
$expected = $this->normalize_line_endings( $this->replace_variables( (string) $expected ) );
258260

259-
$actual_values = json_decode( $output );
260-
$expected_values = json_decode( $expected );
261+
$actual_values = $this->normalize_line_endings_in_data( json_decode( $output ) );
262+
$expected_values = $this->normalize_line_endings_in_data( json_decode( $expected ) );
261263

262264
$missing = array_diff( $expected_values, $actual_values );
263265
if ( ! empty( $missing ) ) {
@@ -293,7 +295,7 @@ public function then_stdout_should_be_a_json_array_containing( PyStringNode $exp
293295
* @Then /^STDOUT should be CSV containing:$/
294296
*/
295297
public function then_stdout_should_be_csv_containing( TableNode $expected ): void {
296-
$output = $this->result->stdout;
298+
$output = $this->normalize_line_endings( $this->result->stdout );
297299

298300
$expected_rows = $expected->getRows();
299301
foreach ( $expected_rows as &$row ) {
@@ -336,8 +338,8 @@ public function then_stdout_should_be_csv_containing( TableNode $expected ): voi
336338
* @Then /^STDOUT should be YAML containing:$/
337339
*/
338340
public function then_stdout_should_be_yaml_containing( PyStringNode $expected ): void {
339-
$output = $this->result->stdout;
340-
$expected = $this->replace_variables( (string) $expected );
341+
$output = $this->normalize_line_endings( $this->result->stdout );
342+
$expected = $this->normalize_line_endings( $this->replace_variables( (string) $expected ) );
341343

342344
if ( ! $this->check_that_yaml_string_contains_yaml_string( $output, $expected ) ) {
343345
$message = (string) $this->result;
@@ -392,8 +394,9 @@ public function then_stdout_stderr_should_be_empty( $stream ): void {
392394
public function then_stdout_stderr_should_not_be_empty( $stream ): void {
393395

394396
$stream = strtolower( $stream );
397+
$output = $this->normalize_line_endings( $this->result->$stream );
395398

396-
if ( '' === rtrim( $this->result->$stream, "\n" ) ) {
399+
if ( '' === rtrim( $output, "\n" ) ) {
397400
throw new Exception( $this->result );
398401
}
399402
}
@@ -419,7 +422,8 @@ public function then_stdout_stderr_should_not_be_empty( $stream ): void {
419422
public function then_stdout_stderr_should_be_a_specific_version_string( $stream, $operator, $goal_ver ): void {
420423
$goal_ver = $this->replace_variables( $goal_ver );
421424
$stream = strtolower( $stream );
422-
if ( false === version_compare( trim( $this->result->$stream, "\n" ), $goal_ver, $operator ) ) {
425+
$output = $this->normalize_line_endings( $this->result->$stream );
426+
if ( false === version_compare( trim( $output, "\n" ), $goal_ver, $operator ) ) {
423427
throw new Exception( $this->result );
424428
}
425429
}
@@ -530,7 +534,7 @@ public function then_the_contents_of_a_specific_file_should_match( $path, $not,
530534
if ( '/' !== $path[0] ) {
531535
$path = $this->variables['RUN_DIR'] . "/$path";
532536
}
533-
$contents = file_get_contents( $path );
537+
$contents = $this->normalize_line_endings( file_get_contents( $path ) );
534538
if ( $not ) {
535539
$this->assert_not_regex( $expected, $contents );
536540
} else {
@@ -559,10 +563,11 @@ public function then_stdout_stderr_should_match_a_string( $stream, $not, $expect
559563
$expected = $this->replace_variables( $expected );
560564

561565
$stream = strtolower( $stream );
566+
$output = $this->normalize_line_endings( $this->result->$stream );
562567
if ( $not ) {
563-
$this->assert_not_regex( $expected, $this->result->$stream );
568+
$this->assert_not_regex( $expected, $output );
564569
} else {
565-
$this->assert_regex( $expected, $this->result->$stream );
570+
$this->assert_regex( $expected, $output );
566571
}
567572
}
568573

0 commit comments

Comments
 (0)