/ test / script / run-vader-tests
run-vader-tests
  1  #!/usr/bin/env bash
  2  
  3  set -e
  4  set -u
  5  
  6  docker_flags=(--rm -v "$PWD:/testplugin" -v "$PWD/test:/home" -w /testplugin "$DOCKER_RUN_IMAGE")
  7  red='\033[0;31m'
  8  green='\033[0;32m'
  9  nc='\033[0m'
 10  verbose=0
 11  quiet=0
 12  
 13  while [ $# -ne 0 ]; do
 14      case $1 in
 15      -v)
 16          verbose=1
 17          shift
 18      ;;
 19      -q)
 20          quiet=1
 21          shift
 22      ;;
 23      --)
 24          shift
 25          break
 26      ;;
 27      -?*)
 28          echo "Invalid argument: $1" 1>&2
 29          exit 1
 30      ;;
 31      *)
 32          break
 33      ;;
 34      esac
 35  done
 36  
 37  vim="$1"
 38  tests="$2"
 39  
 40  echo "$vim"
 41  
 42  case $vim in
 43      neovim-v0.2*)
 44          headless=''
 45      ;;
 46      # Neovim 0.6+ requires headless argument to load Vader tests.
 47      neovim*)
 48          headless='--headless'
 49      ;;
 50      *)
 51          headless=''
 52      ;;
 53  esac
 54  
 55  # This file will be used to track if tests ran or not.
 56  # We can't use a variable, because we need to set a value in a sub-shell.
 57  run_file="$(mktemp -t tests_ran.XXXXXXXX)"
 58  
 59  function filter-vader-output() {
 60      local hit_first_vader_line=0
 61      # When verbose mode is off, suppress output until Vader starts.
 62      local start_output="$verbose"
 63      local filtered_data=''
 64  
 65      while read -r; do
 66          # Search for the first Vader output line.
 67          # We can try starting tests again if they don't start.
 68          if ((!hit_first_vader_line)); then
 69              if [[ "$REPLY" = *'Starting Vader:'* ]]; then
 70                  hit_first_vader_line=1
 71              fi
 72          fi
 73  
 74          if ((!start_output)); then
 75              if ((hit_first_vader_line)); then
 76                  start_output=1
 77              else
 78                  continue
 79              fi
 80          fi
 81  
 82          if ((quiet)); then
 83              if [[ "$REPLY" = *'Starting Vader:'* ]]; then
 84                  filtered_data="$REPLY"
 85              elif [[ "$REPLY" = *'Success/Total'* ]]; then
 86                  success="$(echo -n "$REPLY" | grep -o '[0-9]\+/' | head -n1 | cut -d/ -f1)"
 87                  total="$(echo -n "$REPLY" | grep -o '/[0-9]\+' | head -n1 | cut -d/ -f2)"
 88  
 89                  if [ "$success" -lt "$total" ]; then
 90                      echo "$filtered_data"
 91                      echo "$REPLY"
 92                  fi
 93  
 94                  filtered_data=''
 95              else
 96                  filtered_data="$filtered_data"$'\n'"$REPLY"
 97              fi
 98          else
 99              echo "$REPLY"
100          fi
101      done
102  
103      # Note that we managed to get the Vader tests started if we did.
104      if ((hit_first_vader_line)); then
105          echo 1 > "$run_file"
106      fi
107  }
108  
109  function color-vader-output() {
110      while read -r; do
111          if [[ "$REPLY" = *'[EXECUTE] (X)'* ]]; then
112              echo -en "$red"
113          elif [[ "$REPLY" = *'[EXECUTE]'* ]] || [[ "$REPLY" = *'[  GIVEN]'* ]]; then
114              echo -en "$nc"
115          fi
116  
117          if [[ "$REPLY" = *'Success/Total'* ]]; then
118              success="$(echo -n "$REPLY" | grep -o '[0-9]\+/' | head -n1 | cut -d/ -f1)"
119              total="$(echo -n "$REPLY" | grep -o '/[0-9]\+' | head -n1 | cut -d/ -f2)"
120  
121              if [ "$success" -lt "$total" ]; then
122                  echo -en "$red"
123              else
124                  echo -en "$green"
125              fi
126  
127              echo "$REPLY"
128              echo -en "$nc"
129          else
130              echo "$REPLY"
131          fi
132      done
133  
134      echo -en "$nc"
135  }
136  
137  echo
138  echo '========================================'
139  echo "Running tests for $vim"
140  echo '========================================'
141  echo
142  
143  tries=0
144  
145  while [ "$tries" -lt 5 ]; do
146      tries=$((tries + 1))
147  
148      exit_code=0
149      set -o pipefail
150      # shellcheck disable=SC2086
151      docker run -a stderr -e VADER_OUTPUT_FILE=/dev/stderr "${docker_flags[@]}" \
152          "/vim-build/bin/$vim" -u test/vimrc ${headless} \
153          "+Vader! $tests" 2>&1 | filter-vader-output | color-vader-output || exit_code=$?
154      set +o pipefail
155  
156      if [ -s "$run_file" ]; then
157          break
158      fi
159  done
160  
161  if [ "$tries" -gt 1 ]; then
162      echo
163      echo "Tried to run tests $tries times"
164  fi
165  
166  rm "$run_file"
167  
168  exit "$exit_code"