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 filecmp | 13 import filecmp |
14 import os | 14 import os |
15 import shutil | 15 import shutil |
16 import subprocess | |
16 import tempfile | 17 import tempfile |
17 import unittest | 18 import unittest |
18 | 19 |
19 PARENT_DIR = os.path.dirname(os.path.realpath(__file__)) | 20 PARENT_DIR = os.path.dirname(os.path.realpath(__file__)) |
21 TRUNK_DIR = os.path.dirname(os.path.dirname(PARENT_DIR)) | |
20 TESTDATA_DIR = os.path.join(PARENT_DIR, 'testdata') | 22 TESTDATA_DIR = os.path.join(PARENT_DIR, 'testdata') |
21 OUTPUT_DIR_ACTUAL = os.path.join(TESTDATA_DIR, 'outputs', 'actual') | 23 OUTPUT_DIR_ACTUAL = os.path.join(TESTDATA_DIR, 'outputs', 'actual') |
22 OUTPUT_DIR_EXPECTED = os.path.join(TESTDATA_DIR, 'outputs', 'expected') | 24 OUTPUT_DIR_EXPECTED = os.path.join(TESTDATA_DIR, 'outputs', 'expected') |
23 | 25 |
24 | 26 |
25 class TestCase(unittest.TestCase): | 27 class TestCase(unittest.TestCase): |
26 | 28 |
27 def setUp(self): | 29 def setUp(self): |
28 self._input_dir = os.path.join(TESTDATA_DIR, 'inputs') | 30 self._input_dir = os.path.join(TESTDATA_DIR, 'inputs') |
29 self._output_dir_actual = os.path.join(OUTPUT_DIR_ACTUAL, self.id()) | 31 self._output_dir_actual = os.path.join(OUTPUT_DIR_ACTUAL, self.id()) |
(...skipping 22 matching lines...) Expand all Loading... | |
52 ('found differing files:\n' + | 54 ('found differing files:\n' + |
53 '\n'.join(['tkdiff %s %s &' % ( | 55 '\n'.join(['tkdiff %s %s &' % ( |
54 os.path.join(self._output_dir_actual, basename), | 56 os.path.join(self._output_dir_actual, basename), |
55 os.path.join(self._output_dir_expected, basename)) | 57 os.path.join(self._output_dir_expected, basename)) |
56 for basename in different_files])) | 58 for basename in different_files])) |
57 | 59 |
58 def shortDescription(self): | 60 def shortDescription(self): |
59 """Tell unittest framework to not print docstrings for test cases.""" | 61 """Tell unittest framework to not print docstrings for test cases.""" |
60 return None | 62 return None |
61 | 63 |
64 def run_command(self, args): | |
borenet
2014/05/07 18:47:49
Why not just use subprocess.check_output which see
epoger
2014/05/07 19:27:49
Because I'm stupid, that's why.
| |
65 """Runs a program from the command line and returns stdout. | |
66 | |
67 Args: | |
68 args: Command line to run, as a list of string parameters. args[0] is the | |
69 binary to run. | |
70 | |
71 Returns: | |
72 stdout from the program, as a single string. | |
73 | |
74 Raises: | |
75 Exception: the program exited with a nonzero return code. | |
76 """ | |
77 proc = subprocess.Popen(args, | |
78 stdout=subprocess.PIPE, | |
79 stderr=subprocess.PIPE) | |
80 (stdout, stderr) = proc.communicate() | |
81 if proc.returncode is not 0: | |
82 raise Exception('command "%s" failed: %s' % (args, stderr)) | |
83 return stdout | |
84 | |
85 def find_path_to_program(self, program): | |
86 """Returns path to an existing program binary. | |
87 | |
88 Args: | |
89 program: Basename of the program to find (e.g., 'render_pictures'). | |
90 | |
91 Returns: | |
92 Absolute path to the program binary, as a string. | |
93 | |
94 Raises: | |
95 Exception: unable to find the program binary. | |
96 """ | |
97 possible_paths = [os.path.join(TRUNK_DIR, 'out', 'Release', program), | |
98 os.path.join(TRUNK_DIR, 'out', 'Debug', program), | |
99 os.path.join(TRUNK_DIR, 'out', 'Release', | |
100 program + '.exe'), | |
101 os.path.join(TRUNK_DIR, 'out', 'Debug', | |
102 program + '.exe')] | |
103 for try_path in possible_paths: | |
104 if os.path.isfile(try_path): | |
105 return try_path | |
106 raise Exception('cannot find %s in paths %s; maybe you need to ' | |
107 'build %s?' % (program, possible_paths, program)) | |
108 | |
62 | 109 |
63 def create_empty_dir(path): | 110 def create_empty_dir(path): |
64 """Create an empty directory at the given path.""" | 111 """Create an empty directory at the given path.""" |
65 if os.path.isdir(path): | 112 if os.path.isdir(path): |
66 shutil.rmtree(path) | 113 shutil.rmtree(path) |
67 elif os.path.lexists(path): | 114 elif os.path.lexists(path): |
68 os.remove(path) | 115 os.remove(path) |
69 os.makedirs(path) | 116 os.makedirs(path) |
70 | 117 |
71 | 118 |
(...skipping 22 matching lines...) Expand all Loading... | |
94 for common_dir in dircmp.common_dirs: | 141 for common_dir in dircmp.common_dirs: |
95 differing_files.extend(find_different_files( | 142 differing_files.extend(find_different_files( |
96 os.path.join(dir1, common_dir), os.path.join(dir2, common_dir))) | 143 os.path.join(dir1, common_dir), os.path.join(dir2, common_dir))) |
97 return differing_files | 144 return differing_files |
98 | 145 |
99 | 146 |
100 def main(test_case_class): | 147 def main(test_case_class): |
101 """Run the unit tests within the given class.""" | 148 """Run the unit tests within the given class.""" |
102 suite = unittest.TestLoader().loadTestsFromTestCase(test_case_class) | 149 suite = unittest.TestLoader().loadTestsFromTestCase(test_case_class) |
103 results = unittest.TextTestRunner(verbosity=2).run(suite) | 150 results = unittest.TextTestRunner(verbosity=2).run(suite) |
OLD | NEW |