def testPutEnv(self): "Put an environment variable and ensure that spawned processes see the change" value = "Value we set" os.putenv("NEWVARIABLE", value) newValue = os.popen("echo $NEWVARIABLE").read().strip() if newValue == "$NEWVARIABLE": newValue = os.popen("echo %NEWVARIABLE%").read().strip() if newValue == "%NEWVARIABLE%": raise support.TestSkipped( "Unable to find a subshell to execute echo") assert newValue == value, ("Expected (%s) to equal value we set (%s)" % (newValue, value))
def _create_and_do_getcwd(dirname, current_path_length=0): try: os.mkdir(dirname) except: raise support.TestSkipped( "mkdir cannot create directory sufficiently deep for getcwd test" ) os.chdir(dirname) try: os.getcwd() if current_path_length < 1027: _create_and_do_getcwd( dirname, current_path_length + len(dirname) + 1) finally: os.chdir('..') os.rmdir(dirname)
import unittest from test import support from contextlib import closing, nested import gc import pickle import select import signal import subprocess import traceback import sys, os, time, errno if sys.platform[:3] in ('win', 'os2') or sys.platform == 'riscos': raise support.TestSkipped("Can't test signal on %s" % \ sys.platform) class HandlerBCalled(Exception): pass def exit_subprocess(): """Use os._exit(0) to exit the current subprocess. Otherwise, the test catches the SystemExit and continues executing in parallel with the original test, so you wind up with an exponential number of tests running concurrently. """ os._exit(0) def ignoring_eintr(__func, *args, **kwargs):
""" Tests for kqueue wrapper. """ import socket import errno import time import select import sys import unittest from test import support if not hasattr(select, "kqueue"): raise support.TestSkipped("test works only on BSD") class TestKQueue(unittest.TestCase): def test_create_queue(self): kq = select.kqueue() self.assert_(kq.fileno() > 0, kq.fileno()) self.assert_(not kq.closed) kq.close() self.assert_(kq.closed) self.assertRaises(ValueError, kq.fileno) def test_create_event(self): from operator import lt, le, gt, ge fd = sys.stderr.fileno() ev = select.kevent(fd) other = select.kevent(1000) self.assertEqual(ev.ident, fd) self.assertEqual(ev.filter, select.KQ_FILTER_READ)
def __init__(self, *args, **kw): unittest.TestCase.__init__(self, *args, **kw) try: self.open_mapping_file() # test it to report the error early except IOError: raise support.TestSkipped("Could not retrieve " + self.mapfileurl)
def test_main(): if (os._name if support.is_jython else os.name) != 'nt': raise support.TestSkipped('NT specific test') support.run_unittest(NTAbspathTestCase)
"Test posix functions" from test import support try: import posix except ImportError: raise support.TestSkipped("posix is not available") import time import os import pwd import shutil import unittest import warnings warnings.filterwarnings('ignore', '.* potential security risk .*', RuntimeWarning) class PosixTester(unittest.TestCase): def setUp(self): # create empty file fp = open(support.TESTFN, 'w+') fp.close() def tearDown(self): support.unlink(support.TESTFN) def testNoArgFunctions(self): # test posix functions which take no arguments and have # no side-effects which we need to cleanup (e.g., fork, wait, abort)
from test import support import unittest import nis raise support.TestSkipped("test_nis hangs on Solaris") class NisTests(unittest.TestCase): def test_maps(self): try: maps = nis.maps() except nis.error as msg: # NIS is probably not active, so this test isn't useful if support.verbose: print("Test Skipped:", msg) # Can't raise TestSkipped as regrtest only recognizes the exception # import time. return try: # On some systems, this map is only accessible to the # super user maps.remove("passwd.adjunct.byname") except ValueError: pass done = 0 for nismap in maps: mapping = nis.cat(nismap) for k, v in mapping.items(): if not k: continue
'largefile', 'test requires %s bytes and a long time to run' % str(size)) else: # Only run if the current filesystem supports large files. # (Skip this test on Windows, since we now always support large files.) f = open(support.TESTFN, 'wb') try: # 2**31 == 2147483648 f.seek(2147483649) # Seeking is not enough of a test: you must write and flush, too! f.write("x") f.flush() except (IOError, OverflowError): f.close() os.unlink(support.TESTFN) raise support.TestSkipped("filesystem does not have largefile support") else: f.close() def expect(got_this, expect_this): if support.verbose: print('%r =?= %r ...' % (got_this, expect_this), end=' ') if got_this != expect_this: if support.verbose: print('no') raise support.TestFailed('got %r, but expected %r' %\ (got_this, expect_this)) else: if support.verbose: print('yes')
# Test the Unicode versions of normal file functions # open, os.open, os.stat. os.listdir, os.rename, os.remove, os.mkdir, os.chdir, os.rmdir import sys, os, unittest from test import support if not os.path.supports_unicode_filenames: raise support.TestSkipped("test works only on NT+") filenames = [ 'abc', 'ascii', 'Gr\xfc\xdf-Gott', '\u0393\u03b5\u03b9\u03ac-\u03c3\u03b1\u03c2', '\u0417\u0434\u0440\u0430\u0432\u0441\u0442\u0432\u0443\u0439\u0442\u0435', '\u306b\u307d\u3093', '\u05d4\u05e9\u05e7\u05e6\u05e5\u05e1', '\u66e8\u66e9\u66eb', '\u66e8\u05e9\u3093\u0434\u0393\xdf', ] # Destroy directory dirname and all files under it, to one level. def deltree(dirname): # Don't hide legitimate errors: if one of these suckers exists, it's # an error if we can't remove it. if os.path.exists(dirname): # must pass unicode to os.listdir() so we get back unicode results. for fname in os.listdir(str(dirname)): os.unlink(os.path.join(dirname, fname)) os.rmdir(dirname)
# Tests universal newline support for both reading and parsing files. import unittest import os import sys from test import support if not hasattr(sys.stdin, 'newlines'): raise support.TestSkipped( "This Python does not have universal newline support") FATX = 'x' * (2**14) DATA_TEMPLATE = [ "line1=1", "line2='this is a very long line designed to go past any default " + "buffer limits that exist in io.py but we also want to test " + "the uncommon case, naturally.'", "def line3():pass", "line4 = '%s'" % FATX, ] DATA_LF = "\n".join(DATA_TEMPLATE) + "\n" DATA_CR = "\r".join(DATA_TEMPLATE) + "\r" DATA_CRLF = "\r\n".join(DATA_TEMPLATE) + "\r\n" # Note that DATA_MIXED also tests the ability to recognize a lone \r # before end-of-file. DATA_MIXED = "\n".join(DATA_TEMPLATE) + "\r" DATA_SPLIT = [x + "\n" for x in DATA_TEMPLATE]
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ Tests for epoll wrapper. """ import os import socket import errno import time import select import tempfile import unittest from test import support if not hasattr(select, "epoll"): raise support.TestSkipped("test works only on Linux 2.6") try: select.epoll() except IOError as e: if e.errno == errno.ENOSYS: raise support.TestSkipped("kernel doesn't support epoll()") class TestEPoll(unittest.TestCase): def setUp(self): self.serverSocket = socket.socket() self.serverSocket.bind(('127.0.0.1', 0)) self.serverSocket.listen(1) self.connections = [self.serverSocket]