From bcba9e4dda6a9edf8945b1a15f6504c7e0577ed5 Mon Sep 17 00:00:00 2001 From: Vicente Canales Date: Fri, 9 Jun 2023 13:32:51 -0400 Subject: [PATCH 1/3] Add --recreate-db flag to avoid prompt This adds an option to remove the need for user input when running the script for confirming database recreation, helpful when it is run in a CI context, such as a part of a GitHub Action. Also refactors the help message, and when it is printed. --- templates/install-wp-tests.sh | 47 +++++++++++++++++++++++++++++++---- 1 file changed, 42 insertions(+), 5 deletions(-) diff --git a/templates/install-wp-tests.sh b/templates/install-wp-tests.sh index c6f53dc5..08acb5bb 100644 --- a/templates/install-wp-tests.sh +++ b/templates/install-wp-tests.sh @@ -1,8 +1,40 @@ #!/usr/bin/env bash -if [ $# -lt 3 ]; then - echo "usage: $0 [db-host] [wp-version] [skip-database-creation]" - exit 1 +# Function to display usage instructions +display_usage() { + echo "Usage: $0 [options] [db-host] [wp-version] [skip-database-creation]" + echo "Options:" + echo " --recreate-db Recreate the database" + echo " --help Displays this help message" +} + +RECREATE_DB=0 + +# Parse command-line arguments +for arg in "$@" +do + case $arg in + --recreate-db) + RECREATE_DB=1 + shift + ;; + --help) + SHOW_HELP=1 + shift + ;; + esac +done + +if [[ "$SHOW_HELP" -eq 1 ]] +then + display_help + exit 0 +fi + +# Check if required arguments are provided +if [[ $# -lt 3 ]]; then + display_usage + exit 1 fi DB_NAME=$1 @@ -168,8 +200,13 @@ install_db() { # create database if [ $(mysql --user="$DB_USER" --password="$DB_PASS"$EXTRA --execute='show databases;' | grep ^$DB_NAME$) ] then - echo "Reinstalling will delete the existing test database ($DB_NAME)" - read -p 'Are you sure you want to proceed? [y/N]: ' DELETE_EXISTING_DB + if [[ "$RECREATE_DB" -eq 1 ]] + then + DELETE_EXISTING_DB='y' + else + echo "Reinstalling will delete the existing test database ($DB_NAME)" + read -p 'Are you sure you want to proceed? [y/N]: ' DELETE_EXISTING_DB + fi recreate_db $DELETE_EXISTING_DB else create_db From c28edbd421682ff1c26662010d28f9c76a77570c Mon Sep 17 00:00:00 2001 From: Vicente Canales Date: Fri, 21 Jul 2023 16:43:55 -0400 Subject: [PATCH 2/3] Update usage test --- features/install-wp-tests.feature | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/features/install-wp-tests.feature b/features/install-wp-tests.feature index 1c1790d9..753b2d6c 100644 --- a/features/install-wp-tests.feature +++ b/features/install-wp-tests.feature @@ -10,7 +10,7 @@ Feature: Scaffold install-wp-tests.sh tests When I try `/usr/bin/env bash {PLUGIN_DIR}/hello-world/bin/install-wp-tests.sh` Then STDOUT should contain: """ - usage: + Usage: """ And the return code should be 1 From 64e743e5c0b2f9fa05a7cbdd233355782963f12b Mon Sep 17 00:00:00 2001 From: Vicente Canales Date: Fri, 21 Jul 2023 16:49:30 -0400 Subject: [PATCH 3/3] Add scenario for --recreate-db flow --- features/install-wp-tests.feature | 183 ++++++++++++++++-------------- 1 file changed, 97 insertions(+), 86 deletions(-) diff --git a/features/install-wp-tests.feature b/features/install-wp-tests.feature index 753b2d6c..a2cb3c4b 100644 --- a/features/install-wp-tests.feature +++ b/features/install-wp-tests.feature @@ -18,31 +18,31 @@ Feature: Scaffold install-wp-tests.sh tests Scenario: Install latest version of WordPress Given a WP install And a affirmative-response file: - """ - Y - """ + """ + Y + """ And a negative-response file: - """ - No - """ + """ + No + """ And a get-phpunit-phar-url.php file: - """ - = 50600) { - $version = 5; - } - if(PHP_VERSION_ID >= 70000) { - $version = 6; - } - if(PHP_VERSION_ID >= 70100) { - $version = 7; - } - if(PHP_VERSION_ID >= 80000) { - $version = 9; - } - echo "https://phar.phpunit.de/phpunit-{$version}.phar"; - """ + """ + = 50600) { + $version = 5; + } + if(PHP_VERSION_ID >= 70000) { + $version = 6; + } + if(PHP_VERSION_ID >= 70100) { + $version = 7; + } + if(PHP_VERSION_ID >= 80000) { + $version = 9; + } + echo "https://phar.phpunit.de/phpunit-{$version}.phar"; + """ And I run `wp eval-file get-phpunit-phar-url.php --skip-wordpress` And save STDOUT as {PHPUNIT_PHAR_URL} And I run `wget -q -O phpunit {PHPUNIT_PHAR_URL}` @@ -133,35 +133,46 @@ Feature: Scaffold install-wp-tests.sh tests Leaving the existing database (wp_cli_test_scaffold) in place """ + When I try `WP_TESTS_DIR={RUN_DIR}/wordpress-tests-lib WP_CORE_DIR={RUN_DIR}/wordpress /usr/bin/env bash {PLUGIN_DIR}/hello-world/bin/install-wp-tests.sh --recreate-db wp_cli_test_scaffold {DB_USER} {DB_PASSWORD} {DB_HOST} latest` + Then the return code should be 0 + And STDERR should contain: + """ + Reinstalling + """ + And STDOUT should contain: + """ + Recreated the database (wp_cli_test_scaffold) + """ + @require-php-8.0 @less-than-wp-5.8 Scenario: Install latest version of WordPress on PHP 8.0+ and WordPress less then 5.8 Given a WP install And a affirmative-response file: - """ - Y - """ + """ + Y + """ And a negative-response file: - """ - No - """ + """ + No + """ And a get-phpunit-phar-url.php file: - """ - = 50600) { - $version = 5; - } - if(PHP_VERSION_ID >= 70000) { - $version = 6; - } - if(PHP_VERSION_ID >= 70100) { - $version = 7; - } - if(PHP_VERSION_ID >= 80000) { - $version = 9; - } - echo "https://phar.phpunit.de/phpunit-{$version}.phar"; - """ + """ + = 50600) { + $version = 5; + } + if(PHP_VERSION_ID >= 70000) { + $version = 6; + } + if(PHP_VERSION_ID >= 70100) { + $version = 7; + } + if(PHP_VERSION_ID >= 80000) { + $version = 9; + } + echo "https://phar.phpunit.de/phpunit-{$version}.phar"; + """ And I run `wp eval-file get-phpunit-phar-url.php --skip-wordpress` And save STDOUT as {PHPUNIT_PHAR_URL} And I run `wget -q -O phpunit {PHPUNIT_PHAR_URL}` @@ -268,31 +279,31 @@ Feature: Scaffold install-wp-tests.sh tests Scenario: Install latest version of WordPress on PHP 8.0+ and WordPress above 5.8 Given a WP install And a affirmative-response file: - """ - Y - """ + """ + Y + """ And a negative-response file: - """ - No - """ + """ + No + """ And a get-phpunit-phar-url.php file: - """ - = 50600) { - $version = 5; - } - if(PHP_VERSION_ID >= 70000) { - $version = 6; - } - if(PHP_VERSION_ID >= 70100) { - $version = 7; - } - if(PHP_VERSION_ID >= 80000) { - $version = 9; - } - echo "https://phar.phpunit.de/phpunit-{$version}.phar"; - """ + """ + = 50600) { + $version = 5; + } + if(PHP_VERSION_ID >= 70000) { + $version = 6; + } + if(PHP_VERSION_ID >= 70100) { + $version = 7; + } + if(PHP_VERSION_ID >= 80000) { + $version = 9; + } + echo "https://phar.phpunit.de/phpunit-{$version}.phar"; + """ And I run `wp eval-file get-phpunit-phar-url.php --skip-wordpress` And save STDOUT as {PHPUNIT_PHAR_URL} And I run `wget -q -O phpunit {PHPUNIT_PHAR_URL}` @@ -387,23 +398,23 @@ Feature: Scaffold install-wp-tests.sh tests Scenario: Install WordPress from trunk Given a WP install And a get-phpunit-phar-url.php file: - """ - = 50600) { - $version = 5; - } - if(PHP_VERSION_ID >= 70000) { - $version = 6; - } - if(PHP_VERSION_ID >= 70100) { - $version = 7; - } - if(PHP_VERSION_ID >= 80000) { - $version = 9; - } - echo "https://phar.phpunit.de/phpunit-{$version}.phar"; - """ + """ + = 50600) { + $version = 5; + } + if(PHP_VERSION_ID >= 70000) { + $version = 6; + } + if(PHP_VERSION_ID >= 70100) { + $version = 7; + } + if(PHP_VERSION_ID >= 80000) { + $version = 9; + } + echo "https://phar.phpunit.de/phpunit-{$version}.phar"; + """ And I run `wp eval-file get-phpunit-phar-url.php --skip-wordpress` And save STDOUT as {PHPUNIT_PHAR_URL} And I run `wget -q -O phpunit {PHPUNIT_PHAR_URL}`