OLD | NEW |
1 #!/usr/bin/python | 1 #!/usr/bin/python |
2 | 2 |
3 """ | 3 """ |
4 Copyright 2013 Google Inc. | 4 Copyright 2013 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 Test results.py | 9 Test results.py |
10 | 10 |
11 TODO(epoger): Launch this (and other unittests within this dir) automatically | 11 TODO(epoger): Launch this (and other unittests within this dir) automatically |
12 on the housekeeper bot, but first make sure it works properly after having been | 12 on the housekeeper bot, but first make sure it works properly after having been |
13 checked out (from both git and svn) | 13 checked out (from both git and svn) |
14 | 14 |
15 TODO(epoger): Create a command to update the expected results (in | 15 TODO(epoger): Create a command to update the expected results (in |
16 OUTPUT_DIR_EXPECTATIONS) when appropriate. For now, you should: | 16 OUTPUT_DIR_EXPECTED) when appropriate. For now, you should: |
17 1. examine the results in OUTPUT_DIR and make sure they are ok | 17 1. examine the results in OUTPUT_DIR_ACTUAL and make sure they are ok |
18 2. rm -rf OUTPUT_DIR_EXPECTATIONS | 18 2. rm -rf OUTPUT_DIR_EXPECTED |
19 3. mv OUTPUT_DIR OUTPUT_DIR_EXPECTATIONS | 19 3. mv OUTPUT_DIR_ACTUAL OUTPUT_DIR_EXPECTED |
20 Although, if you're using an SVN checkout, this will blow away .svn directories | 20 Although, if you're using an SVN checkout, this will blow away .svn directories |
21 within OUTPUT_DIR_EXPECTATIONS, which wouldn't be good... | 21 within OUTPUT_DIR_EXPECTED, which wouldn't be good... |
22 | 22 |
23 """ | 23 """ |
24 | 24 |
25 import filecmp | 25 import filecmp |
26 import os | 26 import os |
27 import shutil | 27 import shutil |
28 import sys | 28 import sys |
29 import tempfile | 29 import tempfile |
30 import unittest | 30 import unittest |
31 | 31 |
32 # Imports from within Skia | 32 # Imports from within Skia |
33 import results | 33 import results |
34 import gm_json # must import results first, so that gm_json will be in sys.path | 34 import gm_json # must import results first, so that gm_json will be in sys.path |
35 | 35 |
36 PARENT_DIR = os.path.dirname(os.path.realpath(__file__)) | 36 PARENT_DIR = os.path.dirname(os.path.realpath(__file__)) |
37 INPUT_DIR = os.path.join(PARENT_DIR, 'tests', 'inputs') | 37 INPUT_DIR = os.path.join(PARENT_DIR, 'tests', 'inputs') |
38 OUTPUT_DIR = os.path.join(PARENT_DIR, 'tests', 'outputs', 'actual') | 38 OUTPUT_DIR_ACTUAL = os.path.join(PARENT_DIR, 'tests', 'outputs', 'actual') |
39 OUTPUT_DIR_EXPECTATIONS = os.path.join( | 39 OUTPUT_DIR_EXPECTED = os.path.join(PARENT_DIR, 'tests', 'outputs', 'expected') |
40 PARENT_DIR, 'tests', 'outputs', 'expected') | 40 |
41 | 41 |
42 class ResultsTest(unittest.TestCase): | 42 class ResultsTest(unittest.TestCase): |
43 | 43 |
44 def setUp(self): | 44 def setUp(self): |
45 self._tempdir = tempfile.mkdtemp() | 45 self._temp_dir = tempfile.mkdtemp() |
| 46 self._output_dir_actual = os.path.join(OUTPUT_DIR_ACTUAL, self.id()) |
| 47 self._output_dir_expected = os.path.join(OUTPUT_DIR_EXPECTED, self.id()) |
| 48 create_empty_dir(self._output_dir_actual) |
46 | 49 |
47 def tearDown(self): | 50 def tearDown(self): |
48 shutil.rmtree(self._tempdir) | 51 shutil.rmtree(self._temp_dir) |
| 52 different_files = find_different_files(self._output_dir_actual, |
| 53 self._output_dir_expected) |
| 54 # Maybe we should move this assert elsewhere? It's unusual to see an |
| 55 # assert within tearDown(), but my thinking was: |
| 56 # 1. Every test case will have some collection of output files that need to |
| 57 # be validated. |
| 58 # 2. So put that validation within tearDown(), which will be called after |
| 59 # every test case! |
| 60 # |
| 61 # I have confirmed that the test really does fail if this assert is |
| 62 # triggered. |
| 63 # |
| 64 # Ravi notes: if somebody later comes along and adds cleanup code below the |
| 65 # assert, then if tests fail, the artifacts will not be cleaned up. |
| 66 assert (not different_files), \ |
| 67 ('found differing files between actual dir %s and expected dir %s: %s' % |
| 68 (self._output_dir_actual, self._output_dir_expected, different_files)) |
49 | 69 |
50 def test_gm(self): | 70 def test_gm(self): |
51 """Process results of a GM run with the Results object.""" | 71 """Process results of a GM run with the Results object.""" |
52 results_obj = results.Results( | 72 results_obj = results.Results( |
53 actuals_root=os.path.join(INPUT_DIR, 'gm-actuals'), | 73 actuals_root=os.path.join(INPUT_DIR, 'gm-actuals'), |
54 expected_root=os.path.join(INPUT_DIR, 'gm-expectations'), | 74 expected_root=os.path.join(INPUT_DIR, 'gm-expectations'), |
55 generated_images_root=self._tempdir) | 75 generated_images_root=self._temp_dir) |
56 gm_json.WriteToFile(results_obj.get_results_of_type(results.RESULTS_ALL), | 76 gm_json.WriteToFile(results_obj.get_results_of_type(results.RESULTS_ALL), |
57 os.path.join(OUTPUT_DIR, 'gm.json')) | 77 os.path.join(self._output_dir_actual, 'gm.json')) |
| 78 |
| 79 |
| 80 def create_empty_dir(path): |
| 81 """Create an empty directory at the given path.""" |
| 82 if os.path.isdir(path): |
| 83 shutil.rmtree(path) |
| 84 elif os.path.lexists(path): |
| 85 os.remove(path) |
| 86 os.makedirs(path) |
58 | 87 |
59 | 88 |
60 def find_different_files(dir1, dir2, ignore_subtree_names=None): | 89 def find_different_files(dir1, dir2, ignore_subtree_names=None): |
61 """Returns a list of any files that differ between the directory trees rooted | 90 """Returns a list of any files that differ between the directory trees rooted |
62 at dir1 and dir2. | 91 at dir1 and dir2. |
63 | 92 |
64 Args: | 93 Args: |
65 dir1: root of a directory tree; if nonexistent, will raise OSError | 94 dir1: root of a directory tree; if nonexistent, will raise OSError |
66 dir2: root of another directory tree; if nonexistent, will raise OSError | 95 dir2: root of another directory tree; if nonexistent, will raise OSError |
67 ignore_subtree_names: list of subtree directory names to ignore; | 96 ignore_subtree_names: list of subtree directory names to ignore; |
(...skipping 11 matching lines...) Expand all Loading... |
79 differing_files.extend(dircmp.common_funny) | 108 differing_files.extend(dircmp.common_funny) |
80 differing_files.extend(dircmp.diff_files) | 109 differing_files.extend(dircmp.diff_files) |
81 differing_files.extend(dircmp.funny_files) | 110 differing_files.extend(dircmp.funny_files) |
82 for common_dir in dircmp.common_dirs: | 111 for common_dir in dircmp.common_dirs: |
83 differing_files.extend(find_different_files( | 112 differing_files.extend(find_different_files( |
84 os.path.join(dir1, common_dir), os.path.join(dir2, common_dir))) | 113 os.path.join(dir1, common_dir), os.path.join(dir2, common_dir))) |
85 return differing_files | 114 return differing_files |
86 | 115 |
87 | 116 |
88 def main(): | 117 def main(): |
89 if not os.path.isdir(OUTPUT_DIR): | |
90 os.makedirs(OUTPUT_DIR) | |
91 suite = unittest.TestLoader().loadTestsFromTestCase(ResultsTest) | 118 suite = unittest.TestLoader().loadTestsFromTestCase(ResultsTest) |
92 unittest.TextTestRunner(verbosity=2).run(suite) | 119 unittest.TextTestRunner(verbosity=2).run(suite) |
93 different_files = find_different_files(OUTPUT_DIR, OUTPUT_DIR_EXPECTATIONS) | |
94 assert (not different_files), 'found differing files: %s' % different_files | |
95 | 120 |
96 | 121 |
97 if __name__ == '__main__': | 122 if __name__ == '__main__': |
98 main() | 123 main() |
OLD | NEW |