blob: 470e347dbca6215b2c6a54f21dd05bca8cfe83ff [file] [log] [blame]
Fred Drake38c2ef02001-07-17 20:52:51 +00001# As a test suite for the os module, this is woefully inadequate, but this
2# does add tests for a few functions which have been determined to be more
3# more portable than they had been thought to be.
4
5import os
6import unittest
7
8from test_support import TESTFN, run_unittest
9
10
11class TemporaryFileTests(unittest.TestCase):
12 def setUp(self):
13 self.files = []
14 os.mkdir(TESTFN)
15
16 def tearDown(self):
17 for name in self.files:
18 os.unlink(name)
19 os.rmdir(TESTFN)
20
21 def check_tempfile(self, name):
22 # make sure it doesn't already exist:
23 self.failIf(os.path.exists(name),
24 "file already exists for temporary file")
25 # make sure we can create the file
26 open(name, "w")
27 self.files.append(name)
28
29 def test_tempnam(self):
30 if not hasattr(os, "tempnam"):
31 return
32 self.check_tempfile(os.tempnam())
33
34 name = os.tempnam(TESTFN)
Fred Drake38c2ef02001-07-17 20:52:51 +000035 self.check_tempfile(name)
36
37 name = os.tempnam(TESTFN, "pfx")
Fred Drake38c2ef02001-07-17 20:52:51 +000038 self.assert_(os.path.basename(name)[:3] == "pfx")
39 self.check_tempfile(name)
40
41 def test_tmpfile(self):
42 if not hasattr(os, "tmpfile"):
43 return
44 fp = os.tmpfile()
45 fp.write("foobar")
46 fp.seek(0,0)
47 s = fp.read()
48 fp.close()
49 self.assert_(s == "foobar")
50
51 def test_tmpnam(self):
52 if not hasattr(os, "tmpnam"):
53 return
54 self.check_tempfile(os.tmpnam())
Tim Peters87cc0c32001-07-21 01:41:30 +000055
Fred Drake38c2ef02001-07-17 20:52:51 +000056
57
58run_unittest(TemporaryFileTests)