Commit 9f4b37db authored by Edward Lemur's avatar Edward Lemur Committed by Commit Bot

depot_tools: Make watchlist tests use mock and remove pymox.

Bug: 984182
Change-Id: Iaacb10dfc5ce7c624ac52edb76a9a74c6e2fd1cd
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/1757122
Commit-Queue: Edward Lesmes <ehmaldonado@chromium.org>
Reviewed-by: 's avatarRobbie Iannucci <iannucci@chromium.org>
parent d504dd79
......@@ -37,7 +37,6 @@
# TODO: There are some really junky dependencies in here that we should probably
# move to vpython/cipd.
/third_party/** recipes
/third_party/pymox/** -recipes
/win_toolchain/** recipes
......
python_version: "2.7"
# Used by:
# testing_support/super_mox.py
wheel: <
name: "infra/python/wheels/mox-py2_py3"
version: "version:0.5.3"
>
# 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.
"""Simplify unit tests based on pymox."""
from __future__ import print_function
import os
import random
import shutil
import string
import subprocess
import sys
sys.path.append(os.path.dirname(os.path.dirname(__file__)))
import mox
from third_party.six.moves import StringIO
from testing_support.test_case_utils import TestCaseUtils
class IsOneOf(mox.Comparator):
def __init__(self, keys):
self._keys = keys
def equals(self, rhs):
return rhs in self._keys
def __repr__(self):
return '<sequence or map containing \'%s\'>' % str(self._keys)
class StdoutCheck(object):
def setUp(self):
# Override the mock with a StringIO, it's much less painful to test.
self._old_stdout = sys.stdout
stdout = StringIO()
stdout.flush = lambda: None
sys.stdout = stdout
def tearDown(self):
try:
# If sys.stdout was used, self.checkstdout() must be called.
# pylint: disable=no-member
if not sys.stdout.closed:
self.assertEqual('', sys.stdout.getvalue())
except AttributeError:
pass
sys.stdout = self._old_stdout
def checkstdout(self, expected):
value = sys.stdout.getvalue()
sys.stdout.close()
# pylint: disable=no-member
self.assertEqual(expected, value)
class SuperMoxTestBase(TestCaseUtils, StdoutCheck, mox.MoxTestBase):
def setUp(self):
"""Patch a few functions with know side-effects."""
TestCaseUtils.setUp(self)
mox.MoxTestBase.setUp(self)
os_to_mock = ('chdir', 'chown', 'close', 'closerange', 'dup', 'dup2',
'fchdir', 'fchmod', 'fchown', 'fdopen', 'getcwd', 'listdir', 'lseek',
'makedirs', 'mkdir', 'open', 'popen', 'popen2', 'popen3', 'popen4',
'read', 'remove', 'removedirs', 'rename', 'renames', 'rmdir', 'symlink',
'system', 'tmpfile', 'walk', 'write')
self.MockList(os, os_to_mock)
os_path_to_mock = ('abspath', 'exists', 'getsize', 'isdir', 'isfile',
'islink', 'ismount', 'lexists', 'realpath', 'samefile', 'walk')
self.MockList(os.path, os_path_to_mock)
self.MockList(shutil, ('rmtree'))
self.MockList(subprocess, ('call', 'Popen'))
# Don't mock stderr since it confuses unittests.
self.MockList(sys, ('stdin'))
StdoutCheck.setUp(self)
def tearDown(self):
StdoutCheck.tearDown(self)
mox.MoxTestBase.tearDown(self)
def MockList(self, parent, items_to_mock):
for item in items_to_mock:
# Skip over items not present because of OS-specific implementation,
# implemented only in later python version, etc.
if hasattr(parent, item):
try:
self.mox.StubOutWithMock(parent, item)
except TypeError as e:
raise TypeError(
'Couldn\'t mock %s in %s: %s' % (item, parent.__name__, e))
def UnMock(self, obj, name):
"""Restore an object inside a test."""
for (parent, old_child, child_name) in self.mox.stubs.cache:
if parent == obj and child_name == name:
setattr(parent, child_name, old_child)
break
......@@ -7,7 +7,6 @@
# pylint: disable=E1103
# Import before super_mox to keep valid references.
from shutil import rmtree
from subprocess import Popen, PIPE, STDOUT
......
......@@ -9,26 +9,26 @@
import os
import sys
import unittest
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from testing_support import super_mox
from third_party import mock
import watchlists
class WatchlistsTest(super_mox.SuperMoxTestBase):
class WatchlistsTest(unittest.TestCase):
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')
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().AndReturn(False)
self.mox.ReplayAll()
watchlists.Watchlists._HasWatchlistsFile.return_value = False
wl = watchlists.Watchlists('/some/random/path')
self.assertEqual(wl.GetWatchersForPaths(['some_path']), [])
......@@ -36,10 +36,8 @@ class WatchlistsTest(super_mox.SuperMoxTestBase):
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()
watchlists.Watchlists._HasWatchlistsFile.return_value = True
watchlists.Watchlists._ContentsOfWatchlistsFile.return_value = contents
wl = watchlists.Watchlists('/a/path')
self.assertEqual(wl.GetWatchersForPaths(['some_path']), [])
......@@ -57,9 +55,8 @@ class WatchlistsTest(super_mox.SuperMoxTestBase):
'a_module': [],
},
} """
watchlists.Watchlists._HasWatchlistsFile().AndReturn(True)
watchlists.Watchlists._ContentsOfWatchlistsFile().AndReturn(contents)
self.mox.ReplayAll()
watchlists.Watchlists._HasWatchlistsFile.return_value = True
watchlists.Watchlists._ContentsOfWatchlistsFile.return_value = contents
wl = watchlists.Watchlists('/a/path')
self.assertEqual(wl.GetWatchersForPaths(['a_module']), [])
......@@ -77,9 +74,8 @@ class WatchlistsTest(super_mox.SuperMoxTestBase):
'a_module': %s,
},
} """ % watchers
watchlists.Watchlists._HasWatchlistsFile().AndReturn(True)
watchlists.Watchlists._ContentsOfWatchlistsFile().AndReturn(contents)
self.mox.ReplayAll()
watchlists.Watchlists._HasWatchlistsFile.return_value = True
watchlists.Watchlists._ContentsOfWatchlistsFile.return_value = contents
wl = watchlists.Watchlists('/a/path')
self.assertEqual(wl.GetWatchersForPaths(['a_module']), watchers)
......@@ -101,9 +97,8 @@ class WatchlistsTest(super_mox.SuperMoxTestBase):
'views': ['x2@chromium.org'],
},
} """
watchlists.Watchlists._HasWatchlistsFile().AndReturn(True)
watchlists.Watchlists._ContentsOfWatchlistsFile().AndReturn(contents)
self.mox.ReplayAll()
watchlists.Watchlists._HasWatchlistsFile.return_value = True
watchlists.Watchlists._ContentsOfWatchlistsFile.return_value = contents
wl = watchlists.Watchlists('/a/path')
self.assertEqual(wl.GetWatchersForPaths(['file_views_mac']),
......@@ -127,9 +122,8 @@ class WatchlistsTest(super_mox.SuperMoxTestBase):
'views': %s,
},
} """ % (watchers, watchers)
watchlists.Watchlists._HasWatchlistsFile().AndReturn(True)
watchlists.Watchlists._ContentsOfWatchlistsFile().AndReturn(contents)
self.mox.ReplayAll()
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)
......@@ -150,9 +144,8 @@ class WatchlistsTest(super_mox.SuperMoxTestBase):
} """ % watchers
saved_sep = watchlists.os.sep
watchlists.os.sep = '\\' # to pose as win32
watchlists.Watchlists._HasWatchlistsFile().AndReturn(True)
watchlists.Watchlists._ContentsOfWatchlistsFile().AndReturn(contents)
self.mox.ReplayAll()
watchlists.Watchlists._HasWatchlistsFile.return_value = True
watchlists.Watchlists._ContentsOfWatchlistsFile.return_value = contents
wl = watchlists.Watchlists(r'a\path')
returned_watchers = wl.GetWatchersForPaths(
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment