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