commit-bot@chromium.org | f679e2b | 2014-02-19 15:38:13 +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 filecmp |
| 14 | import os |
| 15 | import shutil |
| 16 | import tempfile |
| 17 | import unittest |
| 18 | |
| 19 | PARENT_DIR = os.path.dirname(os.path.realpath(__file__)) |
commit-bot@chromium.org | a60d037 | 2014-05-07 19:29:03 +0000 | [diff] [blame] | 20 | TRUNK_DIR = os.path.dirname(os.path.dirname(PARENT_DIR)) |
commit-bot@chromium.org | f679e2b | 2014-02-19 15:38:13 +0000 | [diff] [blame] | 21 | TESTDATA_DIR = os.path.join(PARENT_DIR, 'testdata') |
| 22 | OUTPUT_DIR_ACTUAL = os.path.join(TESTDATA_DIR, 'outputs', 'actual') |
| 23 | OUTPUT_DIR_EXPECTED = os.path.join(TESTDATA_DIR, 'outputs', 'expected') |
| 24 | |
| 25 | |
| 26 | class TestCase(unittest.TestCase): |
| 27 | |
| 28 | def setUp(self): |
epoger | 33ac950 | 2014-07-16 08:28:23 -0700 | [diff] [blame^] | 29 | # Get the name of this test, in such a way that it will be consistent |
| 30 | # regardless of the directory it is run from (throw away package names, |
| 31 | # if any). |
| 32 | test_name = '.'.join(self.id().split('.')[-3:]) |
| 33 | |
commit-bot@chromium.org | f679e2b | 2014-02-19 15:38:13 +0000 | [diff] [blame] | 34 | self._input_dir = os.path.join(TESTDATA_DIR, 'inputs') |
epoger | 33ac950 | 2014-07-16 08:28:23 -0700 | [diff] [blame^] | 35 | self._output_dir_actual = os.path.join(OUTPUT_DIR_ACTUAL, test_name) |
| 36 | self._output_dir_expected = os.path.join(OUTPUT_DIR_EXPECTED, test_name) |
commit-bot@chromium.org | f679e2b | 2014-02-19 15:38:13 +0000 | [diff] [blame] | 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), \ |
commit-bot@chromium.org | 9d3fdbe | 2014-04-25 19:59:44 +0000 | [diff] [blame] | 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])) |
commit-bot@chromium.org | f679e2b | 2014-02-19 15:38:13 +0000 | [diff] [blame] | 63 | |
| 64 | def shortDescription(self): |
| 65 | """Tell unittest framework to not print docstrings for test cases.""" |
| 66 | return None |
| 67 | |
commit-bot@chromium.org | a60d037 | 2014-05-07 19:29:03 +0000 | [diff] [blame] | 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 | |
commit-bot@chromium.org | f679e2b | 2014-02-19 15:38:13 +0000 | [diff] [blame] | 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) |
epoger | 33ac950 | 2014-07-16 08:28:23 -0700 | [diff] [blame^] | 133 | unittest.TextTestRunner(verbosity=2).run(suite) |