jattool-runtest 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #!/bin/bash
  2. #shellcheck disable=SC1090,SC2164
  3. . "$(sfpath)" || exit 3
  4. shellfu import pretty
  5. usage() {
  6. mkusage "$@" "[options] TESTDIR" \
  7. -- \
  8. "Initialize session and run JAT test." \
  9. "" \
  10. "Copy whole directory into temp, launch it there under watchdog,"\
  11. "and after the test finishes, collect any relics back into" \
  12. "session directory." \
  13. "" \
  14. -o \
  15. "-k Keep run directory." \
  16. "-t TIMEOUT Kill test after TIMEOUT seconds. Default: 0," \
  17. " which disables the timeout. " \
  18. -- \
  19. "Test body should consist of asserts and/or phases documented" \
  20. "in jat.sh Shellfu module; use \`sfdoc jat\` to access its" \
  21. "documentation."
  22. }
  23. runtest() {
  24. #
  25. # Wrap in boilerplate and run test code in $1
  26. #
  27. JAT__TEST_ID=$TestId \
  28. JAT__TEST_VERSION=$TestVersion \
  29. jattool qrun "./test"
  30. }
  31. JATS__PATH=${JATS__PATH:-/usr/share/jat/suite}
  32. tlocate() {
  33. #
  34. # Locate test $TestId under known paths
  35. #
  36. debug -v JATS__PATH
  37. local trypath
  38. for trypath in ${JATS__PATH//:/ }; do
  39. debug -v trypath
  40. test -n "$trypath" || continue
  41. test -d "$trypath" || continue
  42. case $(jattool tfind -f ALL "$trypath" | grep -c " $TestId$") in
  43. 1) jattool tfind "$trypath"; return 0 ;;
  44. *) continue ;;
  45. esac
  46. done
  47. return 3
  48. }
  49. main() {
  50. local TestHome
  51. local TestTmp
  52. local TestId
  53. local TestVersion
  54. local Timeout
  55. local keeptmp=false
  56. local es=0
  57. while true; do case $1 in
  58. -k) keeptmp=true; shift ;;
  59. -t) Timeout=$2; shift 2 || usage -w "missing TIMEOUT" ;;
  60. -*) usage ;;
  61. *) break ;;
  62. esac done
  63. test -n "$Timeout" && die "sorry TIMEOUT is not supported yet"
  64. case $1 in
  65. "")
  66. usage -w "no TESTDIR or TESTID?"
  67. ;;
  68. jats://*)
  69. TestId=$1
  70. TestHome=$(tlocate) || die "could not locate test: $TestId"
  71. ;;
  72. */*)
  73. TestHome=$1
  74. TestId=$(jattool tfind -f id "$TestHome") || {
  75. warn "could not find test id, using pathname: $TestHome"
  76. TestId="jats://_jats_no_suite_/$TestHome"
  77. }
  78. ;;
  79. esac
  80. test -f "$TestHome/test" \
  81. || die "test body not found: $TestHome/test"
  82. TestVersion=$(jattool tfind -f version "$TestHome")
  83. TestTmp=$(mktemp -dt jattool-runtest.XXXXXXXX)
  84. debug -v TestHome TestId TestTmp
  85. cp -ar "$TestHome"/* "$TestTmp" \
  86. || die "could not copy test to tempdir"
  87. pushd "$TestTmp" >/dev/null
  88. runtest; es=$?
  89. popd >/dev/null
  90. $keeptmp || rm -rf "$TestTmp"
  91. $keeptmp && warn "leaving temporary directory behind: $TestTmp"
  92. return $es
  93. }
  94. main "$@"