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 # System-level imports. |
14 import os | 14 import os |
15 import shutil | 15 import sys |
16 import tempfile | |
17 import unittest | |
18 | 16 |
19 PARENT_DIR = os.path.dirname(os.path.realpath(__file__)) | 17 PARENT_DIR = os.path.abspath(os.path.dirname(__file__)) |
20 TRUNK_DIR = os.path.dirname(os.path.dirname(PARENT_DIR)) | 18 TRUNK_DIR = os.path.abspath(os.path.join(PARENT_DIR, os.pardir, os.pardir)) |
21 TESTDATA_DIR = os.path.join(PARENT_DIR, 'testdata') | 19 |
22 OUTPUT_DIR_ACTUAL = os.path.join(TESTDATA_DIR, 'outputs', 'actual') | 20 # Import the superclass base_unittest module from the tools dir. |
23 OUTPUT_DIR_EXPECTED = os.path.join(TESTDATA_DIR, 'outputs', 'expected') | 21 TOOLS_DIR = os.path.join(TRUNK_DIR, 'tools') |
| 22 if TOOLS_DIR not in sys.path: |
| 23 sys.path.append(TOOLS_DIR) |
| 24 import tests.base_unittest as superclass_module |
24 | 25 |
25 | 26 |
26 class TestCase(unittest.TestCase): | 27 class TestCase(superclass_module.TestCase): |
27 | 28 |
28 def setUp(self): | 29 def __init__(self, *args, **kwargs): |
29 # Get the name of this test, in such a way that it will be consistent | 30 super(TestCase, self).__init__(*args, **kwargs) |
30 # regardless of the directory it is run from (throw away package names, | 31 # Some of the tests within this package want their output validated, |
31 # if any). | 32 # so we declare where the expected and actual output will be. |
32 test_name = '.'.join(self.id().split('.')[-3:]) | 33 self._testdata_dir = os.path.join(PARENT_DIR, 'testdata') |
33 | 34 |
34 self._input_dir = os.path.join(TESTDATA_DIR, 'inputs') | 35 def main(*args, **kwargs): |
35 self._output_dir_actual = os.path.join(OUTPUT_DIR_ACTUAL, test_name) | 36 superclass_module.main(*args, **kwargs) |
36 self._output_dir_expected = os.path.join(OUTPUT_DIR_EXPECTED, test_name) | |
37 create_empty_dir(self._output_dir_actual) | |
38 self._temp_dir = tempfile.mkdtemp() | |
39 | |
40 def tearDown(self): | |
41 shutil.rmtree(self._temp_dir) | |
42 if os.path.exists(self._output_dir_expected): | |
43 different_files = find_different_files(self._output_dir_actual, | |
44 self._output_dir_expected) | |
45 # Maybe we should move this assert elsewhere? It's unusual to see an | |
46 # assert within tearDown(), but my thinking was: | |
47 # 1. Every test case will have some collection of output files that need | |
48 # to be validated. | |
49 # 2. So put that validation within tearDown(), which will be called after | |
50 # every test case! | |
51 # | |
52 # I have confirmed that the test really does fail if this assert is | |
53 # triggered. | |
54 # | |
55 # Ravi notes: if somebody later comes along and adds cleanup code below | |
56 # this assert, then if tests fail, the artifacts will not be cleaned up. | |
57 assert (not different_files), \ | |
58 ('found differing files:\n' + | |
59 '\n'.join(['tkdiff %s %s &' % ( | |
60 os.path.join(self._output_dir_actual, basename), | |
61 os.path.join(self._output_dir_expected, basename)) | |
62 for basename in different_files])) | |
63 | |
64 def shortDescription(self): | |
65 """Tell unittest framework to not print docstrings for test cases.""" | |
66 return None | |
67 | |
68 def find_path_to_program(self, program): | |
69 """Returns path to an existing program binary. | |
70 | |
71 Args: | |
72 program: Basename of the program to find (e.g., 'render_pictures'). | |
73 | |
74 Returns: | |
75 Absolute path to the program binary, as a string. | |
76 | |
77 Raises: | |
78 Exception: unable to find the program binary. | |
79 """ | |
80 possible_paths = [os.path.join(TRUNK_DIR, 'out', 'Release', program), | |
81 os.path.join(TRUNK_DIR, 'out', 'Debug', program), | |
82 os.path.join(TRUNK_DIR, 'out', 'Release', | |
83 program + '.exe'), | |
84 os.path.join(TRUNK_DIR, 'out', 'Debug', | |
85 program + '.exe')] | |
86 for try_path in possible_paths: | |
87 if os.path.isfile(try_path): | |
88 return try_path | |
89 raise Exception('cannot find %s in paths %s; maybe you need to ' | |
90 'build %s?' % (program, possible_paths, program)) | |
91 | |
92 | |
93 def create_empty_dir(path): | |
94 """Create an empty directory at the given path.""" | |
95 if os.path.isdir(path): | |
96 shutil.rmtree(path) | |
97 elif os.path.lexists(path): | |
98 os.remove(path) | |
99 os.makedirs(path) | |
100 | |
101 | |
102 def find_different_files(dir1, dir2, ignore_subtree_names=None): | |
103 """Returns a list of any files that differ between the directory trees rooted | |
104 at dir1 and dir2. | |
105 | |
106 Args: | |
107 dir1: root of a directory tree; if nonexistent, will raise OSError | |
108 dir2: root of another directory tree; if nonexistent, will raise OSError | |
109 ignore_subtree_names: list of subtree directory names to ignore; | |
110 defaults to ['.svn'], so all SVN files are ignores | |
111 | |
112 TODO(epoger): include the dirname within each filename (not just the | |
113 basename), to make it easier to locate any differences | |
114 """ | |
115 differing_files = [] | |
116 if ignore_subtree_names is None: | |
117 ignore_subtree_names = ['.svn'] | |
118 dircmp = filecmp.dircmp(dir1, dir2, ignore=ignore_subtree_names) | |
119 differing_files.extend(dircmp.left_only) | |
120 differing_files.extend(dircmp.right_only) | |
121 differing_files.extend(dircmp.common_funny) | |
122 differing_files.extend(dircmp.diff_files) | |
123 differing_files.extend(dircmp.funny_files) | |
124 for common_dir in dircmp.common_dirs: | |
125 differing_files.extend(find_different_files( | |
126 os.path.join(dir1, common_dir), os.path.join(dir2, common_dir))) | |
127 return differing_files | |
128 | |
129 | |
130 def main(test_case_class): | |
131 """Run the unit tests within the given class.""" | |
132 suite = unittest.TestLoader().loadTestsFromTestCase(test_case_class) | |
133 unittest.TextTestRunner(verbosity=2).run(suite) | |
OLD | NEW |