Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --recreate-db option to install-wp-tests.sh #320

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
185 changes: 98 additions & 87 deletions features/install-wp-tests.feature
Original file line number Diff line number Diff line change
Expand Up @@ -10,39 +10,39 @@ 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

@less-than-php-8.0
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:
"""
<?php
$version = 4;
if(PHP_VERSION_ID >= 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";
"""
"""
<?php
$version = 4;
if(PHP_VERSION_ID >= 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}`
Expand Down Expand Up @@ -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:
"""
<?php
$version = 4;
if(PHP_VERSION_ID >= 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";
"""
"""
<?php
$version = 4;
if(PHP_VERSION_ID >= 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}`
Expand Down Expand Up @@ -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:
"""
<?php
$version = 4;
if(PHP_VERSION_ID >= 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";
"""
"""
<?php
$version = 4;
if(PHP_VERSION_ID >= 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}`
Expand Down Expand Up @@ -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:
"""
<?php
$version = 4;
if(PHP_VERSION_ID >= 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";
"""
"""
<?php
$version = 4;
if(PHP_VERSION_ID >= 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}`
Expand Down
47 changes: 42 additions & 5 deletions templates/install-wp-tests.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,40 @@
#!/usr/bin/env bash

if [ $# -lt 3 ]; then
echo "usage: $0 <db-name> <db-user> <db-pass> [db-host] [wp-version] [skip-database-creation]"
exit 1
# Function to display usage instructions
display_usage() {
echo "Usage: $0 [options] <db-name> <db-user> <db-pass> [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
Expand Down Expand Up @@ -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
Expand Down