native-tests.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #!/usr/bin/env python3
  2. from __future__ import print_function
  3. import argparse
  4. import os
  5. import sys
  6. from lib.native_tests import TestsList, Verbosity, DisabledTestsPolicy
  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. disabled_tests = parser.add_mutually_exclusive_group()
  24. disabled_tests.add_argument('--only-disabled-tests',
  25. dest='disabled_tests_policy',
  26. action='store_const',
  27. const=DisabledTestsPolicy.ONLY,
  28. help='run disabled tests only')
  29. disabled_tests.add_argument('--include-disabled-tests',
  30. dest='disabled_tests_policy',
  31. action='store_const',
  32. const=DisabledTestsPolicy.INCLUDE,
  33. help='if disabled tests should be run as well')
  34. parser.set_defaults(disabled_tests_policy=DisabledTestsPolicy.DISABLE)
  35. verbosity = parser.add_mutually_exclusive_group()
  36. verbosity.add_argument('-v', '--verbosity', required=False,
  37. default=Verbosity.CHATTY,
  38. choices=Verbosity.get_all(),
  39. help='set verbosity level')
  40. verbosity.add_argument('-q', '--quiet', required=False, action='store_const',
  41. const=Verbosity.ERRORS, dest='verbosity',
  42. help='suppress stdout from test binaries')
  43. verbosity.add_argument('-qq', '--quiet-quiet',
  44. # https://youtu.be/bXd-zZLV2i0?t=41s
  45. required=False, action='store_const',
  46. const=Verbosity.SILENT, dest='verbosity',
  47. help='suppress stdout and stderr from test binaries')
  48. args = parser.parse_args()
  49. # Additional checks.
  50. if args.command == Command.RUN and args.tests_dir is None:
  51. parser.error("specify a path to a dir with test binaries via --tests-dir")
  52. # Absolutize and check paths.
  53. # 'config' must exist and be a file.
  54. args.config = os.path.abspath(args.config)
  55. if not os.path.isfile(args.config):
  56. parser.error("file '{}' doesn't exist".format(args.config))
  57. # 'tests_dir' must exist and be a directory.
  58. if args.tests_dir is not None:
  59. args.tests_dir = os.path.abspath(args.tests_dir)
  60. if not os.path.isdir(args.tests_dir):
  61. parser.error("directory '{}' doesn't exist".format(args.tests_dir))
  62. # 'output_dir' must exist and be a directory.
  63. if args.output_dir is not None:
  64. args.output_dir = os.path.abspath(args.output_dir)
  65. if not os.path.isdir(args.output_dir):
  66. parser.error("directory '{}' doesn't exist".format(args.output_dir))
  67. return args
  68. def main():
  69. args = parse_args()
  70. tests_list = TestsList(args.config, args.tests_dir)
  71. if args.command == Command.LIST:
  72. all_binaries_names = tests_list.get_for_current_platform()
  73. print('\n'.join(all_binaries_names))
  74. return 0
  75. if args.command == Command.RUN:
  76. if args.binary is not None:
  77. return tests_list.run(args.binary, args.output_dir, args.verbosity,
  78. args.disabled_tests_policy)
  79. return tests_list.run_all(args.output_dir, args.verbosity,
  80. args.disabled_tests_policy)
  81. raise AssertionError("unexpected command '{}'".format(args.command))
  82. if __name__ == '__main__':
  83. sys.exit(main())