OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/env python |
| 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. |
| 5 |
| 6 """List all the test cases for a google test. |
| 7 |
| 8 See more info at http://code.google.com/p/googletest/. |
| 9 """ |
| 10 |
| 11 import optparse |
| 12 import subprocess |
| 13 import sys |
| 14 |
| 15 |
| 16 class Failure(Exception): |
| 17 pass |
| 18 |
| 19 |
| 20 def gtest_list_tests(executable): |
| 21 cmd = [executable, '--gtest_list_tests'] |
| 22 p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 23 out, err = p.communicate() |
| 24 if p.returncode: |
| 25 raise Failure('Failed to run %s\n%s' % (executable, err), p.returncode) |
| 26 # pylint: disable=E1103 |
| 27 if err and not err.startswith('Xlib: extension "RANDR" missing on display '): |
| 28 raise Failure('Unexpected spew:\n%s' % err, 1) |
| 29 return out |
| 30 |
| 31 |
| 32 def _starts_with(a, b, prefix): |
| 33 return a.startswith(prefix) or b.startswith(prefix) |
| 34 |
| 35 |
| 36 def parse_gtest_cases(out, disabled=False, fails=False, flaky=False): |
| 37 """Expected format is a concatenation of this: |
| 38 TestFixture1 |
| 39 TestCase1 |
| 40 TestCase2 |
| 41 """ |
| 42 tests = [] |
| 43 fixture = None |
| 44 lines = out.splitlines() |
| 45 while lines: |
| 46 line = lines.pop(0) |
| 47 if not line: |
| 48 break |
| 49 if not line.startswith(' '): |
| 50 fixture = line |
| 51 else: |
| 52 case = line[2:] |
| 53 if case.startswith('YOU HAVE'): |
| 54 # It's a 'YOU HAVE foo bar' line. We're done. |
| 55 break |
| 56 assert ' ' not in case |
| 57 |
| 58 if not disabled and _starts_with(fixture, case, 'DISABLED_'): |
| 59 continue |
| 60 if not fails and _starts_with(fixture, case, 'FAILS_'): |
| 61 continue |
| 62 if not flaky and _starts_with(fixture, case, 'FLAKY_'): |
| 63 continue |
| 64 |
| 65 tests.append(fixture + case) |
| 66 return tests |
| 67 |
| 68 |
| 69 def main(): |
| 70 """CLI frontend to validate arguments.""" |
| 71 parser = optparse.OptionParser( |
| 72 usage='%prog <options> [gtest]') |
| 73 parser.add_option( |
| 74 '-d', '--disabled', |
| 75 action='store_true', |
| 76 help='Include DISABLED_ tests') |
| 77 parser.add_option( |
| 78 '-f', '--fails', |
| 79 action='store_true', |
| 80 help='Include FAILS_ tests') |
| 81 parser.add_option( |
| 82 '-F', '--flaky', |
| 83 action='store_true', |
| 84 help='Include FLAKY_ tests') |
| 85 options, args = parser.parse_args() |
| 86 if len(args) != 1: |
| 87 parser.error('Please provide the executable to run') |
| 88 |
| 89 try: |
| 90 out = gtest_list_tests(args[0]) |
| 91 tests = parse_gtest_cases( |
| 92 out, options.disabled, options.fails, options.flaky) |
| 93 for test in tests: |
| 94 print test |
| 95 except Failure, e: |
| 96 print e.args[0] |
| 97 return e.args[1] |
| 98 return 0 |
| 99 |
| 100 |
| 101 if __name__ == '__main__': |
| 102 sys.exit(main()) |
OLD | NEW |