github.com/google/grumpy@v0.0.0-20171122020858-3ec87959189c/lib/tempfile_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 tempfile 18 19 import weetest 20 21 22 def TestMkdTemp(): 23 path = tempfile.mkdtemp() 24 mode = os.stat(path).st_mode 25 os.rmdir(path) 26 assert stat.S_ISDIR(mode), mode 27 assert stat.S_IMODE(mode) == 0o700, mode 28 29 30 def TestMkdTempDir(): 31 tempdir = tempfile.mkdtemp() 32 path = tempfile.mkdtemp(dir=tempdir) 33 os.rmdir(path) 34 os.rmdir(tempdir) 35 assert path.startswith(tempdir) 36 37 38 def TestMkdTempOSError(): 39 tempdir = tempfile.mkdtemp() 40 os.chmod(tempdir, 0o500) 41 try: 42 tempfile.mkdtemp(dir=tempdir) 43 except OSError: 44 pass 45 else: 46 raise AssertionError 47 os.rmdir(tempdir) 48 49 50 def TestMkdTempPrefixSuffix(): 51 path = tempfile.mkdtemp(prefix='foo', suffix='bar') 52 os.rmdir(path) 53 assert 'foo' in path 54 assert 'bar' in path 55 # TODO: assert path.endswith('bar') 56 57 58 def TestMksTemp(): 59 fd, path = tempfile.mkstemp() 60 f = os.fdopen(fd, 'w') 61 f.write('foobar') 62 f.close() 63 f = open(path) 64 contents = f.read() 65 f.close() 66 os.remove(path) 67 assert contents == 'foobar', contents 68 69 70 def TestMksTempDir(): 71 tempdir = tempfile.mkdtemp() 72 fd, path = tempfile.mkstemp(dir=tempdir) 73 os.close(fd) 74 os.remove(path) 75 os.rmdir(tempdir) 76 assert path.startswith(tempdir) 77 78 79 def TestMksTempOSError(): 80 tempdir = tempfile.mkdtemp() 81 os.chmod(tempdir, 0o500) 82 try: 83 tempfile.mkstemp(dir=tempdir) 84 except OSError: 85 pass 86 else: 87 raise AssertionError 88 os.rmdir(tempdir) 89 90 91 def TestMksTempPerms(): 92 fd, path = tempfile.mkstemp() 93 os.close(fd) 94 mode = os.stat(path).st_mode 95 os.remove(path) 96 assert stat.S_IMODE(mode) == 0o600, mode 97 98 99 def TestMksTempPrefixSuffix(): 100 fd, path = tempfile.mkstemp(prefix='foo', suffix='bar') 101 os.close(fd) 102 os.remove(path) 103 assert 'foo' in path 104 assert 'bar' in path 105 # TODO: assert path.endswith('bar') 106 107 108 if __name__ == '__main__': 109 weetest.RunTests()