test.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #!/usr/bin/env python
  2. import argparse
  3. import os
  4. import shutil
  5. import subprocess
  6. import sys
  7. from lib.config import enable_verbose_mode
  8. from lib.util import electron_gyp, execute_stdout, rm_rf
  9. if sys.platform == 'linux2':
  10. # On Linux we use python-dbusmock to create a fake system bus and test
  11. # powerMonitor interaction with org.freedesktop.login1 service. The
  12. # dbus_mock module takes care of setting up the fake server with mock,
  13. # while also setting DBUS_SYSTEM_BUS_ADDRESS environment variable, which
  14. # will be picked up by electron.
  15. try:
  16. import lib.dbus_mock
  17. except ImportError:
  18. # If not available, the powerMonitor tests will be skipped since
  19. # DBUS_SYSTEM_BUS_ADDRESS will not be set
  20. pass
  21. SOURCE_ROOT = os.path.abspath(os.path.dirname(os.path.dirname(__file__)))
  22. PROJECT_NAME = electron_gyp()['project_name%']
  23. PRODUCT_NAME = electron_gyp()['product_name%']
  24. def main():
  25. os.chdir(SOURCE_ROOT)
  26. args = parse_args()
  27. config = args.configuration
  28. if args.verbose:
  29. enable_verbose_mode()
  30. os.environ['ELECTRON_ENABLE_LOGGING'] = '1'
  31. spec_modules = os.path.join(SOURCE_ROOT, 'spec', 'node_modules')
  32. if args.rebuild_native_modules or not os.path.isdir(spec_modules):
  33. rebuild_native_modules(args.verbose, config)
  34. if sys.platform == 'darwin':
  35. electron = os.path.join(SOURCE_ROOT, 'out', config,
  36. '{0}.app'.format(PRODUCT_NAME), 'Contents',
  37. 'MacOS', PRODUCT_NAME)
  38. resources_path = os.path.join(SOURCE_ROOT, 'out', config,
  39. '{0}.app'.format(PRODUCT_NAME), 'Contents',
  40. 'Resources')
  41. elif sys.platform == 'win32':
  42. electron = os.path.join(SOURCE_ROOT, 'out', config,
  43. '{0}.exe'.format(PROJECT_NAME))
  44. resources_path = os.path.join(SOURCE_ROOT, 'out', config)
  45. else:
  46. electron = os.path.join(SOURCE_ROOT, 'out', config, PROJECT_NAME)
  47. resources_path = os.path.join(SOURCE_ROOT, 'out', config)
  48. returncode = 0
  49. try:
  50. if args.use_instrumented_asar:
  51. install_instrumented_asar_file(resources_path)
  52. os.environ["ELECTRON_DISABLE_SECURITY_WARNINGS"] = "1"
  53. subprocess.check_call([electron, 'spec'] + sys.argv[1:])
  54. except subprocess.CalledProcessError as e:
  55. returncode = e.returncode
  56. except KeyboardInterrupt:
  57. returncode = 0
  58. if args.use_instrumented_asar:
  59. restore_uninstrumented_asar_file(resources_path)
  60. if os.environ.has_key('OUTPUT_TO_FILE'):
  61. output_to_file = os.environ['OUTPUT_TO_FILE']
  62. with open(output_to_file, 'r') as f:
  63. print f.read()
  64. rm_rf(output_to_file)
  65. return returncode
  66. def parse_args():
  67. parser = argparse.ArgumentParser(description='Run Electron tests')
  68. parser.add_argument('--use_instrumented_asar',
  69. help='Run tests with coverage instructed asar file',
  70. action='store_true',
  71. required=False)
  72. parser.add_argument('--rebuild_native_modules',
  73. help='Rebuild native modules used by specs',
  74. action='store_true',
  75. required=False)
  76. parser.add_argument('--ci',
  77. help='Run tests in CI mode',
  78. action='store_true',
  79. required=False)
  80. parser.add_argument('-g', '--grep',
  81. help='Only run tests matching <pattern>',
  82. metavar='pattern',
  83. required=False)
  84. parser.add_argument('-i', '--invert',
  85. help='Inverts --grep matches',
  86. action='store_true',
  87. required=False)
  88. parser.add_argument('-v', '--verbose',
  89. action='store_true',
  90. help='Prints the output of the subprocesses')
  91. parser.add_argument('-c', '--configuration',
  92. help='Build configuration to run tests against',
  93. default='D',
  94. required=False)
  95. return parser.parse_args()
  96. def install_instrumented_asar_file(resources_path):
  97. asar_path = os.path.join(resources_path, '{0}.asar'.format(PROJECT_NAME))
  98. uninstrumented_path = os.path.join(resources_path,
  99. '{0}-original.asar'.format(PROJECT_NAME))
  100. instrumented_path = os.path.join(SOURCE_ROOT, 'out', 'coverage',
  101. '{0}.asar'.format(PROJECT_NAME))
  102. shutil.move(asar_path, uninstrumented_path)
  103. shutil.move(instrumented_path, asar_path)
  104. def restore_uninstrumented_asar_file(resources_path):
  105. asar_path = os.path.join(resources_path, '{0}.asar'.format(PROJECT_NAME))
  106. uninstrumented_path = os.path.join(resources_path,
  107. '{0}-original.asar'.format(PROJECT_NAME))
  108. os.remove(asar_path)
  109. shutil.move(uninstrumented_path, asar_path)
  110. def rebuild_native_modules(verbose, configuration):
  111. script_path = os.path.join(SOURCE_ROOT, 'script', 'rebuild-test-modules.py')
  112. args = ['--configuration', configuration]
  113. if verbose:
  114. args += ['--verbose']
  115. execute_stdout([sys.executable, script_path] + args)
  116. if __name__ == '__main__':
  117. sys.exit(main())