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 assert (not different_files), \ | |
rmistry
2014/01/03 15:18:42
I find it slightly strange to see an assert in tea
epoger
2014/01/03 15:42:07
Good point. Added a comment explaining the situat
rmistry
2014/01/03 15:44:18
Also another minor reason to not have it here: if
epoger
2014/01/03 15:48:09
Good point again. Added that to my explanatory co
| |
55 ('found differing files between actual dir %s and expected dir %s: %s' % | |
56 (self._output_dir_actual, self._output_dir_expected, different_files)) | |
49 | 57 |
50 def test_gm(self): | 58 def test_gm(self): |
51 """Process results of a GM run with the Results object.""" | 59 """Process results of a GM run with the Results object.""" |
52 results_obj = results.Results( | 60 results_obj = results.Results( |
53 actuals_root=os.path.join(INPUT_DIR, 'gm-actuals'), | 61 actuals_root=os.path.join(INPUT_DIR, 'gm-actuals'), |
54 expected_root=os.path.join(INPUT_DIR, 'gm-expectations'), | 62 expected_root=os.path.join(INPUT_DIR, 'gm-expectations'), |
55 generated_images_root=self._tempdir) | 63 generated_images_root=self._temp_dir) |
56 gm_json.WriteToFile(results_obj.get_results_of_type(results.RESULTS_ALL), | 64 gm_json.WriteToFile(results_obj.get_results_of_type(results.RESULTS_ALL), |
57 os.path.join(OUTPUT_DIR, 'gm.json')) | 65 os.path.join(self._output_dir_actual, 'gm.json')) |
58 | 66 |
59 | 67 |
68 def create_empty_dir(path): | |
69 """Create an empty directory at the given path.""" | |
70 if os.path.isdir(path): | |
71 shutil.rmtree(path) | |
72 elif os.path.lexists(path): | |
73 os.remove(path) | |
74 os.makedirs(path) | |
75 | |
rmistry
2014/01/03 15:18:42
Nit: Add one more newline here.
epoger
2014/01/03 15:42:07
Done.
| |
60 def find_different_files(dir1, dir2, ignore_subtree_names=None): | 76 def find_different_files(dir1, dir2, ignore_subtree_names=None): |
61 """Returns a list of any files that differ between the directory trees rooted | 77 """Returns a list of any files that differ between the directory trees rooted |
62 at dir1 and dir2. | 78 at dir1 and dir2. |
63 | 79 |
64 Args: | 80 Args: |
65 dir1: root of a directory tree; if nonexistent, will raise OSError | 81 dir1: root of a directory tree; if nonexistent, will raise OSError |
66 dir2: root of another directory tree; if nonexistent, will raise OSError | 82 dir2: root of another directory tree; if nonexistent, will raise OSError |
67 ignore_subtree_names: list of subtree directory names to ignore; | 83 ignore_subtree_names: list of subtree directory names to ignore; |
68 defaults to ['.svn'], so all SVN files are ignores | 84 defaults to ['.svn'], so all SVN files are ignores |
69 | 85 |
70 TODO(epoger): include the dirname within each filename (not just the | 86 TODO(epoger): include the dirname within each filename (not just the |
71 basename), to make it easier to locate any differences | 87 basename), to make it easier to locate any differences |
72 """ | 88 """ |
73 differing_files = [] | 89 differing_files = [] |
74 if ignore_subtree_names is None: | 90 if ignore_subtree_names is None: |
75 ignore_subtree_names = ['.svn'] | 91 ignore_subtree_names = ['.svn'] |
76 dircmp = filecmp.dircmp(dir1, dir2, ignore=ignore_subtree_names) | 92 dircmp = filecmp.dircmp(dir1, dir2, ignore=ignore_subtree_names) |
77 differing_files.extend(dircmp.left_only) | 93 differing_files.extend(dircmp.left_only) |
78 differing_files.extend(dircmp.right_only) | 94 differing_files.extend(dircmp.right_only) |
79 differing_files.extend(dircmp.common_funny) | 95 differing_files.extend(dircmp.common_funny) |
80 differing_files.extend(dircmp.diff_files) | 96 differing_files.extend(dircmp.diff_files) |
81 differing_files.extend(dircmp.funny_files) | 97 differing_files.extend(dircmp.funny_files) |
82 for common_dir in dircmp.common_dirs: | 98 for common_dir in dircmp.common_dirs: |
83 differing_files.extend(find_different_files( | 99 differing_files.extend(find_different_files( |
84 os.path.join(dir1, common_dir), os.path.join(dir2, common_dir))) | 100 os.path.join(dir1, common_dir), os.path.join(dir2, common_dir))) |
85 return differing_files | 101 return differing_files |
86 | 102 |
87 | 103 |
88 def main(): | 104 def main(): |
89 if not os.path.isdir(OUTPUT_DIR): | |
epoger
2014/01/02 21:24:08
Moving all the pre- and post-test steps into setUp
| |
90 os.makedirs(OUTPUT_DIR) | |
91 suite = unittest.TestLoader().loadTestsFromTestCase(ResultsTest) | 105 suite = unittest.TestLoader().loadTestsFromTestCase(ResultsTest) |
92 unittest.TextTestRunner(verbosity=2).run(suite) | 106 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 | 107 |
96 | 108 |
97 if __name__ == '__main__': | 109 if __name__ == '__main__': |
98 main() | 110 main() |
OLD | NEW |