Simple Makefile target helper https://pagure.io/mkit

harness.sh 3.0KB

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