lib_sxng_test.sh 4.0 KB

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