resource_checker_test.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #!/usr/bin/env python3
  2. # Copyright 2015 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.
  5. from os import path as os_path
  6. import re
  7. from . import resource_checker
  8. from sys import path as sys_path
  9. from . import test_util
  10. import unittest
  11. _HERE = os_path.dirname(os_path.abspath(__file__))
  12. sys_path.append(os_path.join(_HERE, '..', '..'))
  13. from PRESUBMIT_test_mocks import MockInputApi, MockOutputApi
  14. class ResourceCheckerTest(unittest.TestCase):
  15. def setUp(self):
  16. super(ResourceCheckerTest, self).setUp()
  17. self.checker = resource_checker.ResourceChecker(MockInputApi(),
  18. MockOutputApi())
  19. def ShouldPassDeprecatedMojoBindingCheck(self, line):
  20. error = self.checker.DeprecatedMojoBindingsCheck(1, line)
  21. self.assertEqual('', error, 'Should not be flagged as error: ' + line)
  22. def ShouldFailDeprecatedMojoBindingCheck(self, line):
  23. error = self.checker.DeprecatedMojoBindingsCheck(1, line)
  24. self.assertNotEqual('', error, 'Should be flagged as error: ' + line)
  25. self.assertEqual('mojo_bindings.js', test_util.GetHighlight(line, error))
  26. def testDeprecatedMojoBindingsCheckPasses(self):
  27. lines = [
  28. '<script src="chrome://resources/js/mojo_bindings_lite.js">',
  29. "script.src = 'chrome://resources/js/mojo_bindings_lite.js';",
  30. ]
  31. for line in lines:
  32. self.ShouldPassDeprecatedMojoBindingCheck(line)
  33. def testDeprecatedMojoBindingsCheckFails(self):
  34. lines = [
  35. '<script src="chrome://resources/js/mojo_bindings.js">',
  36. "script.src = 'chrome://resources/js/mojo_bindings.js';",
  37. ]
  38. for line in lines:
  39. self.ShouldFailDeprecatedMojoBindingCheck(line)
  40. def ShouldPassDisallowIncludeCheck(self, line):
  41. self.assertEqual('', self.checker.DisallowIncludeCheck('msg', 1, line),
  42. 'Should not be flagged as error')
  43. def ShouldFailDisallowIncludeCheck(self, line):
  44. error = self.checker.DisallowIncludeCheck('msg', 1, line)
  45. self.assertNotEqual('', error, 'Should be flagged as error: ' + line)
  46. self.assertEqual('<include', test_util.GetHighlight(line, error))
  47. def testDisallowIncludesFails(self):
  48. lines = [
  49. '<include src="blah.js">',
  50. ' // <include src="blah.js">',
  51. ' /* <include src="blah.js"> */ ',
  52. ]
  53. for line in lines:
  54. self.ShouldFailDisallowIncludeCheck(line)
  55. def testDisallowIncludesPasses(self):
  56. lines = [
  57. 'if (count < includeCount) {',
  58. '// No <include>s allowed.',
  59. ]
  60. for line in lines:
  61. self.ShouldPassDisallowIncludeCheck(line)
  62. def ShouldFailSelfClosingIncludeCheck(self, line):
  63. """Checks that the '</include>' checker flags |line| as a style error."""
  64. error = self.checker.SelfClosingIncludeCheck(1, line)
  65. self.assertNotEqual('', error,
  66. 'Should be flagged as style error: ' + line)
  67. highlight = test_util.GetHighlight(line, error).strip()
  68. self.assertTrue('include' in highlight and highlight[0] == '<')
  69. def ShouldPassSelfClosingIncludeCheck(self, line):
  70. """Checks that the '</include>' checker doesn't flag |line| as an error."""
  71. self.assertEqual('', self.checker.SelfClosingIncludeCheck(1, line),
  72. 'Should not be flagged as style error: ' + line)
  73. def testIncludeFails(self):
  74. lines = [
  75. "</include> ",
  76. " </include>",
  77. " </include> ",
  78. ' <include src="blah.js" /> ',
  79. '<include src="blee.js"/>',
  80. ]
  81. for line in lines:
  82. self.ShouldFailSelfClosingIncludeCheck(line)
  83. def testIncludePasses(self):
  84. lines = [
  85. '<include src="assert.js">',
  86. "<include src='../../assert.js'>",
  87. "<i>include src='blah'</i>",
  88. "</i>nclude",
  89. "</i>include",
  90. ]
  91. for line in lines:
  92. self.ShouldPassSelfClosingIncludeCheck(line)
  93. if __name__ == '__main__':
  94. unittest.main()