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
4 # found in the LICENSE file.
8 from file_system
import FileNotFoundError
9 from link_error_detector
import LinkErrorDetector
10 from servlet
import Response
11 from test_file_system
import TestFileSystem
13 file_system
= TestFileSystem({
20 <h1 id="actual-top">Hello</h1>
21 <a href="#top">world</p>
22 <a href="#actual-top">!</a>
23 <a href="broken.json"></a>
24 <a href="crx.html"></a>
27 <a href="index.html#actual-top">back</a>
28 <a href="broken.html"></a>
29 <a href="devtools.events.html">do to underscore translation</a>
31 'devtools_events.html': '''
32 <a href=" http://www.google.com/">leading space in href</a>
33 <a href=" index.html">home</a>
34 <a href="index.html#invalid"></a>
35 <a href="fake.html#invalid"></a>
37 'unreachable.html': '''
40 <a href="invalid.html"></a>
42 'devtools_disconnected.html': ''
51 class LinkErrorDetectorTest(unittest
.TestCase
):
52 def _Render(self
, path
):
55 content
=file_system
.ReadSingle('docs/templates/public/' + path
).Get(),
57 except FileNotFoundError
:
58 return Response(status
=404)
60 def testGetBrokenLinks(self
):
61 expected_broken_links
= set([
62 (404, 'apps/crx.html', 'apps/broken.html', 'target page not found'),
63 (404, 'apps/index.html', 'apps/broken.json', 'target page not found'),
64 (404, 'apps/unreachable.html', 'apps/invalid.html',
65 'target page not found'),
66 (404, 'apps/devtools_events.html', 'apps/fake.html#invalid',
67 'target page not found'),
68 (200, 'apps/devtools_events.html', 'apps/index.html#invalid',
69 'target anchor not found'),
70 (200, 'apps/unreachable.html', '#aoesu', 'target anchor not found')])
72 link_error_detector
= LinkErrorDetector(
73 file_system
, self
._Render
, 'templates/public/', ('apps/index.html'))
74 broken_links
= link_error_detector
.GetBrokenLinks()
76 self
.assertEqual(expected_broken_links
, set(broken_links
))
78 def testGetOrphanedPages(self
):
79 expected_orphaned_pages
= set([
80 'apps/unreachable.html',
81 'apps/devtools_disconnected.html'])
83 link_error_detector
= LinkErrorDetector(
84 file_system
, self
._Render
, 'templates/public/', ('apps/crx.html',))
85 orphaned_pages
= link_error_detector
.GetOrphanedPages()
87 self
.assertEqual(expected_orphaned_pages
, set(orphaned_pages
))
89 if __name__
== '__main__':