watchlists_unittest.py 5.03 KB
Newer Older
1 2
#!/usr/bin/env python
# Copyright (c) 2011 The Chromium Authors. All rights reserved.
3 4 5 6 7
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""Unit tests for watchlists.py."""

8 9 10 11 12 13
# pylint: disable=E1103,E1120,W0212

import os
import sys

sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
14

15 16
from testing_support import super_mox

17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
import watchlists


class WatchlistsTest(super_mox.SuperMoxTestBase):

  def setUp(self):
    super_mox.SuperMoxTestBase.setUp(self)
    self.mox.StubOutWithMock(watchlists.Watchlists, '_HasWatchlistsFile')
    self.mox.StubOutWithMock(watchlists.Watchlists, '_ContentsOfWatchlistsFile')
    self.mox.StubOutWithMock(watchlists.logging, 'error')

  def testMissingWatchlistsFileOK(self):
    """Test that we act gracefully if WATCHLISTS file is missing."""
    watchlists.Watchlists._HasWatchlistsFile().AndReturn(False)
    self.mox.ReplayAll()

    wl = watchlists.Watchlists('/some/random/path')
    self.assertEqual(wl.GetWatchersForPaths(['some_path']), [])

  def testGarbledWatchlistsFileOK(self):
    """Test that we act gracefully if WATCHLISTS file is garbled."""
    contents = 'some garbled and unwanted text'
    watchlists.Watchlists._HasWatchlistsFile().AndReturn(True)
    watchlists.Watchlists._ContentsOfWatchlistsFile().AndReturn(contents)
    watchlists.logging.error(super_mox.mox.IgnoreArg())
    self.mox.ReplayAll()

    wl = watchlists.Watchlists('/a/path')
    self.assertEqual(wl.GetWatchersForPaths(['some_path']), [])

  def testNoWatchers(self):
    contents = \
      """{
        'WATCHLIST_DEFINITIONS': {
          'a_module': {
            'filepath': 'a_module',
          },
        },

        'WATCHLISTS': {
          'a_module': [],
        },
      } """
    watchlists.Watchlists._HasWatchlistsFile().AndReturn(True)
    watchlists.Watchlists._ContentsOfWatchlistsFile().AndReturn(contents)
    self.mox.ReplayAll()

    wl = watchlists.Watchlists('/a/path')
    self.assertEqual(wl.GetWatchersForPaths(['a_module']), [])

  def testValidWatcher(self):
    watchers = ['abc@def.com', 'x1@xyz.org']
    contents = \
      """{
        'WATCHLIST_DEFINITIONS': {
          'a_module': {
            'filepath': 'a_module',
          },
        },
        'WATCHLISTS': {
          'a_module': %s,
        },
      } """ % watchers
    watchlists.Watchlists._HasWatchlistsFile().AndReturn(True)
    watchlists.Watchlists._ContentsOfWatchlistsFile().AndReturn(contents)
    self.mox.ReplayAll()

    wl = watchlists.Watchlists('/a/path')
    self.assertEqual(wl.GetWatchersForPaths(['a_module']), watchers)

  def testMultipleWatchlistsTrigger(self):
    """Test that multiple watchlists can get triggered for one filepath."""
    contents = \
      """{
        'WATCHLIST_DEFINITIONS': {
          'mac': {
            'filepath': 'mac',
          },
          'views': {
            'filepath': 'views',
          },
        },
        'WATCHLISTS': {
          'mac': ['x1@chromium.org'],
          'views': ['x2@chromium.org'],
        },
      } """
    watchlists.Watchlists._HasWatchlistsFile().AndReturn(True)
    watchlists.Watchlists._ContentsOfWatchlistsFile().AndReturn(contents)
    self.mox.ReplayAll()

    wl = watchlists.Watchlists('/a/path')
    self.assertEqual(wl.GetWatchersForPaths(['file_views_mac']),
        ['x1@chromium.org', 'x2@chromium.org'])

  def testDuplicateWatchers(self):
    """Test that multiple watchlists can get triggered for one filepath."""
    watchers = ['someone@chromium.org']
    contents = \
      """{
        'WATCHLIST_DEFINITIONS': {
          'mac': {
            'filepath': 'mac',
          },
          'views': {
            'filepath': 'views',
          },
        },
        'WATCHLISTS': {
          'mac': %s,
          'views': %s,
        },
      } """ % (watchers, watchers)
    watchlists.Watchlists._HasWatchlistsFile().AndReturn(True)
    watchlists.Watchlists._ContentsOfWatchlistsFile().AndReturn(contents)
    self.mox.ReplayAll()

    wl = watchlists.Watchlists('/a/path')
    self.assertEqual(wl.GetWatchersForPaths(['file_views_mac']), watchers)

nirnimesh@chromium.org's avatar
nirnimesh@chromium.org committed
137 138 139 140 141 142 143 144 145 146 147 148 149 150
  def testWinPathWatchers(self):
    """Test watchers for a windows path (containing backward slashes)."""
    watchers = ['abc@def.com', 'x1@xyz.org']
    contents = \
      """{
        'WATCHLIST_DEFINITIONS': {
          'browser': {
            'filepath': 'chrome/browser/.*',
          },
        },
        'WATCHLISTS': {
          'browser': %s,
        },
      } """ % watchers
151 152
    saved_sep = watchlists.os.sep
    watchlists.os.sep = '\\'  # to pose as win32
nirnimesh@chromium.org's avatar
nirnimesh@chromium.org committed
153 154 155 156 157 158 159
    watchlists.Watchlists._HasWatchlistsFile().AndReturn(True)
    watchlists.Watchlists._ContentsOfWatchlistsFile().AndReturn(contents)
    self.mox.ReplayAll()

    wl = watchlists.Watchlists(r'a\path')
    returned_watchers = wl.GetWatchersForPaths(
          [r'chrome\browser\renderer_host\render_widget_host.h'])
160
    watchlists.os.sep = saved_sep  # revert back os.sep before asserts
nirnimesh@chromium.org's avatar
nirnimesh@chromium.org committed
161 162
    self.assertEqual(returned_watchers, watchers)

163 164

if __name__ == '__main__':
165
  import unittest
166
  unittest.main()