Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 1 | # tempfile.py unit tests. |
Tim Peters | c57a285 | 2001-10-29 21:46:08 +0000 | [diff] [blame] | 2 | |
| 3 | import tempfile |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 4 | import os |
| 5 | import sys |
| 6 | import re |
| 7 | import errno |
| 8 | import warnings |
Tim Peters | c57a285 | 2001-10-29 21:46:08 +0000 | [diff] [blame] | 9 | |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 10 | import unittest |
| 11 | from test import test_support |
| 12 | |
Fred Drake | 7633d23 | 2002-10-17 22:09:03 +0000 | [diff] [blame] | 13 | warnings.filterwarnings("ignore", |
| 14 | category=RuntimeWarning, |
| 15 | message="mktemp", module=__name__) |
| 16 | |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 17 | if hasattr(os, 'stat'): |
| 18 | import stat |
| 19 | has_stat = 1 |
| 20 | else: |
| 21 | has_stat = 0 |
| 22 | |
| 23 | has_textmode = (tempfile._text_openflags != tempfile._bin_openflags) |
Guido van Rossum | 7874106 | 2002-08-17 11:41:01 +0000 | [diff] [blame] | 24 | has_spawnl = hasattr(os, 'spawnl') |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 25 | |
Neal Norwitz | 68ee012 | 2002-08-16 19:28:59 +0000 | [diff] [blame] | 26 | # TEST_FILES may need to be tweaked for systems depending on the maximum |
| 27 | # number of files that can be opened at one time (see ulimit -n) |
| 28 | TEST_FILES = 100 |
| 29 | |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 30 | # This is organized as one test for each chunk of code in tempfile.py, |
| 31 | # in order of their appearance in the file. Testing which requires |
| 32 | # threads is not done here. |
| 33 | |
| 34 | # Common functionality. |
| 35 | class TC(unittest.TestCase): |
| 36 | |
| 37 | str_check = re.compile(r"[a-zA-Z0-9_-]{6}$") |
| 38 | |
| 39 | def failOnException(self, what, ei=None): |
| 40 | if ei is None: |
| 41 | ei = sys.exc_info() |
| 42 | self.fail("%s raised %s: %s" % (what, ei[0], ei[1])) |
| 43 | |
| 44 | def nameCheck(self, name, dir, pre, suf): |
| 45 | (ndir, nbase) = os.path.split(name) |
| 46 | npre = nbase[:len(pre)] |
| 47 | nsuf = nbase[len(nbase)-len(suf):] |
| 48 | |
| 49 | self.assertEqual(ndir, dir, |
| 50 | "file '%s' not in directory '%s'" % (name, dir)) |
| 51 | self.assertEqual(npre, pre, |
| 52 | "file '%s' does not begin with '%s'" % (nbase, pre)) |
| 53 | self.assertEqual(nsuf, suf, |
| 54 | "file '%s' does not end with '%s'" % (nbase, suf)) |
| 55 | |
| 56 | nbase = nbase[len(pre):len(nbase)-len(suf)] |
| 57 | self.assert_(self.str_check.match(nbase), |
| 58 | "random string '%s' does not match /^[a-zA-Z0-9_-]{6}$/" |
| 59 | % nbase) |
| 60 | |
| 61 | test_classes = [] |
| 62 | |
| 63 | class test_exports(TC): |
| 64 | def test_exports(self): |
Guido van Rossum | 8ccd9b6 | 2002-08-22 20:02:03 +0000 | [diff] [blame] | 65 | # There are no surprising symbols in the tempfile module |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 66 | dict = tempfile.__dict__ |
| 67 | |
| 68 | expected = { |
| 69 | "NamedTemporaryFile" : 1, |
| 70 | "TemporaryFile" : 1, |
| 71 | "mkstemp" : 1, |
| 72 | "mkdtemp" : 1, |
| 73 | "mktemp" : 1, |
| 74 | "TMP_MAX" : 1, |
| 75 | "gettempprefix" : 1, |
| 76 | "gettempdir" : 1, |
| 77 | "tempdir" : 1, |
| 78 | "template" : 1 |
| 79 | } |
| 80 | |
| 81 | unexp = [] |
| 82 | for key in dict: |
| 83 | if key[0] != '_' and key not in expected: |
| 84 | unexp.append(key) |
| 85 | self.failUnless(len(unexp) == 0, |
| 86 | "unexpected keys: %s" % unexp) |
| 87 | |
| 88 | test_classes.append(test_exports) |
| 89 | |
| 90 | |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 91 | class test__RandomNameSequence(TC): |
| 92 | """Test the internal iterator object _RandomNameSequence.""" |
| 93 | |
| 94 | def setUp(self): |
| 95 | self.r = tempfile._RandomNameSequence() |
| 96 | |
| 97 | def test_get_six_char_str(self): |
Guido van Rossum | 8ccd9b6 | 2002-08-22 20:02:03 +0000 | [diff] [blame] | 98 | # _RandomNameSequence returns a six-character string |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 99 | s = self.r.next() |
| 100 | self.nameCheck(s, '', '', '') |
| 101 | |
| 102 | def test_many(self): |
Guido van Rossum | 8ccd9b6 | 2002-08-22 20:02:03 +0000 | [diff] [blame] | 103 | # _RandomNameSequence returns no duplicate strings (stochastic) |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 104 | |
| 105 | dict = {} |
| 106 | r = self.r |
Neal Norwitz | 68ee012 | 2002-08-16 19:28:59 +0000 | [diff] [blame] | 107 | for i in xrange(TEST_FILES): |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 108 | s = r.next() |
| 109 | self.nameCheck(s, '', '', '') |
| 110 | self.failIf(s in dict) |
| 111 | dict[s] = 1 |
| 112 | |
| 113 | def test_supports_iter(self): |
Guido van Rossum | 8ccd9b6 | 2002-08-22 20:02:03 +0000 | [diff] [blame] | 114 | # _RandomNameSequence supports the iterator protocol |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 115 | |
| 116 | i = 0 |
| 117 | r = self.r |
| 118 | try: |
| 119 | for s in r: |
| 120 | i += 1 |
| 121 | if i == 20: |
| 122 | break |
| 123 | except: |
| 124 | failOnException("iteration") |
| 125 | |
| 126 | test_classes.append(test__RandomNameSequence) |
| 127 | |
| 128 | |
| 129 | class test__candidate_tempdir_list(TC): |
| 130 | """Test the internal function _candidate_tempdir_list.""" |
| 131 | |
| 132 | def test_nonempty_list(self): |
Guido van Rossum | 8ccd9b6 | 2002-08-22 20:02:03 +0000 | [diff] [blame] | 133 | # _candidate_tempdir_list returns a nonempty list of strings |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 134 | |
| 135 | cand = tempfile._candidate_tempdir_list() |
| 136 | |
| 137 | self.failIf(len(cand) == 0) |
| 138 | for c in cand: |
| 139 | self.assert_(isinstance(c, basestring), |
| 140 | "%s is not a string" % c) |
| 141 | |
| 142 | def test_wanted_dirs(self): |
Guido van Rossum | 8ccd9b6 | 2002-08-22 20:02:03 +0000 | [diff] [blame] | 143 | # _candidate_tempdir_list contains the expected directories |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 144 | |
| 145 | # Make sure the interesting environment variables are all set. |
| 146 | added = [] |
| 147 | try: |
| 148 | for envname in 'TMPDIR', 'TEMP', 'TMP': |
| 149 | dirname = os.getenv(envname) |
| 150 | if not dirname: |
| 151 | os.environ[envname] = os.path.abspath(envname) |
| 152 | added.append(envname) |
| 153 | |
| 154 | cand = tempfile._candidate_tempdir_list() |
| 155 | |
| 156 | for envname in 'TMPDIR', 'TEMP', 'TMP': |
| 157 | dirname = os.getenv(envname) |
| 158 | if not dirname: raise ValueError |
| 159 | self.assert_(dirname in cand) |
| 160 | |
| 161 | try: |
| 162 | dirname = os.getcwd() |
| 163 | except (AttributeError, os.error): |
| 164 | dirname = os.curdir |
| 165 | |
| 166 | self.assert_(dirname in cand) |
| 167 | |
| 168 | # Not practical to try to verify the presence of OS-specific |
| 169 | # paths in this list. |
| 170 | finally: |
| 171 | for p in added: |
| 172 | del os.environ[p] |
| 173 | |
| 174 | test_classes.append(test__candidate_tempdir_list) |
| 175 | |
| 176 | |
| 177 | # We test _get_default_tempdir by testing gettempdir. |
| 178 | |
| 179 | |
| 180 | class test__get_candidate_names(TC): |
| 181 | """Test the internal function _get_candidate_names.""" |
| 182 | |
| 183 | def test_retval(self): |
Guido van Rossum | 8ccd9b6 | 2002-08-22 20:02:03 +0000 | [diff] [blame] | 184 | # _get_candidate_names returns a _RandomNameSequence object |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 185 | obj = tempfile._get_candidate_names() |
| 186 | self.assert_(isinstance(obj, tempfile._RandomNameSequence)) |
| 187 | |
| 188 | def test_same_thing(self): |
Guido van Rossum | 8ccd9b6 | 2002-08-22 20:02:03 +0000 | [diff] [blame] | 189 | # _get_candidate_names always returns the same object |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 190 | a = tempfile._get_candidate_names() |
| 191 | b = tempfile._get_candidate_names() |
| 192 | |
| 193 | self.assert_(a is b) |
| 194 | |
| 195 | test_classes.append(test__get_candidate_names) |
| 196 | |
| 197 | |
| 198 | class test__mkstemp_inner(TC): |
| 199 | """Test the internal function _mkstemp_inner.""" |
| 200 | |
| 201 | class mkstemped: |
| 202 | _bflags = tempfile._bin_openflags |
| 203 | _tflags = tempfile._text_openflags |
| 204 | _close = os.close |
| 205 | _unlink = os.unlink |
| 206 | |
| 207 | def __init__(self, dir, pre, suf, bin): |
| 208 | if bin: flags = self._bflags |
| 209 | else: flags = self._tflags |
| 210 | |
| 211 | (self.fd, self.name) = tempfile._mkstemp_inner(dir, pre, suf, flags) |
| 212 | |
| 213 | def write(self, str): |
| 214 | os.write(self.fd, str) |
| 215 | |
| 216 | def __del__(self): |
| 217 | self._close(self.fd) |
| 218 | self._unlink(self.name) |
Tim Peters | a0d55de | 2002-08-09 18:01:01 +0000 | [diff] [blame] | 219 | |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 220 | def do_create(self, dir=None, pre="", suf="", bin=1): |
| 221 | if dir is None: |
| 222 | dir = tempfile.gettempdir() |
| 223 | try: |
| 224 | file = self.mkstemped(dir, pre, suf, bin) |
| 225 | except: |
| 226 | self.failOnException("_mkstemp_inner") |
| 227 | |
| 228 | self.nameCheck(file.name, dir, pre, suf) |
| 229 | return file |
| 230 | |
| 231 | def test_basic(self): |
Guido van Rossum | 8ccd9b6 | 2002-08-22 20:02:03 +0000 | [diff] [blame] | 232 | # _mkstemp_inner can create files |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 233 | self.do_create().write("blat") |
| 234 | self.do_create(pre="a").write("blat") |
| 235 | self.do_create(suf="b").write("blat") |
| 236 | self.do_create(pre="a", suf="b").write("blat") |
| 237 | self.do_create(pre="aa", suf=".txt").write("blat") |
| 238 | |
| 239 | def test_basic_many(self): |
Guido van Rossum | 8ccd9b6 | 2002-08-22 20:02:03 +0000 | [diff] [blame] | 240 | # _mkstemp_inner can create many files (stochastic) |
Neal Norwitz | 68ee012 | 2002-08-16 19:28:59 +0000 | [diff] [blame] | 241 | extant = range(TEST_FILES) |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 242 | for i in extant: |
| 243 | extant[i] = self.do_create(pre="aa") |
| 244 | |
| 245 | def test_choose_directory(self): |
Guido van Rossum | 8ccd9b6 | 2002-08-22 20:02:03 +0000 | [diff] [blame] | 246 | # _mkstemp_inner can create files in a user-selected directory |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 247 | dir = tempfile.mkdtemp() |
| 248 | try: |
| 249 | self.do_create(dir=dir).write("blat") |
| 250 | finally: |
| 251 | os.rmdir(dir) |
| 252 | |
| 253 | def test_file_mode(self): |
Guido van Rossum | 8ccd9b6 | 2002-08-22 20:02:03 +0000 | [diff] [blame] | 254 | # _mkstemp_inner creates files with the proper mode |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 255 | if not has_stat: |
| 256 | return # ugh, can't use TestSkipped. |
| 257 | |
| 258 | file = self.do_create() |
| 259 | mode = stat.S_IMODE(os.stat(file.name).st_mode) |
Tim Peters | ca3ac7f | 2002-08-09 18:13:51 +0000 | [diff] [blame] | 260 | expected = 0600 |
Andrew MacIntyre | 1d0eeec | 2002-08-18 06:47:19 +0000 | [diff] [blame] | 261 | if sys.platform in ('win32', 'os2emx'): |
Tim Peters | ca3ac7f | 2002-08-09 18:13:51 +0000 | [diff] [blame] | 262 | # There's no distinction among 'user', 'group' and 'world'; |
| 263 | # replicate the 'user' bits. |
| 264 | user = expected >> 6 |
| 265 | expected = user * (1 + 8 + 64) |
| 266 | self.assertEqual(mode, expected) |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 267 | |
| 268 | def test_noinherit(self): |
Guido van Rossum | 8ccd9b6 | 2002-08-22 20:02:03 +0000 | [diff] [blame] | 269 | # _mkstemp_inner file handles are not inherited by child processes |
Guido van Rossum | 7874106 | 2002-08-17 11:41:01 +0000 | [diff] [blame] | 270 | if not has_spawnl: |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 271 | return # ugh, can't use TestSkipped. |
| 272 | |
Guido van Rossum | 7874106 | 2002-08-17 11:41:01 +0000 | [diff] [blame] | 273 | if test_support.verbose: |
| 274 | v="v" |
| 275 | else: |
| 276 | v="q" |
| 277 | |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 278 | file = self.do_create() |
Guido van Rossum | 7874106 | 2002-08-17 11:41:01 +0000 | [diff] [blame] | 279 | fd = "%d" % file.fd |
| 280 | |
| 281 | try: |
| 282 | me = __file__ |
| 283 | except NameError: |
| 284 | me = sys.argv[0] |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 285 | |
| 286 | # We have to exec something, so that FD_CLOEXEC will take |
Guido van Rossum | 7874106 | 2002-08-17 11:41:01 +0000 | [diff] [blame] | 287 | # effect. The core of this test is therefore in |
| 288 | # tf_inherit_check.py, which see. |
| 289 | tester = os.path.join(os.path.dirname(os.path.abspath(me)), |
| 290 | "tf_inherit_check.py") |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 291 | |
Guido van Rossum | 7874106 | 2002-08-17 11:41:01 +0000 | [diff] [blame] | 292 | retval = os.spawnl(os.P_WAIT, sys.executable, |
| 293 | sys.executable, tester, v, fd) |
| 294 | self.failIf(retval < 0, |
| 295 | "child process caught fatal signal %d" % -retval) |
| 296 | self.failIf(retval > 0, "child process reports failure") |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 297 | |
| 298 | def test_textmode(self): |
Guido van Rossum | 8ccd9b6 | 2002-08-22 20:02:03 +0000 | [diff] [blame] | 299 | # _mkstemp_inner can create files in text mode |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 300 | if not has_textmode: |
| 301 | return # ugh, can't use TestSkipped. |
| 302 | |
| 303 | self.do_create(bin=0).write("blat\n") |
| 304 | # XXX should test that the file really is a text file |
| 305 | |
| 306 | test_classes.append(test__mkstemp_inner) |
| 307 | |
| 308 | |
| 309 | class test_gettempprefix(TC): |
| 310 | """Test gettempprefix().""" |
| 311 | |
| 312 | def test_sane_template(self): |
Guido van Rossum | 8ccd9b6 | 2002-08-22 20:02:03 +0000 | [diff] [blame] | 313 | # gettempprefix returns a nonempty prefix string |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 314 | p = tempfile.gettempprefix() |
| 315 | |
| 316 | self.assert_(isinstance(p, basestring)) |
| 317 | self.assert_(len(p) > 0) |
| 318 | |
| 319 | def test_usable_template(self): |
Guido van Rossum | 8ccd9b6 | 2002-08-22 20:02:03 +0000 | [diff] [blame] | 320 | # gettempprefix returns a usable prefix string |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 321 | |
| 322 | # Create a temp directory, avoiding use of the prefix. |
| 323 | # Then attempt to create a file whose name is |
| 324 | # prefix + 'xxxxxx.xxx' in that directory. |
| 325 | p = tempfile.gettempprefix() + "xxxxxx.xxx" |
| 326 | d = tempfile.mkdtemp(prefix="") |
| 327 | try: |
| 328 | p = os.path.join(d, p) |
| 329 | try: |
| 330 | fd = os.open(p, os.O_RDWR | os.O_CREAT) |
| 331 | except: |
| 332 | self.failOnException("os.open") |
| 333 | os.close(fd) |
| 334 | os.unlink(p) |
| 335 | finally: |
| 336 | os.rmdir(d) |
| 337 | |
| 338 | test_classes.append(test_gettempprefix) |
| 339 | |
| 340 | |
| 341 | class test_gettempdir(TC): |
| 342 | """Test gettempdir().""" |
| 343 | |
| 344 | def test_directory_exists(self): |
Guido van Rossum | 8ccd9b6 | 2002-08-22 20:02:03 +0000 | [diff] [blame] | 345 | # gettempdir returns a directory which exists |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 346 | |
| 347 | dir = tempfile.gettempdir() |
| 348 | self.assert_(os.path.isabs(dir) or dir == os.curdir, |
| 349 | "%s is not an absolute path" % dir) |
| 350 | self.assert_(os.path.isdir(dir), |
| 351 | "%s is not a directory" % dir) |
| 352 | |
| 353 | def test_directory_writable(self): |
Guido van Rossum | 8ccd9b6 | 2002-08-22 20:02:03 +0000 | [diff] [blame] | 354 | # gettempdir returns a directory writable by the user |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 355 | |
| 356 | # sneaky: just instantiate a NamedTemporaryFile, which |
| 357 | # defaults to writing into the directory returned by |
| 358 | # gettempdir. |
| 359 | try: |
| 360 | file = tempfile.NamedTemporaryFile() |
| 361 | file.write("blat") |
| 362 | file.close() |
| 363 | except: |
| 364 | self.failOnException("create file in %s" % tempfile.gettempdir()) |
| 365 | |
| 366 | def test_same_thing(self): |
Guido van Rossum | 8ccd9b6 | 2002-08-22 20:02:03 +0000 | [diff] [blame] | 367 | # gettempdir always returns the same object |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 368 | a = tempfile.gettempdir() |
| 369 | b = tempfile.gettempdir() |
| 370 | |
| 371 | self.assert_(a is b) |
| 372 | |
| 373 | test_classes.append(test_gettempdir) |
| 374 | |
| 375 | |
| 376 | class test_mkstemp(TC): |
| 377 | """Test mkstemp().""" |
Guido van Rossum | 8ccd9b6 | 2002-08-22 20:02:03 +0000 | [diff] [blame] | 378 | |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 379 | def do_create(self, dir=None, pre="", suf="", ): |
| 380 | if dir is None: |
| 381 | dir = tempfile.gettempdir() |
| 382 | try: |
| 383 | (fd, name) = tempfile.mkstemp(dir=dir, prefix=pre, suffix=suf) |
| 384 | except: |
| 385 | self.failOnException("mkstemp") |
| 386 | |
| 387 | try: |
| 388 | self.nameCheck(name, dir, pre, suf) |
| 389 | finally: |
| 390 | os.close(fd) |
| 391 | os.unlink(name) |
| 392 | |
| 393 | def test_basic(self): |
Guido van Rossum | 8ccd9b6 | 2002-08-22 20:02:03 +0000 | [diff] [blame] | 394 | # mkstemp can create files |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 395 | self.do_create() |
| 396 | self.do_create(pre="a") |
| 397 | self.do_create(suf="b") |
| 398 | self.do_create(pre="a", suf="b") |
| 399 | self.do_create(pre="aa", suf=".txt") |
| 400 | |
| 401 | def test_choose_directory(self): |
Guido van Rossum | 8ccd9b6 | 2002-08-22 20:02:03 +0000 | [diff] [blame] | 402 | # mkstemp can create directories in a user-selected directory |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 403 | dir = tempfile.mkdtemp() |
| 404 | try: |
| 405 | self.do_create(dir=dir) |
| 406 | finally: |
| 407 | os.rmdir(dir) |
| 408 | |
| 409 | test_classes.append(test_mkstemp) |
| 410 | |
| 411 | |
| 412 | class test_mkdtemp(TC): |
| 413 | """Test mkdtemp().""" |
| 414 | |
| 415 | def do_create(self, dir=None, pre="", suf=""): |
| 416 | if dir is None: |
| 417 | dir = tempfile.gettempdir() |
| 418 | try: |
| 419 | name = tempfile.mkdtemp(dir=dir, prefix=pre, suffix=suf) |
| 420 | except: |
| 421 | self.failOnException("mkdtemp") |
| 422 | |
| 423 | try: |
| 424 | self.nameCheck(name, dir, pre, suf) |
| 425 | return name |
| 426 | except: |
| 427 | os.rmdir(name) |
| 428 | raise |
| 429 | |
| 430 | def test_basic(self): |
Guido van Rossum | 8ccd9b6 | 2002-08-22 20:02:03 +0000 | [diff] [blame] | 431 | # mkdtemp can create directories |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 432 | os.rmdir(self.do_create()) |
| 433 | os.rmdir(self.do_create(pre="a")) |
| 434 | os.rmdir(self.do_create(suf="b")) |
| 435 | os.rmdir(self.do_create(pre="a", suf="b")) |
| 436 | os.rmdir(self.do_create(pre="aa", suf=".txt")) |
Tim Peters | a0d55de | 2002-08-09 18:01:01 +0000 | [diff] [blame] | 437 | |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 438 | def test_basic_many(self): |
Guido van Rossum | 8ccd9b6 | 2002-08-22 20:02:03 +0000 | [diff] [blame] | 439 | # mkdtemp can create many directories (stochastic) |
Neal Norwitz | 68ee012 | 2002-08-16 19:28:59 +0000 | [diff] [blame] | 440 | extant = range(TEST_FILES) |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 441 | try: |
| 442 | for i in extant: |
| 443 | extant[i] = self.do_create(pre="aa") |
| 444 | finally: |
| 445 | for i in extant: |
| 446 | if(isinstance(i, basestring)): |
| 447 | os.rmdir(i) |
| 448 | |
| 449 | def test_choose_directory(self): |
Guido van Rossum | 8ccd9b6 | 2002-08-22 20:02:03 +0000 | [diff] [blame] | 450 | # mkdtemp can create directories in a user-selected directory |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 451 | dir = tempfile.mkdtemp() |
| 452 | try: |
| 453 | os.rmdir(self.do_create(dir=dir)) |
| 454 | finally: |
| 455 | os.rmdir(dir) |
| 456 | |
| 457 | def test_mode(self): |
Guido van Rossum | 8ccd9b6 | 2002-08-22 20:02:03 +0000 | [diff] [blame] | 458 | # mkdtemp creates directories with the proper mode |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 459 | if not has_stat: |
| 460 | return # ugh, can't use TestSkipped. |
| 461 | |
| 462 | dir = self.do_create() |
| 463 | try: |
| 464 | mode = stat.S_IMODE(os.stat(dir).st_mode) |
Tim Peters | ca3ac7f | 2002-08-09 18:13:51 +0000 | [diff] [blame] | 465 | expected = 0700 |
Andrew MacIntyre | 1d0eeec | 2002-08-18 06:47:19 +0000 | [diff] [blame] | 466 | if sys.platform in ('win32', 'os2emx'): |
Tim Peters | ca3ac7f | 2002-08-09 18:13:51 +0000 | [diff] [blame] | 467 | # There's no distinction among 'user', 'group' and 'world'; |
| 468 | # replicate the 'user' bits. |
| 469 | user = expected >> 6 |
| 470 | expected = user * (1 + 8 + 64) |
| 471 | self.assertEqual(mode, expected) |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 472 | finally: |
| 473 | os.rmdir(dir) |
| 474 | |
| 475 | test_classes.append(test_mkdtemp) |
| 476 | |
| 477 | |
| 478 | class test_mktemp(TC): |
| 479 | """Test mktemp().""" |
| 480 | |
| 481 | # For safety, all use of mktemp must occur in a private directory. |
| 482 | # We must also suppress the RuntimeWarning it generates. |
| 483 | def setUp(self): |
| 484 | self.dir = tempfile.mkdtemp() |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 485 | |
| 486 | def tearDown(self): |
| 487 | if self.dir: |
| 488 | os.rmdir(self.dir) |
| 489 | self.dir = None |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 490 | |
| 491 | class mktemped: |
| 492 | _unlink = os.unlink |
| 493 | _bflags = tempfile._bin_openflags |
| 494 | |
| 495 | def __init__(self, dir, pre, suf): |
| 496 | self.name = tempfile.mktemp(dir=dir, prefix=pre, suffix=suf) |
| 497 | # Create the file. This will raise an exception if it's |
| 498 | # mysteriously appeared in the meanwhile. |
| 499 | os.close(os.open(self.name, self._bflags, 0600)) |
| 500 | |
| 501 | def __del__(self): |
| 502 | self._unlink(self.name) |
| 503 | |
| 504 | def do_create(self, pre="", suf=""): |
| 505 | try: |
| 506 | file = self.mktemped(self.dir, pre, suf) |
| 507 | except: |
| 508 | self.failOnException("mktemp") |
| 509 | |
| 510 | self.nameCheck(file.name, self.dir, pre, suf) |
| 511 | return file |
| 512 | |
| 513 | def test_basic(self): |
Guido van Rossum | 8ccd9b6 | 2002-08-22 20:02:03 +0000 | [diff] [blame] | 514 | # mktemp can choose usable file names |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 515 | self.do_create() |
| 516 | self.do_create(pre="a") |
| 517 | self.do_create(suf="b") |
| 518 | self.do_create(pre="a", suf="b") |
| 519 | self.do_create(pre="aa", suf=".txt") |
| 520 | |
| 521 | def test_many(self): |
Guido van Rossum | 8ccd9b6 | 2002-08-22 20:02:03 +0000 | [diff] [blame] | 522 | # mktemp can choose many usable file names (stochastic) |
Neal Norwitz | 68ee012 | 2002-08-16 19:28:59 +0000 | [diff] [blame] | 523 | extant = range(TEST_FILES) |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 524 | for i in extant: |
| 525 | extant[i] = self.do_create(pre="aa") |
| 526 | |
| 527 | def test_warning(self): |
Guido van Rossum | 8ccd9b6 | 2002-08-22 20:02:03 +0000 | [diff] [blame] | 528 | # mktemp issues a warning when used |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 529 | warnings.filterwarnings("error", |
| 530 | category=RuntimeWarning, |
| 531 | message="mktemp") |
| 532 | self.assertRaises(RuntimeWarning, |
| 533 | tempfile.mktemp, (), { 'dir': self.dir }) |
| 534 | |
| 535 | test_classes.append(test_mktemp) |
| 536 | |
| 537 | |
| 538 | # We test _TemporaryFileWrapper by testing NamedTemporaryFile. |
| 539 | |
| 540 | |
| 541 | class test_NamedTemporaryFile(TC): |
| 542 | """Test NamedTemporaryFile().""" |
| 543 | |
| 544 | def do_create(self, dir=None, pre="", suf=""): |
| 545 | if dir is None: |
| 546 | dir = tempfile.gettempdir() |
| 547 | try: |
| 548 | file = tempfile.NamedTemporaryFile(dir=dir, prefix=pre, suffix=suf) |
| 549 | except: |
| 550 | self.failOnException("NamedTemporaryFile") |
| 551 | |
| 552 | self.nameCheck(file.name, dir, pre, suf) |
| 553 | return file |
| 554 | |
| 555 | |
| 556 | def test_basic(self): |
Guido van Rossum | 8ccd9b6 | 2002-08-22 20:02:03 +0000 | [diff] [blame] | 557 | # NamedTemporaryFile can create files |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 558 | self.do_create() |
| 559 | self.do_create(pre="a") |
| 560 | self.do_create(suf="b") |
| 561 | self.do_create(pre="a", suf="b") |
| 562 | self.do_create(pre="aa", suf=".txt") |
| 563 | |
| 564 | def test_creates_named(self): |
Guido van Rossum | 8ccd9b6 | 2002-08-22 20:02:03 +0000 | [diff] [blame] | 565 | # NamedTemporaryFile creates files with names |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 566 | f = tempfile.NamedTemporaryFile() |
| 567 | self.failUnless(os.path.exists(f.name), |
| 568 | "NamedTemporaryFile %s does not exist" % f.name) |
| 569 | |
| 570 | def test_del_on_close(self): |
Guido van Rossum | 8ccd9b6 | 2002-08-22 20:02:03 +0000 | [diff] [blame] | 571 | # A NamedTemporaryFile is deleted when closed |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 572 | dir = tempfile.mkdtemp() |
| 573 | try: |
| 574 | f = tempfile.NamedTemporaryFile(dir=dir) |
| 575 | f.write('blat') |
| 576 | f.close() |
| 577 | self.failIf(os.path.exists(f.name), |
| 578 | "NamedTemporaryFile %s exists after close" % f.name) |
| 579 | finally: |
| 580 | os.rmdir(dir) |
| 581 | |
| 582 | def test_multiple_close(self): |
Guido van Rossum | 8ccd9b6 | 2002-08-22 20:02:03 +0000 | [diff] [blame] | 583 | # A NamedTemporaryFile can be closed many times without error |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 584 | |
| 585 | f = tempfile.NamedTemporaryFile() |
| 586 | f.write('abc\n') |
| 587 | f.close() |
| 588 | try: |
| 589 | f.close() |
| 590 | f.close() |
| 591 | except: |
| 592 | self.failOnException("close") |
| 593 | |
| 594 | # How to test the mode and bufsize parameters? |
| 595 | |
| 596 | test_classes.append(test_NamedTemporaryFile) |
| 597 | |
| 598 | |
| 599 | class test_TemporaryFile(TC): |
| 600 | """Test TemporaryFile().""" |
| 601 | |
| 602 | def test_basic(self): |
Guido van Rossum | 8ccd9b6 | 2002-08-22 20:02:03 +0000 | [diff] [blame] | 603 | # TemporaryFile can create files |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 604 | # No point in testing the name params - the file has no name. |
| 605 | try: |
| 606 | tempfile.TemporaryFile() |
| 607 | except: |
| 608 | self.failOnException("TemporaryFile") |
| 609 | |
| 610 | def test_has_no_name(self): |
Guido van Rossum | 8ccd9b6 | 2002-08-22 20:02:03 +0000 | [diff] [blame] | 611 | # TemporaryFile creates files with no names (on this system) |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 612 | dir = tempfile.mkdtemp() |
| 613 | f = tempfile.TemporaryFile(dir=dir) |
| 614 | f.write('blat') |
| 615 | |
| 616 | # Sneaky: because this file has no name, it should not prevent |
| 617 | # us from removing the directory it was created in. |
| 618 | try: |
| 619 | os.rmdir(dir) |
| 620 | except: |
| 621 | ei = sys.exc_info() |
| 622 | # cleanup |
| 623 | f.close() |
| 624 | os.rmdir(dir) |
| 625 | self.failOnException("rmdir", ei) |
| 626 | |
| 627 | def test_multiple_close(self): |
Guido van Rossum | 8ccd9b6 | 2002-08-22 20:02:03 +0000 | [diff] [blame] | 628 | # A TemporaryFile can be closed many times without error |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 629 | f = tempfile.TemporaryFile() |
| 630 | f.write('abc\n') |
| 631 | f.close() |
| 632 | try: |
| 633 | f.close() |
| 634 | f.close() |
| 635 | except: |
| 636 | self.failOnException("close") |
| 637 | |
| 638 | # How to test the mode and bufsize parameters? |
| 639 | |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 640 | |
Guido van Rossum | 8ccd9b6 | 2002-08-22 20:02:03 +0000 | [diff] [blame] | 641 | if tempfile.NamedTemporaryFile is not tempfile.TemporaryFile: |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 642 | test_classes.append(test_TemporaryFile) |
| 643 | |
| 644 | def test_main(): |
| 645 | suite = unittest.TestSuite() |
| 646 | for c in test_classes: |
| 647 | suite.addTest(unittest.makeSuite(c)) |
| 648 | test_support.run_suite(suite) |
| 649 | |
| 650 | if __name__ == "__main__": |
| 651 | test_main() |