jattool-sfind 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #!/bin/bash
  2. #shellcheck disable=SC1090
  3. . "$(sfpath)" || exit 3
  4. shellfu import pretty
  5. usage() {
  6. mkusage "$@" "[options] BODY" \
  7. -- \
  8. "Discover test in suite located at PATH" \
  9. -o \
  10. "-f FMT Print test name in format FMT. 'jats' or 'path'." \
  11. -- \
  12. "Test body should consist of asserts and/or phases documented" \
  13. "in jat.sh Shellfu module; use \`sfdoc jat\` to access its" \
  14. "documentation."
  15. }
  16. JATS__COMPAT=0.0
  17. sprop() {
  18. #
  19. # Get property $1 of suite at $SuiteDir
  20. #
  21. local pname=$1
  22. local pvalue
  23. local dvalue
  24. local fpath
  25. case $SuiteDir in
  26. .) fpath=.jats/$pname ;;
  27. *) fpath=$SuiteDir/.jats/$pname ;;
  28. esac
  29. case $pname in
  30. domain) dvalue="jats.example.org" ;;
  31. ns) dvalue="anonns" ;;
  32. sut_nicename) dvalue=$(sprop sut_pname) ;;
  33. sut_pname) dvalue="anonsut" ;;
  34. sut_url) dvalue="https://example.org/anonsut" ;;
  35. url) dvalue="https://$(sprop domain)/$(sprop ns)" ;;
  36. format) : ;;
  37. *) warn "unknown property: $pname"; return 2 ;;
  38. esac
  39. pvalue=$(cat "$fpath" 2>/dev/null)
  40. debug -v pname dvalue pvalue
  41. if test -n "$pvalue"; then
  42. echo "$pvalue"
  43. return 0
  44. elif test "$pname" == "format"; then
  45. warn "missing mandatory property: $fpath"
  46. return 3
  47. else
  48. echo "$dvalue"
  49. fi
  50. }
  51. discover() {
  52. #
  53. # Find tests under dirs ${FindDirs[@]}
  54. #
  55. local SuiteDir # each suite root dir
  56. local SuiteFmt # suite format version
  57. find "${FindDirs[@]}" -type d -path '*/.jats' \
  58. | sed 's:/\.jats::' \
  59. | while read -r SuiteDir; do
  60. debug -v SuiteDir
  61. SuiteFmt=$(sprop format) || continue
  62. debug -v SuiteFmt
  63. test "$SuiteFmt" == "$JATS__COMPAT" || continue
  64. SuiteDomain=$(sprop domain)
  65. SuiteNs=$(sprop ns)
  66. SuiteSutPname=$(sprop sut_pname)
  67. case $Fmt in
  68. jats)
  69. echo -n "jats://$SuiteDomain/$SuiteNs"
  70. test -n "$SuiteSutPname" && echo -n "/$SuiteSutPname"
  71. echo
  72. ;;
  73. path)
  74. echo "$SuiteDir"
  75. ;;
  76. esac
  77. done
  78. }
  79. main() {
  80. local SuiteDir
  81. local FindDirs=()
  82. local Fmt=path
  83. while true; do case $1 in
  84. -f) Fmt=$2; shift 2 || usage -w "missing FMT" ;;
  85. -*) usage -w "unknown argument: $1" ;;
  86. *) break ;;
  87. esac done
  88. FindDirs=("$@")
  89. case $Fmt in
  90. jats|path) : ;;
  91. *) usage -w "unknown FMT: $Fmt" ;;
  92. esac
  93. debug -v FindDirs Fmt
  94. discover
  95. }
  96. main "$@"