Commit a8145025 authored by Edward Lemur's avatar Edward Lemur Committed by Commit Bot

depot_tools: Use mock from vpython (or unittest.mock) instead of third_party/mock

Change-Id: I3a188b34ae5f62649108afe08fe0e389a408c2ac
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/1947933
Commit-Queue: Edward Lesmes <ehmaldonado@chromium.org>
Reviewed-by: 's avatarAnthony Polito <apolito@google.com>
parent ed0cc5f6
...@@ -21,3 +21,35 @@ wheel: < ...@@ -21,3 +21,35 @@ wheel: <
name: "infra/python/wheels/six-py2_py3" name: "infra/python/wheels/six-py2_py3"
version: "version:1.10.0" version: "version:1.10.0"
> >
# Used by:
# tests/auth_test.py
# tests/detect_host_arch_test.py
# tests/gclient_scm_test.py
# tests/gclient_test.py
# tests/gclient_utils_test.py
# tests/gerrit_util_test.py
# tests/git_cl_test.py
# tests/git_drover_test.py
# tests/git_footers_test.py
# tests/metrics_test.py
# tests/presubmit_unittest.py
# tests/scm_unittest.py
# tests/subprocess2_test.py
# tests/watchlists_unittest.py
wheel: <
name: "infra/python/wheels/mock-py2_py3"
version: "version:2.0.0"
>
wheel <
name: "infra/python/wheels/funcsigs-py2_py3"
version: "version:1.0.2"
>
wheel: <
name: "infra/python/wheels/pbr-py2_py3"
version: "version:3.0.0"
>
wheel: <
name: "infra/python/wheels/six-py2_py3"
version: "version:1.10.0"
>
...@@ -17,12 +17,6 @@ import operator ...@@ -17,12 +17,6 @@ import operator
import os import os
import pipes import pipes
import platform import platform
try:
import Queue as queue
except ImportError: # For Py3 compatibility
import queue
import re import re
import stat import stat
import subprocess import subprocess
...@@ -30,18 +24,16 @@ import sys ...@@ -30,18 +24,16 @@ import sys
import tempfile import tempfile
import threading import threading
import time import time
try:
import urlparse
except ImportError: # For Py3 compatibility
import urllib.parse as urlparse
import subprocess2 import subprocess2
if sys.version_info.major == 2: if sys.version_info.major == 2:
from cStringIO import StringIO from cStringIO import StringIO
import Queue as queue
import urlparse
else: else:
from io import StringIO from io import StringIO
import queue
import urllib.parse as urlparse
RETRY_MAX = 3 RETRY_MAX = 3
......
...@@ -12,18 +12,18 @@ import os ...@@ -12,18 +12,18 @@ import os
import unittest import unittest
import sys import sys
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from third_party import mock
import auth
import subprocess2
if sys.version_info.major == 2: if sys.version_info.major == 2:
import mock
BUILTIN_OPEN = '__builtin__.open' BUILTIN_OPEN = '__builtin__.open'
else: else:
from unittest import mock
BUILTIN_OPEN = 'builtins.open' BUILTIN_OPEN = 'builtins.open'
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
import auth
import subprocess2
NOW = datetime.datetime(2019, 10, 17, 12, 30, 59, 0) NOW = datetime.datetime(2019, 10, 17, 12, 30, 59, 0)
VALID_EXPIRY = NOW + datetime.timedelta(seconds=31) VALID_EXPIRY = NOW + datetime.timedelta(seconds=31)
...@@ -57,7 +57,7 @@ class AuthenticatorTest(unittest.TestCase): ...@@ -57,7 +57,7 @@ class AuthenticatorTest(unittest.TestCase):
authenticator._access_token = auth.AccessToken('token', None) authenticator._access_token = auth.AccessToken('token', None)
self.assertEqual( self.assertEqual(
auth.AccessToken('token', None), authenticator.get_access_token()) auth.AccessToken('token', None), authenticator.get_access_token())
self.assertEqual(0, len(subprocess2.check_call_out.mock_calls)) subprocess2.check_call_out.assert_not_called()
def testGetAccesstoken_LoggedIn(self): def testGetAccesstoken_LoggedIn(self):
expiry = calendar.timegm(VALID_EXPIRY.timetuple()) expiry = calendar.timegm(VALID_EXPIRY.timetuple())
......
...@@ -8,10 +8,14 @@ import platform ...@@ -8,10 +8,14 @@ import platform
import sys import sys
import unittest import unittest
if sys.version_info.major == 2:
import mock
else:
from unittest import mock
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
import detect_host_arch import detect_host_arch
from third_party import mock
class DetectHostArchTest(unittest.TestCase): class DetectHostArchTest(unittest.TestCase):
......
...@@ -22,12 +22,13 @@ import unittest ...@@ -22,12 +22,13 @@ import unittest
if sys.version_info.major == 2: if sys.version_info.major == 2:
from cStringIO import StringIO from cStringIO import StringIO
import mock
else: else:
from io import StringIO from io import StringIO
from unittest import mock
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from third_party import mock
from testing_support import fake_repos from testing_support import fake_repos
from testing_support import test_case_utils from testing_support import test_case_utils
......
...@@ -16,8 +16,10 @@ import sys ...@@ -16,8 +16,10 @@ import sys
import unittest import unittest
if sys.version_info.major == 2: if sys.version_info.major == 2:
import mock
import Queue import Queue
else: else:
from unittest import mock
import queue as Queue import queue as Queue
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
...@@ -30,7 +32,6 @@ import gclient ...@@ -30,7 +32,6 @@ import gclient
import gclient_utils import gclient_utils
import gclient_scm import gclient_scm
from testing_support import trial_dir from testing_support import trial_dir
from third_party import mock
def write(filename, content): def write(filename, content):
......
...@@ -16,13 +16,14 @@ import unittest ...@@ -16,13 +16,14 @@ import unittest
if sys.version_info.major == 2: if sys.version_info.major == 2:
from StringIO import StringIO from StringIO import StringIO
import mock
else: else:
from io import StringIO from io import StringIO
from unittest import mock
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from testing_support import trial_dir from testing_support import trial_dir
from third_party import mock
import gclient_utils import gclient_utils
import subprocess2 import subprocess2
......
...@@ -14,9 +14,14 @@ import os ...@@ -14,9 +14,14 @@ import os
import sys import sys
import unittest import unittest
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) if sys.version_info.major == 2:
from cStringIO import StringIO
import mock
else:
from io import StringIO
from unittest import mock
from third_party import mock sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
import gerrit_util import gerrit_util
import gclient_utils import gclient_utils
...@@ -24,11 +29,6 @@ import metrics ...@@ -24,11 +29,6 @@ import metrics
import metrics_utils import metrics_utils
import subprocess2 import subprocess2
if sys.version_info.major == 2:
from cStringIO import StringIO
else:
from io import StringIO
class CookiesAuthenticatorTest(unittest.TestCase): class CookiesAuthenticatorTest(unittest.TestCase):
_GITCOOKIES = '\n'.join([ _GITCOOKIES = '\n'.join([
......
...@@ -17,10 +17,16 @@ import sys ...@@ -17,10 +17,16 @@ import sys
import tempfile import tempfile
import unittest import unittest
if sys.version_info.major == 2:
from StringIO import StringIO
import mock
else:
from io import StringIO
from unittest import mock
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from testing_support.auto_stub import TestCase from testing_support.auto_stub import TestCase
from third_party import mock
import metrics import metrics
# We have to disable monitoring before importing git_cl. # We have to disable monitoring before importing git_cl.
...@@ -32,11 +38,6 @@ import git_common ...@@ -32,11 +38,6 @@ import git_common
import git_footers import git_footers
import subprocess2 import subprocess2
if sys.version_info.major == 2:
from StringIO import StringIO
else:
from io import StringIO
def callError(code=1, cmd='', cwd='', stdout=b'', stderr=b''): def callError(code=1, cmd='', cwd='', stdout=b'', stderr=b''):
return subprocess2.CalledProcessError(code, cmd, cwd, stdout, stderr) return subprocess2.CalledProcessError(code, cmd, cwd, stdout, stderr)
......
...@@ -11,9 +11,13 @@ import sys ...@@ -11,9 +11,13 @@ import sys
import tempfile import tempfile
import unittest import unittest
if sys.version_info.major == 2:
import mock
else:
from unittest import mock
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from third_party import mock
import git_drover import git_drover
......
...@@ -10,12 +10,13 @@ import unittest ...@@ -10,12 +10,13 @@ import unittest
if sys.version_info.major == 2: if sys.version_info.major == 2:
from StringIO import StringIO from StringIO import StringIO
import mock
else: else:
from io import StringIO from io import StringIO
from unittest import mock
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from third_party import mock
import git_footers import git_footers
class GitFootersTest(unittest.TestCase): class GitFootersTest(unittest.TestCase):
......
...@@ -8,14 +8,17 @@ import os ...@@ -8,14 +8,17 @@ import os
import sys import sys
import unittest import unittest
if sys.version_info.major == 2:
import mock
else:
from unittest import mock
ROOT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) ROOT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, ROOT_DIR) sys.path.insert(0, ROOT_DIR)
import metrics import metrics
import metrics_utils import metrics_utils
from third_party import mock
class TimeMock(object): class TimeMock(object):
def __init__(self): def __init__(self):
......
...@@ -22,11 +22,21 @@ import threading ...@@ -22,11 +22,21 @@ import threading
import time import time
import unittest import unittest
if sys.version_info.major == 2:
from cStringIO import StringIO
import mock
import urllib2 as urllib_request
BUILTIN_OPEN = '__builtin__.open'
else:
from io import StringIO
from unittest import mock
import urllib.request as urllib_request
BUILTIN_OPEN = 'builtins.open'
_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) _ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, _ROOT) sys.path.insert(0, _ROOT)
from testing_support.test_case_utils import TestCaseUtils from testing_support.test_case_utils import TestCaseUtils
from third_party import mock
import auth import auth
import gclient_utils import gclient_utils
...@@ -39,14 +49,6 @@ import presubmit_support as presubmit ...@@ -39,14 +49,6 @@ import presubmit_support as presubmit
import scm import scm
import subprocess2 as subprocess import subprocess2 as subprocess
if sys.version_info.major == 2:
from cStringIO import StringIO
import urllib2 as urllib_request
BUILTIN_OPEN = '__builtin__.open'
else:
from io import StringIO
import urllib.request as urllib_request
BUILTIN_OPEN = 'builtins.open'
# Shortcut. # Shortcut.
presubmit_canned_checks = presubmit.presubmit_canned_checks presubmit_canned_checks = presubmit.presubmit_canned_checks
...@@ -2576,8 +2578,9 @@ the current line as well! ...@@ -2576,8 +2578,9 @@ the current line as well!
is_committing=False, is_committing=False,
uncovered_files=set()) uncovered_files=set())
@mock.patch(BUILTIN_OPEN, mock.mock_open(read_data='')) @mock.patch(BUILTIN_OPEN, mock.mock_open())
def testCannedRunUnitTests(self): def testCannedRunUnitTests(self):
open().readline.return_value = ''
change = presubmit.Change( change = presubmit.Change(
'foo1', 'description1', self.fake_root_dir, None, 0, 0, None) 'foo1', 'description1', self.fake_root_dir, None, 0, 0, None)
input_api = self.MockInputApi(change, False) input_api = self.MockInputApi(change, False)
...@@ -2621,8 +2624,9 @@ the current line as well! ...@@ -2621,8 +2624,9 @@ the current line as well!
self.checkstdout('') self.checkstdout('')
@mock.patch(BUILTIN_OPEN, mock.mock_open(read_data='')) @mock.patch(BUILTIN_OPEN, mock.mock_open())
def testCannedRunUnitTestsWithTimer(self): def testCannedRunUnitTestsWithTimer(self):
open().readline.return_value = ''
change = presubmit.Change( change = presubmit.Change(
'foo1', 'description1', self.fake_root_dir, None, 0, 0, None) 'foo1', 'description1', self.fake_root_dir, None, 0, 0, None)
input_api = self.MockInputApi(change, False) input_api = self.MockInputApi(change, False)
...@@ -2646,8 +2650,9 @@ the current line as well! ...@@ -2646,8 +2650,9 @@ the current line as well!
self.checkstdout('') self.checkstdout('')
@mock.patch(BUILTIN_OPEN, mock.mock_open(read_data='')) @mock.patch(BUILTIN_OPEN, mock.mock_open())
def testCannedRunUnitTestsWithTimerTimesOut(self): def testCannedRunUnitTestsWithTimerTimesOut(self):
open().readline.return_value = ''
change = presubmit.Change( change = presubmit.Change(
'foo1', 'description1', self.fake_root_dir, None, 0, 0, None) 'foo1', 'description1', self.fake_root_dir, None, 0, 0, None)
input_api = self.MockInputApi(change, False) input_api = self.MockInputApi(change, False)
......
...@@ -10,9 +10,13 @@ import os ...@@ -10,9 +10,13 @@ import os
import sys import sys
import unittest import unittest
if sys.version_info.major == 2:
import mock
else:
from unittest import mock
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from third_party import mock
from testing_support import fake_repos from testing_support import fake_repos
import scm import scm
......
...@@ -9,14 +9,17 @@ import os ...@@ -9,14 +9,17 @@ import os
import sys import sys
import unittest import unittest
if sys.version_info.major == 2:
import mock
else:
from unittest import mock
DEPOT_TOOLS = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) DEPOT_TOOLS = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, DEPOT_TOOLS) sys.path.insert(0, DEPOT_TOOLS)
import subprocess import subprocess
import subprocess2 import subprocess2
from third_party import mock
TEST_COMMAND = [ TEST_COMMAND = [
sys.executable, sys.executable,
......
...@@ -11,9 +11,13 @@ import os ...@@ -11,9 +11,13 @@ import os
import sys import sys
import unittest import unittest
if sys.version_info.major == 2:
import mock
else:
from unittest import mock
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from third_party import mock
import watchlists import watchlists
......
Copyright (c) 2003-2012, Michael Foord
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided
with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
\ No newline at end of file
Name: mock
URL: http://www.voidspace.org.uk/python/mock/
Version: 1.0.0
License: BSD
License File: LICENSE.txt
Description:
mock is a library for testing in Python. It allows you to replace parts of your
system under test with mock objects and make assertions about how they have been
used. mock is now part of the Python standard library, available as
unittest.mock in Python 3.3 onwards.
mock provides a core Mock class removing the need to create a host of stubs
throughout your test suite. After performing an action, you can make assertions
about which methods / attributes were used and arguments they were called with.
You can also specify return values and set needed attributes in the normal way.
Additionally, mock provides a patch() decorator that handles patching module and
class level attributes within the scope of a test, along with sentinel for
creating unique objects. Mock is very easy to use and is designed for use with
unittest. Mock is based on the ‘action -> assertion’ pattern instead of
‘record -> replay’ used by many mocking frameworks. mock is tested on Python
versions 2.4-2.7, Python 3 plus the latest versions of Jython and PyPy.
Modifications:
None.
\ No newline at end of file
This diff is collapsed.
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