watchlists_unittest.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. #!/usr/bin/env vpython3
  2. # Copyright (c) 2011 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. """Unit tests for watchlists.py."""
  6. # pylint: disable=E1103,no-value-for-parameter,protected-access
  7. import os
  8. import sys
  9. import unittest
  10. from unittest import mock
  11. sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
  12. import watchlists
  13. class WatchlistsTest(unittest.TestCase):
  14. def setUp(self):
  15. super(WatchlistsTest, self).setUp()
  16. mock.patch('watchlists.Watchlists._HasWatchlistsFile').start()
  17. mock.patch('watchlists.Watchlists._ContentsOfWatchlistsFile').start()
  18. mock.patch('watchlists.logging.error').start()
  19. self.addCleanup(mock.patch.stopall)
  20. def testMissingWatchlistsFileOK(self):
  21. """Test that we act gracefully if WATCHLISTS file is missing."""
  22. watchlists.Watchlists._HasWatchlistsFile.return_value = False
  23. wl = watchlists.Watchlists('/some/random/path')
  24. self.assertEqual(wl.GetWatchersForPaths(['some_path']), [])
  25. def testGarbledWatchlistsFileOK(self):
  26. """Test that we act gracefully if WATCHLISTS file is garbled."""
  27. contents = 'some garbled and unwanted text'
  28. watchlists.Watchlists._HasWatchlistsFile.return_value = True
  29. watchlists.Watchlists._ContentsOfWatchlistsFile.return_value = contents
  30. wl = watchlists.Watchlists('/a/path')
  31. self.assertEqual(wl.GetWatchersForPaths(['some_path']), [])
  32. def testNoWatchers(self):
  33. contents = \
  34. """{
  35. 'WATCHLIST_DEFINITIONS': {
  36. 'a_module': {
  37. 'filepath': 'a_module',
  38. },
  39. },
  40. 'WATCHLISTS': {
  41. 'a_module': [],
  42. },
  43. } """
  44. watchlists.Watchlists._HasWatchlistsFile.return_value = True
  45. watchlists.Watchlists._ContentsOfWatchlistsFile.return_value = contents
  46. wl = watchlists.Watchlists('/a/path')
  47. self.assertEqual(wl.GetWatchersForPaths(['a_module']), [])
  48. def testValidWatcher(self):
  49. watchers = ['abc@def.com', 'x1@xyz.org']
  50. contents = \
  51. """{
  52. 'WATCHLIST_DEFINITIONS': {
  53. 'a_module': {
  54. 'filepath': 'a_module',
  55. },
  56. },
  57. 'WATCHLISTS': {
  58. 'a_module': %s,
  59. },
  60. } """ % watchers
  61. watchlists.Watchlists._HasWatchlistsFile.return_value = True
  62. watchlists.Watchlists._ContentsOfWatchlistsFile.return_value = contents
  63. wl = watchlists.Watchlists('/a/path')
  64. self.assertEqual(wl.GetWatchersForPaths(['a_module']), watchers)
  65. def testMultipleWatchlistsTrigger(self):
  66. """Test that multiple watchlists can get triggered for one filepath."""
  67. contents = \
  68. """{
  69. 'WATCHLIST_DEFINITIONS': {
  70. 'mac': {
  71. 'filepath': 'mac',
  72. },
  73. 'views': {
  74. 'filepath': 'views',
  75. },
  76. },
  77. 'WATCHLISTS': {
  78. 'mac': ['x1@chromium.org'],
  79. 'views': ['x2@chromium.org'],
  80. },
  81. } """
  82. watchlists.Watchlists._HasWatchlistsFile.return_value = True
  83. watchlists.Watchlists._ContentsOfWatchlistsFile.return_value = contents
  84. wl = watchlists.Watchlists('/a/path')
  85. self.assertEqual(wl.GetWatchersForPaths(['file_views_mac']),
  86. ['x1@chromium.org', 'x2@chromium.org'])
  87. def testDuplicateWatchers(self):
  88. """Test that multiple watchlists can get triggered for one filepath."""
  89. watchers = ['someone@chromium.org']
  90. contents = \
  91. """{
  92. 'WATCHLIST_DEFINITIONS': {
  93. 'mac': {
  94. 'filepath': 'mac',
  95. },
  96. 'views': {
  97. 'filepath': 'views',
  98. },
  99. },
  100. 'WATCHLISTS': {
  101. 'mac': %s,
  102. 'views': %s,
  103. },
  104. } """ % (watchers, watchers)
  105. watchlists.Watchlists._HasWatchlistsFile.return_value = True
  106. watchlists.Watchlists._ContentsOfWatchlistsFile.return_value = contents
  107. wl = watchlists.Watchlists('/a/path')
  108. self.assertEqual(wl.GetWatchersForPaths(['file_views_mac']), watchers)
  109. def testWinPathWatchers(self):
  110. """Test watchers for a windows path (containing backward slashes)."""
  111. watchers = ['abc@def.com', 'x1@xyz.org']
  112. contents = \
  113. """{
  114. 'WATCHLIST_DEFINITIONS': {
  115. 'browser': {
  116. 'filepath': 'chrome/browser/.*',
  117. },
  118. },
  119. 'WATCHLISTS': {
  120. 'browser': %s,
  121. },
  122. } """ % watchers
  123. saved_sep = watchlists.os.sep
  124. watchlists.os.sep = '\\' # to pose as win32
  125. watchlists.Watchlists._HasWatchlistsFile.return_value = True
  126. watchlists.Watchlists._ContentsOfWatchlistsFile.return_value = contents
  127. wl = watchlists.Watchlists(r'a\path')
  128. returned_watchers = wl.GetWatchersForPaths(
  129. [r'chrome\browser\renderer_host\render_widget_host.h'])
  130. watchlists.os.sep = saved_sep # revert back os.sep before asserts
  131. self.assertEqual(returned_watchers, watchers)
  132. if __name__ == '__main__':
  133. import unittest
  134. unittest.main()