OLD | NEW |
---|---|
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright 2013 The Chromium Authors. All rights reserved. | 2 # Copyright 2013 The Chromium Authors. All rights reserved. |
3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 # Run build_server so that files needed by tests are copied to the local | 6 # Run build_server so that files needed by tests are copied to the local |
7 # third_party directory. | 7 # third_party directory. |
8 import build_server | 8 import build_server |
9 build_server.main() | 9 build_server.main() |
10 | 10 |
11 import json | 11 import json |
12 import optparse | 12 import optparse |
13 import os | 13 import os |
14 import posixpath | 14 import posixpath |
15 import sys | 15 import sys |
16 import time | 16 import time |
17 import unittest | 17 import unittest |
18 | 18 |
19 from branch_utility import BranchUtility | 19 from branch_utility import BranchUtility |
20 from chroot_file_system import ChrootFileSystem | 20 from chroot_file_system import ChrootFileSystem |
21 from extensions_paths import EXTENSIONS, PUBLIC_TEMPLATES | 21 from extensions_paths import CONTENT_PROVIDERS, EXTENSIONS, PUBLIC_TEMPLATES |
22 from fake_fetchers import ConfigureFakeFetchers | 22 from fake_fetchers import ConfigureFakeFetchers |
23 from third_party.json_schema_compiler import json_parse | |
23 from handler import Handler | 24 from handler import Handler |
24 from link_error_detector import LinkErrorDetector, StringifyBrokenLinks | 25 from link_error_detector import LinkErrorDetector, StringifyBrokenLinks |
25 from local_file_system import LocalFileSystem | 26 from local_file_system import LocalFileSystem |
26 from local_renderer import LocalRenderer | 27 from local_renderer import LocalRenderer |
27 from servlet import Request | 28 from servlet import Request |
28 from test_util import EnableLogging, DisableLogging, ChromiumPath | 29 from test_util import ChromiumPath, DisableLogging, EnableLogging, ReadFile |
30 | |
29 | 31 |
30 # Arguments set up if __main__ specifies them. | 32 # Arguments set up if __main__ specifies them. |
31 _EXPLICIT_TEST_FILES = None | 33 _EXPLICIT_TEST_FILES = None |
32 _REBASE = False | 34 _REBASE = False |
33 _VERBOSE = False | 35 _VERBOSE = False |
34 | 36 |
35 | 37 |
36 def _ToPosixPath(os_path): | 38 def _ToPosixPath(os_path): |
37 return os_path.replace(os.sep, '/') | 39 return os_path.replace(os.sep, '/') |
38 | 40 |
41 | |
42 def _FilterHidden(paths): | |
43 '''Returns a list of the non-hidden paths from |paths| | |
Yoyo Zhou
2014/02/04 21:42:13
nit: trailing .
not at google - send to devlin
2014/02/04 21:57:13
Done.
| |
44 ''' | |
45 # Hidden files start with a '.' but paths like './foo' and '../foo' are not | |
46 # hidden. | |
47 return [path for path in paths if (not path.startswith('.')) or | |
Yoyo Zhou
2014/02/04 21:42:13
What I meant by 'any component starting with .' is
not at google - send to devlin
2014/02/04 21:57:13
Ah right. So - you're right in that this method wo
Yoyo Zhou
2014/02/04 21:59:56
Ah, I see. I should've actually tried out os.walk.
| |
48 path.startswith('./') or | |
49 path.startswith('../')] | |
50 | |
51 | |
39 def _GetPublicFiles(): | 52 def _GetPublicFiles(): |
40 '''Gets all public files mapped to their contents. | 53 '''Gets all public file paths mapped to their contents. |
41 ''' | 54 ''' |
42 public_path = ChromiumPath(PUBLIC_TEMPLATES) | 55 def walk(path, prefix=''): |
56 path = ChromiumPath(path) | |
57 public_files = {} | |
58 for root, dirs, files in os.walk(path, topdown=True): | |
59 relative_root = root[len(path):].lstrip(os.path.sep) | |
60 dirs[:] = _FilterHidden(dirs) | |
61 for filename in _FilterHidden(files): | |
62 with open(os.path.join(root, filename), 'r') as f: | |
63 request_path = posixpath.join(prefix, relative_root, filename) | |
64 public_files[request_path] = f.read() | |
65 return public_files | |
66 | |
67 # Public file locations are defined in content_providers.json, sort of. Epic | |
68 # hack to pull them out; list all the files from the directories that | |
69 # Chromium content providers ask for. | |
43 public_files = {} | 70 public_files = {} |
44 for path, dirs, files in os.walk(public_path, topdown=True): | 71 content_providers = json_parse.Parse(ReadFile(CONTENT_PROVIDERS)) |
45 dirs[:] = [d for d in dirs if d != '.svn'] | 72 for content_provider in content_providers.itervalues(): |
46 relative_posix_path = _ToPosixPath(path[len(public_path):]) | 73 if 'chromium' in content_provider: |
47 for filename in files: | 74 public_files.update(walk(content_provider['chromium']['dir'], |
48 with open(os.path.join(path, filename), 'r') as f: | 75 prefix=content_provider['serveFrom'])) |
49 public_files['/'.join((relative_posix_path, filename))] = f.read() | |
50 return public_files | 76 return public_files |
51 | 77 |
78 | |
52 class IntegrationTest(unittest.TestCase): | 79 class IntegrationTest(unittest.TestCase): |
53 def setUp(self): | 80 def setUp(self): |
54 ConfigureFakeFetchers() | 81 ConfigureFakeFetchers() |
55 | 82 |
56 @EnableLogging('info') | 83 @EnableLogging('info') |
57 def testCronAndPublicFiles(self): | 84 def testCronAndPublicFiles(self): |
58 '''Runs cron then requests every public file. Cron needs to be run first | 85 '''Runs cron then requests every public file. Cron needs to be run first |
59 because the public file requests are offline. | 86 because the public file requests are offline. |
60 ''' | 87 ''' |
61 if _EXPLICIT_TEST_FILES is not None: | 88 if _EXPLICIT_TEST_FILES is not None: |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
116 # for page in orphaned_pages: | 143 # for page in orphaned_pages: |
117 # print(page) | 144 # print(page) |
118 #print('Took %s seconds.' % (time.time() - start_time)) | 145 #print('Took %s seconds.' % (time.time() - start_time)) |
119 | 146 |
120 public_files = _GetPublicFiles() | 147 public_files = _GetPublicFiles() |
121 | 148 |
122 print('Rendering %s public files...' % len(public_files.keys())) | 149 print('Rendering %s public files...' % len(public_files.keys())) |
123 start_time = time.time() | 150 start_time = time.time() |
124 try: | 151 try: |
125 for path, content in public_files.iteritems(): | 152 for path, content in public_files.iteritems(): |
126 assert path.startswith('/') | 153 assert not path.startswith('/') |
127 if path.endswith('redirects.json'): | 154 if path.endswith('redirects.json'): |
128 continue | 155 continue |
129 | 156 |
130 def check_result(response): | 157 def check_result(response): |
131 self.assertEqual(200, response.status, | 158 self.assertEqual(200, response.status, |
132 'Got %s when rendering %s' % (response.status, path)) | 159 'Got %s when rendering %s' % (response.status, path)) |
133 # This is reaaaaally rough since usually these will be tiny templates | 160 # This is reaaaaally rough since usually these will be tiny templates |
134 # that render large files. At least it'll catch zero-length responses. | 161 # that render large files. At least it'll catch zero-length responses. |
135 self.assertTrue(len(response.content) >= len(content), | 162 self.assertTrue(len(response.content) >= len(content), |
136 'Content was "%s" when rendering %s' % (response.content, path)) | 163 'Rendered content length was %s vs template content length %s ' |
164 'when rendering %s' % (len(response.content), len(content), path)) | |
137 | 165 |
138 check_result(Handler(Request.ForTest(path)).Get()) | 166 check_result(Handler(Request.ForTest(path)).Get()) |
139 | 167 |
140 # Make sure that leaving out the .html will temporarily redirect to the | 168 if path.startswith(('apps/', 'extensions/')): |
141 # path with the .html. | 169 # Make sure that leaving out the .html will temporarily redirect to |
142 if path.startswith(('/apps/', '/extensions/')): | 170 # the path with the .html for APIs and articles. |
143 redirect_result = Handler( | 171 if '/examples/' not in path: |
144 Request.ForTest(posixpath.splitext(path)[0])).Get() | 172 base, _ = posixpath.splitext(path) |
145 self.assertEqual((path, False), redirect_result.GetRedirect()) | 173 self.assertEqual( |
174 ('/' + path, False), | |
175 Handler(Request.ForTest(base)).Get().GetRedirect(), | |
176 '"%s" did not (temporary) redirect to "%s.html"' % (path, path)) | |
Yoyo Zhou
2014/02/04 21:42:13
nit: temporarily
not at google - send to devlin
2014/02/04 21:57:13
done, and then a bit to make it <= 80 chars :)
| |
146 | 177 |
147 # Make sure including a channel will permanently redirect to the same | 178 # Make sure including a channel will permanently redirect to the same |
148 # path without a channel. | 179 # path without a channel. |
149 for channel in BranchUtility.GetAllChannelNames(): | 180 for channel in BranchUtility.GetAllChannelNames(): |
150 redirect_result = Handler( | 181 redirect_result = Handler( |
151 Request.ForTest('%s%s' % (channel, path))).Get() | 182 Request.ForTest(posixpath.join(channel, path))).Get() |
152 self.assertEqual((path, True), redirect_result.GetRedirect()) | 183 self.assertEqual( |
184 ('/' + path, True), | |
185 redirect_result.GetRedirect(), | |
186 '"%s" did not redirect to strip channel "%s"' % (path, channel)) | |
153 | 187 |
154 # Samples are internationalized, test some locales. | 188 # Samples are internationalized, test some locales. |
155 if path.endswith('/samples.html'): | 189 if path.endswith('/samples.html'): |
156 for lang in ['en-US', 'es', 'ar']: | 190 for lang in ('en-US', 'es', 'ar'): |
157 check_result(Handler(Request.ForTest( | 191 check_result(Handler(Request.ForTest( |
158 path, | 192 path, |
159 headers={'Accept-Language': '%s;q=0.8' % lang})).Get()) | 193 headers={'Accept-Language': '%s;q=0.8' % lang})).Get()) |
160 finally: | 194 finally: |
161 print('Took %s seconds' % (time.time() - start_time)) | 195 print('Took %s seconds' % (time.time() - start_time)) |
162 | 196 |
163 #if _REBASE: | 197 #if _REBASE: |
164 # print('Rebasing broken links with %s newly broken and %s fixed links.' % | 198 # print('Rebasing broken links with %s newly broken and %s fixed links.' % |
165 # (len(newly_broken_links), len(fixed_links))) | 199 # (len(newly_broken_links), len(fixed_links))) |
166 # with open(known_broken_links_path, 'w') as f: | 200 # with open(known_broken_links_path, 'w') as f: |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
219 parser.add_option('-v', '--verbose', action='store_true', default=False, | 253 parser.add_option('-v', '--verbose', action='store_true', default=False, |
220 help='Show verbose output like currently broken links') | 254 help='Show verbose output like currently broken links') |
221 (opts, args) = parser.parse_args() | 255 (opts, args) = parser.parse_args() |
222 if not opts.all: | 256 if not opts.all: |
223 _EXPLICIT_TEST_FILES = args | 257 _EXPLICIT_TEST_FILES = args |
224 _REBASE = opts.rebase | 258 _REBASE = opts.rebase |
225 _VERBOSE = opts.verbose | 259 _VERBOSE = opts.verbose |
226 # Kill sys.argv because we have our own flags. | 260 # Kill sys.argv because we have our own flags. |
227 sys.argv = [sys.argv[0]] | 261 sys.argv = [sys.argv[0]] |
228 unittest.main() | 262 unittest.main() |
OLD | NEW |