OLD | NEW |
1 #!/usr/bin/python | 1 #!/usr/bin/python |
2 | 2 |
3 """ | 3 """ |
4 Copyright 2014 Google Inc. | 4 Copyright 2014 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 download.py | 9 Test download.py |
10 | 10 |
11 TODO(epoger): Create a command to update the expected results (in | 11 TODO(epoger): Create a command to update the expected results (in |
12 OUTPUT_DIR_EXPECTED) when appropriate. For now, you should: | 12 self._output_dir_expected) when appropriate. For now, you should: |
13 1. examine the results in OUTPUT_DIR_ACTUAL and make sure they are ok | 13 1. examine the results in self._output_dir_actual and make sure they are ok |
14 2. rm -rf OUTPUT_DIR_EXPECTED | 14 2. rm -rf self._output_dir_expected |
15 3. mv OUTPUT_DIR_ACTUAL OUTPUT_DIR_EXPECTED | 15 3. mv self._output_dir_actual self._output_dir_expected |
16 Although, if you're using an SVN checkout, this will blow away .svn directories | 16 Although, if you're using an SVN checkout, this will blow away .svn directories |
17 within OUTPUT_DIR_EXPECTED, which wouldn't be good... | 17 within self._output_dir_expected, which wouldn't be good... |
18 | 18 |
19 """ | 19 """ |
20 | 20 |
21 # System-level imports | 21 # System-level imports |
22 import filecmp | |
23 import os | 22 import os |
24 import shutil | 23 import shutil |
25 import tempfile | 24 import tempfile |
26 import unittest | |
27 import urllib | 25 import urllib |
28 | 26 |
29 # Imports from within Skia | 27 # Imports from within Skia |
| 28 import base_unittest |
30 import download_actuals | 29 import download_actuals |
31 | 30 |
32 PARENT_DIR = os.path.dirname(os.path.realpath(__file__)) | |
33 INPUT_DIR = os.path.join(PARENT_DIR, 'tests', 'inputs') | |
34 OUTPUT_DIR_ACTUAL = os.path.join(PARENT_DIR, 'tests', 'outputs', 'actual') | |
35 OUTPUT_DIR_EXPECTED = os.path.join(PARENT_DIR, 'tests', 'outputs', 'expected') | |
36 | 31 |
37 | 32 class DownloadTest(base_unittest.TestCase): |
38 class DownloadTest(unittest.TestCase): | |
39 | |
40 def setUp(self): | |
41 self._temp_dir = tempfile.mkdtemp() | |
42 self._output_dir_actual = os.path.join(OUTPUT_DIR_ACTUAL, self.id()) | |
43 self._output_dir_expected = os.path.join(OUTPUT_DIR_EXPECTED, self.id()) | |
44 create_empty_dir(self._output_dir_actual) | |
45 | |
46 def tearDown(self): | |
47 shutil.rmtree(self._temp_dir) | |
48 if os.path.exists(self._output_dir_expected): | |
49 different_files = find_different_files(self._output_dir_actual, | |
50 self._output_dir_expected) | |
51 # Maybe we should move this assert elsewhere? It's unusual to see an | |
52 # assert within tearDown(), but my thinking was: | |
53 # 1. Every test case will have some collection of output files that need | |
54 # to be validated. | |
55 # 2. So put that validation within tearDown(), which will be called after | |
56 # every test case! | |
57 # | |
58 # I have confirmed that the test really does fail if this assert is | |
59 # triggered. | |
60 # | |
61 # Ravi notes: if somebody later comes along and adds cleanup code below | |
62 # this assert, then if tests fail, the artifacts will not be cleaned up. | |
63 assert (not different_files), \ | |
64 ('found differing files between actual dir %s and expected dir %s: %s' % | |
65 (self._output_dir_actual, self._output_dir_expected, different_files)) | |
66 | |
67 def shortDescription(self): | |
68 """Tell unittest framework to not print docstrings for test cases.""" | |
69 return None | |
70 | 33 |
71 def test_fetch(self): | 34 def test_fetch(self): |
72 """Tests fetch() of GM results from actual-results.json .""" | 35 """Tests fetch() of GM results from actual-results.json .""" |
73 downloader = download_actuals.Download( | 36 downloader = download_actuals.Download( |
74 actuals_base_url=download_actuals.create_filepath_url( | 37 actuals_base_url=download_actuals.create_filepath_url( |
75 os.path.join(INPUT_DIR, 'gm-actuals')), | 38 os.path.join(self._input_dir, 'gm-actuals')), |
76 gm_actuals_root_url=download_actuals.create_filepath_url( | 39 gm_actuals_root_url=download_actuals.create_filepath_url( |
77 os.path.join(INPUT_DIR, 'fake-gm-imagefiles'))) | 40 os.path.join(self._input_dir, 'fake-gm-imagefiles'))) |
78 downloader.fetch( | 41 downloader.fetch( |
79 builder_name='Test-Android-GalaxyNexus-SGX540-Arm7-Release', | 42 builder_name='Test-Android-GalaxyNexus-SGX540-Arm7-Release', |
80 dest_dir=self._output_dir_actual) | 43 dest_dir=self._output_dir_actual) |
81 | 44 |
82 def test_create_filepath_url(self): | 45 def test_create_filepath_url(self): |
83 """Tests create_filepath_url(). """ | 46 """Tests create_filepath_url(). """ |
84 with self.assertRaises(Exception): | 47 with self.assertRaises(Exception): |
85 url_or_path.create_filepath_url('http://1.2.3.4/path') | 48 url_or_path.create_filepath_url('http://1.2.3.4/path') |
86 # Pass absolute filepath. | 49 # Pass absolute filepath. |
87 self.assertEquals( | 50 self.assertEquals( |
(...skipping 23 matching lines...) Expand all Loading... |
111 create_subdirs_if_needed=False) | 74 create_subdirs_if_needed=False) |
112 # If create_subdirs_if_needed is True, it should work. | 75 # If create_subdirs_if_needed is True, it should work. |
113 download_actuals.copy_contents(source_url=source_url, | 76 download_actuals.copy_contents(source_url=source_url, |
114 dest_path=dest_path, | 77 dest_path=dest_path, |
115 create_subdirs_if_needed=True) | 78 create_subdirs_if_needed=True) |
116 self.assertEquals(open(dest_path).read(), contents) | 79 self.assertEquals(open(dest_path).read(), contents) |
117 finally: | 80 finally: |
118 shutil.rmtree(tempdir_path) | 81 shutil.rmtree(tempdir_path) |
119 | 82 |
120 | 83 |
121 # TODO(epoger): create_empty_dir(), find_different_files(), etc. should be | |
122 # extracted from this file to some common location, where they can be shared | |
123 # with results_test.py and other users. | |
124 | |
125 def create_empty_dir(path): | |
126 """Create an empty directory at the given path.""" | |
127 if os.path.isdir(path): | |
128 shutil.rmtree(path) | |
129 elif os.path.lexists(path): | |
130 os.remove(path) | |
131 os.makedirs(path) | |
132 | |
133 | |
134 def find_different_files(dir1, dir2, ignore_subtree_names=None): | |
135 """Returns a list of any files that differ between the directory trees rooted | |
136 at dir1 and dir2. | |
137 | |
138 Args: | |
139 dir1: root of a directory tree; if nonexistent, will raise OSError | |
140 dir2: root of another directory tree; if nonexistent, will raise OSError | |
141 ignore_subtree_names: list of subtree directory names to ignore; | |
142 defaults to ['.svn'], so all SVN files are ignores | |
143 | |
144 TODO(epoger): include the dirname within each filename (not just the | |
145 basename), to make it easier to locate any differences | |
146 """ | |
147 differing_files = [] | |
148 if ignore_subtree_names is None: | |
149 ignore_subtree_names = ['.svn'] | |
150 dircmp = filecmp.dircmp(dir1, dir2, ignore=ignore_subtree_names) | |
151 differing_files.extend(dircmp.left_only) | |
152 differing_files.extend(dircmp.right_only) | |
153 differing_files.extend(dircmp.common_funny) | |
154 differing_files.extend(dircmp.diff_files) | |
155 differing_files.extend(dircmp.funny_files) | |
156 for common_dir in dircmp.common_dirs: | |
157 differing_files.extend(find_different_files( | |
158 os.path.join(dir1, common_dir), os.path.join(dir2, common_dir))) | |
159 return differing_files | |
160 | |
161 | |
162 def main(): | 84 def main(): |
163 suite = unittest.TestLoader().loadTestsFromTestCase(DownloadTest) | 85 base_unittest.main(DownloadTest) |
164 unittest.TextTestRunner(verbosity=2).run(suite) | |
165 | 86 |
166 | 87 |
167 if __name__ == '__main__': | 88 if __name__ == '__main__': |
168 main() | 89 main() |
OLD | NEW |