Poor man's XPath library https://pagure.io/shellfu-bash-pxpath

TF_RUN 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. #!/bin/bash
  2. #shellcheck disable=SC2016,SC1090
  3. . "$TF_DIR/include/subtest.sh"
  4. . "$TF_DIR/include/tools.sh"
  5. PRETTY=plain
  6. . "$(sfpath)" || tf_exit_error "failed to init shellfu"
  7. shellfu import pxpath || tf_exit_error "failed to import pxpath"
  8. tf_enum_subtests() {
  9. echo elem_abs
  10. echo elem_rel
  11. echo attr
  12. echo inv_xpath
  13. echo usage_noargs
  14. echo usage_1arg
  15. echo io_nofile
  16. echo io_pipe
  17. #FIXME: missing coverage: more usage/xml/io errors, [] expressions, namespaces...
  18. }
  19. mkxml() {
  20. echo '<benchmark>'
  21. echo ' <testresult>'
  22. echo ' <foo id="one">'
  23. echo ' <bar>pass</bar>'
  24. echo ' </foo>'
  25. echo ' <foo id="two">'
  26. echo ' <bar>none</bar>'
  27. echo ' </foo>'
  28. echo ' <foo id="three">'
  29. echo ' <bar>pass</bar>'
  30. echo ' </foo>'
  31. echo ' </testresult>'
  32. echo '</benchmark>'
  33. }
  34. mka() {
  35. local what=$1
  36. case $name:$what in
  37. elem_abs:xpath)
  38. echo '/benchmark/testresult/foo'
  39. ;;
  40. elem_abs:o_out)
  41. echo '<foo id="one">'
  42. echo ' <bar>pass</bar>'
  43. echo ' </foo>'
  44. echo '<foo id="two">'
  45. echo ' <bar>none</bar>'
  46. echo ' </foo>'
  47. echo '<foo id="three">'
  48. echo ' <bar>pass</bar>'
  49. echo ' </foo>'
  50. ;;
  51. elem_rel:xpath)
  52. echo '//foo/bar'
  53. ;;
  54. elem_rel:o_out)
  55. echo '<bar>pass</bar>'
  56. echo '<bar>none</bar>'
  57. echo '<bar>pass</bar>'
  58. ;;
  59. attr:xpath)
  60. echo '//foo/@id'
  61. ;;
  62. attr:o_out)
  63. echo 'one'
  64. echo 'two'
  65. echo 'three'
  66. ;;
  67. inv_xpath:xpath)
  68. echo '//foo@id'
  69. ;;
  70. inv_xpath:o_err)
  71. echo 'Invalid expression'
  72. echo 'Traceback (most recent call last):'
  73. echo ' File _SOME_FILE_, line _SOME_LINE_, in <module>'
  74. echo ' matches = context.xpathEvalExpression(expr)'
  75. echo ' File _SOME_FILE_, line _SOME_LINE_, in xpathEvalExpression'
  76. echo " if ret is None:raise xpathError('xmlXPathEvalExpression() failed')"
  77. echo 'libxml2.xpathError: xmlXPathEvalExpression() failed'
  78. ;;
  79. inv_xpath:o_es)
  80. echo 1
  81. ;;
  82. usage_*:o_err)
  83. echo 'usage: __pxpath__usage usage: pxpath [-f FMT] EXPR FILE [NSNAME:NSURI]'
  84. ;;
  85. usage_*:o_es)
  86. echo 2
  87. ;;
  88. io_nofile:o_err)
  89. echo 'no such file, ignoring query: nonexistent.xml'
  90. ;;
  91. io_nofile:o_es)
  92. echo 1
  93. ;;
  94. io_pipe:o_out)
  95. echo '<bar>pass</bar>'
  96. echo '<bar>none</bar>'
  97. echo '<bar>pass</bar>'
  98. ;;
  99. *:o_out) true ;;
  100. *:o_err) true ;;
  101. *:o_es) echo 0 ;;
  102. *:xpath) echo '//foo/bar' ;;
  103. *:*) tf_exit_error "bad test name or part name: $name:$what" ;;
  104. esac
  105. }
  106. runtest() {
  107. local xpath
  108. local r_es=0
  109. mkdir -p "test" "result/$name"
  110. mkxml > "test.xml"
  111. xpath=$(mka xpath)
  112. case $name in
  113. usage_1arg) pxpath "$xpath" ;;
  114. usage_noargs) pxpath ;;
  115. io_nofile) pxpath "$xpath" "nonexistent.xml" ;;
  116. io_adir) pxpath "$xpath" oracle ;;
  117. io_pipe) <"test.xml" pxpath "$xpath" - ;;
  118. *) pxpath "$xpath" "test.xml" ;;
  119. esac >out.cache 2>err.cache; r_es=$?
  120. cat out.cache
  121. <err.cache sed '
  122. /^ File /s/File ".*",/File _SOME_FILE_,/
  123. /^ File /s/line [1-9][0-9]*,/line _SOME_LINE_,/
  124. ' >&2
  125. return $r_es
  126. }
  127. tf_do_subtest() {
  128. local name=$1
  129. local o_out=oracle/$name.stdout
  130. local o_err=oracle/$name.stderr
  131. mka o_out >"$o_out"
  132. mka o_err >"$o_err"
  133. o_es=$(mka o_es)
  134. tf_testflt -n "$name" -E "$o_err" -O "$o_out" -S "$o_es" runtest
  135. }
  136. mkdir -p oracle
  137. tf_do_subtests