__main__.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. # set robot settings path
  22. os.environ['SEARXNG_SETTINGS_PATH'] = str(tests_path / 'robot' / 'settings_robot.yml')
  23. # run the server
  24. self.server = subprocess.Popen( # pylint: disable=consider-using-with
  25. [exe, webapp], stdout=subprocess.PIPE, stderr=subprocess.STDOUT
  26. )
  27. if hasattr(self.server.stdout, 'read1'):
  28. print(self.server.stdout.read1(1024).decode())
  29. def tearDown(self):
  30. os.kill(self.server.pid, 9)
  31. # remove previously set environment variable
  32. del os.environ['SEARXNG_SETTINGS_PATH']
  33. def run_robot_tests(tests):
  34. print('Running {0} tests'.format(len(tests)))
  35. print(f'{shutil.which("geckodriver")}')
  36. print(f'{shutil.which("firefox")}')
  37. for test in tests:
  38. with Browser('firefox', headless=True, profile_preferences={'intl.accept_languages': 'en'}) as browser:
  39. test(browser)
  40. def main():
  41. test_layer = SearxRobotLayer()
  42. try:
  43. test_layer.setUp()
  44. run_robot_tests([getattr(test_webapp, x) for x in dir(test_webapp) if x.startswith('test_')])
  45. except Exception: # pylint: disable=broad-except
  46. print('Error occurred: {0}'.format(traceback.format_exc()))
  47. sys.exit(1)
  48. finally:
  49. test_layer.tearDown()
  50. if __name__ == '__main__':
  51. main()