gitlab-ce/scripts/rspec_check_order_dependence

90 lines
2.3 KiB
Bash
Executable File

#!/usr/bin/env bash
## Usage: scripts/rspec_check_order_dependence <files...>
#
# List of RSpec files to be checked for their order dependency.
#
# If the files pass the following checks it's likely they are not
# order-dependent and are removed from `spec/support/rspec_order_todo.yml`
# to make them run in random order.
#
# The following checks are available:
# * Run specs in _defined_ order
# * Run specs in _reverse_ order
# * Run specs in _random_ order, 5 times by default.
# - Adjustable via RANDOM_ORDER_RUNS=10 scripts/rspec_check_order_dependence
if [ $# -eq 0 ]; then
echo "Usage: $0 <files...>"
echo " RANDOM_ORDER_RUNS=5 $0 <files...>"
exit
fi
TODO_YAML='./spec/support/rspec_order_todo.yml'
RSPEC_ARGS=(--fail-fast --format progress --tag ~quarantine)
RANDOM_ORDER_RUNS=${RANDOM_ORDER_RUNS:-5}
abort() {
echo "$@"
echo "Aborting..."
exit 1
}
for file in "$@"
do
# Drop potential file prefix `./`
file=${file#./}
# Match only the prefix so we can specify a directory to match all the files
# under it. For example, `spec/rubocop` will match, test and remove all TODO
# entries starting with `./spec/rubocop`.
grep -E -- "- './$file" "$TODO_YAML" > /dev/null || abort "Could not find '$file' in '$TODO_YAML'"
done
set -xe
export RSPEC_WARN_MISSING_FEATURE_CATEGORY=0
echo "Running in defined order:"
bin/rspec --order defined "${RSPEC_ARGS[@]}" "$@"
echo "Running in reverse order:"
RSPEC_ORDER=reverse bin/rspec "${RSPEC_ARGS[@]}" "$@"
for try in $(seq "$RANDOM_ORDER_RUNS")
do
echo "Running in random order ($try/$RANDOM_ORDER_RUNS):"
bin/rspec --order random "${RSPEC_ARGS[@]}" "$@"
done
set +xe
green='\033[0;32m'
clear='\033[0m' # No Color
echo -e "$green"
echo "
The files passed all checks!
They are likely not order-dependent and can be run in random order and thus
are being removed from 'spec/support/rspec_order_todo.yml':
"
for file in "$@"
do
# Drop potential file prefix `./`
file=${file#./}
echo " * Removing '$file'"
# Escape forward slashes to make it compatible with sed below
escaped_file=${file//\//\\/}
# We must use -i.bak to make sed work on Linux and MacOS.
# See https://riptutorial.com/sed/topic/9436/bsd-macos-sed-vs--gnu-sed-vs--the-posix-sed-specification
sed -i.bak "/- '.\/$escaped_file/d" "$TODO_YAML"
rm "$TODO_YAML.bak"
done
echo -e "$clear"