jattool-tfind 1.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. 'id', 'version' or" \
  11. " 'path'."
  12. }
  13. discover() {
  14. #
  15. # Find tests under dirs ${FindDirs[@]}
  16. #
  17. local s_dir # suite root dir
  18. local t_cand # possible test path
  19. local t_apath # absolute path to test directory
  20. local t_rpath # relative path to test directory within $s_dir
  21. local s_id # suite's jats: id
  22. find "${FindDirs[@]}" -name test \
  23. | while read -r t_cand; do
  24. s_dir=$(straverse "$t_cand") || continue
  25. t_apath=$(dirname "$(readlink -f "$t_cand")")
  26. t_rpath=${t_apath#$s_dir/}
  27. s_id=$(jattool sfind -f id "$s_dir")
  28. debug -v s_dir s_id t_apath t_rpath
  29. case $Fmt in
  30. path) echo "$s_dir/$t_rpath" ;;
  31. id) echo "$s_id$t_rpath" ;;
  32. version) jattool sfind -f version "$s_dir" ;;
  33. esac
  34. done
  35. }
  36. straverse() {
  37. #
  38. # Traverse back from test dir $1 and print its suite dir
  39. #
  40. local tdir=$1
  41. local dir
  42. dir=$(readlink -f "$tdir")
  43. while true; do
  44. test -d "$dir/.jats" && echo "$dir" && return 0
  45. test "$dir" == "/" && return 1
  46. dir=${dir%/*}; test -n "$dir" || dir=/
  47. done
  48. }
  49. main() {
  50. local FindDirs=()
  51. local Fmt=path
  52. while true; do case $1 in
  53. -f) Fmt=$2; shift 2 || usage -w "missing FMT" ;;
  54. -*) usage -w "unknown argument: $1" ;;
  55. *) break ;;
  56. esac done
  57. FindDirs=("$@")
  58. case $Fmt in
  59. id|version|path) : ;;
  60. *) usage -w "unknown FMT: $Fmt" ;;
  61. esac
  62. debug -v FindDirs Fmt
  63. discover | grep .
  64. }
  65. main "$@"