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 self._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 self._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 self._output_dir_expected | 14 2. rm -rf self._output_dir_expected |
15 3. mv self._output_dir_actual self._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 self._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 os | 22 import os |
23 import shutil | 23 import shutil |
24 import tempfile | 24 import tempfile |
25 import urllib | 25 import urllib |
26 | 26 |
27 # Imports from within Skia | 27 # Imports from within Skia |
| 28 import fix_pythonpath # must do this first |
| 29 from pyutils import url_utils |
28 import base_unittest | 30 import base_unittest |
29 import download_actuals | 31 import download_actuals |
30 | 32 |
31 | 33 |
32 class DownloadTest(base_unittest.TestCase): | 34 class DownloadTest(base_unittest.TestCase): |
33 | 35 |
34 def test_fetch(self): | 36 def test_fetch(self): |
35 """Tests fetch() of GM results from actual-results.json .""" | 37 """Tests fetch() of GM results from actual-results.json .""" |
36 downloader = download_actuals.Download( | 38 downloader = download_actuals.Download( |
37 actuals_base_url=download_actuals.create_filepath_url( | 39 actuals_base_url=url_utils.create_filepath_url( |
38 os.path.join(self._input_dir, 'gm-actuals')), | 40 os.path.join(self._input_dir, 'gm-actuals')), |
39 gm_actuals_root_url=download_actuals.create_filepath_url( | 41 gm_actuals_root_url=url_utils.create_filepath_url( |
40 os.path.join(self._input_dir, 'fake-gm-imagefiles'))) | 42 os.path.join(self._input_dir, 'fake-gm-imagefiles'))) |
41 downloader.fetch( | 43 downloader.fetch( |
42 builder_name='Test-Android-GalaxyNexus-SGX540-Arm7-Release', | 44 builder_name='Test-Android-GalaxyNexus-SGX540-Arm7-Release', |
43 dest_dir=self._output_dir_actual) | 45 dest_dir=self._output_dir_actual) |
44 | 46 |
45 def test_create_filepath_url(self): | |
46 """Tests create_filepath_url(). """ | |
47 with self.assertRaises(Exception): | |
48 url_or_path.create_filepath_url('http://1.2.3.4/path') | |
49 # Pass absolute filepath. | |
50 self.assertEquals( | |
51 download_actuals.create_filepath_url( | |
52 '%sdir%sfile' % (os.path.sep, os.path.sep)), | |
53 'file:///dir/file') | |
54 # Pass relative filepath. | |
55 self.assertEquals( | |
56 download_actuals.create_filepath_url(os.path.join('dir', 'file')), | |
57 'file://%s/dir/file' % urllib.pathname2url(os.getcwd())) | |
58 | |
59 def test_copy_contents(self): | |
60 """Tests copy_contents(). """ | |
61 contents = 'these are the contents' | |
62 tempdir_path = tempfile.mkdtemp() | |
63 try: | |
64 source_path = os.path.join(tempdir_path, 'source') | |
65 source_url = download_actuals.create_filepath_url(source_path) | |
66 with open(source_path, 'w') as source_handle: | |
67 source_handle.write(contents) | |
68 dest_path = os.path.join(tempdir_path, 'new_subdir', 'dest') | |
69 # Destination subdir does not exist, so copy_contents() should fail | |
70 # if create_subdirs_if_needed is False. | |
71 with self.assertRaises(Exception): | |
72 download_actuals.copy_contents(source_url=source_url, | |
73 dest_path=dest_path, | |
74 create_subdirs_if_needed=False) | |
75 # If create_subdirs_if_needed is True, it should work. | |
76 download_actuals.copy_contents(source_url=source_url, | |
77 dest_path=dest_path, | |
78 create_subdirs_if_needed=True) | |
79 self.assertEquals(open(dest_path).read(), contents) | |
80 finally: | |
81 shutil.rmtree(tempdir_path) | |
82 | |
83 | 47 |
84 def main(): | 48 def main(): |
85 base_unittest.main(DownloadTest) | 49 base_unittest.main(DownloadTest) |
86 | 50 |
87 | 51 |
88 if __name__ == '__main__': | 52 if __name__ == '__main__': |
89 main() | 53 main() |
OLD | NEW |