Skip to content
Merged
Prev Previous commit
Next Next commit
Skip lots of socket tests
  • Loading branch information
tiran committed Mar 19, 2022
commit 3313066e97900f4a143b33fa5e5e69c620843030
3 changes: 2 additions & 1 deletion Lib/test/pythoninfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import re
import sys
import traceback
import unittest
import warnings


Expand Down Expand Up @@ -615,7 +616,7 @@ def collect_resource(info_add):
def collect_test_socket(info_add):
try:
from test import test_socket
except ImportError:
except (ImportError, unittest.SkipTest):
return

# all check attributes like HAVE_SOCKET_CAN
Expand Down
12 changes: 12 additions & 0 deletions Lib/test/support/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"requires_IEEE_754", "requires_zlib",
"has_fork_support", "requires_fork",
"has_subprocess_support", "requires_subprocess",
"has_socket_support", "requires_working_socket",
"anticipate_failure", "load_package_tests", "detect_api_mismatch",
"check__all__", "skip_if_buggy_ucrt_strfptime",
"check_disallow_instantiation", "check_sanitizer", "skip_if_sanitizer",
Expand Down Expand Up @@ -520,6 +521,17 @@ def requires_subprocess():
"""Used for subprocess, os.spawn calls, fd inheritance"""
return unittest.skipUnless(has_subprocess_support, "requires subprocess support")

# Emscripten's socket emulation has limitation. WASI doesn't have sockets yet.
has_socket_support = not is_emscripten and not is_wasi

def requires_working_socket(*, module=False):
Comment thread
tiran marked this conversation as resolved.
msg = "requires socket support"
if module:
if not has_socket_support:
raise unittest.SkipTest(msg)
else:
return unittest.skipUnless(has_socket_support, msg)

# Does strftime() support glibc extension like '%4Y'?
has_strftime_extensions = False
if sys.platform != "win32":
Expand Down
4 changes: 3 additions & 1 deletion Lib/test/test_asyncgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
import contextlib

from test.support.import_helper import import_module
from test.support import gc_collect
from test.support import gc_collect, requires_working_socket
asyncio = import_module("asyncio")


requires_working_socket(module=True)

_no_default = object()


Expand Down
2 changes: 2 additions & 0 deletions Lib/test/test_asynchat.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import asynchat
import asyncore

support.requires_working_socket(module=True)

HOST = socket_helper.HOST
SERVER_QUIT = b'QUIT\n'

Expand Down
2 changes: 2 additions & 0 deletions Lib/test/test_asyncio/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import os
from test import support
from test.support import load_package_tests
from test.support import import_helper

support.requires_working_socket(module=True)

# Skip tests if we don't have concurrent.futures.
import_helper.import_module('concurrent.futures')
Expand Down
2 changes: 2 additions & 0 deletions Lib/test/test_asyncore.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
if support.PGO:
raise unittest.SkipTest("test is not helpful for PGO")

support.requires_working_socket(module=True)

import warnings
with warnings.catch_warnings():
warnings.simplefilter('ignore', DeprecationWarning)
Expand Down
1 change: 1 addition & 0 deletions Lib/test/test_contextlib_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from test.test_contextlib import TestBaseExitStack

support.requires_working_socket(module=True)

def _async_test(func):
"""Decorator to turn an async function into a test case."""
Expand Down
5 changes: 5 additions & 0 deletions Lib/test/test_doctest.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
import types
import contextlib


if not support.has_subprocess_support:
raise unittest.SkipTest("test_CLI requires subprocess support.")


# NOTE: There are some additional tests relating to interaction with
# zipimport in the test_zipimport_support test module.

Expand Down
3 changes: 3 additions & 0 deletions Lib/test/test_docxmlrpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
import sys
import threading
import unittest
from test import support

support.requires_working_socket(module=True)

def make_request_and_skipIf(condition, reason):
# If we skip the test, we have to make a request because
Expand Down
1 change: 1 addition & 0 deletions Lib/test/test_ftplib.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import asyncore
import asynchat

support.requires_working_socket(module=True)

TIMEOUT = support.LOOPBACK_TIMEOUT
DEFAULT_ENCODING = 'utf-8'
Expand Down
1 change: 1 addition & 0 deletions Lib/test/test_httplib.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from test.support import socket_helper
from test.support import warnings_helper

support.requires_working_socket(module=True)

here = os.path.dirname(__file__)
# Self-signed cert file for 'localhost'
Expand Down
1 change: 1 addition & 0 deletions Lib/test/test_httpservers.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
from test.support import os_helper
from test.support import threading_helper

support.requires_working_socket(module=True)

class NoLogRequestHandler:
def log_message(self, *args):
Expand Down
5 changes: 4 additions & 1 deletion Lib/test/test_imaplib.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
import socket

from test.support import (verbose,
run_with_tz, run_with_locale, cpython_only)
run_with_tz, run_with_locale, cpython_only,
requires_working_socket)
from test.support import hashlib_helper
from test.support import threading_helper
from test.support import warnings_helper
Expand All @@ -23,6 +24,8 @@
except ImportError:
ssl = None

support.requires_working_socket(module=True)

CERTFILE = os.path.join(os.path.dirname(__file__) or os.curdir, "keycert3.pem")
CAFILE = os.path.join(os.path.dirname(__file__) or os.curdir, "pycacert.pem")

Expand Down
24 changes: 8 additions & 16 deletions Lib/test/test_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,6 @@
pass


# Emscripten's socket support is limited. Logging's socket tests do not work
# on Emscripten, yet.
skip_on_emscripten = unittest.skipIf(
support.is_emscripten,
"Socket tests don't work on Emscripten."
)


class BaseTest(unittest.TestCase):

"""Base class for logging tests."""
Expand Down Expand Up @@ -1070,7 +1062,7 @@ class TestUnixDatagramServer(TestUDPServer):

# - end of server_helper section

@skip_on_emscripten
@support.requires_working_socket()
class SMTPHandlerTest(BaseTest):
# bpo-14314, bpo-19665, bpo-34092: don't wait forever
TIMEOUT = support.LONG_TIMEOUT
Expand Down Expand Up @@ -1694,7 +1686,7 @@ def test_defaults_do_no_interpolation(self):
os.unlink(fn)


@skip_on_emscripten
@support.requires_working_socket()
class SocketHandlerTest(BaseTest):

"""Test for SocketHandler objects."""
Expand Down Expand Up @@ -1809,7 +1801,7 @@ def tearDown(self):
SocketHandlerTest.tearDown(self)
os_helper.unlink(self.address)

@skip_on_emscripten
@support.requires_working_socket()
class DatagramHandlerTest(BaseTest):

"""Test for DatagramHandler."""
Expand Down Expand Up @@ -1891,7 +1883,7 @@ def tearDown(self):
DatagramHandlerTest.tearDown(self)
os_helper.unlink(self.address)

@skip_on_emscripten
@support.requires_working_socket()
class SysLogHandlerTest(BaseTest):

"""Test for SysLogHandler using UDP."""
Expand Down Expand Up @@ -2001,7 +1993,7 @@ def tearDown(self):
self.server_class.address_family = socket.AF_INET
super(IPv6SysLogHandlerTest, self).tearDown()

@skip_on_emscripten
@support.requires_working_socket()
class HTTPHandlerTest(BaseTest):
"""Test for HTTPHandler."""

Expand Down Expand Up @@ -3278,7 +3270,7 @@ def setup_via_listener(self, text, verify=None):
logging.config.stopListening()
threading_helper.join_thread(t)

@skip_on_emscripten
@support.requires_working_socket()
def test_listen_config_10_ok(self):
with support.captured_stdout() as output:
self.setup_via_listener(json.dumps(self.config10))
Expand All @@ -3298,7 +3290,7 @@ def test_listen_config_10_ok(self):
('ERROR', '4'),
], stream=output)

@skip_on_emscripten
@support.requires_working_socket()
def test_listen_config_1_ok(self):
with support.captured_stdout() as output:
self.setup_via_listener(textwrap.dedent(ConfigFileTest.config1))
Expand All @@ -3313,7 +3305,7 @@ def test_listen_config_1_ok(self):
# Original logger output is empty.
self.assert_log_lines([])

@skip_on_emscripten
@support.requires_working_socket()
def test_listen_verify(self):

def verify_fail(stuff):
Expand Down
5 changes: 4 additions & 1 deletion Lib/test/test_poll.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
import threading
import time
import unittest
from test.support import cpython_only, requires_subprocess
from test.support import (
cpython_only, requires_subprocess, requires_working_socket
)
from test.support import threading_helper
from test.support.os_helper import TESTFN

Expand All @@ -17,6 +19,7 @@
except AttributeError:
raise unittest.SkipTest("select.poll not defined")

requires_working_socket(module=True)

def find_ready_matching(ready, flag):
match = []
Expand Down
2 changes: 2 additions & 0 deletions Lib/test/test_poplib.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import asynchat
import asyncore

test_support.requires_working_socket(module=True)

HOST = socket_helper.HOST
PORT = 0

Expand Down
2 changes: 2 additions & 0 deletions Lib/test/test_select.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import unittest
from test import support

support.requires_working_socket(module=True)

@unittest.skipIf((sys.platform[:3]=='win'),
"can't easily test on this system")
class SelectTestCase(unittest.TestCase):
Expand Down
2 changes: 2 additions & 0 deletions Lib/test/test_smtplib.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import asyncore
import smtpd

support.requires_working_socket(module=True)

HOST = socket_helper.HOST

if sys.platform == 'darwin':
Expand Down
2 changes: 2 additions & 0 deletions Lib/test/test_socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
except ImportError:
fcntl = None

support.requires_working_socket(module=True)

HOST = socket_helper.HOST
# test unicode string and carriage return
MSG = 'Michael Gilfix was here\u1234\r\n'.encode('utf-8')
Expand Down
1 change: 1 addition & 0 deletions Lib/test/test_sys_settrace.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from functools import wraps
import asyncio

support.requires_working_socket(module=True)

class tracecontext:
"""Context manager that traces its enter and exit."""
Expand Down
2 changes: 2 additions & 0 deletions Lib/test/test_telnetlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
from test.support import socket_helper
import unittest

support.requires_working_socket(module=True)

HOST = socket_helper.HOST

def server(evt, serv):
Expand Down
2 changes: 2 additions & 0 deletions Lib/test/test_urllib2.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import urllib.error
import http.client

support.requires_working_socket(module=True)

# XXX
# Request
# CacheFTPHandler (hard to write)
Expand Down
3 changes: 3 additions & 0 deletions Lib/test/test_urllib2_localnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import unittest
import hashlib

from test import support
from test.support import hashlib_helper
from test.support import threading_helper
from test.support import warnings_helper
Expand All @@ -17,6 +18,8 @@
except ImportError:
ssl = None

support.requires_working_socket(module=True)

here = os.path.dirname(__file__)
# Self-signed cert file for 'localhost'
CERT_localhost = os.path.join(here, 'keycert.pem')
Expand Down
2 changes: 2 additions & 0 deletions Lib/test/test_xmlrpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
except ImportError:
gzip = None

support.requires_working_socket(module=True)

alist = [{'astring': 'foo@bar.baz.spam',
'afloat': 7283.43,
'anint': 2**20,
Expand Down
5 changes: 4 additions & 1 deletion Lib/unittest/test/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ def suite():
for fn in os.listdir(here):
if fn.startswith("test") and fn.endswith(".py"):
modname = "unittest.test." + fn[:-3]
__import__(modname)
try:
__import__(modname)
except unittest.SkipTest:
continue
module = sys.modules[modname]
suite.addTest(loader.loadTestsFromModule(module))
suite.addTest(loader.loadTestsFromName('unittest.test.testmock'))
Expand Down
2 changes: 2 additions & 0 deletions Lib/unittest/test/test_async_case.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import unittest
from test import support

support.requires_working_socket(module=True)


class MyException(Exception):
pass
Expand Down
1 change: 1 addition & 0 deletions Lib/unittest/test/test_program.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ def run(self, test):
return RESULT


@support.requires_subprocess()
class TestCommandLineArgs(unittest.TestCase):

def setUp(self):
Expand Down
2 changes: 2 additions & 0 deletions Lib/unittest/test/test_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import sys
import pickle
import subprocess
from test import support

import unittest
from unittest.case import _Outcome
Expand Down Expand Up @@ -1139,6 +1140,7 @@ def MockResultClass(*args):
expectedresult = (runner.stream, DESCRIPTIONS, VERBOSITY)
self.assertEqual(runner._makeResult(), expectedresult)

@support.requires_subprocess()
def test_warnings(self):
"""
Check that warnings argument of TextTestRunner correctly affects the
Expand Down
Loading