jattool-runtest 2.9KB

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