mirror of
https://github.com/python/cpython.git
synced 2025-08-30 21:48:47 +00:00
Issue #11049: adding some tests to test.support
Based on original patch by Giampaolo Rodola with contributions from R. David Murray
This commit is contained in:
parent
54ef40b000
commit
6c51999221
2 changed files with 189 additions and 10 deletions
|
@ -170,7 +170,7 @@ def get_attribute(obj, name):
|
||||||
attribute = getattr(obj, name)
|
attribute = getattr(obj, name)
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
raise unittest.SkipTest("module %s has no attribute %s" % (
|
raise unittest.SkipTest("module %s has no attribute %s" % (
|
||||||
obj.__name__, name))
|
repr(obj), name))
|
||||||
else:
|
else:
|
||||||
return attribute
|
return attribute
|
||||||
|
|
||||||
|
@ -577,14 +577,15 @@ def temp_cwd(name='tempcwd', quiet=False, path=None):
|
||||||
rmtree(name)
|
rmtree(name)
|
||||||
|
|
||||||
|
|
||||||
@contextlib.contextmanager
|
if hasattr(os, "umask"):
|
||||||
def temp_umask(umask):
|
@contextlib.contextmanager
|
||||||
"""Context manager that temporarily sets the process umask."""
|
def temp_umask(umask):
|
||||||
oldmask = os.umask(umask)
|
"""Context manager that temporarily sets the process umask."""
|
||||||
try:
|
oldmask = os.umask(umask)
|
||||||
yield
|
try:
|
||||||
finally:
|
yield
|
||||||
os.umask(oldmask)
|
finally:
|
||||||
|
os.umask(oldmask)
|
||||||
|
|
||||||
|
|
||||||
def findfile(file, here=__file__, subdir=None):
|
def findfile(file, here=__file__, subdir=None):
|
||||||
|
@ -1029,7 +1030,7 @@ def python_is_optimized():
|
||||||
for opt in cflags.split():
|
for opt in cflags.split():
|
||||||
if opt.startswith('-O'):
|
if opt.startswith('-O'):
|
||||||
final_opt = opt
|
final_opt = opt
|
||||||
return final_opt and final_opt != '-O0'
|
return final_opt != '' and final_opt != '-O0'
|
||||||
|
|
||||||
|
|
||||||
#=======================================================================
|
#=======================================================================
|
||||||
|
|
178
Lib/test/test_support.py
Normal file
178
Lib/test/test_support.py
Normal file
|
@ -0,0 +1,178 @@
|
||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
import sys
|
||||||
|
import os
|
||||||
|
import unittest
|
||||||
|
import socket
|
||||||
|
import tempfile
|
||||||
|
import errno
|
||||||
|
from test import support
|
||||||
|
|
||||||
|
TESTFN = support.TESTFN
|
||||||
|
TESTDIRN = os.path.basename(tempfile.mkdtemp(dir='.'))
|
||||||
|
|
||||||
|
|
||||||
|
class TestSupport(unittest.TestCase):
|
||||||
|
def setUp(self):
|
||||||
|
support.unlink(TESTFN)
|
||||||
|
support.rmtree(TESTDIRN)
|
||||||
|
tearDown = setUp
|
||||||
|
|
||||||
|
def test_import_module(self):
|
||||||
|
support.import_module("ftplib")
|
||||||
|
self.assertRaises(unittest.SkipTest, support.import_module, "foo")
|
||||||
|
|
||||||
|
def test_import_fresh_module(self):
|
||||||
|
support.import_fresh_module("ftplib")
|
||||||
|
|
||||||
|
def test_get_attribute(self):
|
||||||
|
self.assertEqual(support.get_attribute(self, "test_get_attribute"),
|
||||||
|
self.test_get_attribute)
|
||||||
|
self.assertRaises(unittest.SkipTest, support.get_attribute, self, "foo")
|
||||||
|
|
||||||
|
def test_get_original_stdout(self):
|
||||||
|
self.assertEqual(support.get_original_stdout(), sys.stdout)
|
||||||
|
|
||||||
|
def test_unload(self):
|
||||||
|
import sched
|
||||||
|
self.assertIn("sched", sys.modules)
|
||||||
|
support.unload("sched")
|
||||||
|
self.assertNotIn("sched", sys.modules)
|
||||||
|
|
||||||
|
def test_unlink(self):
|
||||||
|
with open(TESTFN, "w") as f:
|
||||||
|
pass
|
||||||
|
support.unlink(TESTFN)
|
||||||
|
self.assertFalse(os.path.exists(TESTFN))
|
||||||
|
support.unlink(TESTFN)
|
||||||
|
|
||||||
|
def test_rmtree(self):
|
||||||
|
os.mkdir(TESTDIRN)
|
||||||
|
os.mkdir(os.path.join(TESTDIRN, TESTDIRN))
|
||||||
|
support.rmtree(TESTDIRN)
|
||||||
|
self.assertFalse(os.path.exists(TESTDIRN))
|
||||||
|
support.rmtree(TESTDIRN)
|
||||||
|
|
||||||
|
def test_forget(self):
|
||||||
|
import smtplib
|
||||||
|
support.forget("smtplib")
|
||||||
|
self.assertNotIn("smtplib", sys.modules)
|
||||||
|
|
||||||
|
def test_HOST(self):
|
||||||
|
s = socket.socket()
|
||||||
|
s.bind((support.HOST, 0))
|
||||||
|
s.close()
|
||||||
|
|
||||||
|
def test_find_unused_port(self):
|
||||||
|
port = support.find_unused_port()
|
||||||
|
s = socket.socket()
|
||||||
|
s.bind((support.HOST, port))
|
||||||
|
s.close()
|
||||||
|
|
||||||
|
def test_bind_port(self):
|
||||||
|
s = socket.socket()
|
||||||
|
support.bind_port(s)
|
||||||
|
s.listen(1)
|
||||||
|
s.close()
|
||||||
|
|
||||||
|
def test_temp_cwd(self):
|
||||||
|
here = os.getcwd()
|
||||||
|
with support.temp_cwd(name=TESTFN):
|
||||||
|
self.assertEqual(os.path.basename(os.getcwd()), TESTFN)
|
||||||
|
self.assertFalse(os.path.exists(TESTFN))
|
||||||
|
self.assertTrue(os.path.basename(os.getcwd()), here)
|
||||||
|
|
||||||
|
def test_sortdict(self):
|
||||||
|
self.assertEqual(support.sortdict({3:3, 2:2, 1:1}), "{1: 1, 2: 2, 3: 3}")
|
||||||
|
|
||||||
|
def test_make_bad_fd(self):
|
||||||
|
fd = support.make_bad_fd()
|
||||||
|
with self.assertRaises(OSError) as cm:
|
||||||
|
os.write(fd, b"foo")
|
||||||
|
self.assertEqual(cm.exception.errno, errno.EBADF)
|
||||||
|
|
||||||
|
def test_check_syntax_error(self):
|
||||||
|
support.check_syntax_error(self, "def class")
|
||||||
|
self.assertRaises(AssertionError, support.check_syntax_error, self, "1")
|
||||||
|
|
||||||
|
def test_CleanImport(self):
|
||||||
|
import importlib
|
||||||
|
with support.CleanImport("asyncore"):
|
||||||
|
importlib.import_module("asyncore")
|
||||||
|
|
||||||
|
def test_DirsOnSysPath(self):
|
||||||
|
with support.DirsOnSysPath('foo', 'bar'):
|
||||||
|
self.assertIn("foo", sys.path)
|
||||||
|
self.assertIn("bar", sys.path)
|
||||||
|
self.assertNotIn("foo", sys.path)
|
||||||
|
self.assertNotIn("bar", sys.path)
|
||||||
|
|
||||||
|
def test_captured_stdout(self):
|
||||||
|
with support.captured_stdout() as s:
|
||||||
|
print("hello")
|
||||||
|
self.assertEqual(s.getvalue(), "hello\n")
|
||||||
|
|
||||||
|
def test_captured_stderr(self):
|
||||||
|
with support.captured_stderr() as s:
|
||||||
|
print("hello", file=sys.stderr)
|
||||||
|
self.assertEqual(s.getvalue(), "hello\n")
|
||||||
|
|
||||||
|
def test_captured_stdin(self):
|
||||||
|
with support.captured_stdin() as s:
|
||||||
|
print("hello", file=sys.stdin)
|
||||||
|
self.assertEqual(s.getvalue(), "hello\n")
|
||||||
|
|
||||||
|
def test_gc_collect(self):
|
||||||
|
support.gc_collect()
|
||||||
|
|
||||||
|
def test_python_is_optimized(self):
|
||||||
|
self.assertIsInstance(support.python_is_optimized(), bool)
|
||||||
|
|
||||||
|
def test_swap_attr(self):
|
||||||
|
class Obj:
|
||||||
|
x = 1
|
||||||
|
obj = Obj()
|
||||||
|
with support.swap_attr(obj, "x", 5):
|
||||||
|
self.assertEqual(obj.x, 5)
|
||||||
|
self.assertEqual(obj.x, 1)
|
||||||
|
|
||||||
|
def test_swap_item(self):
|
||||||
|
D = {"item":1}
|
||||||
|
with support.swap_item(D, "item", 5):
|
||||||
|
self.assertEqual(D["item"], 5)
|
||||||
|
self.assertEqual(D["item"], 1)
|
||||||
|
|
||||||
|
# XXX -follows a list of untested API
|
||||||
|
# make_legacy_pyc
|
||||||
|
# is_resource_enabled
|
||||||
|
# requires
|
||||||
|
# fcmp
|
||||||
|
# umaks
|
||||||
|
# findfile
|
||||||
|
# check_warnings
|
||||||
|
# EnvironmentVarGuard
|
||||||
|
# TransientResource
|
||||||
|
# transient_internet
|
||||||
|
# run_with_locale
|
||||||
|
# set_memlimit
|
||||||
|
# bigmemtest
|
||||||
|
# precisionbigmemtest
|
||||||
|
# bigaddrspacetest
|
||||||
|
# requires_resource
|
||||||
|
# run_doctest
|
||||||
|
# threading_cleanup
|
||||||
|
# reap_threads
|
||||||
|
# reap_children
|
||||||
|
# strip_python_stderr
|
||||||
|
# args_from_interpreter_flags
|
||||||
|
# can_symlink
|
||||||
|
# skip_unless_symlink
|
||||||
|
|
||||||
|
|
||||||
|
def test_main():
|
||||||
|
tests = [TestSupport]
|
||||||
|
support.run_unittest(*tests)
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
test_main()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue