find-suites.sh 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #!/usr/bin/env bash
  2. ## If EMQX_CT_SUITES or SUITES is provided, it prints the variable.
  3. ## Otherwise this script tries to find all test/*_SUITE.erl files of then given app,
  4. ## file names are separated by comma for rebar3 ct's `--suite` option
  5. ## If SUITEGROUP is set as M_N, it prints the Nth chunk of all suites.
  6. ## SUITEGROUP default value is 1_1
  7. set -euo pipefail
  8. # ensure dir
  9. cd -P -- "$(dirname -- "$0")/.."
  10. DIR="$1"
  11. complete_path() {
  12. local filename="$1"
  13. # Check if path prefix is present
  14. if [[ "$filename" != */* ]]; then
  15. filename="$DIR/test/$filename"
  16. fi
  17. # Check if suffix is present
  18. if [[ "$filename" != *.erl ]]; then
  19. filename="$filename.erl"
  20. fi
  21. echo "$filename"
  22. }
  23. ## EMQX_CT_SUITES or SUITES is useful in ad-hoc runs
  24. EMQX_CT_SUITES="${EMQX_CT_SUITES:-${SUITES:-}}"
  25. if [ -n "${EMQX_CT_SUITES:-}" ]; then
  26. OUTPUT=""
  27. IFS=',' read -ra FILE_ARRAY <<< "$EMQX_CT_SUITES"
  28. for file in "${FILE_ARRAY[@]}"; do
  29. path=$(complete_path "$file")
  30. if [ ! -f "$path" ]; then
  31. echo ''
  32. echo "ERROR: '$path' is not a file. Ignored!" >&2
  33. exit 1
  34. fi
  35. if [ -z "$OUTPUT" ]; then
  36. OUTPUT="$path"
  37. else
  38. OUTPUT="$OUTPUT,$path"
  39. fi
  40. done
  41. echo "${OUTPUT}"
  42. exit 0
  43. fi
  44. TESTDIR="$DIR/test"
  45. INTEGRATION_TESTDIR="$DIR/integration_test"
  46. # Get the output of the find command
  47. IFS=$'\n' read -r -d '' -a FILES < <(find "${TESTDIR}" -name "*_SUITE.erl" 2>/dev/null | sort && printf '\0')
  48. if [[ -d "${INTEGRATION_TESTDIR}" ]]; then
  49. IFS=$'\n' read -r -d '' -a FILES_INTEGRATION < <(find "${INTEGRATION_TESTDIR}" -name "*_SUITE.erl" 2>/dev/null | sort && printf '\0')
  50. fi
  51. # shellcheck disable=SC2206
  52. FILES+=(${FILES_INTEGRATION:-})
  53. SUITEGROUP_RAW="${SUITEGROUP:-1_1}"
  54. SUITEGROUP="$(echo "$SUITEGROUP_RAW" | cut -d '_' -f1)"
  55. SUITEGROUP_COUNT="$(echo "$SUITEGROUP_RAW" | cut -d '_' -f2)"
  56. # Calculate the total number of files
  57. FILE_COUNT=${#FILES[@]}
  58. if (( SUITEGROUP > SUITEGROUP_COUNT )); then
  59. echo "Error: SUITEGROUP in the format of M_N, M must be not greater than M"
  60. exit 1
  61. fi
  62. # Calculate the number of files per group
  63. FILES_PER_GROUP=$(( (FILE_COUNT + SUITEGROUP_COUNT - 1) / SUITEGROUP_COUNT ))
  64. START_INDEX=$(( (SUITEGROUP - 1) * FILES_PER_GROUP ))
  65. END_INDEX=$(( START_INDEX + FILES_PER_GROUP ))
  66. # Print the desired suite group
  67. sep=''
  68. for (( i=START_INDEX; i<END_INDEX; i++ )); do
  69. if (( i < FILE_COUNT )); then
  70. echo -n "${sep}${FILES[$i]}"
  71. sep=','
  72. fi
  73. done
  74. echo