__main__.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. """Shared testing code."""
  3. # pylint: disable=missing-function-docstring
  4. import sys
  5. import os
  6. import subprocess
  7. import traceback
  8. import pathlib
  9. from splinter import Browser
  10. import tests as searx_tests
  11. from tests.robot import test_webapp
  12. class SearxRobotLayer:
  13. """Searx Robot Test Layer"""
  14. def setUp(self):
  15. os.setpgrp() # create new process group, become its leader
  16. tests_path = pathlib.Path(searx_tests.__file__).resolve().parent
  17. # get program paths
  18. webapp = str(tests_path.parent / 'searx' / 'webapp.py')
  19. exe = 'python'
  20. # The Flask app is started by Flask.run(...), don't enable Flask's debug
  21. # mode, the debugger from Flask will cause wired process model, where
  22. # the server never dies. Further read:
  23. #
  24. # - debug mode: https://flask.palletsprojects.com/quickstart/#debug-mode
  25. # - Flask.run(..): https://flask.palletsprojects.com/api/#flask.Flask.run
  26. os.environ['SEARXNG_DEBUG'] = '0'
  27. # set robot settings path
  28. os.environ['SEARXNG_SETTINGS_PATH'] = str(tests_path / 'robot' / 'settings_robot.yml')
  29. # run the server
  30. self.server = subprocess.Popen( # pylint: disable=consider-using-with
  31. [exe, webapp], stdout=subprocess.PIPE, stderr=subprocess.STDOUT
  32. )
  33. if hasattr(self.server.stdout, 'read1'):
  34. print(self.server.stdout.read1(1024).decode())
  35. def tearDown(self):
  36. os.kill(self.server.pid, 9)
  37. # remove previously set environment variable
  38. del os.environ['SEARXNG_SETTINGS_PATH']
  39. def run_robot_tests(tests):
  40. print('Running {0} tests'.format(len(tests)))
  41. for test in tests:
  42. with Browser('firefox', headless=True, profile_preferences={'intl.accept_languages': 'en'}) as browser:
  43. test(browser)
  44. def main():
  45. test_layer = SearxRobotLayer()
  46. try:
  47. test_layer.setUp()
  48. run_robot_tests([getattr(test_webapp, x) for x in dir(test_webapp) if x.startswith('test_')])
  49. except Exception: # pylint: disable=broad-except
  50. print('Error occurred: {0}'.format(traceback.format_exc()))
  51. sys.exit(1)
  52. finally:
  53. test_layer.tearDown()
  54. if __name__ == '__main__':
  55. main()