From e8c751b72be3a9853efc6a844fb549987a9588c8 Mon Sep 17 00:00:00 2001 From: Mike Fiedler Date: Mon, 20 Dec 2021 18:56:47 -0500 Subject: [PATCH 1/2] chore: remove post-yield behaviors These actions can conflict with other tests within scope, and should already be covered by `pytest_runtest_teardown()`. Signed-off-by: Mike Fiedler --- pytest_socket.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/pytest_socket.py b/pytest_socket.py index 43ac958..f262198 100644 --- a/pytest_socket.py +++ b/pytest_socket.py @@ -65,7 +65,6 @@ def socket_disabled(pytestconfig): """ disable socket.socket for duration of this test function """ disable_socket(allow_unix_socket=pytestconfig.__socket_allow_unix_socket) yield - enable_socket() @pytest.fixture @@ -73,7 +72,6 @@ def socket_enabled(pytestconfig): """ enable socket.socket for duration of this test function """ enable_socket() yield - disable_socket(allow_unix_socket=pytestconfig.__socket_allow_unix_socket) def disable_socket(allow_unix_socket=False): From 143e495a7f3e66f130fe9bdd65b552d62dc3098d Mon Sep 17 00:00:00 2001 From: Mike Fiedler Date: Mon, 20 Dec 2021 19:00:18 -0500 Subject: [PATCH 2/2] refactor: rename the post-test function Make this a private function, since it's not meant to be used outside this plugin. Add resetting the socket back to normal as well. Signed-off-by: Mike Fiedler --- pytest_socket.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/pytest_socket.py b/pytest_socket.py index f262198..2360eef 100644 --- a/pytest_socket.py +++ b/pytest_socket.py @@ -124,7 +124,7 @@ def pytest_runtest_setup(item): def pytest_runtest_teardown(): - remove_host_restrictions() + _remove_restrictions() def host_from_address(address): @@ -157,7 +157,8 @@ def guarded_connect(inst, *args): socket.socket.connect = guarded_connect -def remove_host_restrictions(): - """ restore socket.socket.connect() to allow access to the Internet. useful in testing. +def _remove_restrictions(): + """ restore socket.socket.* to allow access to the Internet. useful in testing. """ + socket.socket = _true_socket socket.socket.connect = _true_connect