Skip to content

Instantly share code, notes, and snippets.

@jenkoian
Created December 23, 2021 17:06
Show Gist options
  • Save jenkoian/d4a3fe45cdbbfc7a0f0eeafc1e85480f to your computer and use it in GitHub Desktop.
Save jenkoian/d4a3fe45cdbbfc7a0f0eeafc1e85480f to your computer and use it in GitHub Desktop.
Small bash script to do some _very_ basic plugin mutation testing against a website. i.e. check the site doesn't fall over if a random plugin is deactivated.
#!/usr/bin/env bash
URL_TO_CHECK=${1}
if [ -z "$URL_TO_CHECK" ]; then
echo 'Please supply a URL'
exit 1;
fi
WP="wp"
GREEN='\033[0;32m'
RED='\033[0;31m'
BOLD='\033[1m'
NC='\033[0m' # No Color
echo "Finding random plugin to deactivate..."
PLUGINS=()
i=0
for plugin_name in $($WP plugin list --status=active --field=name); do
PLUGINS[i]="$plugin_name"
i=$((i+1))
done
size=${#PLUGINS[@]}
index=$(($RANDOM % $size))
random_plugin=${PLUGINS[$index]}
echo -e "Deactivating ${BOLD}$random_plugin${NC}"
$WP plugin deactivate $random_plugin --quiet
echo "Checking if site is still working..."
status=$(curl -L -s -k -o /dev/null -w "%{http_code}" "$URL_TO_CHECK");
if [ $status != 200 ]; then
echo -e "${RED}Site breaks if you deactivate $random_plugin${NC}"
exit=1
else
echo -e "${GREEN}Site works if you deactivate $random_plugin${NC}"
printf "${TICK}"
exit=0
fi
echo -e "Reactivating ${BOLD}$random_plugin${NC}"
$WP plugin activate $random_plugin --quiet
exit $exit
@davidwren-boxuk
Copy link

Seems pretty cool, maybe specify an arguement so the plugin isn't random if you wanted to test specific ones?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment