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