[email protected] | 45d8db0 | 2011-03-31 20:43:56 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | b82e409 | 2009-06-18 14:24:08 | [diff] [blame] | 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | |
| 6 | """Unit tests for watchlists.py.""" |
| 7 | |
[email protected] | 428342a | 2011-11-10 15:46:33 | [diff] [blame] | 8 | # pylint: disable=E1103,E1120,W0212 |
| 9 | |
| 10 | import os |
| 11 | import sys |
| 12 | |
| 13 | sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) |
[email protected] | bf38a7e | 2010-12-14 18:15:54 | [diff] [blame] | 14 | |
[email protected] | 0927b7e | 2011-11-11 16:06:22 | [diff] [blame] | 15 | from testing_support import super_mox |
| 16 | |
[email protected] | b82e409 | 2009-06-18 14:24:08 | [diff] [blame] | 17 | import watchlists |
| 18 | |
| 19 | |
| 20 | class WatchlistsTest(super_mox.SuperMoxTestBase): |
| 21 | |
| 22 | def setUp(self): |
| 23 | super_mox.SuperMoxTestBase.setUp(self) |
| 24 | self.mox.StubOutWithMock(watchlists.Watchlists, '_HasWatchlistsFile') |
| 25 | self.mox.StubOutWithMock(watchlists.Watchlists, '_ContentsOfWatchlistsFile') |
| 26 | self.mox.StubOutWithMock(watchlists.logging, 'error') |
| 27 | |
| 28 | def testMissingWatchlistsFileOK(self): |
| 29 | """Test that we act gracefully if WATCHLISTS file is missing.""" |
| 30 | watchlists.Watchlists._HasWatchlistsFile().AndReturn(False) |
| 31 | self.mox.ReplayAll() |
| 32 | |
| 33 | wl = watchlists.Watchlists('/some/random/path') |
| 34 | self.assertEqual(wl.GetWatchersForPaths(['some_path']), []) |
| 35 | |
| 36 | def testGarbledWatchlistsFileOK(self): |
| 37 | """Test that we act gracefully if WATCHLISTS file is garbled.""" |
| 38 | contents = 'some garbled and unwanted text' |
| 39 | watchlists.Watchlists._HasWatchlistsFile().AndReturn(True) |
| 40 | watchlists.Watchlists._ContentsOfWatchlistsFile().AndReturn(contents) |
| 41 | watchlists.logging.error(super_mox.mox.IgnoreArg()) |
| 42 | self.mox.ReplayAll() |
| 43 | |
| 44 | wl = watchlists.Watchlists('/a/path') |
| 45 | self.assertEqual(wl.GetWatchersForPaths(['some_path']), []) |
| 46 | |
| 47 | def testNoWatchers(self): |
| 48 | contents = \ |
| 49 | """{ |
| 50 | 'WATCHLIST_DEFINITIONS': { |
| 51 | 'a_module': { |
| 52 | 'filepath': 'a_module', |
| 53 | }, |
| 54 | }, |
| 55 | |
| 56 | 'WATCHLISTS': { |
| 57 | 'a_module': [], |
| 58 | }, |
| 59 | } """ |
| 60 | watchlists.Watchlists._HasWatchlistsFile().AndReturn(True) |
| 61 | watchlists.Watchlists._ContentsOfWatchlistsFile().AndReturn(contents) |
| 62 | self.mox.ReplayAll() |
| 63 | |
| 64 | wl = watchlists.Watchlists('/a/path') |
| 65 | self.assertEqual(wl.GetWatchersForPaths(['a_module']), []) |
| 66 | |
| 67 | def testValidWatcher(self): |
| 68 | watchers = ['[email protected]', '[email protected]'] |
| 69 | contents = \ |
| 70 | """{ |
| 71 | 'WATCHLIST_DEFINITIONS': { |
| 72 | 'a_module': { |
| 73 | 'filepath': 'a_module', |
| 74 | }, |
| 75 | }, |
| 76 | 'WATCHLISTS': { |
| 77 | 'a_module': %s, |
| 78 | }, |
| 79 | } """ % watchers |
| 80 | watchlists.Watchlists._HasWatchlistsFile().AndReturn(True) |
| 81 | watchlists.Watchlists._ContentsOfWatchlistsFile().AndReturn(contents) |
| 82 | self.mox.ReplayAll() |
| 83 | |
| 84 | wl = watchlists.Watchlists('/a/path') |
| 85 | self.assertEqual(wl.GetWatchersForPaths(['a_module']), watchers) |
| 86 | |
| 87 | def testMultipleWatchlistsTrigger(self): |
| 88 | """Test that multiple watchlists can get triggered for one filepath.""" |
| 89 | contents = \ |
| 90 | """{ |
| 91 | 'WATCHLIST_DEFINITIONS': { |
| 92 | 'mac': { |
| 93 | 'filepath': 'mac', |
| 94 | }, |
| 95 | 'views': { |
| 96 | 'filepath': 'views', |
| 97 | }, |
| 98 | }, |
| 99 | 'WATCHLISTS': { |
| 100 | 'mac': ['[email protected]'], |
| 101 | 'views': ['[email protected]'], |
| 102 | }, |
| 103 | } """ |
| 104 | watchlists.Watchlists._HasWatchlistsFile().AndReturn(True) |
| 105 | watchlists.Watchlists._ContentsOfWatchlistsFile().AndReturn(contents) |
| 106 | self.mox.ReplayAll() |
| 107 | |
| 108 | wl = watchlists.Watchlists('/a/path') |
| 109 | self.assertEqual(wl.GetWatchersForPaths(['file_views_mac']), |
| 110 | ['[email protected]', '[email protected]']) |
| 111 | |
| 112 | def testDuplicateWatchers(self): |
| 113 | """Test that multiple watchlists can get triggered for one filepath.""" |
| 114 | watchers = ['[email protected]'] |
| 115 | contents = \ |
| 116 | """{ |
| 117 | 'WATCHLIST_DEFINITIONS': { |
| 118 | 'mac': { |
| 119 | 'filepath': 'mac', |
| 120 | }, |
| 121 | 'views': { |
| 122 | 'filepath': 'views', |
| 123 | }, |
| 124 | }, |
| 125 | 'WATCHLISTS': { |
| 126 | 'mac': %s, |
| 127 | 'views': %s, |
| 128 | }, |
| 129 | } """ % (watchers, watchers) |
| 130 | watchlists.Watchlists._HasWatchlistsFile().AndReturn(True) |
| 131 | watchlists.Watchlists._ContentsOfWatchlistsFile().AndReturn(contents) |
| 132 | self.mox.ReplayAll() |
| 133 | |
| 134 | wl = watchlists.Watchlists('/a/path') |
| 135 | self.assertEqual(wl.GetWatchersForPaths(['file_views_mac']), watchers) |
| 136 | |
[email protected] | 76256af | 2009-06-18 22:52:12 | [diff] [blame] | 137 | def testWinPathWatchers(self): |
| 138 | """Test watchers for a windows path (containing backward slashes).""" |
| 139 | watchers = ['[email protected]', '[email protected]'] |
| 140 | contents = \ |
| 141 | """{ |
| 142 | 'WATCHLIST_DEFINITIONS': { |
| 143 | 'browser': { |
| 144 | 'filepath': 'chrome/browser/.*', |
| 145 | }, |
| 146 | }, |
| 147 | 'WATCHLISTS': { |
| 148 | 'browser': %s, |
| 149 | }, |
| 150 | } """ % watchers |
[email protected] | 8ef5f54 | 2009-11-12 02:05:02 | [diff] [blame] | 151 | saved_sep = watchlists.os.sep |
| 152 | watchlists.os.sep = '\\' # to pose as win32 |
[email protected] | 76256af | 2009-06-18 22:52:12 | [diff] [blame] | 153 | watchlists.Watchlists._HasWatchlistsFile().AndReturn(True) |
| 154 | watchlists.Watchlists._ContentsOfWatchlistsFile().AndReturn(contents) |
| 155 | self.mox.ReplayAll() |
| 156 | |
| 157 | wl = watchlists.Watchlists(r'a\path') |
| 158 | returned_watchers = wl.GetWatchersForPaths( |
| 159 | [r'chrome\browser\renderer_host\render_widget_host.h']) |
[email protected] | 8ef5f54 | 2009-11-12 02:05:02 | [diff] [blame] | 160 | watchlists.os.sep = saved_sep # revert back os.sep before asserts |
[email protected] | 76256af | 2009-06-18 22:52:12 | [diff] [blame] | 161 | self.assertEqual(returned_watchers, watchers) |
| 162 | |
[email protected] | b82e409 | 2009-06-18 14:24:08 | [diff] [blame] | 163 | |
| 164 | if __name__ == '__main__': |
[email protected] | 8ef5f54 | 2009-11-12 02:05:02 | [diff] [blame] | 165 | import unittest |
[email protected] | b82e409 | 2009-06-18 14:24:08 | [diff] [blame] | 166 | unittest.main() |