Skip to content

Commit 3c60271

Browse files
committed
Keep the feature file round trip lossless
Extraction took a number of leading whitespace characters off every line of a block and syncing put the indentation of the block's first line back, which is only the same thing when that line is the least indented one. A block whose opening tag sits deeper than the code below it drifted further to the right on every `composer phpcbf`, and one mixing tabs and spaces came back with one swapped for the other, both without a single sniff having fired. Extraction now takes off the indentation prefix that all lines of a block share, and syncing puts exactly that prefix back. Extraction also counted a docstring opening with `<?php` as a PHP block while syncing only accepted one belonging to a step creating a `.php` file. A docstring stating an expectation about the contents of a file was therefore checked, fixed, and then refused, which failed the run and threw the fixes away. Only the step decides now: reformatting an expectation would make it stop matching what it is checked against, so the analysis in `phpstan-feature-files.php` keeps the second rule and the fixer does not. Both scripts let the filesystem root through as an extraction target. The check meant to catch it appends a separator to the target before comparing, which never matches a root that already ends in one, so `remove_extracted_files()` would have walked the whole filesystem removing every empty directory it could. Roots are now rejected up front, and the walk refuses to start on one even if a caller gets past that. Also names the feature file and the line in the two messages reporting a block that could not be synced, so it is possible to tell which one it was.
1 parent 0328764 commit 3c60271

4 files changed

Lines changed: 289 additions & 41 deletions

File tree

‎tests/tests/TestExtractFeaturePhp.php‎

Lines changed: 98 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
use WP_CLI\Tests\TestCase;
66
use WP_CLI\Utils;
77

8-
// phpcs:ignore Universal.Files.SeparateFunctionsFromOO.Mixed
98
class TestExtractFeaturePhp extends TestCase {
109

1110
/**
@@ -604,7 +603,104 @@ public function test_update_skips_block_when_source_coordinates_mismatch(): void
604603
$result = $this->run_script( array( 'update', 'features', 'extracted' ) );
605604

606605
$this->assertSame( 1, $result['exit_code'] );
607-
$this->assertStringContainsString( 'Unexpected content in', $result['output'] );
606+
$this->assertStringContainsString( 'is no longer the one that was checked', $result['output'] );
608607
$this->assertSame( $modified_contents, file_get_contents( $feature_file ) );
609608
}
609+
610+
public function test_extraction_skips_php_docstrings_that_do_not_belong_to_a_php_file_step(): void {
611+
// The block opens with `<?php`, but the step it belongs to states an
612+
// expectation instead of creating a file. Reformatting it would make it
613+
// stop matching the file it is checked against.
614+
$contents = "Feature: Example\n"
615+
. " Scenario: An expectation about a file\n"
616+
. " Then the wp-config.php file should contain:\n"
617+
. " \"\"\"\n"
618+
. " <?php\n"
619+
. " define('FOO',true);\n"
620+
. " \"\"\"\n";
621+
622+
$feature_file = $this->create_feature_file( 'example.feature', $contents );
623+
624+
$result = $this->run_script( array( 'extract', 'features', 'extracted' ) );
625+
626+
$this->assertSame( 0, $result['exit_code'], $result['output'] );
627+
$this->assertSame( array(), $this->get_extracted_files() );
628+
$this->assertSame( $contents, file_get_contents( $feature_file ) );
629+
}
630+
631+
public function test_update_preserves_a_block_indented_below_its_opening_tag(): void {
632+
// The first line of the block is not the one carrying the least
633+
// indentation, so the indentation to restore cannot be read off it.
634+
$contents = "Feature: Example\n"
635+
. " Scenario: A PHP block\n"
636+
. " Given a test.php file:\n"
637+
. " \"\"\"\n"
638+
. " <?php\n"
639+
. " \$foo = 'bar';\n"
640+
. " \"\"\"\n";
641+
642+
$feature_file = $this->create_feature_file( 'example.feature', $contents );
643+
644+
$this->run_script( array( 'extract', 'features', 'extracted' ) );
645+
$result = $this->run_script( array( 'update', 'features', 'extracted' ) );
646+
647+
$this->assertSame( 0, $result['exit_code'], $result['output'] );
648+
$this->assertSame( $contents, file_get_contents( $feature_file ) );
649+
}
650+
651+
public function test_update_preserves_mixed_tab_and_space_indentation(): void {
652+
// Extraction takes a shared prefix off the block rather than a number of
653+
// characters, so a tab never comes back as a space or the other way round.
654+
$contents = "Feature: Example\n"
655+
. " Scenario: A PHP block\n"
656+
. " Given a test.php file:\n"
657+
. " \"\"\"\n"
658+
. " <?php\n"
659+
. "\tif ( true ) {}\n"
660+
. " \"\"\"\n";
661+
662+
$feature_file = $this->create_feature_file( 'example.feature', $contents );
663+
664+
$this->run_script( array( 'extract', 'features', 'extracted' ) );
665+
$result = $this->run_script( array( 'update', 'features', 'extracted' ) );
666+
667+
$this->assertSame( 0, $result['exit_code'], $result['output'] );
668+
$this->assertSame( $contents, file_get_contents( $feature_file ) );
669+
}
670+
671+
public function test_extraction_refuses_to_use_a_root_directory_as_target(): void {
672+
$contents = "Feature: Example\n"
673+
. " Scenario: A PHP block\n"
674+
. " Given a test.php file:\n"
675+
. " \"\"\"\n"
676+
. " <?php\n"
677+
. " \$foo = 'bar';\n"
678+
. " \"\"\"\n";
679+
680+
$feature_file = $this->create_feature_file( 'example.feature', $contents );
681+
682+
$result = $this->run_script( array( 'extract', 'features', '/' ) );
683+
684+
$this->assertSame( 1, $result['exit_code'] );
685+
$this->assertStringContainsString( 'Refusing to use', $result['output'] );
686+
$this->assertSame( $contents, file_get_contents( $feature_file ) );
687+
}
688+
689+
public function test_extraction_refuses_a_target_that_only_resolves_to_a_root(): void {
690+
$contents = "Feature: Example\n"
691+
. " Scenario: A PHP block\n"
692+
. " Given a test.php file:\n"
693+
. " \"\"\"\n"
694+
. " <?php\n"
695+
. " \$foo = 'bar';\n"
696+
. " \"\"\"\n";
697+
698+
$feature_file = $this->create_feature_file( 'example.feature', $contents );
699+
700+
$result = $this->run_script( array( 'extract', 'features', str_repeat( '../', 64 ) ) );
701+
702+
$this->assertSame( 1, $result['exit_code'] );
703+
$this->assertStringContainsString( 'Refusing to use', $result['output'] );
704+
$this->assertSame( $contents, file_get_contents( $feature_file ) );
705+
}
610706
}

‎tests/tests/TestPhpStanFeatureFiles.php‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,42 @@ public function test_extraction_refuses_to_use_the_source_directory_as_target():
546546
$this->assertSame( $contents, file_get_contents( $feature_file ) );
547547
}
548548

549+
public function test_extraction_refuses_to_use_a_root_directory_as_target(): void {
550+
$contents = "Feature: Example\n"
551+
. " Scenario: A PHP block\n"
552+
. " Given a test.php file:\n"
553+
. " \"\"\"\n"
554+
. " <?php\n"
555+
. " \$foo = 'bar';\n"
556+
. " \"\"\"\n";
557+
558+
$feature_file = $this->create_feature_file( 'example.feature', $contents );
559+
560+
$result = $this->run_script( array( 'extract', 'features', '/' ) );
561+
562+
$this->assertSame( 1, $result['exit_code'] );
563+
$this->assertStringContainsString( 'Refusing to use', $result['output'] );
564+
$this->assertSame( $contents, file_get_contents( $feature_file ) );
565+
}
566+
567+
public function test_extraction_refuses_a_target_that_only_resolves_to_a_root(): void {
568+
$contents = "Feature: Example\n"
569+
. " Scenario: A PHP block\n"
570+
. " Given a test.php file:\n"
571+
. " \"\"\"\n"
572+
. " <?php\n"
573+
. " \$foo = 'bar';\n"
574+
. " \"\"\"\n";
575+
576+
$feature_file = $this->create_feature_file( 'example.feature', $contents );
577+
578+
$result = $this->run_script( array( 'extract', 'features', str_repeat( '../', 64 ) ) );
579+
580+
$this->assertSame( 1, $result['exit_code'] );
581+
$this->assertStringContainsString( 'Refusing to use', $result['output'] );
582+
$this->assertSame( $contents, file_get_contents( $feature_file ) );
583+
}
584+
549585
public function test_extraction_refuses_to_use_the_current_directory_as_target(): void {
550586
$contents = "Feature: Example\n"
551587
. " Scenario: A PHP block\n"

0 commit comments

Comments
 (0)