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 | |
commit-bot@chromium.org | 3f04517 | 2014-05-15 15:10:48 +0000 | [diff] [blame] | 13 | import errno |
commit-bot@chromium.org | 11f1562 | 2014-01-07 17:03:40 +0000 | [diff] [blame] | 14 | import os |
commit-bot@chromium.org | 3f04517 | 2014-05-15 15:10:48 +0000 | [diff] [blame] | 15 | import shutil |
commit-bot@chromium.org | 164052e | 2014-02-07 18:41:49 +0000 | [diff] [blame] | 16 | import sys |
commit-bot@chromium.org | 11f1562 | 2014-01-07 17:03:40 +0000 | [diff] [blame] | 17 | import unittest |
| 18 | |
commit-bot@chromium.org | 164052e | 2014-02-07 18:41:49 +0000 | [diff] [blame] | 19 | # Set the PYTHONPATH to include the tools directory. |
| 20 | sys.path.append( |
| 21 | os.path.join(os.path.dirname(os.path.realpath(__file__)), os.pardir)) |
| 22 | import find_run_binary |
| 23 | |
commit-bot@chromium.org | 11f1562 | 2014-01-07 17:03:40 +0000 | [diff] [blame] | 24 | |
| 25 | class TestCase(unittest.TestCase): |
| 26 | |
| 27 | def shortDescription(self): |
| 28 | """Tell unittest framework to not print docstrings for test cases.""" |
| 29 | return None |
| 30 | |
commit-bot@chromium.org | 3f04517 | 2014-05-15 15:10:48 +0000 | [diff] [blame] | 31 | def create_empty_dir(self, path): |
| 32 | """Creates an empty directory at path and returns path. |
| 33 | |
| 34 | Args: |
| 35 | path: path on local disk |
| 36 | """ |
| 37 | shutil.rmtree(path=path, ignore_errors=True) |
| 38 | try: |
| 39 | os.makedirs(path) |
| 40 | except OSError as exc: |
| 41 | if exc.errno != errno.EEXIST: |
| 42 | raise |
| 43 | return path |
| 44 | |
commit-bot@chromium.org | 11f1562 | 2014-01-07 17:03:40 +0000 | [diff] [blame] | 45 | def run_command(self, args): |
| 46 | """Runs a program from the command line and returns stdout. |
| 47 | |
| 48 | Args: |
| 49 | args: Command line to run, as a list of string parameters. args[0] is the |
| 50 | binary to run. |
| 51 | |
| 52 | Returns: |
| 53 | stdout from the program, as a single string. |
| 54 | |
| 55 | Raises: |
| 56 | Exception: the program exited with a nonzero return code. |
| 57 | """ |
commit-bot@chromium.org | 164052e | 2014-02-07 18:41:49 +0000 | [diff] [blame] | 58 | return find_run_binary.run_command(args) |
commit-bot@chromium.org | 11f1562 | 2014-01-07 17:03:40 +0000 | [diff] [blame] | 59 | |
| 60 | def find_path_to_program(self, program): |
| 61 | """Returns path to an existing program binary. |
| 62 | |
| 63 | Args: |
| 64 | program: Basename of the program to find (e.g., 'render_pictures'). |
| 65 | |
| 66 | Returns: |
| 67 | Absolute path to the program binary, as a string. |
| 68 | |
| 69 | Raises: |
| 70 | Exception: unable to find the program binary. |
| 71 | """ |
commit-bot@chromium.org | 164052e | 2014-02-07 18:41:49 +0000 | [diff] [blame] | 72 | return find_run_binary.find_path_to_program(program) |
commit-bot@chromium.org | 11f1562 | 2014-01-07 17:03:40 +0000 | [diff] [blame] | 73 | |
| 74 | |
| 75 | def main(test_case_class): |
| 76 | """Run the unit tests within the given class. |
| 77 | |
| 78 | Raises an Exception if any of those tests fail (in case we are running in the |
| 79 | context of run_all.py, which depends on that Exception to signal failures). |
| 80 | |
| 81 | TODO(epoger): Make all of our unit tests use the Python unittest framework, |
| 82 | so we can leverage its ability to run *all* the tests and report failures at |
| 83 | the end. |
| 84 | """ |
| 85 | suite = unittest.TestLoader().loadTestsFromTestCase(test_case_class) |
| 86 | results = unittest.TextTestRunner(verbosity=2).run(suite) |
| 87 | if not results.wasSuccessful(): |
| 88 | raise Exception('failed unittest %s' % test_case_class) |