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__)) |
| 20 | TESTDATA_DIR = os.path.join(PARENT_DIR, 'testdata') |
| 21 | OUTPUT_DIR_ACTUAL = os.path.join(TESTDATA_DIR, 'outputs', 'actual') |
| 22 | OUTPUT_DIR_EXPECTED = os.path.join(TESTDATA_DIR, 'outputs', 'expected') |
| 23 | |
| 24 | |
| 25 | class TestCase(unittest.TestCase): |
| 26 | |
| 27 | def setUp(self): |
| 28 | self._input_dir = os.path.join(TESTDATA_DIR, 'inputs') |
| 29 | self._output_dir_actual = os.path.join(OUTPUT_DIR_ACTUAL, self.id()) |
| 30 | self._output_dir_expected = os.path.join(OUTPUT_DIR_EXPECTED, self.id()) |
| 31 | create_empty_dir(self._output_dir_actual) |
| 32 | self._temp_dir = tempfile.mkdtemp() |
| 33 | |
| 34 | def tearDown(self): |
| 35 | shutil.rmtree(self._temp_dir) |
| 36 | if os.path.exists(self._output_dir_expected): |
| 37 | different_files = find_different_files(self._output_dir_actual, |
| 38 | self._output_dir_expected) |
| 39 | # Maybe we should move this assert elsewhere? It's unusual to see an |
| 40 | # assert within tearDown(), but my thinking was: |
| 41 | # 1. Every test case will have some collection of output files that need |
| 42 | # to be validated. |
| 43 | # 2. So put that validation within tearDown(), which will be called after |
| 44 | # every test case! |
| 45 | # |
| 46 | # I have confirmed that the test really does fail if this assert is |
| 47 | # triggered. |
| 48 | # |
| 49 | # Ravi notes: if somebody later comes along and adds cleanup code below |
| 50 | # this assert, then if tests fail, the artifacts will not be cleaned up. |
| 51 | assert (not different_files), \ |
| 52 | ('found differing files between actual dir %s and expected dir %s: %s' % |
| 53 | (self._output_dir_actual, self._output_dir_expected, different_files)) |
| 54 | |
| 55 | def shortDescription(self): |
| 56 | """Tell unittest framework to not print docstrings for test cases.""" |
| 57 | return None |
| 58 | |
| 59 | |
| 60 | def create_empty_dir(path): |
| 61 | """Create an empty directory at the given path.""" |
| 62 | if os.path.isdir(path): |
| 63 | shutil.rmtree(path) |
| 64 | elif os.path.lexists(path): |
| 65 | os.remove(path) |
| 66 | os.makedirs(path) |
| 67 | |
| 68 | |
| 69 | def find_different_files(dir1, dir2, ignore_subtree_names=None): |
| 70 | """Returns a list of any files that differ between the directory trees rooted |
| 71 | at dir1 and dir2. |
| 72 | |
| 73 | Args: |
| 74 | dir1: root of a directory tree; if nonexistent, will raise OSError |
| 75 | dir2: root of another directory tree; if nonexistent, will raise OSError |
| 76 | ignore_subtree_names: list of subtree directory names to ignore; |
| 77 | defaults to ['.svn'], so all SVN files are ignores |
| 78 | |
| 79 | TODO(epoger): include the dirname within each filename (not just the |
| 80 | basename), to make it easier to locate any differences |
| 81 | """ |
| 82 | differing_files = [] |
| 83 | if ignore_subtree_names is None: |
| 84 | ignore_subtree_names = ['.svn'] |
| 85 | dircmp = filecmp.dircmp(dir1, dir2, ignore=ignore_subtree_names) |
| 86 | differing_files.extend(dircmp.left_only) |
| 87 | differing_files.extend(dircmp.right_only) |
| 88 | differing_files.extend(dircmp.common_funny) |
| 89 | differing_files.extend(dircmp.diff_files) |
| 90 | differing_files.extend(dircmp.funny_files) |
| 91 | for common_dir in dircmp.common_dirs: |
| 92 | differing_files.extend(find_different_files( |
| 93 | os.path.join(dir1, common_dir), os.path.join(dir2, common_dir))) |
| 94 | return differing_files |
| 95 | |
| 96 | |
| 97 | def main(test_case_class): |
| 98 | """Run the unit tests within the given class.""" |
| 99 | suite = unittest.TestLoader().loadTestsFromTestCase(test_case_class) |
| 100 | results = unittest.TextTestRunner(verbosity=2).run(suite) |