From 19d2df5188ef0958d3785b033e9cdabcd91e764e Mon Sep 17 00:00:00 2001 From: Mostafa Soufi Date: Sat, 14 Sep 2024 20:47:18 +0300 Subject: [PATCH 1/3] feat: add svn and download tool checks with error handling --- templates/install-wp-tests.sh | 37 ++++++++++++++--------------------- 1 file changed, 15 insertions(+), 22 deletions(-) diff --git a/templates/install-wp-tests.sh b/templates/install-wp-tests.sh index c6f53dc5..d3a590fb 100644 --- a/templates/install-wp-tests.sh +++ b/templates/install-wp-tests.sh @@ -17,23 +17,31 @@ TMPDIR=$(echo $TMPDIR | sed -e "s/\/$//") WP_TESTS_DIR=${WP_TESTS_DIR-$TMPDIR/wordpress-tests-lib} WP_CORE_DIR=${WP_CORE_DIR-$TMPDIR/wordpress} +# Function to download files using curl or wget download() { if [ `which curl` ]; then curl -s "$1" > "$2"; elif [ `which wget` ]; then wget -nv -O "$2" "$1" + else + echo "Error: Neither curl nor wget is installed." + exit 1 fi } +# Check if svn is installed +if ! command -v svn &> /dev/null; then + echo "Error: svn is not installed. Please install svn to proceed." + exit 1 +fi + if [[ $WP_VERSION =~ ^[0-9]+\.[0-9]+\-(beta|RC)[0-9]+$ ]]; then WP_BRANCH=${WP_VERSION%\-*} WP_TESTS_TAG="branches/$WP_BRANCH" - elif [[ $WP_VERSION =~ ^[0-9]+\.[0-9]+$ ]]; then WP_TESTS_TAG="branches/$WP_VERSION" elif [[ $WP_VERSION =~ [0-9]+\.[0-9]+\.[0-9]+ ]]; then if [[ $WP_VERSION =~ [0-9]+\.[0-9]+\.[0] ]]; then - # version x.x.0 means the first release of the major version, so strip off the .0 and download version x.x WP_TESTS_TAG="tags/${WP_VERSION%??}" else WP_TESTS_TAG="tags/$WP_VERSION" @@ -41,9 +49,7 @@ elif [[ $WP_VERSION =~ [0-9]+\.[0-9]+\.[0-9]+ ]]; then elif [[ $WP_VERSION == 'nightly' || $WP_VERSION == 'trunk' ]]; then WP_TESTS_TAG="trunk" else - # http serves a single offer, whereas https serves multiple. we only want one download http://api.wordpress.org/core/version-check/1.7/ /tmp/wp-latest.json - grep '[0-9]+\.[0-9]+(\.[0-9]+)?' /tmp/wp-latest.json LATEST_VERSION=$(grep -o '"version":"[^"]*' /tmp/wp-latest.json | sed 's/"version":"//') if [[ -z "$LATEST_VERSION" ]]; then echo "Latest WordPress version could not be found" @@ -70,13 +76,10 @@ install_wp() { if [ $WP_VERSION == 'latest' ]; then local ARCHIVE_NAME='latest' elif [[ $WP_VERSION =~ [0-9]+\.[0-9]+ ]]; then - # https serves multiple offers, whereas http serves single. download https://api.wordpress.org/core/version-check/1.7/ $TMPDIR/wp-latest.json if [[ $WP_VERSION =~ [0-9]+\.[0-9]+\.[0] ]]; then - # version x.x.0 means the first release of the major version, so strip off the .0 and download version x.x LATEST_VERSION=${WP_VERSION%??} else - # otherwise, scan the releases and get the most up to date minor version of the major release local VERSION_ESCAPED=`echo $WP_VERSION | sed 's/\./\\\\./g'` LATEST_VERSION=$(grep -o '"version":"'$VERSION_ESCAPED'[^"]*' $TMPDIR/wp-latest.json | sed 's/"version":"//' | head -1) fi @@ -96,16 +99,13 @@ install_wp() { } install_test_suite() { - # portable in-place argument for both GNU sed and Mac OSX sed if [[ $(uname -s) == 'Darwin' ]]; then local ioption='-i.bak' else local ioption='-i' fi - # set up testing suite if it doesn't yet exist if [ ! -d $WP_TESTS_DIR ]; then - # set up testing suite mkdir -p $WP_TESTS_DIR rm -rf $WP_TESTS_DIR/{includes,data} svn export --quiet --ignore-externals https://develop.svn.wordpress.org/${WP_TESTS_TAG}/tests/phpunit/includes/ $WP_TESTS_DIR/includes @@ -114,7 +114,6 @@ install_test_suite() { if [ ! -f wp-tests-config.php ]; then download https://develop.svn.wordpress.org/${WP_TESTS_TAG}/wp-tests-config-sample.php "$WP_TESTS_DIR"/wp-tests-config.php - # remove all forward slashes in the end WP_CORE_DIR=$(echo $WP_CORE_DIR | sed "s:/\+$::") sed $ioption "s:dirname( __FILE__ ) . '/src/':'$WP_CORE_DIR/':" "$WP_TESTS_DIR"/wp-tests-config.php sed $ioption "s:__DIR__ . '/src/':'$WP_CORE_DIR/':" "$WP_TESTS_DIR"/wp-tests-config.php @@ -123,13 +122,11 @@ install_test_suite() { sed $ioption "s/yourpasswordhere/$DB_PASS/" "$WP_TESTS_DIR"/wp-tests-config.php sed $ioption "s|localhost|${DB_HOST}|" "$WP_TESTS_DIR"/wp-tests-config.php fi - } recreate_db() { shopt -s nocasematch - if [[ $1 =~ ^(y|yes)$ ]] - then + if [[ $1 =~ ^(y|yes)$ ]]; then mysqladmin drop $DB_NAME -f --user="$DB_USER" --password="$DB_PASS"$EXTRA create_db echo "Recreated the database ($DB_NAME)." @@ -144,30 +141,26 @@ create_db() { } install_db() { - if [ ${SKIP_DB_CREATE} = "true" ]; then return 0 fi - # parse DB_HOST for port or socket references local PARTS=(${DB_HOST//\:/ }) local DB_HOSTNAME=${PARTS[0]}; local DB_SOCK_OR_PORT=${PARTS[1]}; local EXTRA="" - if ! [ -z $DB_HOSTNAME ] ; then + if ! [ -z $DB_HOSTNAME ]; then if [ $(echo $DB_SOCK_OR_PORT | grep -e '^[0-9]\{1,\}$') ]; then EXTRA=" --host=$DB_HOSTNAME --port=$DB_SOCK_OR_PORT --protocol=tcp" - elif ! [ -z $DB_SOCK_OR_PORT ] ; then + elif ! [ -z $DB_SOCK_OR_PORT ]; then EXTRA=" --socket=$DB_SOCK_OR_PORT" - elif ! [ -z $DB_HOSTNAME ] ; then + elif ! [ -z $DB_HOSTNAME ]; then EXTRA=" --host=$DB_HOSTNAME --protocol=tcp" fi fi - # create database - if [ $(mysql --user="$DB_USER" --password="$DB_PASS"$EXTRA --execute='show databases;' | grep ^$DB_NAME$) ] - then + 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 recreate_db $DELETE_EXISTING_DB From 851e1cf0b2ff32339ceb9ee051dd998326eb665f Mon Sep 17 00:00:00 2001 From: Mostafa Soufi Date: Sun, 15 Sep 2024 14:33:32 +0300 Subject: [PATCH 2/3] revert: the comments --- templates/install-wp-tests.sh | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/templates/install-wp-tests.sh b/templates/install-wp-tests.sh index d3a590fb..43265c60 100644 --- a/templates/install-wp-tests.sh +++ b/templates/install-wp-tests.sh @@ -35,6 +35,7 @@ if ! command -v svn &> /dev/null; then exit 1 fi +# Set WP_TESTS_TAG based on WP_VERSION if [[ $WP_VERSION =~ ^[0-9]+\.[0-9]+\-(beta|RC)[0-9]+$ ]]; then WP_BRANCH=${WP_VERSION%\-*} WP_TESTS_TAG="branches/$WP_BRANCH" @@ -42,6 +43,7 @@ elif [[ $WP_VERSION =~ ^[0-9]+\.[0-9]+$ ]]; then WP_TESTS_TAG="branches/$WP_VERSION" elif [[ $WP_VERSION =~ [0-9]+\.[0-9]+\.[0-9]+ ]]; then if [[ $WP_VERSION =~ [0-9]+\.[0-9]+\.[0] ]]; then + # version x.x.0 means the first release of the major version, so strip off the .0 and download version x.x WP_TESTS_TAG="tags/${WP_VERSION%??}" else WP_TESTS_TAG="tags/$WP_VERSION" @@ -49,6 +51,7 @@ elif [[ $WP_VERSION =~ [0-9]+\.[0-9]+\.[0-9]+ ]]; then elif [[ $WP_VERSION == 'nightly' || $WP_VERSION == 'trunk' ]]; then WP_TESTS_TAG="trunk" else + # Download latest version if no specific version is provided download http://api.wordpress.org/core/version-check/1.7/ /tmp/wp-latest.json LATEST_VERSION=$(grep -o '"version":"[^"]*' /tmp/wp-latest.json | sed 's/"version":"//') if [[ -z "$LATEST_VERSION" ]]; then @@ -57,8 +60,10 @@ else fi WP_TESTS_TAG="tags/$LATEST_VERSION" fi + set -ex +# Install WordPress core install_wp() { if [ -d $WP_CORE_DIR ]; then @@ -98,20 +103,25 @@ install_wp() { download https://raw.githubusercontent.com/markoheijnen/wp-mysqli/master/db.php $WP_CORE_DIR/wp-content/db.php } +# Install the WordPress test suite install_test_suite() { + # portable in-place argument for both GNU sed and Mac OSX sed if [[ $(uname -s) == 'Darwin' ]]; then local ioption='-i.bak' else local ioption='-i' fi + # set up testing suite if it doesn't yet exist if [ ! -d $WP_TESTS_DIR ]; then + # set up testing suite mkdir -p $WP_TESTS_DIR rm -rf $WP_TESTS_DIR/{includes,data} svn export --quiet --ignore-externals https://develop.svn.wordpress.org/${WP_TESTS_TAG}/tests/phpunit/includes/ $WP_TESTS_DIR/includes svn export --quiet --ignore-externals https://develop.svn.wordpress.org/${WP_TESTS_TAG}/tests/phpunit/data/ $WP_TESTS_DIR/data fi + # Configure wp-tests-config.php if [ ! -f wp-tests-config.php ]; then download https://develop.svn.wordpress.org/${WP_TESTS_TAG}/wp-tests-config-sample.php "$WP_TESTS_DIR"/wp-tests-config.php WP_CORE_DIR=$(echo $WP_CORE_DIR | sed "s:/\+$::") @@ -124,9 +134,11 @@ install_test_suite() { fi } +# Recreate the database if needed recreate_db() { shopt -s nocasematch - if [[ $1 =~ ^(y|yes)$ ]]; then + if [[ $1 =~ ^(y|yes)$ ]] + then mysqladmin drop $DB_NAME -f --user="$DB_USER" --password="$DB_PASS"$EXTRA create_db echo "Recreated the database ($DB_NAME)." @@ -136,31 +148,37 @@ recreate_db() { shopt -u nocasematch } +# Create the database create_db() { mysqladmin create $DB_NAME --user="$DB_USER" --password="$DB_PASS"$EXTRA } +# Install the test database install_db() { + if [ ${SKIP_DB_CREATE} = "true" ]; then return 0 fi + # parse DB_HOST for port or socket references local PARTS=(${DB_HOST//\:/ }) local DB_HOSTNAME=${PARTS[0]}; local DB_SOCK_OR_PORT=${PARTS[1]}; local EXTRA="" - if ! [ -z $DB_HOSTNAME ]; then + if ! [ -z $DB_HOSTNAME ] ; then if [ $(echo $DB_SOCK_OR_PORT | grep -e '^[0-9]\{1,\}$') ]; then EXTRA=" --host=$DB_HOSTNAME --port=$DB_SOCK_OR_PORT --protocol=tcp" - elif ! [ -z $DB_SOCK_OR_PORT ]; then + elif ! [ -z $DB_SOCK_OR_PORT ] ; then EXTRA=" --socket=$DB_SOCK_OR_PORT" - elif ! [ -z $DB_HOSTNAME ]; then + elif ! [ -z $DB_HOSTNAME ] ; then EXTRA=" --host=$DB_HOSTNAME --protocol=tcp" fi fi - if [ $(mysql --user="$DB_USER" --password="$DB_PASS"$EXTRA --execute='show databases;' | grep ^$DB_NAME$) ]; then + # 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 recreate_db $DELETE_EXISTING_DB @@ -169,6 +187,7 @@ install_db() { fi } +# Run the installation functions install_wp install_test_suite install_db From efdc0aebe792eaa7ddf6725eae45d70fe6c6ce2a Mon Sep 17 00:00:00 2001 From: Mostafa Soufi Date: Sun, 15 Sep 2024 14:39:42 +0300 Subject: [PATCH 3/3] Update install-wp-tests.sh --- templates/install-wp-tests.sh | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/templates/install-wp-tests.sh b/templates/install-wp-tests.sh index 43265c60..d2605fe0 100644 --- a/templates/install-wp-tests.sh +++ b/templates/install-wp-tests.sh @@ -17,7 +17,6 @@ TMPDIR=$(echo $TMPDIR | sed -e "s/\/$//") WP_TESTS_DIR=${WP_TESTS_DIR-$TMPDIR/wordpress-tests-lib} WP_CORE_DIR=${WP_CORE_DIR-$TMPDIR/wordpress} -# Function to download files using curl or wget download() { if [ `which curl` ]; then curl -s "$1" > "$2"; @@ -30,15 +29,17 @@ download() { } # Check if svn is installed -if ! command -v svn &> /dev/null; then - echo "Error: svn is not installed. Please install svn to proceed." - exit 1 -fi +check_svn_installed() { + if ! command -v svn > /dev/null; then + echo "Error: svn is not installed. Please install svn and try again." + exit 1 + fi +} -# Set WP_TESTS_TAG based on WP_VERSION if [[ $WP_VERSION =~ ^[0-9]+\.[0-9]+\-(beta|RC)[0-9]+$ ]]; then WP_BRANCH=${WP_VERSION%\-*} WP_TESTS_TAG="branches/$WP_BRANCH" + elif [[ $WP_VERSION =~ ^[0-9]+\.[0-9]+$ ]]; then WP_TESTS_TAG="branches/$WP_VERSION" elif [[ $WP_VERSION =~ [0-9]+\.[0-9]+\.[0-9]+ ]]; then @@ -51,8 +52,9 @@ elif [[ $WP_VERSION =~ [0-9]+\.[0-9]+\.[0-9]+ ]]; then elif [[ $WP_VERSION == 'nightly' || $WP_VERSION == 'trunk' ]]; then WP_TESTS_TAG="trunk" else - # Download latest version if no specific version is provided + # http serves a single offer, whereas https serves multiple. we only want one download http://api.wordpress.org/core/version-check/1.7/ /tmp/wp-latest.json + grep '[0-9]+\.[0-9]+(\.[0-9]+)?' /tmp/wp-latest.json LATEST_VERSION=$(grep -o '"version":"[^"]*' /tmp/wp-latest.json | sed 's/"version":"//') if [[ -z "$LATEST_VERSION" ]]; then echo "Latest WordPress version could not be found" @@ -60,10 +62,8 @@ else fi WP_TESTS_TAG="tags/$LATEST_VERSION" fi - set -ex -# Install WordPress core install_wp() { if [ -d $WP_CORE_DIR ]; then @@ -75,16 +75,20 @@ install_wp() { if [[ $WP_VERSION == 'nightly' || $WP_VERSION == 'trunk' ]]; then mkdir -p $TMPDIR/wordpress-trunk rm -rf $TMPDIR/wordpress-trunk/* + check_svn_installed svn export --quiet https://core.svn.wordpress.org/trunk $TMPDIR/wordpress-trunk/wordpress mv $TMPDIR/wordpress-trunk/wordpress/* $WP_CORE_DIR else if [ $WP_VERSION == 'latest' ]; then local ARCHIVE_NAME='latest' elif [[ $WP_VERSION =~ [0-9]+\.[0-9]+ ]]; then + # https serves multiple offers, whereas http serves single. download https://api.wordpress.org/core/version-check/1.7/ $TMPDIR/wp-latest.json if [[ $WP_VERSION =~ [0-9]+\.[0-9]+\.[0] ]]; then + # version x.x.0 means the first release of the major version, so strip off the .0 and download version x.x LATEST_VERSION=${WP_VERSION%??} else + # otherwise, scan the releases and get the most up to date minor version of the major release local VERSION_ESCAPED=`echo $WP_VERSION | sed 's/\./\\\\./g'` LATEST_VERSION=$(grep -o '"version":"'$VERSION_ESCAPED'[^"]*' $TMPDIR/wp-latest.json | sed 's/"version":"//' | head -1) fi @@ -103,7 +107,6 @@ install_wp() { download https://raw.githubusercontent.com/markoheijnen/wp-mysqli/master/db.php $WP_CORE_DIR/wp-content/db.php } -# Install the WordPress test suite install_test_suite() { # portable in-place argument for both GNU sed and Mac OSX sed if [[ $(uname -s) == 'Darwin' ]]; then @@ -117,13 +120,14 @@ install_test_suite() { # set up testing suite mkdir -p $WP_TESTS_DIR rm -rf $WP_TESTS_DIR/{includes,data} + check_svn_installed svn export --quiet --ignore-externals https://develop.svn.wordpress.org/${WP_TESTS_TAG}/tests/phpunit/includes/ $WP_TESTS_DIR/includes svn export --quiet --ignore-externals https://develop.svn.wordpress.org/${WP_TESTS_TAG}/tests/phpunit/data/ $WP_TESTS_DIR/data fi - # Configure wp-tests-config.php if [ ! -f wp-tests-config.php ]; then download https://develop.svn.wordpress.org/${WP_TESTS_TAG}/wp-tests-config-sample.php "$WP_TESTS_DIR"/wp-tests-config.php + # remove all forward slashes in the end WP_CORE_DIR=$(echo $WP_CORE_DIR | sed "s:/\+$::") sed $ioption "s:dirname( __FILE__ ) . '/src/':'$WP_CORE_DIR/':" "$WP_TESTS_DIR"/wp-tests-config.php sed $ioption "s:__DIR__ . '/src/':'$WP_CORE_DIR/':" "$WP_TESTS_DIR"/wp-tests-config.php @@ -132,9 +136,9 @@ install_test_suite() { sed $ioption "s/yourpasswordhere/$DB_PASS/" "$WP_TESTS_DIR"/wp-tests-config.php sed $ioption "s|localhost|${DB_HOST}|" "$WP_TESTS_DIR"/wp-tests-config.php fi + } -# Recreate the database if needed recreate_db() { shopt -s nocasematch if [[ $1 =~ ^(y|yes)$ ]] @@ -148,12 +152,10 @@ recreate_db() { shopt -u nocasematch } -# Create the database create_db() { mysqladmin create $DB_NAME --user="$DB_USER" --password="$DB_PASS"$EXTRA } -# Install the test database install_db() { if [ ${SKIP_DB_CREATE} = "true" ]; then @@ -187,7 +189,6 @@ install_db() { fi } -# Run the installation functions install_wp install_test_suite install_db