example_check.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. # -*- coding: utf-8 -*-
  2. """
  3. This file is mean to be called with a path to an example directory as
  4. its argument. We import the application entry point for the example
  5. and add instrument them with a puppeteer test that makes sure
  6. there are no console errors or uncaught errors prior to a sentinel
  7. string being printed.
  8. e.g. python example_check.py ./app
  9. """
  10. import importlib.util
  11. import logging
  12. from os import path as osp
  13. import os
  14. import shutil
  15. import sys
  16. from tornado.ioloop import IOLoop
  17. from traitlets import Bool, Unicode
  18. from jupyterlab.labapp import get_app_dir
  19. from jupyterlab.browser_check import run_test, run_async_process
  20. here = osp.abspath(osp.dirname(__file__))
  21. def main():
  22. # Load the main file and grab the example class so we can subclass
  23. example_dir = sys.argv.pop()
  24. mod_path = osp.abspath(osp.join(example_dir, 'main.py'))
  25. spec = importlib.util.spec_from_file_location("example", mod_path)
  26. mod = importlib.util.module_from_spec(spec)
  27. spec.loader.exec_module(mod)
  28. class App(mod.ExampleApp):
  29. """An app that launches an example and waits for it to start up, checking for
  30. JS console errors, JS errors, and Python logged errors.
  31. """
  32. open_browser = Bool(False)
  33. default_url = '/example'
  34. base_url = '/foo/'
  35. ip = '127.0.0.1'
  36. def start(self):
  37. run_test(self, run_browser)
  38. super().start()
  39. App.__name__ = osp.basename(example_dir).capitalize() + 'Test'
  40. App.launch_instance()
  41. async def run_browser(url):
  42. """Run the browser test and return an exit code.
  43. """
  44. target = osp.join(get_app_dir(), 'example_test')
  45. if not osp.exists(osp.join(target, 'node_modules')):
  46. os.makedirs(target)
  47. await run_async_process(["jlpm", "init", "-y"], cwd=target)
  48. await run_async_process(["jlpm", "add", "puppeteer"], cwd=target)
  49. shutil.copy(osp.join(here, 'chrome-example-test.js'), osp.join(target, 'chrome-example-test.js'))
  50. await run_async_process(["node", "chrome-example-test.js", url], cwd=target)
  51. if __name__ == '__main__':
  52. main()