Skip to content

Commit

Permalink
[3.11] pythongh-114099: Add test exclusions to support running the te…
Browse files Browse the repository at this point in the history
…st suite on iOS (python#114889)

Add test annotations required to run the test suite on iOS (PEP 730).

The majority of the change involve annotating tests that use subprocess,
but are skipped on Emscripten/WASI for other reasons, and including
iOS/tvOS/watchOS under the same umbrella as macOS/darwin checks.

`is_apple` and `is_apple_mobile` test helpers have been added to
identify *any* Apple platform, and "any Apple platform except macOS",
respectively.
  • Loading branch information
freakboy3742 committed Aug 5, 2024
1 parent ed74a0f commit 9c5c950
Show file tree
Hide file tree
Showing 27 changed files with 156 additions and 92 deletions.
26 changes: 21 additions & 5 deletions Lib/test/support/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
"check_disallow_instantiation", "check_sanitizer", "skip_if_sanitizer",
# sys
"MS_WINDOWS", "is_jython", "is_android", "is_emscripten", "is_wasi",
"check_impl_detail", "unix_shell", "setswitchinterval",
"is_apple_mobile", "check_impl_detail", "unix_shell", "setswitchinterval",
# network
"open_urlresource",
# processes
Expand Down Expand Up @@ -517,7 +517,7 @@ def requires_debug_ranges(reason='requires co_positions / debug_ranges'):

is_android = hasattr(sys, 'getandroidapilevel')

if sys.platform not in ('win32', 'vxworks'):
if sys.platform not in {"win32", "vxworks", "ios", "tvos", "watchos"}:
unix_shell = '/system/bin/sh' if is_android else '/bin/sh'
else:
unix_shell = None
Expand All @@ -527,19 +527,35 @@ def requires_debug_ranges(reason='requires co_positions / debug_ranges'):
is_emscripten = sys.platform == "emscripten"
is_wasi = sys.platform == "wasi"

has_fork_support = hasattr(os, "fork") and not is_emscripten and not is_wasi
# Apple mobile platforms (iOS/tvOS/watchOS) are POSIX-like but do not
# have subprocess or fork support.
is_apple_mobile = sys.platform in {"ios", "tvos", "watchos"}
is_apple = is_apple_mobile or sys.platform == "darwin"

has_fork_support = hasattr(os, "fork") and not (
is_emscripten
or is_wasi
or is_apple_mobile
)

def requires_fork():
return unittest.skipUnless(has_fork_support, "requires working os.fork()")

has_subprocess_support = not is_emscripten and not is_wasi
has_subprocess_support = not (
is_emscripten
or is_wasi
or is_apple_mobile
)

def requires_subprocess():
"""Used for subprocess, os.spawn calls, fd inheritance"""
return unittest.skipUnless(has_subprocess_support, "requires subprocess support")

# Emscripten's socket emulation and WASI sockets have limitations.
has_socket_support = not is_emscripten and not is_wasi
has_socket_support = not (
is_emscripten
or is_wasi
)

def requires_working_socket(*, module=False):
"""Skip tests or modules that require working sockets
Expand Down
8 changes: 4 additions & 4 deletions Lib/test/support/os_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@

# TESTFN_UNICODE is a non-ascii filename
TESTFN_UNICODE = TESTFN_ASCII + "-\xe0\xf2\u0258\u0141\u011f"
if sys.platform == 'darwin':
# In Mac OS X's VFS API file names are, by definition, canonically
if support.is_apple:
# On Apple's VFS API file names are, by definition, canonically
# decomposed Unicode, encoded using UTF-8. See QA1173:
# http://developer.apple.com/mac/library/qa/qa2001/qa1173.html
import unicodedata
Expand All @@ -49,8 +49,8 @@
'encoding (%s). Unicode filename tests may not be effective'
% (TESTFN_UNENCODABLE, sys.getfilesystemencoding()))
TESTFN_UNENCODABLE = None
# macOS and Emscripten deny unencodable filenames (invalid utf-8)
elif sys.platform not in {'darwin', 'emscripten', 'wasi'}:
# Apple and Emscripten deny unencodable filenames (invalid utf-8)
elif not support.is_apple and sys.platform not in {"emscripten", "wasi"}:
try:
# ascii and utf-8 cannot encode the byte 0xff
b'\xff'.decode(sys.getfilesystemencoding())
Expand Down
14 changes: 14 additions & 0 deletions Lib/test/test_asyncio/test_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -1867,6 +1867,7 @@ def check_killed(self, returncode):
else:
self.assertEqual(-signal.SIGKILL, returncode)

@support.requires_subprocess()
def test_subprocess_exec(self):
prog = os.path.join(os.path.dirname(__file__), 'echo.py')

Expand All @@ -1888,6 +1889,7 @@ def test_subprocess_exec(self):
self.check_killed(proto.returncode)
self.assertEqual(b'Python The Winner', proto.data[1])

@support.requires_subprocess()
def test_subprocess_interactive(self):
prog = os.path.join(os.path.dirname(__file__), 'echo.py')

Expand Down Expand Up @@ -1915,6 +1917,7 @@ def test_subprocess_interactive(self):
self.loop.run_until_complete(proto.completed)
self.check_killed(proto.returncode)

@support.requires_subprocess()
def test_subprocess_shell(self):
connect = self.loop.subprocess_shell(
functools.partial(MySubprocessProtocol, self.loop),
Expand All @@ -1931,6 +1934,7 @@ def test_subprocess_shell(self):
self.assertEqual(proto.data[2], b'')
transp.close()

@support.requires_subprocess()
def test_subprocess_exitcode(self):
connect = self.loop.subprocess_shell(
functools.partial(MySubprocessProtocol, self.loop),
Expand All @@ -1942,6 +1946,7 @@ def test_subprocess_exitcode(self):
self.assertEqual(7, proto.returncode)
transp.close()

@support.requires_subprocess()
def test_subprocess_close_after_finish(self):
connect = self.loop.subprocess_shell(
functools.partial(MySubprocessProtocol, self.loop),
Expand All @@ -1956,6 +1961,7 @@ def test_subprocess_close_after_finish(self):
self.assertEqual(7, proto.returncode)
self.assertIsNone(transp.close())

@support.requires_subprocess()
def test_subprocess_kill(self):
prog = os.path.join(os.path.dirname(__file__), 'echo.py')

Expand All @@ -1972,6 +1978,7 @@ def test_subprocess_kill(self):
self.check_killed(proto.returncode)
transp.close()

@support.requires_subprocess()
def test_subprocess_terminate(self):
prog = os.path.join(os.path.dirname(__file__), 'echo.py')

Expand All @@ -1989,6 +1996,7 @@ def test_subprocess_terminate(self):
transp.close()

@unittest.skipIf(sys.platform == 'win32', "Don't have SIGHUP")
@support.requires_subprocess()
def test_subprocess_send_signal(self):
# bpo-31034: Make sure that we get the default signal handler (killing
# the process). The parent process may have decided to ignore SIGHUP,
Expand All @@ -2013,6 +2021,7 @@ def test_subprocess_send_signal(self):
finally:
signal.signal(signal.SIGHUP, old_handler)

@support.requires_subprocess()
def test_subprocess_stderr(self):
prog = os.path.join(os.path.dirname(__file__), 'echo2.py')

Expand All @@ -2034,6 +2043,7 @@ def test_subprocess_stderr(self):
self.assertTrue(proto.data[2].startswith(b'ERR:test'), proto.data[2])
self.assertEqual(0, proto.returncode)

@support.requires_subprocess()
def test_subprocess_stderr_redirect_to_stdout(self):
prog = os.path.join(os.path.dirname(__file__), 'echo2.py')

Expand All @@ -2059,6 +2069,7 @@ def test_subprocess_stderr_redirect_to_stdout(self):
transp.close()
self.assertEqual(0, proto.returncode)

@support.requires_subprocess()
def test_subprocess_close_client_stream(self):
prog = os.path.join(os.path.dirname(__file__), 'echo3.py')

Expand Down Expand Up @@ -2093,6 +2104,7 @@ def test_subprocess_close_client_stream(self):
self.loop.run_until_complete(proto.completed)
self.check_killed(proto.returncode)

@support.requires_subprocess()
def test_subprocess_wait_no_same_group(self):
# start the new process in a new session
connect = self.loop.subprocess_shell(
Expand All @@ -2105,6 +2117,7 @@ def test_subprocess_wait_no_same_group(self):
self.assertEqual(7, proto.returncode)
transp.close()

@support.requires_subprocess()
def test_subprocess_exec_invalid_args(self):
async def connect(**kwds):
await self.loop.subprocess_exec(
Expand All @@ -2118,6 +2131,7 @@ async def connect(**kwds):
with self.assertRaises(ValueError):
self.loop.run_until_complete(connect(shell=True))

@support.requires_subprocess()
def test_subprocess_shell_invalid_args(self):

async def connect(cmd=None, **kwds):
Expand Down
3 changes: 2 additions & 1 deletion Lib/test/test_asyncio/test_streams.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import threading
import unittest
from unittest import mock
from test.support import socket_helper
from test.support import requires_subprocess, socket_helper
try:
import ssl
except ImportError:
Expand Down Expand Up @@ -769,6 +769,7 @@ async def client(addr):
self.assertEqual(msg2, b"hello world 2!\n")

@unittest.skipIf(sys.platform == 'win32', "Don't have pipes")
@requires_subprocess()
def test_read_all_from_pipe_reader(self):
# See asyncio issue 168. This test is derived from the example
# subprocess_attach_read_pipe.py, but we configure the
Expand Down
2 changes: 2 additions & 0 deletions Lib/test/test_asyncio/test_subprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ def _start(self, *args, **kwargs):
self._proc.pid = -1


@support.requires_subprocess()
class SubprocessTransportTests(test_utils.TestCase):
def setUp(self):
super().setUp()
Expand Down Expand Up @@ -111,6 +112,7 @@ def test_subprocess_repr(self):
transport.close()


@support.requires_subprocess()
class SubprocessMixin:

def test_stdin_stdout(self):
Expand Down
16 changes: 10 additions & 6 deletions Lib/test/test_cmd_line_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@

import textwrap
from test import support
from test.support import import_helper
from test.support import os_helper
from test.support import import_helper, is_apple, os_helper
from test.support.script_helper import (
make_pkg, make_script, make_zip_pkg, make_zip_script,
assert_python_ok, assert_python_failure, spawn_python, kill_python)
Expand Down Expand Up @@ -555,12 +554,17 @@ def test_pep_409_verbiage(self):
self.assertTrue(text[3].startswith('NameError'))

def test_non_ascii(self):
# Mac OS X denies the creation of a file with an invalid UTF-8 name.
# Apple platforms deny the creation of a file with an invalid UTF-8 name.
# Windows allows creating a name with an arbitrary bytes name, but
# Python cannot a undecodable bytes argument to a subprocess.
# WASI does not permit invalid UTF-8 names.
if (os_helper.TESTFN_UNDECODABLE
and sys.platform not in ('win32', 'darwin', 'emscripten', 'wasi')):
# Emscripten/WASI does not permit invalid UTF-8 names.
if (
os_helper.TESTFN_UNDECODABLE
and sys.platform not in {
"win32", "emscripten", "wasi"
}
and not is_apple
):
name = os.fsdecode(os_helper.TESTFN_UNDECODABLE)
elif os_helper.TESTFN_NONASCII:
name = os_helper.TESTFN_NONASCII
Expand Down
4 changes: 3 additions & 1 deletion Lib/test/test_fcntl.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import sys
import unittest
from multiprocessing import Process
from test.support import verbose, cpython_only
from test.support import cpython_only, requires_subprocess, verbose
from test.support.import_helper import import_module
from test.support.os_helper import TESTFN, unlink

Expand Down Expand Up @@ -156,6 +156,7 @@ def test_flock(self):
self.assertRaises(TypeError, fcntl.flock, 'spam', fcntl.LOCK_SH)

@unittest.skipIf(platform.system() == "AIX", "AIX returns PermissionError")
@requires_subprocess()
def test_lockf_exclusive(self):
self.f = open(TESTFN, 'wb+')
cmd = fcntl.LOCK_EX | fcntl.LOCK_NB
Expand All @@ -167,6 +168,7 @@ def test_lockf_exclusive(self):
self.assertEqual(p.exitcode, 0)

@unittest.skipIf(platform.system() == "AIX", "AIX returns PermissionError")
@requires_subprocess()
def test_lockf_share(self):
self.f = open(TESTFN, 'wb+')
cmd = fcntl.LOCK_SH | fcntl.LOCK_NB
Expand Down
3 changes: 3 additions & 0 deletions Lib/test/test_ftplib.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

from unittest import TestCase, skipUnless
from test import support
from test.support import requires_subprocess
from test.support import threading_helper
from test.support import socket_helper
from test.support import warnings_helper
Expand Down Expand Up @@ -902,6 +903,7 @@ def retr():


@skipUnless(ssl, "SSL not available")
@requires_subprocess()
class TestTLS_FTPClassMixin(TestFTPClass):
"""Repeat TestFTPClass tests starting the TLS layer for both control
and data connections first.
Expand All @@ -918,6 +920,7 @@ def setUp(self, encoding=DEFAULT_ENCODING):


@skipUnless(ssl, "SSL not available")
@requires_subprocess()
class TestTLS_FTPClass(TestCase):
"""Specific TLS_FTP class tests."""

Expand Down
22 changes: 13 additions & 9 deletions Lib/test/test_genericpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
import sys
import unittest
import warnings
from test.support import is_emscripten
from test.support import os_helper
from test.support import warnings_helper
from test.support import (
is_apple, is_emscripten, os_helper, warnings_helper
)
from test.support.script_helper import assert_python_ok
from test.support.os_helper import FakePath

Expand Down Expand Up @@ -483,12 +483,16 @@ def test_abspath_issue3426(self):
self.assertIsInstance(abspath(path), str)

def test_nonascii_abspath(self):
if (os_helper.TESTFN_UNDECODABLE
# macOS and Emscripten deny the creation of a directory with an
# invalid UTF-8 name. Windows allows creating a directory with an
# arbitrary bytes name, but fails to enter this directory
# (when the bytes name is used).
and sys.platform not in ('win32', 'darwin', 'emscripten', 'wasi')):
if (
os_helper.TESTFN_UNDECODABLE
# Apple platforms and Emscripten/WASI deny the creation of a
# directory with an invalid UTF-8 name. Windows allows creating a
# directory with an arbitrary bytes name, but fails to enter this
# directory (when the bytes name is used).
and sys.platform not in {
"win32", "emscripten", "wasi"
} and not is_apple
):
name = os_helper.TESTFN_UNDECODABLE
elif os_helper.TESTFN_NONASCII:
name = os_helper.TESTFN_NONASCII
Expand Down
18 changes: 10 additions & 8 deletions Lib/test/test_httpservers.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@

import unittest
from test import support
from test.support import os_helper
from test.support import threading_helper
from test.support import (
is_apple, os_helper, requires_subprocess, threading_helper
)

support.requires_working_socket(module=True)

Expand Down Expand Up @@ -410,8 +411,8 @@ def close_conn():
reader.close()
return body

@unittest.skipIf(sys.platform == 'darwin',
'undecodable name cannot always be decoded on macOS')
@unittest.skipIf(is_apple,
'undecodable name cannot always be decoded on Apple platforms')
@unittest.skipIf(sys.platform == 'win32',
'undecodable name cannot be decoded on win32')
@unittest.skipUnless(os_helper.TESTFN_UNDECODABLE,
Expand All @@ -422,11 +423,11 @@ def test_undecodable_filename(self):
with open(os.path.join(self.tempdir, filename), 'wb') as f:
f.write(os_helper.TESTFN_UNDECODABLE)
response = self.request(self.base_url + '/')
if sys.platform == 'darwin':
# On Mac OS the HFS+ filesystem replaces bytes that aren't valid
# UTF-8 into a percent-encoded value.
if is_apple:
# On Apple platforms the HFS+ filesystem replaces bytes that
# aren't valid UTF-8 into a percent-encoded value.
for name in os.listdir(self.tempdir):
if name != 'test': # Ignore a filename created in setUp().
if name != 'test': # Ignore a filename created in setUp().
filename = name
break
body = self.check_status_and_reason(response, HTTPStatus.OK)
Expand Down Expand Up @@ -697,6 +698,7 @@ def test_html_escape_filename(self):

@unittest.skipIf(hasattr(os, 'geteuid') and os.geteuid() == 0,
"This test can't be run reliably as root (issue #13308).")
@requires_subprocess()
class CGIHTTPServerTestCase(BaseTestCase):
class request_handler(NoLogRequestHandler, CGIHTTPRequestHandler):
pass
Expand Down
Loading

0 comments on commit 9c5c950

Please sign in to comment.