__main__.py 2.4 KB

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