|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# This script makes it easy to run the Simpletests for the modules in the |
| 4 | +# MongoDB package. Use it in just three steps if you havec Drush in your path: |
| 5 | +# |
| 6 | +# 1 enable Simpletest: "drush en -y simpletest" |
| 7 | +# 2 edit your your settings*.php to configure MongoDB connection and cache plugin. |
| 8 | +# 3 run: "bash tests.bash <absolute path to Drupal root>" |
| 9 | +# |
| 10 | +# Notice: the script itself uses Drush to flush caches. |
| 11 | + |
| 12 | +# The path where the script will leave JUnit test results |
| 13 | +OUTPUT_DIR=/tmp/xml |
| 14 | +# The user name under which your web server runs |
| 15 | +WEB_USER=www-data |
| 16 | +# The prefix of the test groups to run. |
| 17 | +PREFIX="MongoDB:" |
| 18 | +# The test groups to run. |
| 19 | +TESTS="Base Cache Watchdog" |
| 20 | +# The Drupal root directory. Dynamic from script arguments. |
| 21 | +BASE= |
| 22 | + |
| 23 | +# Is the specified directory a Drupal root ? |
| 24 | +# |
| 25 | +# @return int |
| 26 | +# - 0 on success |
| 27 | +# - 1 on failure (not a Drupal root) |
| 28 | +function isDrupal { |
| 29 | + local base=$1 |
| 30 | + local result |
| 31 | + |
| 32 | + if [ ! -d "$base" ]; then |
| 33 | + return 1 |
| 34 | + fi |
| 35 | + |
| 36 | + pushd "$base" > /dev/null |
| 37 | + [ -d includes -a -d misc -a -d modules -a -d profiles -a -d sites -a -d themes ] |
| 38 | + result=$? |
| 39 | + popd > /dev/null |
| 40 | + |
| 41 | + return "$result" |
| 42 | +} |
| 43 | + |
| 44 | +# Validate script arguments look sane. |
| 45 | +function validate_arguments { |
| 46 | + # http://tldp.org/LDP/abs/html/exitcodes.html#EXITCODESREF |
| 47 | + if [ "$#" -ne 1 ]; then |
| 48 | + echo -e "Usage:\nbash $0 <site-root-directory>" |
| 49 | + exit 64 |
| 50 | + else |
| 51 | + BASE=$1 |
| 52 | + isDrupal $BASE |
| 53 | + if [ $? -ne 0 ]; then |
| 54 | + echo "$BASE does not look like a Drupal 7 site root." |
| 55 | + exit 65 |
| 56 | + fi |
| 57 | + fi |
| 58 | +} |
| 59 | + |
| 60 | +# Run the module tests groups |
| 61 | +function run { |
| 62 | + local base=$1 |
| 63 | + local user=$2 |
| 64 | + local output_dir=$3 |
| 65 | + local prefix=$4 |
| 66 | + local tests=${@:5} |
| 67 | + local group |
| 68 | + |
| 69 | + echo -e "Running tests in $base\n" |
| 70 | + cd "$base" |
| 71 | + mkdir -p "$output_dir" |
| 72 | + |
| 73 | + # Do not wrap $tests in "". It needs to be split on IFS. |
| 74 | + for test in $tests; do |
| 75 | + group="$prefix $test" |
| 76 | + echo Running $group test group |
| 77 | + |
| 78 | + # Testers might well have renamed run-tests.sh to run-tests.php, |
| 79 | + # so allow it with a wildcard. |
| 80 | + sudo -u $user php scripts/run-tests* \ |
| 81 | + --php /usr/bin/php \ |
| 82 | + --concurrency 1 \ |
| 83 | + --color \ |
| 84 | + --xml "$output_dir" \ |
| 85 | + "$group" |
| 86 | + if [ $? -ne 0 ]; then |
| 87 | + break; |
| 88 | + fi |
| 89 | + done |
| 90 | +} |
| 91 | + |
| 92 | +# Clean the Simpletest leftovers. |
| 93 | +function clean { |
| 94 | + local base=$1 |
| 95 | + local user=$2 |
| 96 | + local output=$3 |
| 97 | + |
| 98 | + echo "Cleaning leftover test results in $base" |
| 99 | + cd "$base" |
| 100 | + drush cc all |
| 101 | + echo "Cleaning leftover test collections in MongoDB" |
| 102 | + drush mdct |
| 103 | + sudo -u $user php scripts/run-tests* --clean |
| 104 | + echo "Removing results directory $output" |
| 105 | + rm -fr "$output" |
| 106 | +} |
| 107 | + |
| 108 | +# ---- Main logic ------------------------------------------------------------- |
| 109 | + |
| 110 | +validate_arguments $1 |
| 111 | +valid=$? |
| 112 | +if [ "$valid" -ne 0 ]; then |
| 113 | + exit "$valid" |
| 114 | +fi |
| 115 | + |
| 116 | +run "$BASE" "$WEB_USER" "$OUTPUT_DIR" "$PREFIX" "$TESTS" |
| 117 | +clean "$BASE" "$WEB_USER" "$OUTPUT_DIR" |
0 commit comments