Skip to content

Commit a99b801

Browse files
BrianPughdpgeorge
authored andcommitted
tempfile: Add unit tests for tempfile, and don't use os.path.join.
1 parent 8ce4adf commit a99b801

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

python-stdlib/tempfile/tempfile.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def mkdtemp(suffix=None, prefix=None, dir=None):
3737

3838
while True:
3939
name = _get_candidate_name()
40-
file = os.path.join(dir, prefix + name + suffix)
40+
file = dir + "/" + prefix + name + suffix
4141
if _try(os.mkdir, file):
4242
return file
4343

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import os
2+
import tempfile
3+
import unittest
4+
5+
6+
class Base(unittest.TestCase):
7+
def assertExists(self, fn):
8+
os.stat(fn)
9+
10+
def assertNotExists(self, fn):
11+
with self.assertRaises(OSError):
12+
os.stat(fn)
13+
14+
15+
class TestMkdtemp(Base):
16+
def test_no_args(self):
17+
fn = tempfile.mkdtemp()
18+
self.assertTrue(fn.startswith("/tmp"))
19+
self.assertExists(fn)
20+
os.rmdir(fn)
21+
22+
def test_prefix(self):
23+
fn = tempfile.mkdtemp(prefix="foo")
24+
self.assertTrue(fn.startswith("/tmp"))
25+
self.assertTrue("foo" in fn)
26+
self.assertFalse(fn.endswith("foo"))
27+
self.assertExists(fn)
28+
os.rmdir(fn)
29+
30+
def test_suffix(self):
31+
fn = tempfile.mkdtemp(suffix="foo")
32+
self.assertTrue(fn.startswith("/tmp"))
33+
self.assertTrue(fn.endswith("foo"))
34+
self.assertExists(fn)
35+
os.rmdir(fn)
36+
37+
def test_dir(self):
38+
fn = tempfile.mkdtemp(dir="tmp_micropython")
39+
self.assertTrue(fn.startswith("tmp_micropython"))
40+
self.assertExists(fn)
41+
os.rmdir(fn)
42+
43+
44+
class TestTemporaryDirectory(Base):
45+
def test_context_manager_no_args(self):
46+
with tempfile.TemporaryDirectory() as fn:
47+
self.assertTrue(isinstance(fn, str))
48+
self.assertTrue(fn.startswith("/tmp"))
49+
self.assertExists(fn)
50+
self.assertNotExists(fn)

0 commit comments

Comments
 (0)