shell dot on steroids https://pagure.io/shellfu

harness.sh 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #!/bin/bash
  2. #shellcheck disable=SC1090
  3. # tfkit - Shellfu's movable test framework
  4. # See LICENSE file for copyright and license details.
  5. #shellcheck disable=SC2153
  6. . "$TF_DIR/include/common.sh"
  7. __tf_collect_if_needed() {
  8. #
  9. # Collect relic if exit status suggests it
  10. #
  11. # Use test exit status $1 to help decide if relics are
  12. # needed, and collect them if so.
  13. #
  14. # If TF_COLLECT is set to "always", collect regardless of
  15. # the status. If set to "never", do not collect anything.
  16. # The default setting, "auto" collects unless status is 0
  17. # or 1 (pass or bailout); in that case do nothing.
  18. #
  19. local tes=$1 # test exit status
  20. local will # should we collect relics?
  21. case "$TF_COLLECT:$tes" in
  22. always:*) will=true ;;
  23. never:*) will=false ;;
  24. auto:0) will=false ;;
  25. auto:2) will=false ;;
  26. auto:*) will=true ;;
  27. *) tf_exit_bailout "bad value of TF_COLLECT: $TF_COLLECT" ;;
  28. esac
  29. $will || return 0
  30. mkdir -p "$relic_dir/$stamp"
  31. cp -r "$tmpdir"/* "$relic_dir/$stamp"
  32. }
  33. __tf_header() {
  34. #
  35. # create header to add to output before test
  36. #
  37. local hdrline # each line from generator
  38. local hdrgen="$TF_SUITE/TF_HEADER" # header generator script
  39. tf_think "#"
  40. tf_think "# ---"
  41. tf_think "# tfkit:"
  42. if test -x "$hdrgen";
  43. then
  44. tf_think "# sut:"
  45. $hdrgen \
  46. | while IFS= read -r hdrline;
  47. do
  48. test -n "$hdrline" || break
  49. tf_think "# $hdrline"
  50. done
  51. else
  52. tf_think "# hint: >"
  53. tf_think "# Add $hdrgen executable for own header."
  54. tf_think "# It should output YAML (\"key: value\" pairs) about"
  55. tf_think "# your SUT ('version' at least')."
  56. fi
  57. tf_think "# run:"
  58. tf_think "# start_time: $(date -Iseconds)"
  59. test -n "$TF_FILTER_TEST" \
  60. && tf_think "# TF_FILTER_TEST: $TF_FILTER_TEST"
  61. test -n "$TF_FILTER_SUBTEST" \
  62. && tf_think "# TF_FILTER_SUBTEST: $TF_FILTER_SUBTEST"
  63. tf_think "#"
  64. tf_think ""
  65. }
  66. tf_enum_tests() {
  67. #
  68. # List what looks like test; relative to $TF_SUITE
  69. #
  70. tf_debug "TF_SUITE='$TF_SUITE'"
  71. test -d "$TF_SUITE" || return 0
  72. find -L \
  73. "$TF_SUITE" \
  74. -mindepth 2 \
  75. -maxdepth 2 \
  76. -type f \
  77. -perm /111 \
  78. -name TF_RUN \
  79. | cut -d/ -f2
  80. }
  81. tf_run_tests() {
  82. #
  83. # Discover and run tests
  84. #
  85. local es=0 # overall exit status
  86. local tmpdir="" # test temporary dir
  87. local tname="" # test name
  88. local tes=0 # test result
  89. local stamp="" # test stamp to use as relic name
  90. local tf_dir # to keep absolute path for TF_RUN
  91. local relic_dir # where to keep relics
  92. relic_dir="$(readlink -f "$TF_RELICS")"
  93. __tf_header
  94. tf_debug "TF_VERSION='$TF_VERSION'"
  95. tf_dir="$(readlink -f "$TF_DIR")"
  96. es=0
  97. for tname in $(tf_enum_tests | grep -e "$TF_FILTER_TEST");
  98. do
  99. tf_think "... $tname"
  100. tmpdir=$(mktemp -d)
  101. stamp=$(date "+relics-$tname-%Y%m%d-%H%M%S")
  102. cp -r "$TF_SUITE/$tname/"* "$tmpdir"
  103. pushd "$tmpdir" >/dev/null || tf_exit_panic "could not chdir to: $tmpdir"
  104. TF_DEBUG=$TF_DEBUG TF_VERBOSE=$TF_VERBOSE \
  105. TF_DIR="$tf_dir" TF_TEST="$tname" \
  106. TF_FILTER_SUBTEST=$TF_FILTER_SUBTEST \
  107. ./TF_RUN
  108. tes=$?
  109. __tf_collect_if_needed $tes
  110. test $tes -gt $es && es=$tes
  111. popd >/dev/null || tf_exit_panic "could not chdir from: $tmpdir to $OLDPWD"
  112. rm -rf "$tmpdir"
  113. if test $tes -eq 0;
  114. then
  115. tf_think "''' $tname ($tes)"
  116. else
  117. tf_warn "??? $tname ($tes)"
  118. fi
  119. done
  120. return $es
  121. }