jattool-tfind 1.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #!/bin/bash
  2. #shellcheck disable=SC1090,SC2164
  3. . "$(sfpath)" || exit 3
  4. shellfu import pretty
  5. usage() {
  6. mkusage "$@" "[options] PATH" \
  7. -- \
  8. "Discover all JATS tests under directory PATH." \
  9. -o \
  10. "-f FMT Print test name in format FMT. 'jats' or 'path'."
  11. }
  12. discover() {
  13. #
  14. # Find tests under dirs ${FindDirs[@]}
  15. #
  16. local s_dir # suite root dir
  17. local t_cand # possible test path
  18. local t_apath # absolute path to test directory
  19. local t_rpath # relative path to test directory within $s_dir
  20. local s_id # suite's jats: id
  21. find "${FindDirs[@]}" -name test \
  22. | while read -r t_cand; do
  23. s_dir=$(straverse "$t_cand") || continue
  24. t_apath=$(dirname "$(readlink -f "$t_cand")")
  25. t_rpath=${t_apath#$s_dir/}
  26. s_id=$(jattool sfind -f jats "$s_dir")
  27. debug -v s_dir s_id t_apath t_rpath
  28. case $Fmt in
  29. path) echo "$s_dir/$t_rpath" ;;
  30. jats) echo "$s_id$t_rpath" ;;
  31. esac
  32. done
  33. }
  34. straverse() {
  35. #
  36. # Traverse back from test dir $1 and print its suite dir
  37. #
  38. local tdir=$1
  39. local dir
  40. dir=$(readlink -f "$tdir")
  41. while true; do
  42. test -d "$dir/.jats" && echo "$dir" && return 0
  43. test "$dir" == "/" && return 1
  44. dir=${dir%/*}; test -n "$dir" || dir=/
  45. done
  46. }
  47. main() {
  48. local FindDirs=()
  49. local Fmt=path
  50. while true; do case $1 in
  51. -f) Fmt=$2; shift 2 || usage -w "missing FMT" ;;
  52. -*) usage -w "unknown argument: $1" ;;
  53. *) break ;;
  54. esac done
  55. FindDirs=("$@")
  56. case $Fmt in
  57. jats|path) : ;;
  58. *) usage -w "unknown FMT: $Fmt" ;;
  59. esac
  60. debug -v FindDirs Fmt
  61. discover | grep .
  62. }
  63. main "$@"