Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use standard library unittest.mock on Python 3.6 and later #2168

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion dev-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pytest==3.2.5
pytest-cov==2.5.1
six==1.10.0
# Mock for test mocking
mock==2.0.0
mock==2.0.0; python_version < "3.6"
# Linting!
flake8==3.6.0
# Coverage!
Expand Down
6 changes: 5 additions & 1 deletion fabric/testing/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,13 @@
from itertools import chain, repeat
from io import BytesIO
import os
from sys import version_info

try:
from mock import Mock, PropertyMock, call, patch, ANY
if version_info >= (3, 6):
from unittest.mock import Mock, PropertyMock, call, patch, ANY
else:
from mock import Mock, PropertyMock, call, patch, ANY
except ImportError:
import warnings

Expand Down
7 changes: 6 additions & 1 deletion fabric/testing/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,14 @@
.. versionadded:: 2.1
"""

from sys import version_info

try:
from pytest import fixture
from mock import patch, Mock
if version_info >= (3, 6):
from unittest.mock import patch, Mock
else:
from mock import patch, Mock
except ImportError:
import warnings

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
open("README.rst").read()
)

testing_deps = ["mock>=2.0.0,<3.0"]
testing_deps = ['mock>=2.0.0,<3.0; python_version < "3.6"']
pytest_deps = ["pytest>=3.2.5,<4.0"]

setuptools.setup(
Expand Down
8 changes: 6 additions & 2 deletions tests/config.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
import errno
from os.path import join, expanduser
from sys import version_info

from paramiko.config import SSHConfig
from invoke.vendor.lexicon import Lexicon

from fabric import Config
from fabric.util import get_local_user

from mock import patch, call

from _util import support, faux_v1_env

if version_info >= (3, 6):
from unittest.mock import patch, call
else:
from mock import patch, call


class Config_:
def defaults_to_merger_of_global_defaults(self):
Expand Down
6 changes: 5 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@
from fabric.testing.fixtures import client, remote, sftp, sftp_objs, transfer

from os.path import isfile, expanduser
from sys import version_info

from pytest import fixture

from mock import patch
if version_info >= (3, 6):
from unittest.mock import patch
else:
from mock import patch


# TODO: does this want to end up in the public fixtures module too?
Expand Down
7 changes: 6 additions & 1 deletion tests/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
import errno
from os.path import join
import socket
from sys import version_info
import time

from mock import patch, Mock, call, ANY
from paramiko.client import SSHClient, AutoAddPolicy
from paramiko import SSHConfig
import pytest # for mark
Expand All @@ -26,6 +26,11 @@

from _util import support, faux_v1_env

if version_info >= (3, 6):
from unittest.mock import patch, Mock, call, ANY
else:
from mock import patch, Mock, call, ANY


# Remote is woven in as a config default, so must be patched there
remote_path = "fabric.config.Remote"
Expand Down
8 changes: 7 additions & 1 deletion tests/executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,15 @@
from fabric.executor import ConnectionCall
from fabric.exceptions import NothingToDo

from mock import Mock
from sys import version_info

from pytest import skip, raises # noqa

if version_info >= (3, 6):
from unittest.mock import Mock
else:
from mock import Mock


def _get_executor(hosts_flag=None, hosts_kwarg=None, post=None, remainder=""):
post_tasks = []
Expand Down
8 changes: 7 additions & 1 deletion tests/group.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
from mock import Mock, patch, call
from sys import version_info

from pytest import mark, raises

from fabric import Connection, Group, SerialGroup, ThreadingGroup, GroupResult
from fabric.group import thread_worker
from fabric.exceptions import GroupException

if version_info >= (3, 6):
from unittest.mock import Mock, patch, call
else:
from mock import Mock, patch, call


RUNNER_METHODS = ("run", "sudo")
TRANSFER_METHODS = ("put", "get")
Expand Down
7 changes: 6 additions & 1 deletion tests/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

import os
import sys
from sys import version_info
import re

from invoke import run
from invoke.util import cd
from mock import patch
import pytest # because WHY would you expose @skip normally? -_-
from pytest_relaxed import raises

Expand All @@ -19,6 +19,11 @@
from fabric.testing.base import Session
from _util import expect, support, config_file, trap

if version_info >= (3, 6):
from unittest.mock import patch
else:
from mock import patch


# Designate a runtime config file intended for the test environment; it does
# things like automatically mute stdin so test harnesses that care about stdin
Expand Down
8 changes: 7 additions & 1 deletion tests/runners.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,19 @@
except ImportError:
from six import StringIO

from mock import Mock, patch
from sys import version_info

from pytest import skip # noqa

from invoke import pty_size, Result

from fabric import Config, Connection, Remote

if version_info >= (3, 6):
from unittest.mock import Mock, patch
else:
from mock import Mock, patch


# On most systems this will explode if actually executed as a shell command;
# this lets us detect holes in our network mocking.
Expand Down
10 changes: 8 additions & 2 deletions tests/task.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
# NOTE: named task.py, not tasks.py, to avoid some occasional pytest weirdness

from mock import Mock
from sys import version_info

from pytest import skip # noqa

import fabric
import fabric # noqa: E402
from fabric.tasks import ConnectionCall

if version_info >= (3, 6):
from unittest.mock import Mock
else:
from mock import Mock


class Task_:
def accepts_Invoke_level_init_kwargs(self):
Expand Down
8 changes: 7 additions & 1 deletion tests/transfer.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,20 @@
except ImportError:
from six import StringIO

from mock import Mock, call, patch
from sys import version_info

from pytest_relaxed import raises
from pytest import skip # noqa
from paramiko import SFTPAttributes

from fabric import Connection
from fabric.transfer import Transfer

if version_info >= (3, 6):
from unittest.mock import Mock, call, patch
else:
from mock import Mock, call, patch


# TODO: pull in all edge/corner case tests from fabric v1

Expand Down
9 changes: 7 additions & 2 deletions tests/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@
Tests testing the fabric.util module, not utils for the tests!
"""

from mock import patch

from fabric.util import get_local_user

from sys import version_info

if version_info >= (3, 6):
from unittest.mock import patch
else:
from mock import patch


# Basically implementation tests, because it's not feasible to do a "real" test
# on random platforms (where we have no idea what the actual invoking user is)
Expand Down