Working Saturnin-based meta-command

harness.sh 3.7KB

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