Skip to content

Commit 2c962f3

Browse files
committed
Address some code review feedback
1 parent 979fa28 commit 2c962f3

2 files changed

Lines changed: 76 additions & 12 deletions

File tree

‎tests/tests/TestExtractFeaturePhp.php‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -523,4 +523,50 @@ public function test_update_reports_unexpected_content_without_changing_the_feat
523523
$this->assertSame( 1, $result['exit_code'] );
524524
$this->assertSame( $contents, file_get_contents( $feature_file ) );
525525
}
526+
527+
public function test_update_reports_missing_feature_file(): void {
528+
$feature_file = $this->create_feature_file(
529+
'example.feature',
530+
"Feature: Example\n"
531+
. " Scenario: A PHP block\n"
532+
. " Given a test.php file:\n"
533+
. " \"\"\"\n"
534+
. " <?php\n"
535+
. " \$foo = 'bar';\n"
536+
. " \"\"\"\n"
537+
);
538+
539+
$this->run_script( array( 'extract', 'features', 'extracted' ) );
540+
541+
unlink( $feature_file );
542+
543+
$result = $this->run_script( array( 'update', 'features', 'extracted' ) );
544+
545+
$this->assertSame( 1, $result['exit_code'] );
546+
$this->assertStringContainsString( 'does not exist', $result['output'] );
547+
}
548+
549+
public function test_update_skips_block_when_source_coordinates_mismatch(): 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+
$this->run_script( array( 'extract', 'features', 'extracted' ) );
561+
562+
// Modify the feature file so the step preceding the docstring no longer creates a PHP file.
563+
$modified_contents = str_replace( 'Given a test.php file:', 'Given a non-php step:', $contents );
564+
file_put_contents( $feature_file, $modified_contents );
565+
566+
$result = $this->run_script( array( 'update', 'features', 'extracted' ) );
567+
568+
$this->assertSame( 1, $result['exit_code'] );
569+
$this->assertStringContainsString( 'Unexpected content in', $result['output'] );
570+
$this->assertSame( $modified_contents, file_get_contents( $feature_file ) );
571+
}
526572
}

‎utils/extract-feature-php.php‎

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ function is_valid_target_dir( $target_dir, $source_dir ) {
3030
return false;
3131
}
3232

33-
// A Windows drive root, such as `C:` or `C:\`.
34-
if ( preg_match( '/^[a-z]:\\\\?$/i', $target_dir ) ) {
33+
// A Windows drive root, such as `C:`, `C:\`, or `C:/`.
34+
if ( preg_match( '/^[a-z]:[\\\\\/]?$/i', $target_dir ) ) {
3535
return false;
3636
}
3737

@@ -119,8 +119,8 @@ function is_php_file_step( $line ) {
119119
* @return bool Whether extraction completed successfully.
120120
*/
121121
function extract_feature_php( $source_dir, $target_dir ) {
122-
$source_dir = rtrim( $source_dir, '/' );
123-
$target_dir = rtrim( $target_dir, '/' );
122+
$source_dir = rtrim( str_replace( '\\', '/', $source_dir ), '/' );
123+
$target_dir = rtrim( str_replace( '\\', '/', $target_dir ), '/' );
124124

125125
if ( ! is_dir( $source_dir ) ) {
126126
fwrite( STDERR, sprintf( 'Source directory "%s" does not exist.', $source_dir ) . PHP_EOL );
@@ -141,7 +141,7 @@ function extract_feature_php( $source_dir, $target_dir ) {
141141

142142
foreach ( $iterator as $file ) {
143143
if ( $file->isFile() && 'feature' === $file->getExtension() ) {
144-
$filepath = $file->getPathname();
144+
$filepath = str_replace( '\\', '/', $file->getPathname() );
145145
$relative = substr( $filepath, strlen( $source_dir ) + 1 );
146146
$lines = file( $filepath );
147147

@@ -327,8 +327,8 @@ function strip_extraction_padding( $temp_lines, $code_start, $had_php_tag ) {
327327
* @return bool Whether all blocks were synced successfully.
328328
*/
329329
function update_feature_php( $source_dir, $target_dir ) {
330-
$source_dir = rtrim( $source_dir, '/' );
331-
$target_dir = rtrim( $target_dir, '/' );
330+
$source_dir = rtrim( str_replace( '\\', '/', $source_dir ), '/' );
331+
$target_dir = rtrim( str_replace( '\\', '/', $target_dir ), '/' );
332332

333333
if ( ! is_dir( $target_dir ) ) {
334334
fwrite( STDERR, sprintf( 'Target directory "%s" does not exist.', $target_dir ) . PHP_EOL );
@@ -342,7 +342,7 @@ function update_feature_php( $source_dir, $target_dir ) {
342342

343343
foreach ( $iterator as $file ) {
344344
if ( $file->isFile() && 'php' === $file->getExtension() ) {
345-
$temp_filepath = $file->getPathname();
345+
$temp_filepath = str_replace( '\\', '/', $file->getPathname() );
346346
$temp_filename = $file->getFilename();
347347

348348
if ( ! preg_match( EXTRACTED_FILE_PATTERN, $temp_filename, $matches ) ) {
@@ -366,6 +366,8 @@ function update_feature_php( $source_dir, $target_dir ) {
366366

367367
foreach ( $files_by_feature as $feature_path => $blocks ) {
368368
if ( ! file_exists( $feature_path ) ) {
369+
fwrite( STDERR, sprintf( 'Feature file "%s" does not exist.', $feature_path ) . PHP_EOL );
370+
$success = false;
369371
continue;
370372
}
371373

@@ -385,17 +387,33 @@ function ( $a, $b ) {
385387
}
386388

387389
foreach ( $blocks as $block ) {
388-
$code_start = $block['docstring_start'] + 1;
389-
$code_end = $block['docstring_end'] - 1;
390-
$temp_lines = file( $block['temp_filepath'] );
390+
$docstring_start = $block['docstring_start'];
391+
$docstring_end = $block['docstring_end'];
392+
$code_start = $docstring_start + 1;
393+
$code_end = $docstring_end - 1;
394+
$temp_lines = file( $block['temp_filepath'] );
391395

392396
if ( false === $temp_lines ) {
393397
fwrite( STDERR, sprintf( 'Could not read "%s".', $block['temp_filepath'] ) . PHP_EOL );
394398
$success = false;
395399
continue;
396400
}
397401

398-
if ( ! isset( $feature_lines[ $code_start ] ) || $code_start > $code_end ) {
402+
if (
403+
$code_start > $code_end
404+
|| ! isset( $feature_lines[ $docstring_start ] )
405+
|| ! isset( $feature_lines[ $docstring_end ] )
406+
|| ( 0 !== strpos( trim( $feature_lines[ $docstring_start ] ), '"""' ) && 0 !== strpos( trim( $feature_lines[ $docstring_start ] ), "'''" ) )
407+
|| ( 0 !== strpos( trim( $feature_lines[ $docstring_end ] ), '"""' ) && 0 !== strpos( trim( $feature_lines[ $docstring_end ] ), "'''" ) )
408+
|| 0 === $docstring_start
409+
|| ! isset( $feature_lines[ $docstring_start - 1 ] )
410+
|| ! is_php_file_step( $feature_lines[ $docstring_start - 1 ] )
411+
) {
412+
fwrite(
413+
STDERR,
414+
sprintf( 'Unexpected content in "%s", not syncing this block.', $feature_path ) . PHP_EOL
415+
);
416+
$success = false;
399417
continue;
400418
}
401419

0 commit comments

Comments
 (0)