Chromium Code Reviews| 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 logging | 11 from itertools import groupby |
| 12 from operator import itemgetter | |
| 12 import optparse | 13 import optparse |
| 13 import os | 14 import os |
| 14 import sys | 15 import sys |
| 15 import time | 16 import time |
| 16 import unittest | 17 import unittest |
| 17 | 18 |
| 19 from link_error_detector import LinkErrorDetector | |
| 20 from local_file_system import LocalFileSystem | |
| 18 from local_renderer import LocalRenderer | 21 from local_renderer import LocalRenderer |
| 19 from fake_fetchers import ConfigureFakeFetchers | 22 from fake_fetchers import ConfigureFakeFetchers |
| 20 from handler import Handler | 23 from handler import Handler |
| 21 from servlet import Request | 24 from servlet import Request |
| 22 from test_util import EnableLogging, DisableLogging | 25 from test_util import EnableLogging, DisableLogging |
| 23 | 26 |
| 24 # Arguments set up if __main__ specifies them. | 27 # Arguments set up if __main__ specifies them. |
| 25 _EXPLICIT_TEST_FILES = None | 28 _EXPLICIT_TEST_FILES = None |
| 26 | 29 |
| 27 def _ToPosixPath(os_path): | 30 def _ToPosixPath(os_path): |
| 28 return os_path.replace(os.sep, '/') | 31 return os_path.replace(os.sep, '/') |
| 29 | 32 |
| 30 def _GetPublicFiles(): | 33 def _GetPublicFiles(): |
| 31 '''Gets all public files mapped to their contents. | 34 '''Gets all public files mapped to their contents. |
| 32 ''' | 35 ''' |
| 33 public_path = os.path.join(sys.path[0], os.pardir, 'templates', 'public') | 36 public_path = os.path.join(sys.path[0], os.pardir, 'templates', 'public') |
| 34 public_files = {} | 37 public_files = {} |
| 35 for path, dirs, files in os.walk(public_path, topdown=True): | 38 for path, dirs, files in os.walk(public_path, topdown=True): |
| 36 dirs[:] = [d for d in dirs if d != '.svn'] | 39 dirs[:] = [d for d in dirs if d != '.svn'] |
| 37 relative_posix_path = _ToPosixPath(path[len(public_path):]) | 40 relative_posix_path = _ToPosixPath(path[len(public_path):]) |
| 38 for filename in files: | 41 for filename in files: |
| 39 with open(os.path.join(path, filename), 'r') as f: | 42 with open(os.path.join(path, filename), 'r') as f: |
| 40 public_files['/'.join((relative_posix_path, filename))] = f.read() | 43 public_files['/'.join((relative_posix_path, filename))] = f.read() |
| 41 return public_files | 44 return public_files |
| 42 | 45 |
| 46 def _PrintBrokenLinks(broken_links): | |
| 47 '''Prints out broken links in a more readable format. | |
| 48 ''' | |
| 49 col_width = max([len(l[0]) for l in broken_links]) | |
|
not at google - send to devlin
2013/07/17 20:19:40
the square brackets aren't needed (just creates re
jshumway
2013/07/17 22:15:47
Done.
| |
| 50 getter = itemgetter(1) | |
| 51 | |
| 52 for target, links in groupby(sorted(broken_links, key=getter), getter): | |
| 53 links = [l[0] for l in links] | |
| 54 if len(links) > 50: | |
| 55 out = "%s and %d others" % (links[0], len(links) - 1) | |
| 56 print("%s%s -> %s" % (out, (col_width - len(out)) * ' ', target)) | |
| 57 else: | |
| 58 for link in links: | |
| 59 print("%s%s -> %s" % (link, (col_width - len(link)) * ' ', target)) | |
|
not at google - send to devlin
2013/07/17 20:19:40
can you pull this print logic into a function? Lik
jshumway
2013/07/17 22:15:47
Done.
| |
| 60 | |
| 43 class IntegrationTest(unittest.TestCase): | 61 class IntegrationTest(unittest.TestCase): |
| 44 def setUp(self): | 62 def setUp(self): |
| 45 ConfigureFakeFetchers() | 63 ConfigureFakeFetchers() |
| 46 | 64 |
| 47 @EnableLogging('info') | 65 @EnableLogging('info') |
| 48 def testCronAndPublicFiles(self): | 66 def testCronAndPublicFiles(self): |
| 49 '''Runs cron then requests every public file. Cron needs to be run first | 67 '''Runs cron then requests every public file. Cron needs to be run first |
| 50 because the public file requests are offline. | 68 because the public file requests are offline. |
| 51 ''' | 69 ''' |
| 52 if _EXPLICIT_TEST_FILES is not None: | 70 if _EXPLICIT_TEST_FILES is not None: |
| 53 return | 71 return |
| 54 | 72 |
| 55 print('Running cron...') | 73 print('Running cron...') |
| 56 start_time = time.time() | 74 start_time = time.time() |
| 57 try: | 75 try: |
| 58 response = Handler(Request.ForTest('/_cron/stable')).Get() | 76 response = Handler(Request.ForTest('/_cron/stable')).Get() |
| 59 self.assertEqual(200, response.status) | 77 self.assertEqual(200, response.status) |
| 60 self.assertEqual('Success', response.content.ToString()) | 78 self.assertEqual('Success', response.content.ToString()) |
| 61 finally: | 79 finally: |
| 62 print('Took %s seconds' % (time.time() - start_time)) | 80 print('Took %s seconds' % (time.time() - start_time)) |
| 63 | 81 |
| 82 # Check for broken links. | |
|
not at google - send to devlin
2013/07/17 20:19:40
comment is redundant given the next line
jshumway
2013/07/17 22:15:47
Done.
| |
| 83 print("Checking for broken links...") | |
| 84 start_time = time.time() | |
| 85 link_error_detector = LinkErrorDetector( | |
| 86 LocalFileSystem(os.path.join(sys.path[0], os.pardir, os.pardir)), | |
| 87 lambda path: Handler(Request.ForTest(path)).Get(), | |
| 88 'templates/public', | |
| 89 ('extensions/index.html', 'apps/about_apps.html')) | |
| 90 | |
| 91 broken_links, broken_anchors = link_error_detector.GetBrokenLinks() | |
| 92 if broken_links or broken_anchors: | |
| 93 print('[%d broken links] page of occurrence\t-> href of broken link' % ( | |
| 94 len(broken_links) + len(broken_anchors))) | |
|
not at google - send to devlin
2013/07/17 20:19:40
logging will look a little bit awkward since align
jshumway
2013/07/17 22:15:47
Done.
| |
| 95 if broken_links: | |
| 96 _PrintBrokenLinks(broken_links) | |
| 97 if broken_anchors: | |
| 98 _PrintBrokenLinks(broken_anchors) | |
|
not at google - send to devlin
2013/07/17 20:19:40
make sure you actually fail the test at this point
jshumway
2013/07/17 22:15:47
Prints a warning.
| |
| 99 | |
| 100 print('Took %s seconds.' % (time.time() - start_time)) | |
| 101 | |
| 102 # Check for orphaned pages. | |
|
not at google - send to devlin
2013/07/17 20:19:40
comment also redundant
jshumway
2013/07/17 22:15:47
Done.
| |
| 103 print('Searching for orphaned pages...') | |
| 104 start_time = time.time() | |
| 105 orphaned_pages = link_error_detector.GetOrphanedPages() | |
| 106 if orphaned_pages: | |
| 107 print('[%d orphaned pages]' % len(orphaned_pages)) | |
|
not at google - send to devlin
2013/07/17 20:19:40
Found %d orphaned pages:
jshumway
2013/07/17 22:15:47
Done.
| |
| 108 for page in orphaned_pages: | |
| 109 print(page) | |
| 110 print('Took %s seconds.' % (time.time() - start_time)) | |
|
not at google - send to devlin
2013/07/17 20:19:40
likewise, fail the test (self.fail maybe?)
jshumway
2013/07/17 22:15:47
Prints a warning.
| |
| 111 | |
| 64 public_files = _GetPublicFiles() | 112 public_files = _GetPublicFiles() |
| 65 | 113 |
| 66 print('Rendering %s public files...' % len(public_files.keys())) | 114 print('Rendering %s public files...' % len(public_files.keys())) |
| 67 start_time = time.time() | 115 start_time = time.time() |
| 68 try: | 116 try: |
| 69 for path, content in public_files.iteritems(): | 117 for path, content in public_files.iteritems(): |
| 70 if path.endswith('redirects.json'): | 118 if path.endswith('redirects.json'): |
| 71 continue | 119 continue |
| 72 def check_result(response): | 120 def check_result(response): |
| 73 self.assertEqual(200, response.status, | 121 self.assertEqual(200, response.status, |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 96 for filename in _EXPLICIT_TEST_FILES: | 144 for filename in _EXPLICIT_TEST_FILES: |
| 97 print('Rendering %s...' % filename) | 145 print('Rendering %s...' % filename) |
| 98 start_time = time.time() | 146 start_time = time.time() |
| 99 try: | 147 try: |
| 100 response = LocalRenderer.Render(_ToPosixPath(filename)) | 148 response = LocalRenderer.Render(_ToPosixPath(filename)) |
| 101 self.assertEqual(200, response.status) | 149 self.assertEqual(200, response.status) |
| 102 self.assertTrue(response.content != '') | 150 self.assertTrue(response.content != '') |
| 103 finally: | 151 finally: |
| 104 print('Took %s seconds' % (time.time() - start_time)) | 152 print('Took %s seconds' % (time.time() - start_time)) |
| 105 | 153 |
| 154 # TODO: Check page for broken links (currently prohibited by the time it | |
|
not at google - send to devlin
2013/07/17 20:19:40
always name TODOs. so TODO(jshumway) here.
jshumway
2013/07/17 22:15:47
Done.
| |
| 155 # takes to render the pages). | |
| 156 | |
| 106 @DisableLogging('warning') | 157 @DisableLogging('warning') |
| 107 def testFileNotFound(self): | 158 def testFileNotFound(self): |
| 108 response = Handler(Request.ForTest('/extensions/notfound.html')).Get() | 159 response = Handler(Request.ForTest('/extensions/notfound.html')).Get() |
| 109 self.assertEqual(404, response.status) | 160 self.assertEqual(404, response.status) |
| 110 | 161 |
| 111 if __name__ == '__main__': | 162 if __name__ == '__main__': |
| 112 parser = optparse.OptionParser() | 163 parser = optparse.OptionParser() |
| 113 parser.add_option('-a', '--all', action='store_true', default=False) | 164 parser.add_option('-a', '--all', action='store_true', default=False) |
| 114 (opts, args) = parser.parse_args() | 165 (opts, args) = parser.parse_args() |
| 115 if not opts.all: | 166 if not opts.all: |
| 116 _EXPLICIT_TEST_FILES = args | 167 _EXPLICIT_TEST_FILES = args |
| 117 # Kill sys.argv because we have our own flags. | 168 # Kill sys.argv because we have our own flags. |
| 118 sys.argv = [sys.argv[0]] | 169 sys.argv = [sys.argv[0]] |
| 119 unittest.main() | 170 unittest.main() |
| OLD | NEW |