123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. #
  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 "hint = Add $__TF_HDRCMD executable for own header."
  40. echo "hint = It should output 'key = value' pairs about"
  41. echo "hint = your SUT ('version' at least')."
  42. }
  43. __tf_header() {
  44. #
  45. # create header to add to output before test
  46. #
  47. local field
  48. local hdrcmd="$__TF_HDRCMD"
  49. test -x "$hdrcmd" || hdrcmd="__tf_default_header"
  50. tf_think "#"
  51. tf_think "# [tfkit.sut]"
  52. tf_think "#"
  53. $hdrcmd \
  54. | while read field;
  55. do
  56. test -n "$field" || break
  57. tf_think "# $field"
  58. done
  59. tf_think "#"
  60. tf_think "# [tfkit.run]"
  61. tf_think "#"
  62. tf_think "# start_time = $(date -Iseconds)"
  63. test "$TF_FILTER_TEST" = '.*' \
  64. || tf_think "# TF_FILTER_TEST = $TF_FILTER_TEST"
  65. test "$TF_FILTER_SUBTEST" = '.*' \
  66. || tf_think "# TF_FILTER_SUBTEST = $TF_FILTER_SUBTEST"
  67. tf_think "#"
  68. tf_think ""
  69. }
  70. tf_enum_tests() {
  71. #
  72. # List what looks like test; relative to $TF_SUITE
  73. #
  74. tf_debug "TF_SUITE='$TF_SUITE'"
  75. test -d "$TF_SUITE" || return 0
  76. find -L \
  77. "$TF_SUITE" \
  78. -mindepth 2 \
  79. -maxdepth 2 \
  80. -type f \
  81. -perm /111 \
  82. -name TF_RUN \
  83. | cut -d/ -f2
  84. }
  85. tf_run_tests() {
  86. #
  87. # Discover and run tests
  88. #
  89. local es=0 # overall exit status
  90. local tmpdir="" # test temporary dir
  91. local tname="" # test name
  92. local tes=0 # test result
  93. local stamp="" # test stamp to use as artifact name
  94. local tf_dir tf_suite # to keep absolute paths for TF_RUN
  95. local artifact_dir="$(readlink -f "$TF_ARTIFACTS")"
  96. __tf_header
  97. tf_debug "TF_VERSION='$TF_VERSION'"
  98. tf_dir="$(readlink -f "$TF_DIR")"
  99. tf_suite="$(readlink -f "$TF_SUITE")"
  100. es=0
  101. for tname in $(tf_enum_tests | grep -e "$TF_FILTER_TEST");
  102. do
  103. tf_think "... $tname"
  104. tmpdir=$(mktemp -d)
  105. stamp=$(date "+artifacts-$tname-%Y%m%d-%H%M%S")
  106. cp -r "$TF_SUITE/$tname/"* "$tmpdir"
  107. pushd "$tmpdir" >/dev/null
  108. TF_DIR="$tf_dir" TF_SUITE=$tf_suite TF_TNAME="$tname" \
  109. ./TF_RUN
  110. tes=$?
  111. __tf_collect_if_needed $tes
  112. test $tes -gt $es && es=$tes
  113. popd >/dev/null
  114. rm -rf "$tmpdir"
  115. if test $tes -eq 0;
  116. then
  117. tf_think "''' $tname ($tes)"
  118. else
  119. tf_warn "??? $tname ($tes)"
  120. fi
  121. done
  122. return $es
  123. }