7
7
8
8
os .close (fd )
9
9
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
10
assert_raises (FileNotFoundError , lambda : os .open ('DOES_NOT_EXIST' , 0 ))
38
11
39
12
@@ -59,7 +32,44 @@ def __exit__(self, exc_type, exc_val, exc_tb):
59
32
os .unsetenv (ENV_KEY )
60
33
assert os .getenv (ENV_KEY ) == None
61
34
35
+
62
36
if os .name == "nt" :
63
37
assert os .sep == "\\ "
64
38
else :
65
39
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