# tempfile.py unit tests.
from test
import test_support
warnings
.filterwarnings("ignore",
message
="mktemp", module
=__name__
)
has_textmode
= (tempfile
._text
_openflags
!= tempfile
._bin
_openflags
)
has_spawnl
= hasattr(os
, 'spawnl')
# TEST_FILES may need to be tweaked for systems depending on the maximum
# number of files that can be opened at one time (see ulimit -n)
if sys
.platform
== 'mac':
elif sys
.platform
== 'openbsd3':
# This is organized as one test for each chunk of code in tempfile.py,
# in order of their appearance in the file. Testing which requires
# threads is not done here.
class TC(unittest
.TestCase
):
str_check
= re
.compile(r
"[a-zA-Z0-9_-]{6}$")
def failOnException(self
, what
, ei
=None):
self
.fail("%s raised %s: %s" % (what
, ei
[0], ei
[1]))
def nameCheck(self
, name
, dir, pre
, suf
):
(ndir
, nbase
) = os
.path
.split(name
)
nsuf
= nbase
[len(nbase
)-len(suf
):]
# check for equality of the absolute paths!
self
.assertEqual(os
.path
.abspath(ndir
), os
.path
.abspath(dir),
"file '%s' not in directory '%s'" % (name
, dir))
self
.assertEqual(npre
, pre
,
"file '%s' does not begin with '%s'" % (nbase
, pre
))
self
.assertEqual(nsuf
, suf
,
"file '%s' does not end with '%s'" % (nbase
, suf
))
nbase
= nbase
[len(pre
):len(nbase
)-len(suf
)]
self
.assert_(self
.str_check
.match(nbase
),
"random string '%s' does not match /^[a-zA-Z0-9_-]{6}$/"
# There are no surprising symbols in the tempfile module
"NamedTemporaryFile" : 1,
if key
[0] != '_' and key
not in expected
:
self
.failUnless(len(unexp
) == 0,
"unexpected keys: %s" % unexp
)
test_classes
.append(test_exports
)
class test__RandomNameSequence(TC
):
"""Test the internal iterator object _RandomNameSequence."""
self
.r
= tempfile
._RandomNameSequence
()
def test_get_six_char_str(self
):
# _RandomNameSequence returns a six-character string
self
.nameCheck(s
, '', '', '')
# _RandomNameSequence returns no duplicate strings (stochastic)
for i
in xrange(TEST_FILES
):
self
.nameCheck(s
, '', '', '')
def test_supports_iter(self
):
# _RandomNameSequence supports the iterator protocol
failOnException("iteration")
test_classes
.append(test__RandomNameSequence
)
class test__candidate_tempdir_list(TC
):
"""Test the internal function _candidate_tempdir_list."""
def test_nonempty_list(self
):
# _candidate_tempdir_list returns a nonempty list of strings
cand
= tempfile
._candidate
_tempdir
_list
()
self
.failIf(len(cand
) == 0)
self
.assert_(isinstance(c
, basestring
),
"%s is not a string" % c
)
def test_wanted_dirs(self
):
# _candidate_tempdir_list contains the expected directories
# Make sure the interesting environment variables are all set.
for envname
in 'TMPDIR', 'TEMP', 'TMP':
dirname
= os
.getenv(envname
)
os
.environ
[envname
] = os
.path
.abspath(envname
)
cand
= tempfile
._candidate
_tempdir
_list
()
for envname
in 'TMPDIR', 'TEMP', 'TMP':
dirname
= os
.getenv(envname
)
if not dirname
: raise ValueError
self
.assert_(dirname
in cand
)
except (AttributeError, os
.error
):
self
.assert_(dirname
in cand
)
# Not practical to try to verify the presence of OS-specific
test_classes
.append(test__candidate_tempdir_list
)
# We test _get_default_tempdir by testing gettempdir.
class test__get_candidate_names(TC
):
"""Test the internal function _get_candidate_names."""
# _get_candidate_names returns a _RandomNameSequence object
obj
= tempfile
._get
_candidate
_names
()
self
.assert_(isinstance(obj
, tempfile
._RandomNameSequence
))
def test_same_thing(self
):
# _get_candidate_names always returns the same object
a
= tempfile
._get
_candidate
_names
()
b
= tempfile
._get
_candidate
_names
()
test_classes
.append(test__get_candidate_names
)
class test__mkstemp_inner(TC
):
"""Test the internal function _mkstemp_inner."""
_bflags
= tempfile
._bin
_openflags
_tflags
= tempfile
._text
_openflags
def __init__(self
, dir, pre
, suf
, bin
):
if bin
: flags
= self
._bflags
else: flags
= self
._tflags
(self
.fd
, self
.name
) = tempfile
._mkstemp
_inner
(dir, pre
, suf
, flags
)
def do_create(self
, dir=None, pre
="", suf
="", bin
=1):
dir = tempfile
.gettempdir()
file = self
.mkstemped(dir, pre
, suf
, bin
)
self
.failOnException("_mkstemp_inner")
self
.nameCheck(file.name
, dir, pre
, suf
)
# _mkstemp_inner can create files
self
.do_create().write("blat")
self
.do_create(pre
="a").write("blat")
self
.do_create(suf
="b").write("blat")
self
.do_create(pre
="a", suf
="b").write("blat")
self
.do_create(pre
="aa", suf
=".txt").write("blat")
def test_basic_many(self
):
# _mkstemp_inner can create many files (stochastic)
extant
= range(TEST_FILES
)
extant
[i
] = self
.do_create(pre
="aa")
def test_choose_directory(self
):
# _mkstemp_inner can create files in a user-selected directory
self
.do_create(dir=dir).write("blat")
def test_file_mode(self
):
# _mkstemp_inner creates files with the proper mode
return # ugh, can't use TestSkipped.
mode
= stat
.S_IMODE(os
.stat(file.name
).st_mode
)
if sys
.platform
in ('win32', 'os2emx', 'mac'):
# There's no distinction among 'user', 'group' and 'world';
# replicate the 'user' bits.
expected
= user
* (1 + 8 + 64)
self
.assertEqual(mode
, expected
)
def test_noinherit(self
):
# _mkstemp_inner file handles are not inherited by child processes
return # ugh, can't use TestSkipped.
# We have to exec something, so that FD_CLOEXEC will take
# effect. The core of this test is therefore in
# tf_inherit_check.py, which see.
tester
= os
.path
.join(os
.path
.dirname(os
.path
.abspath(me
)),
# On Windows a spawn* /path/ with embedded spaces shouldn't be quoted,
# but an arg with embedded spaces should be decorated with double
if sys
.platform
in ('win32'):
decorated
= '"%s"' % sys
.executable
decorated
= sys
.executable
retval
= os
.spawnl(os
.P_WAIT
, sys
.executable
, decorated
, tester
, v
, fd
)
"child process caught fatal signal %d" % -retval
)
self
.failIf(retval
> 0, "child process reports failure")
# _mkstemp_inner can create files in text mode
return # ugh, can't use TestSkipped.
self
.do_create(bin
=0).write("blat\n")
# XXX should test that the file really is a text file
test_classes
.append(test__mkstemp_inner
)
class test_gettempprefix(TC
):
"""Test gettempprefix()."""
def test_sane_template(self
):
# gettempprefix returns a nonempty prefix string
p
= tempfile
.gettempprefix()
self
.assert_(isinstance(p
, basestring
))
def test_usable_template(self
):
# gettempprefix returns a usable prefix string
# Create a temp directory, avoiding use of the prefix.
# Then attempt to create a file whose name is
# prefix + 'xxxxxx.xxx' in that directory.
p
= tempfile
.gettempprefix() + "xxxxxx.xxx"
d
= tempfile
.mkdtemp(prefix
="")
fd
= os
.open(p
, os
.O_RDWR | os
.O_CREAT
)
self
.failOnException("os.open")
test_classes
.append(test_gettempprefix
)
class test_gettempdir(TC
):
def test_directory_exists(self
):
# gettempdir returns a directory which exists
dir = tempfile
.gettempdir()
self
.assert_(os
.path
.isabs(dir) or dir == os
.curdir
,
"%s is not an absolute path" % dir)
self
.assert_(os
.path
.isdir(dir),
"%s is not a directory" % dir)
def test_directory_writable(self
):
# gettempdir returns a directory writable by the user
# sneaky: just instantiate a NamedTemporaryFile, which
# defaults to writing into the directory returned by
file = tempfile
.NamedTemporaryFile()
self
.failOnException("create file in %s" % tempfile
.gettempdir())
def test_same_thing(self
):
# gettempdir always returns the same object
a
= tempfile
.gettempdir()
b
= tempfile
.gettempdir()
test_classes
.append(test_gettempdir
)
def do_create(self
, dir=None, pre
="", suf
="", ):
dir = tempfile
.gettempdir()
(fd
, name
) = tempfile
.mkstemp(dir=dir, prefix
=pre
, suffix
=suf
)
(ndir
, nbase
) = os
.path
.split(name
)
adir
= os
.path
.abspath(dir)
self
.assertEqual(adir
, ndir
,
"Directory '%s' incorrectly returned as '%s'" % (adir
, ndir
))
self
.failOnException("mkstemp")
self
.nameCheck(name
, dir, pre
, suf
)
# mkstemp can create files
self
.do_create(pre
="a", suf
="b")
self
.do_create(pre
="aa", suf
=".txt")
def test_choose_directory(self
):
# mkstemp can create directories in a user-selected directory
test_classes
.append(test_mkstemp
)
def do_create(self
, dir=None, pre
="", suf
=""):
dir = tempfile
.gettempdir()
name
= tempfile
.mkdtemp(dir=dir, prefix
=pre
, suffix
=suf
)
self
.failOnException("mkdtemp")
self
.nameCheck(name
, dir, pre
, suf
)
# mkdtemp can create directories
os
.rmdir(self
.do_create())
os
.rmdir(self
.do_create(pre
="a"))
os
.rmdir(self
.do_create(suf
="b"))
os
.rmdir(self
.do_create(pre
="a", suf
="b"))
os
.rmdir(self
.do_create(pre
="aa", suf
=".txt"))
def test_basic_many(self
):
# mkdtemp can create many directories (stochastic)
extant
= range(TEST_FILES
)
extant
[i
] = self
.do_create(pre
="aa")
if(isinstance(i
, basestring
)):
def test_choose_directory(self
):
# mkdtemp can create directories in a user-selected directory
os
.rmdir(self
.do_create(dir=dir))
# mkdtemp creates directories with the proper mode
return # ugh, can't use TestSkipped.
mode
= stat
.S_IMODE(os
.stat(dir).st_mode
)
mode
&= 0777 # Mask off sticky bits inherited from /tmp
if sys
.platform
in ('win32', 'os2emx', 'mac'):
# There's no distinction among 'user', 'group' and 'world';
# replicate the 'user' bits.
expected
= user
* (1 + 8 + 64)
self
.assertEqual(mode
, expected
)
test_classes
.append(test_mkdtemp
)
# For safety, all use of mktemp must occur in a private directory.
# We must also suppress the RuntimeWarning it generates.
self
.dir = tempfile
.mkdtemp()
_bflags
= tempfile
._bin
_openflags
def __init__(self
, dir, pre
, suf
):
self
.name
= tempfile
.mktemp(dir=dir, prefix
=pre
, suffix
=suf
)
# Create the file. This will raise an exception if it's
# mysteriously appeared in the meanwhile.
os
.close(os
.open(self
.name
, self
._bflags
, 0600))
def do_create(self
, pre
="", suf
=""):
file = self
.mktemped(self
.dir, pre
, suf
)
self
.failOnException("mktemp")
self
.nameCheck(file.name
, self
.dir, pre
, suf
)
# mktemp can choose usable file names
self
.do_create(pre
="a", suf
="b")
self
.do_create(pre
="aa", suf
=".txt")
# mktemp can choose many usable file names (stochastic)
extant
= range(TEST_FILES
)
extant
[i
] = self
.do_create(pre
="aa")
## def test_warning(self):
## # mktemp issues a warning when used
## warnings.filterwarnings("error",
## category=RuntimeWarning,
## self.assertRaises(RuntimeWarning,
## tempfile.mktemp, dir=self.dir)
test_classes
.append(test_mktemp
)
# We test _TemporaryFileWrapper by testing NamedTemporaryFile.
class test_NamedTemporaryFile(TC
):
"""Test NamedTemporaryFile()."""
def do_create(self
, dir=None, pre
="", suf
=""):
dir = tempfile
.gettempdir()
file = tempfile
.NamedTemporaryFile(dir=dir, prefix
=pre
, suffix
=suf
)
self
.failOnException("NamedTemporaryFile")
self
.nameCheck(file.name
, dir, pre
, suf
)
# NamedTemporaryFile can create files
self
.do_create(pre
="a", suf
="b")
self
.do_create(pre
="aa", suf
=".txt")
def test_creates_named(self
):
# NamedTemporaryFile creates files with names
f
= tempfile
.NamedTemporaryFile()
self
.failUnless(os
.path
.exists(f
.name
),
"NamedTemporaryFile %s does not exist" % f
.name
)
def test_del_on_close(self
):
# A NamedTemporaryFile is deleted when closed
f
= tempfile
.NamedTemporaryFile(dir=dir)
self
.failIf(os
.path
.exists(f
.name
),
"NamedTemporaryFile %s exists after close" % f
.name
)
def test_multiple_close(self
):
# A NamedTemporaryFile can be closed many times without error
f
= tempfile
.NamedTemporaryFile()
self
.failOnException("close")
# How to test the mode and bufsize parameters?
test_classes
.append(test_NamedTemporaryFile
)
class test_TemporaryFile(TC
):
"""Test TemporaryFile()."""
# TemporaryFile can create files
# No point in testing the name params - the file has no name.
self
.failOnException("TemporaryFile")
def test_has_no_name(self
):
# TemporaryFile creates files with no names (on this system)
f
= tempfile
.TemporaryFile(dir=dir)
# Sneaky: because this file has no name, it should not prevent
# us from removing the directory it was created in.
self
.failOnException("rmdir", ei
)
def test_multiple_close(self
):
# A TemporaryFile can be closed many times without error
f
= tempfile
.TemporaryFile()
self
.failOnException("close")
# How to test the mode and bufsize parameters?
if tempfile
.NamedTemporaryFile
is not tempfile
.TemporaryFile
:
test_classes
.append(test_TemporaryFile
)
test_support
.run_unittest(*test_classes
)
if __name__
== "__main__":