mach_commands.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. # This Source Code Form is subject to the terms of the Mozilla Public
  2. # License, v. 2.0. If a copy of the MPL was not distributed with this
  3. # file, You can obtain one at http://mozilla.org/MPL/2.0/.
  4. from __future__ import absolute_import, unicode_literals
  5. import os
  6. import sys
  7. import argparse
  8. from mozbuild.base import (
  9. MachCommandBase,
  10. MachCommandConditions as conditions,
  11. )
  12. from mach.decorators import (
  13. CommandArgument,
  14. CommandProvider,
  15. Command,
  16. )
  17. def is_firefox_or_android(cls):
  18. """Must have Firefox build or Android build."""
  19. return conditions.is_firefox(cls) or conditions.is_android(cls)
  20. def setup_marionette_argument_parser():
  21. from marionette_harness.runtests import MarionetteArguments
  22. from mozlog.structured import commandline
  23. parser = MarionetteArguments()
  24. commandline.add_logging_group(parser)
  25. return parser
  26. def run_marionette(tests, binary=None, topsrcdir=None, **kwargs):
  27. from mozlog.structured import commandline
  28. from marionette_harness.runtests import (
  29. MarionetteTestRunner,
  30. MarionetteHarness
  31. )
  32. parser = setup_marionette_argument_parser()
  33. if not tests:
  34. tests = [os.path.join(topsrcdir,
  35. 'testing/marionette/harness/marionette_harness/tests/unit-tests.ini')]
  36. args = argparse.Namespace(tests=tests)
  37. args.binary = binary
  38. for k, v in kwargs.iteritems():
  39. setattr(args, k, v)
  40. parser.verify_usage(args)
  41. args.logger = commandline.setup_logging("Marionette Unit Tests",
  42. args,
  43. {"mach": sys.stdout})
  44. failed = MarionetteHarness(MarionetteTestRunner, args=vars(args)).run()
  45. if failed > 0:
  46. return 1
  47. else:
  48. return 0
  49. @CommandProvider
  50. class MachCommands(MachCommandBase):
  51. @Command('marionette-test', category='testing',
  52. description='Run a Marionette test (Check UI or the internal JavaScript using marionette).',
  53. conditions=[is_firefox_or_android],
  54. parser=setup_marionette_argument_parser,
  55. )
  56. def run_marionette_test(self, tests, **kwargs):
  57. if 'test_objects' in kwargs:
  58. tests = []
  59. for obj in kwargs['test_objects']:
  60. tests.append(obj['file_relpath'])
  61. del kwargs['test_objects']
  62. if not kwargs.get('binary') and conditions.is_firefox(self):
  63. kwargs['binary'] = self.get_binary_path('app')
  64. return run_marionette(tests, topsrcdir=self.topsrcdir, **kwargs)