lib_sxng_test.sh 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #!/usr/bin/env bash
  2. # SPDX-License-Identifier: AGPL-3.0-or-later
  3. test.help() {
  4. cat <<EOF
  5. test.:
  6. yamllint : lint YAML files (YAMLLINT_FILES)
  7. pylint : lint ./searx, ./searxng_extra and ./tests
  8. pyright : check Python types
  9. black : check Python code format
  10. shfmt : check Shell script code format
  11. unit : run unit tests
  12. coverage : run unit tests with coverage
  13. robot : run robot test
  14. rst : test .rst files incl. README.rst
  15. clean : clean intermediate test stuff
  16. EOF
  17. }
  18. if [ "$VERBOSE" = "1" ]; then
  19. TEST_NOSE2_VERBOSE="-vvv"
  20. fi
  21. test.yamllint() {
  22. build_msg TEST "[yamllint] $YAMLLINT_FILES"
  23. pyenv.cmd yamllint --strict --format parsable "${YAMLLINT_FILES[@]}"
  24. dump_return $?
  25. }
  26. test.pylint() {
  27. (
  28. set -e
  29. pyenv.activate
  30. PYLINT_OPTIONS="--rcfile .pylintrc"
  31. build_msg TEST "[pylint] ./searx/engines"
  32. # shellcheck disable=SC2086
  33. pylint ${PYLINT_OPTIONS} ${PYLINT_VERBOSE} \
  34. --additional-builtins="traits,supported_languages,language_aliases,logger,categories" \
  35. searx/engines
  36. build_msg TEST "[pylint] ./searx ./searxng_extra ./tests"
  37. # shellcheck disable=SC2086
  38. pylint ${PYLINT_OPTIONS} ${PYLINT_VERBOSE} \
  39. --ignore=searx/engines \
  40. searx searx/searxng.msg \
  41. searxng_extra searxng_extra/docs_prebuild \
  42. tests
  43. )
  44. dump_return $?
  45. }
  46. test.pyright() {
  47. # use this pyright test for local tests in development / it suppress
  48. # warnings related to intentional monkey patching but gives good hints where
  49. # we need to work on SearXNG's typification.
  50. build_msg TEST "[pyright/types] static type check of python sources"
  51. node.env.dev
  52. build_msg TEST "[pyright/types] suppress warnings related to intentional monkey patching"
  53. # We run Pyright in the virtual environment because pyright executes
  54. # "python" to determine the Python version.
  55. pyenv.cmd npx --no-install pyright -p pyrightconfig.json |
  56. grep -E '\.py:[0-9]+:[0-9]+' |
  57. grep -v '/engines/.*.py.* - warning: "logger" is not defined' |
  58. grep -v '/plugins/.*.py.* - error: "logger" is not defined' |
  59. grep -v '/engines/.*.py.* - warning: "supported_languages" is not defined' |
  60. grep -v '/engines/.*.py.* - warning: "language_aliases" is not defined' |
  61. grep -v '/engines/.*.py.* - warning: "categories" is not defined'
  62. # ignore exit value from pyright
  63. # dump_return ${PIPESTATUS[0]}
  64. return 0
  65. }
  66. test.black() {
  67. build_msg TEST "[black] $BLACK_TARGETS"
  68. pyenv.cmd black --check --diff "${BLACK_OPTIONS[@]}" "${BLACK_TARGETS[@]}"
  69. dump_return $?
  70. }
  71. test.shfmt() {
  72. build_msg TEST "[shfmt] ${SHFMT_SCRIPTS[*]}"
  73. go.tool shfmt --list --diff "${SHFMT_SCRIPTS[@]}"
  74. dump_return $?
  75. }
  76. test.unit() {
  77. build_msg TEST 'tests/unit'
  78. # shellcheck disable=SC2086
  79. pyenv.cmd python -m nose2 ${TEST_NOSE2_VERBOSE} -s tests/unit
  80. dump_return $?
  81. }
  82. test.coverage() {
  83. build_msg TEST 'unit test coverage'
  84. (
  85. set -e
  86. pyenv.activate
  87. # shellcheck disable=SC2086
  88. python -m nose2 ${TEST_NOSE2_VERBOSE} -C --log-capture --with-coverage --coverage searx -s tests/unit
  89. coverage report
  90. coverage html
  91. )
  92. dump_return $?
  93. }
  94. test.robot() {
  95. build_msg TEST 'robot'
  96. gecko.driver
  97. PYTHONPATH=. pyenv.cmd python -m tests.robot
  98. dump_return $?
  99. }
  100. test.rst() {
  101. build_msg TEST "[reST markup] ${RST_FILES[*]}"
  102. for rst in "${RST_FILES[@]}"; do
  103. pyenv.cmd rst2html --halt error "$rst" >/dev/null || die 42 "fix issue in $rst"
  104. done
  105. }
  106. test.themes() {
  107. build_msg TEST 'SearXNG themes'
  108. themes.test
  109. dump_return $?
  110. }
  111. test.pybabel() {
  112. TEST_BABEL_FOLDER="build/test/pybabel"
  113. build_msg TEST "[extract messages] pybabel"
  114. mkdir -p "${TEST_BABEL_FOLDER}"
  115. pyenv.cmd pybabel extract -F babel.cfg -o "${TEST_BABEL_FOLDER}/messages.pot" searx
  116. }
  117. test.clean() {
  118. build_msg CLEAN "test stuff"
  119. rm -rf geckodriver.log .coverage coverage/
  120. dump_return $?
  121. }