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