commit-bot@chromium.org | 11f1562 | 2014-01-07 17:03:40 +0000 | [diff] [blame^] | 1 | #!/usr/bin/python |
| 2 | |
| 3 | """ |
| 4 | Copyright 2014 Google Inc. |
| 5 | |
| 6 | Use of this source code is governed by a BSD-style license that can be |
| 7 | found in the LICENSE file. |
| 8 | |
| 9 | A wrapper around the standard Python unittest library, adding features we need |
| 10 | for various unittests within this directory. |
| 11 | """ |
| 12 | |
| 13 | import os |
| 14 | import subprocess |
| 15 | import unittest |
| 16 | |
| 17 | |
| 18 | class TestCase(unittest.TestCase): |
| 19 | |
| 20 | def shortDescription(self): |
| 21 | """Tell unittest framework to not print docstrings for test cases.""" |
| 22 | return None |
| 23 | |
| 24 | def run_command(self, args): |
| 25 | """Runs a program from the command line and returns stdout. |
| 26 | |
| 27 | Args: |
| 28 | args: Command line to run, as a list of string parameters. args[0] is the |
| 29 | binary to run. |
| 30 | |
| 31 | Returns: |
| 32 | stdout from the program, as a single string. |
| 33 | |
| 34 | Raises: |
| 35 | Exception: the program exited with a nonzero return code. |
| 36 | """ |
| 37 | proc = subprocess.Popen(args, |
| 38 | stdout=subprocess.PIPE, |
| 39 | stderr=subprocess.PIPE) |
| 40 | (stdout, stderr) = proc.communicate() |
| 41 | if proc.returncode is not 0: |
| 42 | raise Exception('command "%s" failed: %s' % (args, stderr)) |
| 43 | return stdout |
| 44 | |
| 45 | def find_path_to_program(self, program): |
| 46 | """Returns path to an existing program binary. |
| 47 | |
| 48 | Args: |
| 49 | program: Basename of the program to find (e.g., 'render_pictures'). |
| 50 | |
| 51 | Returns: |
| 52 | Absolute path to the program binary, as a string. |
| 53 | |
| 54 | Raises: |
| 55 | Exception: unable to find the program binary. |
| 56 | """ |
| 57 | trunk_path = os.path.abspath(os.path.join(os.path.dirname(__file__), |
| 58 | os.pardir, os.pardir)) |
| 59 | possible_paths = [os.path.join(trunk_path, 'out', 'Release', program), |
| 60 | os.path.join(trunk_path, 'out', 'Debug', program), |
| 61 | os.path.join(trunk_path, 'out', 'Release', |
| 62 | program + '.exe'), |
| 63 | os.path.join(trunk_path, 'out', 'Debug', |
| 64 | program + '.exe')] |
| 65 | for try_path in possible_paths: |
| 66 | if os.path.isfile(try_path): |
| 67 | return try_path |
| 68 | raise Exception('cannot find %s in paths %s; maybe you need to ' |
| 69 | 'build %s?' % (program, possible_paths, program)) |
| 70 | |
| 71 | |
| 72 | def main(test_case_class): |
| 73 | """Run the unit tests within the given class. |
| 74 | |
| 75 | Raises an Exception if any of those tests fail (in case we are running in the |
| 76 | context of run_all.py, which depends on that Exception to signal failures). |
| 77 | |
| 78 | TODO(epoger): Make all of our unit tests use the Python unittest framework, |
| 79 | so we can leverage its ability to run *all* the tests and report failures at |
| 80 | the end. |
| 81 | """ |
| 82 | suite = unittest.TestLoader().loadTestsFromTestCase(test_case_class) |
| 83 | results = unittest.TextTestRunner(verbosity=2).run(suite) |
| 84 | if not results.wasSuccessful(): |
| 85 | raise Exception('failed unittest %s' % test_case_class) |