summaryrefslogtreecommitdiff
path: root/scripts/rspec_check_order_dependence
blob: 00f1176e2518837bf76f979fbb5998e302ac6233 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/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

if [ $# -eq 0 ]; then
  echo "Usage: $0 <files...>"
  exit
fi

TODO_YAML='./spec/support/rspec_order_todo.yml'
RSPEC_ARGS=(--format progress)

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

bin/rspec --order defined "${RSPEC_ARGS[@]}" "$@"
RSPEC_ORDER=reverse bin/rspec "${RSPEC_ARGS[@]}" "$@"
bin/rspec --order random "${RSPEC_ARGS[@]}" "$@"

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"