jattool-tfind 2.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. s_ver=$(jattool sfind -f version "$s_dir")
  29. t_id=$s_id$t_rpath
  30. t_path=$s_dir/$t_rpath
  31. debug -v s_dir s_id t_apath t_rpath t_id t_path
  32. case $Fmt in
  33. ALL) echo "$t_id $s_ver $t_path" ;;
  34. path) echo "$t_path" ;;
  35. id) echo "$t_id" ;;
  36. version) echo "$s_ver" ;;
  37. esac
  38. done
  39. }
  40. straverse() {
  41. #
  42. # Traverse back from test dir $1 and print its suite dir
  43. #
  44. local tdir=$1
  45. local dir
  46. dir=$(readlink -f "$tdir")
  47. while true; do
  48. test -d "$dir/.jats" && echo "$dir" && return 0
  49. test "$dir" == "/" && return 1
  50. dir=${dir%/*}; test -n "$dir" || dir=/
  51. done
  52. }
  53. main() {
  54. local FindDirs=()
  55. local Fmt=path
  56. while true; do case $1 in
  57. -f) Fmt=$2; shift 2 || usage -w "missing FMT" ;;
  58. -*) usage -w "unknown argument: $1" ;;
  59. *) break ;;
  60. esac done
  61. FindDirs=("$@")
  62. case $Fmt in
  63. id|version|path|ALL) : ;;
  64. *) usage -w "unknown FMT: $Fmt" ;;
  65. esac
  66. debug -v FindDirs Fmt
  67. discover | grep .
  68. }
  69. main "$@"