OLD | NEW |
---|---|
(Empty) | |
1 #!/usr/bin/python | |
2 | |
3 """ | |
4 Copyright 2013 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 Test results.py | |
10 | |
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 | |
13 checked out (from both git and svn) | |
14 | |
15 TODO(epoger): Create a command to update the expected results (in | |
16 OUTPUT_DIR_EXPECTATIONS) when appropriate. For now, you should: | |
17 1. examine the results in OUTPUT_DIR and make sure they are ok | |
18 2. rm -rf OUTPUT_DIR_EXPECTATIONS | |
19 3. mv OUTPUT_DIR OUTPUT_DIR_EXPECTATIONS | |
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... | |
22 | |
23 """ | |
24 | |
25 import filecmp | |
26 import os | |
27 import shutil | |
28 import sys | |
29 import tempfile | |
30 import unittest | |
31 | |
32 # Imports from within Skia | |
33 # | |
34 # We need to add the 'gm' directory, so that we can import gm_json.py within | |
35 # that directory. | |
36 # Make sure that the 'gm' dir is in the PYTHONPATH, but add it at the *end* | |
37 # so any dirs that are already in the PYTHONPATH will be preferred. | |
38 PARENT_DIRECTORY = os.path.dirname(os.path.realpath(__file__)) | |
39 GM_DIRECTORY = os.path.dirname(PARENT_DIRECTORY) | |
rmistry
2013/12/23 15:07:55
Can instead use results.GM_DIRECTORY ?
epoger
2013/12/23 22:45:44
Don't need it at all, thanks to your suggestion be
| |
40 if GM_DIRECTORY not in sys.path: | |
41 sys.path.append(GM_DIRECTORY) | |
rmistry
2013/12/23 15:07:55
Is this required? doesnt importing results first a
epoger
2013/12/23 22:45:44
Good call, I didn't think about that!
| |
42 import gm_json | |
43 import results | |
44 | |
45 INPUT_DIR = os.path.join(PARENT_DIRECTORY, 'tests', 'inputs') | |
46 OUTPUT_DIR = os.path.join(PARENT_DIRECTORY, 'tests', 'outputs', 'actual') | |
47 OUTPUT_DIR_EXPECTATIONS = os.path.join( | |
48 PARENT_DIRECTORY, 'tests', 'outputs', 'expected') | |
49 | |
50 class ResultsTest(unittest.TestCase): | |
51 | |
52 def setUp(self): | |
53 self._tempdir = tempfile.mkdtemp() | |
54 | |
55 def tearDown(self): | |
56 shutil.rmtree(self._tempdir) | |
57 | |
58 def test_gm(self): | |
59 """Process results of a GM run with the Results object.""" | |
60 results_obj = results.Results( | |
61 actuals_root=os.path.join(INPUT_DIR, 'gm-actuals'), | |
62 expected_root=os.path.join(INPUT_DIR, 'gm-expectations'), | |
63 generated_images_root=self._tempdir) | |
64 gm_json.WriteToFile(results_obj.get_results_of_type(results.RESULTS_ALL), | |
65 os.path.join(OUTPUT_DIR, 'gm.json')) | |
66 | |
67 | |
68 def find_different_files(dir1, dir2, ignore_subtree_names=None): | |
69 """Returns a list of any files that differ between the directory trees rooted | |
70 at dir1 and dir2. | |
71 | |
72 Args: | |
73 dir1: root of a directory tree; if nonexistent, will raise OSError | |
74 dir2: root of another directory tree; if nonexistent, will raise OSError | |
75 ignore_subtree_names: list of subtree directory names to ignore; | |
76 defaults to ['.svn'], so all SVN files are ignores | |
77 | |
78 TODO(epoger): include the dirname within each filename (not just the | |
79 basename), to make it easier to locate any differences | |
80 """ | |
81 differing_files = [] | |
82 if ignore_subtree_names is None: | |
83 ignore_subtree_names = ['.svn'] | |
84 dircmp = filecmp.dircmp(dir1, dir2, ignore=ignore_subtree_names) | |
85 differing_files.extend(dircmp.left_only) | |
86 differing_files.extend(dircmp.right_only) | |
87 differing_files.extend(dircmp.common_funny) | |
88 differing_files.extend(dircmp.diff_files) | |
89 differing_files.extend(dircmp.funny_files) | |
90 for common_dir in dircmp.common_dirs: | |
91 differing_files.extend(find_different_files( | |
92 os.path.join(dir1, common_dir), os.path.join(dir2, common_dir))) | |
93 return differing_files | |
94 | |
rmistry
2013/12/23 15:07:55
Nit: Add a newline here.
epoger
2013/12/23 22:45:44
Done.
| |
95 def main(): | |
96 if not os.path.isdir(OUTPUT_DIR): | |
97 os.makedirs(OUTPUT_DIR) | |
98 suite = unittest.TestLoader().loadTestsFromTestCase(ResultsTest) | |
99 unittest.TextTestRunner(verbosity=2).run(suite) | |
100 different_files = find_different_files(OUTPUT_DIR, OUTPUT_DIR_EXPECTATIONS) | |
101 assert (not different_files), 'found differing files: %s' % different_files | |
102 | |
rmistry
2013/12/23 15:07:55
Nit: Add a newline here.
epoger
2013/12/23 22:45:44
Done.
| |
103 if __name__ == '__main__': | |
104 main() | |
OLD | NEW |