native-tests.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #!/usr/bin/env python
  2. from __future__ import print_function
  3. import argparse
  4. import os
  5. import sys
  6. from lib.native_tests import TestsList, Verbosity
  7. class Command:
  8. LIST = 'list'
  9. RUN = 'run'
  10. def parse_args():
  11. parser = argparse.ArgumentParser(description='Run Google Test binaries')
  12. parser.add_argument('command',
  13. choices=[Command.LIST, Command.RUN],
  14. help='command to execute')
  15. parser.add_argument('-b', '--binary', nargs='+', required=False,
  16. help='binaries to run')
  17. parser.add_argument('-c', '--config', required=True,
  18. help='path to a tests config')
  19. parser.add_argument('-t', '--tests-dir', required=False,
  20. help='path to a directory with test binaries')
  21. parser.add_argument('-o', '--output-dir', required=False,
  22. help='path to a folder to save tests results')
  23. verbosity = parser.add_mutually_exclusive_group()
  24. verbosity.add_argument('-v', '--verbosity', required=False,
  25. default=Verbosity.CHATTY,
  26. choices=Verbosity.get_all(),
  27. help='set verbosity level')
  28. verbosity.add_argument('-q', '--quiet', required=False, action='store_const',
  29. const=Verbosity.ERRORS, dest='verbosity',
  30. help='suppress stdout from test binaries')
  31. verbosity.add_argument('-qq', '--quiet-quiet',
  32. # https://youtu.be/bXd-zZLV2i0?t=41s
  33. required=False, action='store_const',
  34. const=Verbosity.SILENT, dest='verbosity',
  35. help='suppress stdout and stderr from test binaries')
  36. args = parser.parse_args()
  37. # Additional checks.
  38. if args.command == Command.RUN and args.tests_dir is None:
  39. parser.error("specify a path to a dir with test binaries via --tests-dir")
  40. # Absolutize and check paths.
  41. # 'config' must exist and be a file.
  42. args.config = os.path.abspath(args.config)
  43. if not os.path.isfile(args.config):
  44. parser.error("file '{}' doesn't exist".format(args.config))
  45. # 'tests_dir' must exist and be a directory.
  46. if args.tests_dir is not None:
  47. args.tests_dir = os.path.abspath(args.tests_dir)
  48. if not os.path.isdir(args.tests_dir):
  49. parser.error("directory '{}' doesn't exist".format(args.tests_dir))
  50. # 'output_dir' must exist and be a directory.
  51. if args.output_dir is not None:
  52. args.output_dir = os.path.abspath(args.output_dir)
  53. if not os.path.isdir(args.output_dir):
  54. parser.error("directory '{}' doesn't exist".format(args.output_dir))
  55. return args
  56. def main():
  57. args = parse_args()
  58. tests_list = TestsList(args.config, args.tests_dir)
  59. if args.command == Command.LIST:
  60. all_binaries_names = tests_list.get_for_current_platform()
  61. print('\n'.join(all_binaries_names))
  62. return 0
  63. if args.command == Command.RUN:
  64. if args.binary is not None:
  65. return tests_list.run(args.binary, args.output_dir, args.verbosity)
  66. else:
  67. return tests_list.run_all(args.output_dir, args.verbosity)
  68. assert False, "unexpected command '{}'".format(args.command)
  69. if __name__ == '__main__':
  70. sys.exit(main())