jattool-runtest 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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/jats/suite}
  32. tlocate() {
  33. #
  34. # Locate test $TestId under known paths
  35. #
  36. debug -v JATS__PATH
  37. local trypath
  38. local tmatches
  39. for trypath in ${JATS__PATH//:/ }; do
  40. debug -v trypath
  41. test -n "$trypath" || continue
  42. test -d "$trypath" || continue
  43. tmatches=$(jattool tfind -f ALL "$trypath" | grep "^$TestId " )
  44. case $(wc -l <<<"$tmatches") in
  45. 1) cut -d' ' -f3 <<<"$tmatches"; return 0 ;;
  46. *) continue ;;
  47. esac
  48. done
  49. return 3
  50. }
  51. main() {
  52. local TestHome
  53. local TestTmp
  54. local TestId
  55. local TestVersion
  56. local Timeout
  57. local keeptmp=false
  58. local es=0
  59. while true; do case $1 in
  60. -k) keeptmp=true; shift ;;
  61. -t) Timeout=$2; shift 2 || usage -w "missing TIMEOUT" ;;
  62. -*) usage ;;
  63. *) break ;;
  64. esac done
  65. test -n "$Timeout" && die "sorry TIMEOUT is not supported yet"
  66. case $1 in
  67. "")
  68. usage -w "no TESTDIR or TESTID?"
  69. ;;
  70. jats://*)
  71. TestId=$1
  72. TestHome=$(tlocate) || die "could not locate test: $TestId"
  73. ;;
  74. */*)
  75. TestHome=$1
  76. TestId=$(jattool tfind -f id "$TestHome") || {
  77. warn "could not find test id, using pathname: $TestHome"
  78. TestId="jats://_jats_no_suite_/$TestHome"
  79. }
  80. ;;
  81. esac
  82. test -f "$TestHome/test" \
  83. || die "test body not found: $TestHome/test"
  84. TestVersion=$(jattool tfind -f version "$TestHome")
  85. TestTmp=$(mktemp -dt jattool-runtest.XXXXXXXX)
  86. debug -v TestHome TestId TestTmp
  87. cp -ar "$TestHome"/* "$TestTmp" \
  88. || die "could not copy test to tempdir"
  89. pushd "$TestTmp" >/dev/null
  90. runtest; es=$?
  91. popd >/dev/null
  92. $keeptmp || rm -rf "$TestTmp"
  93. $keeptmp && warn "leaving temporary directory behind: $TestTmp"
  94. return $es
  95. }
  96. main "$@"