• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2015 The Chromium Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5import unittest
6
7from catapult_build import html_checks
8
9
10class MockAffectedFile(object):
11
12  def __init__(self, path, lines):
13    self.path = path
14    self.lines = lines
15
16  def NewContents(self):
17    return (l for l in self.lines)
18
19  def LocalPath(self):
20    return self.path
21
22
23class MockInputApi(object):
24
25  def __init__(self, affected_files):
26    self.affected_files = affected_files
27
28  def AffectedFiles(self, file_filter=None, **_):
29    if file_filter:
30      return filter(file_filter, self.affected_files)
31    return self.affected_files
32
33
34class MockOutputApi(object):
35
36  def PresubmitError(self, error_text):
37    return error_text
38
39
40class HtmlChecksTest(unittest.TestCase):
41
42  def testRunChecksShowsErrorForWrongDoctype(self):
43    f = MockAffectedFile('foo/x.html', ['<!DOCTYPE XHTML1.0>'])
44    errors = html_checks.RunChecks(MockInputApi([f]), MockOutputApi())
45    self.assertEqual(1, len(errors))
46
47  def testRunChecksReturnsErrorForEmptyFile(self):
48    f = MockAffectedFile('foo/x.html', [])
49    errors = html_checks.RunChecks(MockInputApi([f]), MockOutputApi())
50    self.assertEqual(1, len(errors))
51
52  def testRunChecksNoErrorsForFileWithCorrectDocstring(self):
53    f = MockAffectedFile('foo/x.html', ['<!DOCTYPE html> '])
54    errors = html_checks.RunChecks(MockInputApi([f]), MockOutputApi())
55    self.assertEqual([], errors)
56
57  def testRunChecksAcceptsDifferentCapitalization(self):
58    f = MockAffectedFile('foo/x.html', ['<!doctype HtMl> '])
59    errors = html_checks.RunChecks(MockInputApi([f]), MockOutputApi())
60    self.assertEqual([], errors)
61
62  def testRunChecksAcceptsCommentsBeforeDoctype(self):
63    f = MockAffectedFile('foo/x.html', ['<!-- asdf -->\n<!doctype html> '])
64    errors = html_checks.RunChecks(MockInputApi([f]), MockOutputApi())
65    self.assertEqual([], errors)
66
67  def testRunChecksSkipsFilesInExcludedPaths(self):
68    f = MockAffectedFile('foo/x.html', ['<!DOCTYPE html XHTML1.0>'])
69    errors = html_checks.RunChecks(
70        MockInputApi([f]), MockOutputApi(), excluded_paths=['^foo/.*'])
71    self.assertEqual([], errors)
72
73  def testRunChecksSkipsNonHtmlFiles(self):
74    f = MockAffectedFile('foo/bar.py', ['#!/usr/bin/python', 'print 10'])
75    errors = html_checks.RunChecks(MockInputApi([f]), MockOutputApi())
76    self.assertEqual([], errors)
77
78  def testRunChecksShowsErrorForOutOfOrderImports(self):
79    f = MockAffectedFile('foo/x.html', [
80        '<!DOCTYPE html>',
81        '<link rel="import" href="b.html">',
82        '<link rel="import" href="a.html">',
83    ])
84    errors = html_checks.RunChecks(MockInputApi([f]), MockOutputApi())
85    self.assertEqual(1, len(errors))
86
87  def testRunChecksSkipsSuppressedOutOfOrderImports(self):
88    f = MockAffectedFile('foo/x.html', [
89        '<!DOCTYPE html>',
90        '<link rel="import" href="b.html" data-suppress-import-order>',
91        '<link rel="import" href="a.html">',
92    ])
93    errors = html_checks.RunChecks(MockInputApi([f]), MockOutputApi())
94    self.assertEqual([], errors)
95