Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(423)

Side by Side Diff: src/caterpillar_test.py

Issue 1675133002: inject_script_tags no longer crashes when there is no body tag. (Closed) Base URL: https://github.com/chromium/caterpillar.git@master
Patch Set: Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« src/caterpillar.py ('K') | « src/caterpillar.py ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 #!/usr/bin/env python2 1 #!/usr/bin/env python2
2 # -*- coding: utf-8 -*- 2 # -*- coding: utf-8 -*-
3 3
4 # Copyright 2016 Google Inc. All Rights Reserved. 4 # Copyright 2016 Google Inc. All Rights Reserved.
5 # 5 #
6 # Licensed under the Apache License, Version 2.0 (the "License"); 6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License. 7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at 8 # You may obtain a copy of the License at
9 # 9 #
10 # http://www.apache.org/licenses/LICENSE-2.0 10 # http://www.apache.org/licenses/LICENSE-2.0
(...skipping 326 matching lines...) Expand 10 before | Expand all | Expand 10 after
337 caterpillar.inject_script_tags( 337 caterpillar.inject_script_tags(
338 self.soup, [requirement], 'path', BOILERPLATE_DIR, '') 338 self.soup, [requirement], 'path', BOILERPLATE_DIR, '')
339 self.assertIsNotNone( 339 self.assertIsNotNone(
340 self.soup.find('script', 340 self.soup.find('script',
341 src=os.path.join('path', BOILERPLATE_DIR, requirement))) 341 src=os.path.join('path', BOILERPLATE_DIR, requirement)))
342 342
343 def test_many_requirements_tag_existence(self): 343 def test_many_requirements_tag_existence(self):
344 """Tests the correct tags are injected if there are many requirements.""" 344 """Tests the correct tags are injected if there are many requirements."""
345 requirements = ['réq', 'uir', 'edf', 'ile', 's.j', 'ava', 'scr', 'ipt'] 345 requirements = ['réq', 'uir', 'edf', 'ile', 's.j', 'ava', 'scr', 'ipt']
346 caterpillar.inject_script_tags( 346 caterpillar.inject_script_tags(
347 self.soup, requirements, 'path', BOILERPLATE_DIR, '') 347 self.soup, requirements, 'path', BOILERPLATE_DIR, '')
348 for requirement in requirements: 348 for requirement in requirements:
349 self.assertIsNotNone( 349 self.assertIsNotNone(
350 self.soup.find('script', 350 self.soup.find('script',
351 src=os.path.join('path', BOILERPLATE_DIR, 351 src=os.path.join('path', BOILERPLATE_DIR,
352 requirement))) 352 requirement)))
353 353
354 def test_no_body_yes_html(self):
355 """Tests the correct tags are injected if there is no body tag."""
356 html = """\
357 <!DOCTYPE html>
358 <html>
359 <h1>
360 Hello, Wo®ld!
361 </h1>
362 </html>"""
363 soup = bs4.BeautifulSoup(html, 'html.parser')
364 requirements = ['réq', 'uir']
365 caterpillar.inject_script_tags(
366 soup, requirements, 'path', BOILERPLATE_DIR, '')
367 self.assertEqual(re.sub(r'\s+', ' ', unicode(soup)),
368 re.sub(r'\s+', ' ', """\
369 <!DOCTYPE html>
370 <html>
371 <h1>
372 Hello, Wo®ld!
373 </h1>
374 <script src="path/bóilerplate dir/réq"></script><script
375 src="path/bóilerplate dir/uir"></script></html>"""))
376
377 def test_no_body_or_html(self):
378 """Tests the correct tags are injected if there is no body or html tag."""
379 html = """\
380 <!DOCTYPE html>
381 <h1>
382 Hello, Wo®ld!
383 </h1>
384 """
385 soup = bs4.BeautifulSoup(html, 'html.parser')
386 requirements = ['réq', 'uir']
387 caterpillar.inject_script_tags(
388 soup, requirements, 'path', BOILERPLATE_DIR, '')
389 self.assertEqual(re.sub(r'\s+', ' ', unicode(soup)),
390 re.sub(r'\s+', ' ', """\
391 <!DOCTYPE html>
392 <h1>
393 Hello, Wo®ld!
394 </h1>
395 <script src="path/bóilerplate dir/réq"></script><script
396 src="path/bóilerplate dir/uir"></script>"""))
397
354 def test_many_requirements_tag_order(self): 398 def test_many_requirements_tag_order(self):
355 """Tests tags are injected in order.""" 399 """Tests tags are injected in order."""
356 requirements = ['réq', 'uir', 'edf', 'ile', 's.j', 'ava', 'scr', 'ipt'] 400 requirements = ['réq', 'uir', 'edf', 'ile', 's.j', 'ava', 'scr', 'ipt']
357 caterpillar.inject_script_tags( 401 caterpillar.inject_script_tags(
358 self.soup, requirements, 'path', BOILERPLATE_DIR, '') 402 self.soup, requirements, 'path', BOILERPLATE_DIR, '')
359 srcs = [script['src'] for script in self.soup('script')] 403 srcs = [script['src'] for script in self.soup('script')]
360 upto = -1 404 upto = -1
361 for requirement in requirements: 405 for requirement in requirements:
362 # Monotonically increasing indices <-> order is preserved. 406 # Monotonically increasing indices <-> order is preserved.
363 index = srcs.index(os.path.join('path', BOILERPLATE_DIR, requirement)) 407 index = srcs.index(os.path.join('path', BOILERPLATE_DIR, requirement))
(...skipping 336 matching lines...) Expand 10 before | Expand all | Expand 10 after
700 "app": { 744 "app": {
701 "background": {} 745 "background": {}
702 }, 746 },
703 "name": "t\\u00e9st app'" 747 "name": "t\\u00e9st app'"
704 }; 748 };
705 """) 749 """)
706 750
707 751
708 if __name__ == '__main__': 752 if __name__ == '__main__':
709 unittest.main() 753 unittest.main()
OLDNEW
« src/caterpillar.py ('K') | « src/caterpillar.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698