This repository was archived by the owner on Mar 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 635
Expand file tree
/
Copy pathos_test.py
More file actions
254 lines (208 loc) · 4.51 KB
/
os_test.py
File metadata and controls
254 lines (208 loc) · 4.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# Copyright 2016 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os
import stat
import time
import tempfile
import weetest
def TestChdirAndGetCwd():
path = os.getcwd()
os.chdir('.')
assert os.getcwd() == path
tempdir = tempfile.mkdtemp()
try:
os.chdir(tempdir)
assert tempdir in os.getcwd()
finally:
os.chdir(path)
os.rmdir(tempdir)
assert os.getcwd() == path
def TestChmod():
fd, path = tempfile.mkstemp()
os.close(fd)
os.chmod(path, 0o644)
mode = os.stat(path).st_mode & 0o777
os.remove(path)
assert mode == 0o644
def TestChmodOSError():
tempdir = tempfile.mkdtemp()
try:
os.chmod(tempdir + '/DoesNotExist', 0o644)
except OSError:
pass
else:
raise AssertionError
def TestClose():
fd, _ = tempfile.mkstemp()
os.close(fd)
try:
os.fdopen(fd)
except OSError:
pass
else:
raise AssertionError
def TestCloseOSError():
fd, _ = tempfile.mkstemp()
os.close(fd)
try:
os.close(fd)
except OSError:
pass
else:
raise AssertionError
def TestEnviron():
assert 'HOME' in os.environ
def TestFDOpen():
fd, path = tempfile.mkstemp()
f = os.fdopen(fd, 'w')
f.write('foobar')
f.close()
f = open(path)
contents = f.read()
f.close()
assert contents == 'foobar', contents
def TestFDOpenOSError():
fd, _ = tempfile.mkstemp()
os.close(fd)
try:
os.fdopen(fd)
except OSError:
pass
else:
raise AssertionError
def TestMkdir():
path = 'foobarqux'
try:
os.stat(path)
except OSError:
pass
else:
raise AssertionError
try:
os.mkdir(path)
assert stat.S_ISDIR(os.stat(path).st_mode)
except OSError:
raise AssertionError
finally:
os.rmdir(path)
def TestPopenRead():
f = os.popen('qux')
assert f.close() == 32512
f = os.popen('echo hello')
try:
assert f.read() == 'hello\n'
finally:
assert f.close() == 0
def TestPopenWrite():
# TODO: We should verify the output but there's no good way to swap out stdout
# at the moment.
f = os.popen('cat', 'w')
f.write('popen write\n')
f.close()
def TestRemove():
fd, path = tempfile.mkstemp()
os.close(fd)
os.stat(path)
os.remove(path)
try:
os.stat(path)
except OSError:
pass
else:
raise AssertionError
def TestRemoveNoExist():
path = tempfile.mkdtemp()
try:
os.remove(path + '/nonexistent')
except OSError:
pass
else:
raise AssertionError
finally:
os.rmdir(path)
def TestRemoveDir():
path = tempfile.mkdtemp()
try:
os.remove(path)
except OSError:
pass
else:
raise AssertionError
finally:
os.rmdir(path)
def TestRmDir():
path = tempfile.mkdtemp()
assert stat.S_ISDIR(os.stat(path).st_mode)
os.rmdir(path)
try:
os.stat(path)
except OSError:
pass
else:
raise AssertionError
def TestRmDirNoExist():
path = tempfile.mkdtemp()
try:
os.rmdir(path + '/nonexistent')
except OSError:
pass
else:
raise AssertionError
finally:
os.rmdir(path)
def TestRmDirFile():
fd, path = tempfile.mkstemp()
os.close(fd)
try:
os.rmdir(path)
except OSError:
pass
else:
raise AssertionError
finally:
os.remove(path)
def TestStatFile():
t = time.time()
fd, path = tempfile.mkstemp()
os.close(fd)
st = os.stat(path)
os.remove(path)
assert not stat.S_ISDIR(st.st_mode)
assert stat.S_IMODE(st.st_mode) == 0o600
# System time and mtime may have different precision so give 10 sec leeway.
assert st.st_mtime + 10 > t
assert st.st_size == 0
def TestStatDir():
path = tempfile.mkdtemp()
mode = os.stat(path).st_mode
os.rmdir(path)
assert stat.S_ISDIR(mode)
assert stat.S_IMODE(mode) == 0o700
def TestStatNoExist():
path = tempfile.mkdtemp()
try:
os.stat(path + '/nonexistent')
except OSError:
pass
else:
raise AssertionError
finally:
os.rmdir(path)
def TestWaitPid():
try:
pid, status = os.waitpid(-1, os.WNOHANG)
except OSError as e:
assert 'no child processes' in str(e).lower()
if __name__ == '__main__':
weetest.RunTests()