Files
chromium_depot_tools/tests/watchlists_unittest.py
Mike Frysinger 677616322a tests: switch to 4 space indent
Reformat this dir by itself to help merging with conflicts with other CLs.

Reformatted using:
parallel ./yapf -i -- tests/*.py
~/chromiumos/chromite/contrib/reflow_overlong_comments tests/*.py

These files still had lines (strings) that were too long, so the pylint
warnings were suppressed with a TODO.
tests/bot_update_coverage_test.py
tests/cipd_bootstrap_test.py
tests/gclient_eval_unittest.py
tests/gclient_git_smoketest.py
tests/gclient_scm_test.py
tests/gclient_smoketest.py
tests/gclient_test.py
tests/gclient_transitions_smoketest.py
tests/gclient_utils_test.py
tests/git_cl_test.py
tests/git_hyper_blame_test.py
tests/git_rebase_update_test.py
tests/lockfile_test.py
tests/metrics_test.py
tests/presubmit_canned_checks_test.py
tests/presubmit_unittest.py
tests/roll_dep_test.py

Change-Id: I8fed04b4ba81d54b8f45da612213aad27a9e1a2c
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/4842592
Commit-Queue: Josip Sokcevic <sokcevic@chromium.org>
Reviewed-by: Josip Sokcevic <sokcevic@chromium.org>
Auto-Submit: Mike Frysinger <vapier@chromium.org>
2023-09-05 20:24:16 +00:00

158 lines
5.1 KiB
Python
Executable File

#!/usr/bin/env vpython3
# Copyright (c) 2011 The Chromium Authors. All rights reserved.
# 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."""
# pylint: disable=E1103,no-value-for-parameter,protected-access
import os
import sys
import unittest
from unittest import mock
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
import watchlists
class WatchlistsTest(unittest.TestCase):
def setUp(self):
super(WatchlistsTest, self).setUp()
mock.patch('watchlists.Watchlists._HasWatchlistsFile').start()
mock.patch('watchlists.Watchlists._ContentsOfWatchlistsFile').start()
mock.patch('watchlists.logging.error').start()
self.addCleanup(mock.patch.stopall)
def testMissingWatchlistsFileOK(self):
"""Test that we act gracefully if WATCHLISTS file is missing."""
watchlists.Watchlists._HasWatchlistsFile.return_value = False
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.return_value = True
watchlists.Watchlists._ContentsOfWatchlistsFile.return_value = contents
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.return_value = True
watchlists.Watchlists._ContentsOfWatchlistsFile.return_value = contents
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.return_value = True
watchlists.Watchlists._ContentsOfWatchlistsFile.return_value = contents
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.return_value = True
watchlists.Watchlists._ContentsOfWatchlistsFile.return_value = contents
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.return_value = True
watchlists.Watchlists._ContentsOfWatchlistsFile.return_value = contents
wl = watchlists.Watchlists('/a/path')
self.assertEqual(wl.GetWatchersForPaths(['file_views_mac']), watchers)
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
saved_sep = watchlists.os.sep
watchlists.os.sep = '\\' # to pose as win32
watchlists.Watchlists._HasWatchlistsFile.return_value = True
watchlists.Watchlists._ContentsOfWatchlistsFile.return_value = contents
wl = watchlists.Watchlists(r'a\path')
returned_watchers = wl.GetWatchersForPaths(
[r'chrome\browser\renderer_host\render_widget_host.h'])
watchlists.os.sep = saved_sep # revert back os.sep before asserts
self.assertEqual(returned_watchers, watchers)
if __name__ == '__main__':
import unittest
unittest.main()