Skip to content

Commit 148ef5c

Browse files
authored
Merge pull request RustPython#849 from palaviv/os-temp-dir
Use temp directory for os test
2 parents 19be5c9 + d704a0f commit 148ef5c

File tree

2 files changed

+48
-28
lines changed

2 files changed

+48
-28
lines changed

Lib/os.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
from _os import *
22

3+
if name == 'nt':
4+
sep = '\\'
5+
else:
6+
sep = '/'
7+
38
# Change environ to automatically call putenv(), unsetenv if they exist.
49
from _collections_abc import MutableMapping
510

tests/snippets/stdlib_os.py

Lines changed: 43 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import os
1+
import os
2+
import time
23

34
from testutils import assert_raises
45

@@ -7,33 +8,6 @@
78

89
os.close(fd)
910
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-
3711
assert_raises(FileNotFoundError, lambda: os.open('DOES_NOT_EXIST', 0))
3812

3913

@@ -58,3 +32,44 @@ def __exit__(self, exc_type, exc_val, exc_tb):
5832
os.putenv(ENV_KEY, ENV_VALUE)
5933
os.unsetenv(ENV_KEY)
6034
assert os.getenv(ENV_KEY) == None
35+
36+
37+
if os.name == "nt":
38+
assert os.sep == "\\"
39+
else:
40+
assert os.sep == "/"
41+
42+
class TestWithTempDir():
43+
def __enter__(self):
44+
if os.name == "nt":
45+
base_folder = os.environ["TEMP"]
46+
else:
47+
base_folder = "/tmp"
48+
name = base_folder + os.sep + "rustpython_test_os_" + str(int(time.time()))
49+
os.mkdir(name)
50+
self.name = name
51+
return name
52+
53+
def __exit__(self, exc_type, exc_val, exc_tb):
54+
# TODO: Delete temp dir
55+
pass
56+
57+
58+
FILE_NAME = "test1"
59+
CONTENT = b"testing"
60+
CONTENT2 = b"rustpython"
61+
CONTENT3 = b"BOYA"
62+
63+
with TestWithTempDir() as tmpdir:
64+
fname = tmpdir + os.sep + FILE_NAME
65+
with open(fname, "wb"):
66+
pass
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)