1
- import os
1
+ import os
2
+ import time
2
3
3
4
from testutils import assert_raises
4
5
7
8
8
9
os .close (fd )
9
10
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
-
37
11
assert_raises (FileNotFoundError , lambda : os .open ('DOES_NOT_EXIST' , 0 ))
38
12
39
13
@@ -58,3 +32,44 @@ def __exit__(self, exc_type, exc_val, exc_tb):
58
32
os .putenv (ENV_KEY , ENV_VALUE )
59
33
os .unsetenv (ENV_KEY )
60
34
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