Skip to content

Commit abb9cde

Browse files
committed
Use temp dir for os test
1 parent a75e763 commit abb9cde

File tree

1 file changed

+37
-27
lines changed

1 file changed

+37
-27
lines changed

tests/snippets/stdlib_os.py

Lines changed: 37 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,33 +7,6 @@
77

88
os.close(fd)
99
assert_raises(OSError, lambda: os.read(fd, 10))
10-
11-
FNAME = "test_file_that_no_one_will_have_on_disk"
12-
CONTENT = b"testing"
13-
CONTENT2 = b"rustpython"
14-
CONTENT3 = b"BOYA"
15-
16-
class TestWithFile():
17-
def __enter__(self):
18-
open(FNAME, "wb")
19-
return FNAME
20-
21-
def __exit__(self, exc_type, exc_val, exc_tb):
22-
os.remove(FNAME)
23-
24-
25-
with TestWithFile() as fname:
26-
fd = os.open(fname, 1)
27-
assert os.write(fd, CONTENT2) == len(CONTENT2)
28-
assert os.write(fd, CONTENT3) == len(CONTENT3)
29-
os.close(fd)
30-
31-
fd = os.open(fname, 0)
32-
assert os.read(fd, len(CONTENT2)) == CONTENT2
33-
assert os.read(fd, len(CONTENT3)) == CONTENT3
34-
os.close(fd)
35-
36-
3710
assert_raises(FileNotFoundError, lambda: os.open('DOES_NOT_EXIST', 0))
3811

3912

@@ -59,7 +32,44 @@ def __exit__(self, exc_type, exc_val, exc_tb):
5932
os.unsetenv(ENV_KEY)
6033
assert os.getenv(ENV_KEY) == None
6134

35+
6236
if os.name == "nt":
6337
assert os.sep == "\\"
6438
else:
6539
assert os.sep == "/"
40+
41+
class TestWithTempDir():
42+
def __enter__(self):
43+
if os.name == "nt":
44+
base_folder = os.environ["%TEMP%"]
45+
else:
46+
base_folder = "/tmp"
47+
name = base_folder + os.sep + "test_os"
48+
os.mkdir(name)
49+
self.name = name
50+
return name
51+
52+
def __exit__(self, exc_type, exc_val, exc_tb):
53+
for f in os.listdir(self.name):
54+
# Currently don't support dir delete.
55+
os.remove(self.name + os.sep + f)
56+
os.rmdir(self.name)
57+
58+
59+
FILE_NAME = "test1"
60+
CONTENT = b"testing"
61+
CONTENT2 = b"rustpython"
62+
CONTENT3 = b"BOYA"
63+
64+
with TestWithTempDir() as tmpdir:
65+
fname = tmpdir + os.sep + FILE_NAME
66+
open(fname, "wb")
67+
fd = os.open(fname, 1)
68+
assert os.write(fd, CONTENT2) == len(CONTENT2)
69+
assert os.write(fd, CONTENT3) == len(CONTENT3)
70+
os.close(fd)
71+
72+
fd = os.open(fname, 0)
73+
assert os.read(fd, len(CONTENT2)) == CONTENT2
74+
assert os.read(fd, len(CONTENT3)) == CONTENT3
75+
os.close(fd)

0 commit comments

Comments
 (0)