Skip to content

Commit

Permalink
[3.10] 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 Dec 13, 2024
1 parent bba3828 commit 26a474a
Show file tree
Hide file tree
Showing 26 changed files with 146 additions and 75 deletions.
20 changes: 18 additions & 2 deletions Lib/test/support/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
"check__all__", "skip_if_buggy_ucrt_strfptime",
"check_disallow_instantiation", "check_sanitizer", "skip_if_sanitizer",
# sys
"is_jython", "is_android", "check_impl_detail", "unix_shell",
"is_jython", "is_android", "is_apple_mobile", "check_impl_detail", "unix_shell",
"setswitchinterval",
# network
"open_urlresource",
Expand Down Expand Up @@ -469,11 +469,27 @@ def requires_lzma(reason='requires lzma'):

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

# 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_apple_mobile

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

has_subprocess_support = not is_apple_mobile

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

# Define the URL of a dedicated HTTP server for the network tests.
# The URL must use clear-text HTTP: no redirection to encrypted HTTPS.
TEST_HTTP_URL = "http://www.pythontest.net"
Expand Down
9 changes: 5 additions & 4 deletions Lib/test/support/os_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import os
import re
import stat
import support
import sys
import time
import unittest
Expand All @@ -23,8 +24,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 +50,8 @@
'encoding (%s). Unicode filename tests may not be effective'
% (TESTFN_UNENCODABLE, sys.getfilesystemencoding()))
TESTFN_UNENCODABLE = None
# Mac OS X denies unencodable filenames (invalid utf-8)
elif sys.platform != 'darwin':
# Apple denies unencodable filenames (invalid utf-8)
elif not support.is_apple:
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 @@ -1799,6 +1799,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 @@ -1820,6 +1821,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 @@ -1847,6 +1849,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 @@ -1863,6 +1866,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 @@ -1874,6 +1878,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 @@ -1888,6 +1893,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 @@ -1904,6 +1910,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 @@ -1921,6 +1928,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 @@ -1945,6 +1953,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 @@ -1966,6 +1975,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 @@ -1991,6 +2001,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 @@ -2025,6 +2036,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 @@ -2037,6 +2049,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 @@ -2050,6 +2063,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 @@ -707,6 +707,7 @@ async def client(path):
self.assertEqual(messages, [])

@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 @@ -39,6 +39,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 @@ -104,6 +105,7 @@ def test_subprocess_repr(self):
transport.close()


@support.requires_subprocess()
class SubprocessMixin:

def test_stdin_stdout(self):
Expand Down
12 changes: 7 additions & 5 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 @@ -554,11 +553,14 @@ 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.
if (os_helper.TESTFN_UNDECODABLE
and sys.platform not in ('win32', 'darwin')):
if (
os_helper.TESTFN_UNDECODABLE
and sys.platform not in {"win32"}
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 @@ -904,6 +905,7 @@ def callback(data):


@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 @@ -920,6 +922,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
19 changes: 11 additions & 8 deletions Lib/test/test_genericpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
import sys
import unittest
import warnings
from test.support import os_helper
from test.support import warnings_helper
from test.support import (
is_apple, os_helper, warnings_helper
)
from test.support.script_helper import assert_python_ok
from test.support.os_helper import FakePath

Expand Down Expand Up @@ -475,12 +476,14 @@ def test_abspath_issue3426(self):
self.assertIsInstance(abspath(path), str)

def test_nonascii_abspath(self):
if (os_helper.TESTFN_UNDECODABLE
# Mac OS X denies 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')):
if (
os_helper.TESTFN_UNDECODABLE
# Apple platforms 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"} 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
)


class NoLogRequestHandler:
Expand Down Expand Up @@ -388,8 +389,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 @@ -400,11 +401,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 @@ -670,6 +671,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
16 changes: 7 additions & 9 deletions Lib/test/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,9 @@
from test import support
from test.support.script_helper import (
assert_python_ok, assert_python_failure, run_python_until_end)
from test.support import import_helper
from test.support import os_helper
from test.support import threading_helper
from test.support import warnings_helper
from test.support import skip_if_sanitizer
from test.support import (
import_helper, is_apple, os_helper, skip_if_sanitizer, threading_helper, warnings_helper
)
from test.support.os_helper import FakePath

import codecs
Expand Down Expand Up @@ -598,10 +596,10 @@ def test_raw_bytes_io(self):
self.read_ops(f, True)

def test_large_file_ops(self):
# On Windows and Mac OSX this test consumes large resources; It takes
# a long time to build the >2 GiB file and takes >2 GiB of disk space
# therefore the resource must be enabled to run this test.
if sys.platform[:3] == 'win' or sys.platform == 'darwin':
# On Windows and Apple platforms this test consumes large resources; It
# takes a long time to build the >2 GiB file and takes >2 GiB of disk
# space therefore the resource must be enabled to run this test.
if sys.platform[:3] == 'win' or is_apple:
support.requires(
'largefile',
'test requires %s bytes and a long time to run' % self.LARGE)
Expand Down
Loading

0 comments on commit 26a474a

Please sign in to comment.