Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix behat tests and prevent them from freezing
  • Loading branch information
jeroenpf committed Jul 24, 2024
commit 1d369699f8b08e2f5a12a6f05bf06976db65740d
4 changes: 4 additions & 0 deletions features/db-export.feature
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ Feature: Export a WordPress database
-- Dump completed on
"""

@require-mysql
Scenario: Export database with mysql defaults to STDOUT
Given a WP install

Expand All @@ -52,6 +53,7 @@ Feature: Export a WordPress database
-- Dump completed on
"""

@require-mysql
Scenario: Export database with mysql --no-defaults to STDOUT
Given a WP install

Expand All @@ -61,6 +63,7 @@ Feature: Export a WordPress database
-- Dump completed on
"""

@require-mysql
Scenario: Export database with passed-in options
Given a WP install

Expand All @@ -78,6 +81,7 @@ Feature: Export a WordPress database
"""
And STDOUT should be empty

@require-mysql
Scenario: MySQL defaults are available as appropriate with --defaults flag
Given a WP install

Expand Down
38 changes: 17 additions & 21 deletions features/db-import.feature
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Feature: Import a WordPress database
Success: Imported from 'wp_cli_test.sql'.
"""

@require-mysql
Scenario: Import from database name path by default with mysql defaults
Given a WP install

Expand All @@ -24,6 +25,7 @@ Feature: Import a WordPress database
Success: Imported from 'wp_cli_test.sql'.
"""

@require-mysql
Scenario: Import from database name path by default with --no-defaults
Given a WP install

Expand All @@ -36,27 +38,19 @@ Feature: Import a WordPress database
Success: Imported from 'wp_cli_test.sql'.
"""

# Scenario: Import from STDIN
# Given a WP install
#
# When I run `wp db import -`
# Then STDOUT should be:
# """
# Success: Imported from 'STDIN'.
# """
Scenario: Import from STDIN
Given a WP install

Scenario: Test plugin
Given a WP install
And these installed and active plugins:
"""
sqlite-database-integration
"""
When I run `wp plugin list`
Then STDOUT should contain:
"""
sqlite-database-integration
"""
When I run `wp db export wp_cli_test.sql`

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Nice catch and fix. I see this test was there for 5 years. How did it even work before?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Maybe it depends on the environment. We both experienced this issue locally but the CI seems to be fine with it.

Then the wp_cli_test.sql file should exist

When I run `cat wp_cli_test.sql | wp db import -`
Then STDOUT should be:
"""
Success: Imported from 'STDIN'.
"""

@require-mysql
Scenario: Import from database name path by default and skip speed optimization
Given a WP install

Expand All @@ -68,7 +62,7 @@ Feature: Import a WordPress database
"""
Success: Imported from 'wp_cli_test.sql'.
"""

@require-mysql
Scenario: Import from database name path by default with passed-in dbuser/dbpass
Given a WP install

Expand Down Expand Up @@ -103,6 +97,7 @@ Feature: Import a WordPress database
Success: Imported from 'debug.sql'.
"""

@require-mysql
Scenario: Help runs properly at various points of a functional WP install
Given an empty directory

Expand Down Expand Up @@ -140,6 +135,7 @@ Feature: Import a WordPress database
"""
wp db import
"""
@require-mysql
Scenario: MySQL defaults are available as appropriate with --defaults flag
Given a WP install

Expand All @@ -164,7 +160,7 @@ Feature: Import a WordPress database
Debug (db): Running shell command: /usr/bin/env mysql --no-defaults --no-auto-rehash
"""

@require-wp-4.2
@require-wp-4.2 @require-mysql
Scenario: Import db that has emoji in post
Given a WP install

Expand Down
8 changes: 4 additions & 4 deletions features/db-query.feature
Original file line number Diff line number Diff line change
Expand Up @@ -75,23 +75,23 @@ Feature: Query the database with WordPress' MySQL config
Scenario: MySQL defaults are available as appropriate with --defaults flag
Given a WP install

When I try `wp db query --defaults --debug`
When I try `"select 1" | wp db query --defaults --debug`

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

What is the context of this change?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Test command expects STDIN, none was given so it would cause the command to freeze indefinitely and the test would be stuck (at least locally).

Then STDERR should contain:
"""
Debug (db): Running shell command: /usr/bin/env mysql --no-auto-rehash
"""

When I try `wp db query --debug`
When I try `"select 1" | wp db query --debug`
Then STDERR should contain:
"""
Debug (db): Running shell command: /usr/bin/env mysql --no-defaults --no-auto-rehash
"""

When I try `wp db query --no-defaults --debug`
When I try `"select 1" | wp db query --no-defaults --debug`
Then STDERR should contain:
"""
Debug (db): Running shell command: /usr/bin/env mysql --no-defaults --no-auto-rehash
"""
"""

Scenario: SQL modes do not include any of the modes incompatible with WordPress
Given a WP install
Expand Down
24 changes: 12 additions & 12 deletions src/DB_Command.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

use WP_CLI\DB\WP_SQLite_Export;
use WP_CLI\DB\WP_SQLite_Import;
use WP_CLI\DB\SQLite\Export;
use WP_CLI\DB\SQLite\Import;
use WP_CLI\Formatter;
use WP_CLI\Utils;

Expand Down Expand Up @@ -599,13 +599,6 @@ public function export( $args, $assoc_args ) {

}

// Check if SQLite is enabled and use it if it is.
if ( WP_SQLite_Export::get_sqlite_version() ) {
$export = new WP_SQLite_Export();
$export->run( $result_file, $assoc_args );
return;
}

$stdout = ( '-' === $result_file );
$porcelain = Utils\get_flag_value( $assoc_args, 'porcelain' );

Expand All @@ -614,6 +607,13 @@ public function export( $args, $assoc_args ) {
WP_CLI::error( 'Porcelain is not allowed when output mode is STDOUT.' );
}

// Check if SQLite is enabled and use it if it is.
if ( Export::get_sqlite_version() ) {
$export = new Export();
$export->run( $result_file, $assoc_args );
return;
}

if ( ! $stdout ) {
$assoc_args['result-file'] = $result_file;
}
Expand Down Expand Up @@ -773,9 +773,9 @@ public function import( $args, $assoc_args ) {
}

// Check if SQLite is enabled and use it if it is.
if ( WP_SQLite_Import::get_sqlite_version() ) {
$importer = new WP_SQLite_Import();
$importer->run( $result_file, $args );
if ( Import::get_sqlite_version() ) {
$importer = new Import();
$importer->run( $result_file, $assoc_args );
return;
}

Expand Down
4 changes: 2 additions & 2 deletions src/WP_SQLite_Base.php → src/SQLite/Base.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
namespace WP_CLI\DB;
namespace WP_CLI\DB\SQLite;

class WP_SQLite_Base {
class Base {

protected $unsupported_arguments = [];

Expand Down
18 changes: 13 additions & 5 deletions src/WP_SQLite_Export.php → src/SQLite/Export.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?php
namespace WP_CLI\DB;
namespace WP_CLI\DB\SQLite;

use Exception;
use PDO;
use WP_CLI;
use WP_SQLite_Translator;

class WP_SQLite_Export extends WP_SQLite_Base {
class Export extends Base {

protected $unsupported_arguments = [
'fields',
Expand All @@ -29,6 +29,7 @@ public function run( $result_file, $args ) {

$this->check_arguments( $args );
$this->load_dependencies();
$is_stdout = '-' === $result_file;

$exclude_tables = isset( $args['exclude_tables'] ) ? explode( ',', $args['exclude_tables'] ) : [];
$exclude_tables = array_merge(
Expand All @@ -43,7 +44,7 @@ public function run( $result_file, $args ) {
$include_tables = isset( $args['tables'] ) ? explode( ',', $args['tables'] ) : [];

$translator = new WP_SQLite_Translator();
$handle = fopen( $result_file, 'w' );
$handle = $is_stdout ? fopen( 'php://stdout', 'w' ) : fopen( $result_file, 'w' );

foreach ( $translator->query( 'SHOW TABLES' ) as $table ) {

Expand All @@ -65,13 +66,20 @@ public function run( $result_file, $args ) {
}
}

fwrite( $handle, sprintf( '-- Dump completed on %s', date('c') ) );
fclose( $handle );

if ( $is_stdout ) {
return;
}

if ( isset( $args['porcelain'] ) ) {
WP_CLI::line( $result_file );
} else {
WP_CLI::line( 'Export complete. File written to ' . $result_file );
WP_CLI::success( 'Export complete. File written to ' . $result_file );
}

fclose( $handle );

}

protected function get_create_statement( $table, $translator ) {
Expand Down
19 changes: 14 additions & 5 deletions src/WP_SQLite_Import.php → src/SQLite/Import.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
<?php
namespace WP_CLI\DB;
namespace WP_CLI\DB\SQLite;

use Exception;
use Generator;
use WP_CLI;
use WP_SQLite_Translator;

class WP_SQLite_Import extends WP_SQLite_Base {
class Import extends Base {

protected $unsupported_arguments = [
'skip-optimization',
'defaults',
'fields',
'dbuser',
'dbpass',
];

/**
Expand All @@ -23,14 +25,19 @@ public function run( $sql_file_path, $args ) {
$this->check_arguments( $args );
$this->load_dependencies();
$translator = new WP_SQLite_Translator();
foreach ( $this->parse_statements( $sql_file_path ) as $statement ) {

$is_stdin = '-' === $sql_file_path;
$import_file = $is_stdin ? 'php://stdin' : $sql_file_path;

foreach ( $this->parse_statements( $import_file ) as $statement ) {
$result = $translator->query( $statement );
if ( false === $result ) {
WP_CLI::warning( 'Could not execute statement: ' . $statement );
}
}

WP_CLI::success( sprintf("Imported from '%s'.", $sql_file_path ) );
$imported_from = $is_stdin ? 'STDIN' : $sql_file_path;
WP_CLI::success( sprintf("Imported from '%s'.", $imported_from ) );
}

/**
Expand All @@ -41,9 +48,11 @@ public function run( $sql_file_path, $args ) {
* @throws Exception
*/
public function parse_statements( $sql_file_path ) {

$handle = fopen( $sql_file_path, 'r' );

if ( ! $handle ) {
throw new Exception( "Unable to open file: $sql_file_path" );
WP_CLI::error( "Unable to open file: $sql_file_path" );
}

$single_quotes = 0;
Expand Down
File renamed without changes.