Working Saturnin-based meta-command

harness.sh 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 artifact if exit status suggests it
  8. #
  9. # Use test exit status $1 to help decide if artifacts 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 artifacts?
  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 "$artifact_dir/$stamp"
  29. cp -r "$tmpdir"/* "$artifact_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 artifact name
  88. local tf_dir tf_suite # to keep absolute paths for TF_RUN
  89. local artifact_dir # where to keep artifacts
  90. artifact_dir="$(readlink -f "$TF_ARTIFACTS")"
  91. __tf_header
  92. tf_debug "TF_VERSION='$TF_VERSION'"
  93. tf_dir="$(readlink -f "$TF_DIR")"
  94. tf_suite="$(readlink -f "$TF_SUITE")"
  95. es=0
  96. for tname in $(tf_enum_tests | grep -e "$TF_FILTER_TEST");
  97. do
  98. tf_think "... $tname"
  99. tmpdir=$(mktemp -d)
  100. stamp=$(date "+artifacts-$tname-%Y%m%d-%H%M%S")
  101. cp -r "$TF_SUITE/$tname/"* "$tmpdir"
  102. pushd "$tmpdir" >/dev/null
  103. TF_DIR="$tf_dir" TF_SUITE=$tf_suite TF_TEST="$tname" \
  104. ./TF_RUN
  105. tes=$?
  106. __tf_collect_if_needed $tes
  107. test $tes -gt $es && es=$tes
  108. popd >/dev/null
  109. rm -rf "$tmpdir"
  110. if test $tes -eq 0;
  111. then
  112. tf_think "''' $tname ($tes)"
  113. else
  114. tf_warn "??? $tname ($tes)"
  115. fi
  116. done
  117. return $es
  118. }