OLD | NEW |
(Empty) | |
| 1 #! /usr/bin/env python |
| 2 # |
| 3 # Copyright 2009 Google Inc. All Rights Reserved. |
| 4 # |
| 5 # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 # you may not use this file except in compliance with the License. |
| 7 # You may obtain a copy of the License at |
| 8 # |
| 9 # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 # |
| 11 # Unless required by applicable law or agreed to in writing, software |
| 12 # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 # See the License for the specific language governing permissions and |
| 15 # limitations under the License. |
| 16 |
| 17 """Unittest for fake_filesystem_shutil.""" |
| 18 |
| 19 import stat |
| 20 import time |
| 21 import sys |
| 22 if sys.version_info < (2, 7): |
| 23 import unittest2 as unittest |
| 24 else: |
| 25 import unittest |
| 26 |
| 27 import fake_filesystem |
| 28 import fake_filesystem_shutil |
| 29 |
| 30 |
| 31 class FakeShutilModuleTest(unittest.TestCase): |
| 32 |
| 33 def setUp(self): |
| 34 self.filesystem = fake_filesystem.FakeFilesystem(path_separator='/') |
| 35 self.shutil = fake_filesystem_shutil.FakeShutilModule(self.filesystem) |
| 36 |
| 37 def testRmtree(self): |
| 38 directory = 'xyzzy' |
| 39 self.filesystem.CreateDirectory(directory) |
| 40 self.filesystem.CreateDirectory('%s/subdir' % directory) |
| 41 self.filesystem.CreateFile('%s/subfile' % directory) |
| 42 self.assertTrue(self.filesystem.Exists(directory)) |
| 43 self.shutil.rmtree(directory) |
| 44 self.assertFalse(self.filesystem.Exists(directory)) |
| 45 |
| 46 def testCopy(self): |
| 47 src_file = 'xyzzy' |
| 48 dst_file = 'xyzzy_copy' |
| 49 src_obj = self.filesystem.CreateFile(src_file) |
| 50 src_obj.st_mode = ((src_obj.st_mode & ~0o7777) | 0o750) |
| 51 self.assertTrue(self.filesystem.Exists(src_file)) |
| 52 self.assertFalse(self.filesystem.Exists(dst_file)) |
| 53 self.shutil.copy(src_file, dst_file) |
| 54 self.assertTrue(self.filesystem.Exists(dst_file)) |
| 55 dst_obj = self.filesystem.GetObject(dst_file) |
| 56 self.assertEqual(src_obj.st_mode, dst_obj.st_mode) |
| 57 |
| 58 def testCopyDirectory(self): |
| 59 src_file = 'xyzzy' |
| 60 parent_directory = 'parent' |
| 61 dst_file = '%s/%s' % (parent_directory, src_file) |
| 62 src_obj = self.filesystem.CreateFile(src_file) |
| 63 self.filesystem.CreateDirectory(parent_directory) |
| 64 src_obj.st_mode = ((src_obj.st_mode & ~0o7777) | 0o750) |
| 65 self.assertTrue(self.filesystem.Exists(src_file)) |
| 66 self.assertTrue(self.filesystem.Exists(parent_directory)) |
| 67 self.assertFalse(self.filesystem.Exists(dst_file)) |
| 68 self.shutil.copy(src_file, parent_directory) |
| 69 self.assertTrue(self.filesystem.Exists(dst_file)) |
| 70 dst_obj = self.filesystem.GetObject(dst_file) |
| 71 self.assertEqual(src_obj.st_mode, dst_obj.st_mode) |
| 72 |
| 73 def testCopystat(self): |
| 74 src_file = 'xyzzy' |
| 75 dst_file = 'xyzzy_copy' |
| 76 src_obj = self.filesystem.CreateFile(src_file) |
| 77 dst_obj = self.filesystem.CreateFile(dst_file) |
| 78 src_obj.st_mode = ((src_obj.st_mode & ~0o7777) | 0o750) |
| 79 src_obj.st_uid = 123 |
| 80 src_obj.st_gid = 123 |
| 81 src_obj.st_atime = time.time() |
| 82 src_obj.st_mtime = time.time() |
| 83 self.assertTrue(self.filesystem.Exists(src_file)) |
| 84 self.assertTrue(self.filesystem.Exists(dst_file)) |
| 85 self.shutil.copystat(src_file, dst_file) |
| 86 self.assertEqual(src_obj.st_mode, dst_obj.st_mode) |
| 87 self.assertEqual(src_obj.st_uid, dst_obj.st_uid) |
| 88 self.assertEqual(src_obj.st_gid, dst_obj.st_gid) |
| 89 self.assertEqual(src_obj.st_atime, dst_obj.st_atime) |
| 90 self.assertEqual(src_obj.st_mtime, dst_obj.st_mtime) |
| 91 |
| 92 def testCopy2(self): |
| 93 src_file = 'xyzzy' |
| 94 dst_file = 'xyzzy_copy' |
| 95 src_obj = self.filesystem.CreateFile(src_file) |
| 96 src_obj.st_mode = ((src_obj.st_mode & ~0o7777) | 0o750) |
| 97 src_obj.st_uid = 123 |
| 98 src_obj.st_gid = 123 |
| 99 src_obj.st_atime = time.time() |
| 100 src_obj.st_mtime = time.time() |
| 101 self.assertTrue(self.filesystem.Exists(src_file)) |
| 102 self.assertFalse(self.filesystem.Exists(dst_file)) |
| 103 self.shutil.copy2(src_file, dst_file) |
| 104 self.assertTrue(self.filesystem.Exists(dst_file)) |
| 105 dst_obj = self.filesystem.GetObject(dst_file) |
| 106 self.assertEqual(src_obj.st_mode, dst_obj.st_mode) |
| 107 self.assertEqual(src_obj.st_uid, dst_obj.st_uid) |
| 108 self.assertEqual(src_obj.st_gid, dst_obj.st_gid) |
| 109 self.assertEqual(src_obj.st_atime, dst_obj.st_atime) |
| 110 self.assertEqual(src_obj.st_mtime, dst_obj.st_mtime) |
| 111 |
| 112 def testCopy2Directory(self): |
| 113 src_file = 'xyzzy' |
| 114 parent_directory = 'parent' |
| 115 dst_file = '%s/%s' % (parent_directory, src_file) |
| 116 src_obj = self.filesystem.CreateFile(src_file) |
| 117 self.filesystem.CreateDirectory(parent_directory) |
| 118 src_obj.st_mode = ((src_obj.st_mode & ~0o7777) | 0o750) |
| 119 src_obj.st_uid = 123 |
| 120 src_obj.st_gid = 123 |
| 121 src_obj.st_atime = time.time() |
| 122 src_obj.st_mtime = time.time() |
| 123 self.assertTrue(self.filesystem.Exists(src_file)) |
| 124 self.assertTrue(self.filesystem.Exists(parent_directory)) |
| 125 self.assertFalse(self.filesystem.Exists(dst_file)) |
| 126 self.shutil.copy2(src_file, parent_directory) |
| 127 self.assertTrue(self.filesystem.Exists(dst_file)) |
| 128 dst_obj = self.filesystem.GetObject(dst_file) |
| 129 self.assertEqual(src_obj.st_mode, dst_obj.st_mode) |
| 130 self.assertEqual(src_obj.st_uid, dst_obj.st_uid) |
| 131 self.assertEqual(src_obj.st_gid, dst_obj.st_gid) |
| 132 self.assertEqual(src_obj.st_atime, dst_obj.st_atime) |
| 133 self.assertEqual(src_obj.st_mtime, dst_obj.st_mtime) |
| 134 |
| 135 def testCopytree(self): |
| 136 src_directory = 'xyzzy' |
| 137 dst_directory = 'xyzzy_copy' |
| 138 self.filesystem.CreateDirectory(src_directory) |
| 139 self.filesystem.CreateDirectory('%s/subdir' % src_directory) |
| 140 self.filesystem.CreateFile('%s/subfile' % src_directory) |
| 141 self.assertTrue(self.filesystem.Exists(src_directory)) |
| 142 self.assertFalse(self.filesystem.Exists(dst_directory)) |
| 143 self.shutil.copytree(src_directory, dst_directory) |
| 144 self.assertTrue(self.filesystem.Exists(dst_directory)) |
| 145 self.assertTrue(self.filesystem.Exists('%s/subdir' % dst_directory)) |
| 146 self.assertTrue(self.filesystem.Exists('%s/subfile' % dst_directory)) |
| 147 |
| 148 def testCopytreeSrcIsFile(self): |
| 149 src_file = 'xyzzy' |
| 150 dst_directory = 'xyzzy_copy' |
| 151 self.filesystem.CreateFile(src_file) |
| 152 self.assertTrue(self.filesystem.Exists(src_file)) |
| 153 self.assertFalse(self.filesystem.Exists(dst_directory)) |
| 154 self.assertRaises(OSError, |
| 155 self.shutil.copytree, |
| 156 src_file, |
| 157 dst_directory) |
| 158 |
| 159 def testMoveFile(self): |
| 160 src_file = 'original_xyzzy' |
| 161 dst_file = 'moved_xyzzy' |
| 162 self.filesystem.CreateFile(src_file) |
| 163 self.assertTrue(self.filesystem.Exists(src_file)) |
| 164 self.assertFalse(self.filesystem.Exists(dst_file)) |
| 165 self.shutil.move(src_file, dst_file) |
| 166 self.assertTrue(self.filesystem.Exists(dst_file)) |
| 167 self.assertFalse(self.filesystem.Exists(src_file)) |
| 168 |
| 169 def testMoveFileIntoDirectory(self): |
| 170 src_file = 'xyzzy' |
| 171 dst_directory = 'directory' |
| 172 dst_file = '%s/%s' % (dst_directory, src_file) |
| 173 self.filesystem.CreateFile(src_file) |
| 174 self.filesystem.CreateDirectory(dst_directory) |
| 175 self.assertTrue(self.filesystem.Exists(src_file)) |
| 176 self.assertFalse(self.filesystem.Exists(dst_file)) |
| 177 self.shutil.move(src_file, dst_directory) |
| 178 self.assertTrue(self.filesystem.Exists(dst_file)) |
| 179 self.assertFalse(self.filesystem.Exists(src_file)) |
| 180 |
| 181 def testMoveDirectory(self): |
| 182 src_directory = 'original_xyzzy' |
| 183 dst_directory = 'moved_xyzzy' |
| 184 self.filesystem.CreateDirectory(src_directory) |
| 185 self.filesystem.CreateFile('%s/subfile' % src_directory) |
| 186 self.filesystem.CreateDirectory('%s/subdir' % src_directory) |
| 187 self.assertTrue(self.filesystem.Exists(src_directory)) |
| 188 self.assertFalse(self.filesystem.Exists(dst_directory)) |
| 189 self.shutil.move(src_directory, dst_directory) |
| 190 self.assertTrue(self.filesystem.Exists(dst_directory)) |
| 191 self.assertTrue(self.filesystem.Exists('%s/subfile' % dst_directory)) |
| 192 self.assertTrue(self.filesystem.Exists('%s/subdir' % dst_directory)) |
| 193 self.assertFalse(self.filesystem.Exists(src_directory)) |
| 194 |
| 195 |
| 196 class CopyFileTest(unittest.TestCase): |
| 197 |
| 198 def setUp(self): |
| 199 self.filesystem = fake_filesystem.FakeFilesystem(path_separator='/') |
| 200 self.shutil = fake_filesystem_shutil.FakeShutilModule(self.filesystem) |
| 201 |
| 202 def testCommonCase(self): |
| 203 src_file = 'xyzzy' |
| 204 dst_file = 'xyzzy_copy' |
| 205 contents = 'contents of file' |
| 206 self.filesystem.CreateFile(src_file, contents=contents) |
| 207 self.assertTrue(self.filesystem.Exists(src_file)) |
| 208 self.assertFalse(self.filesystem.Exists(dst_file)) |
| 209 self.shutil.copyfile(src_file, dst_file) |
| 210 self.assertTrue(self.filesystem.Exists(dst_file)) |
| 211 self.assertEqual(contents, self.filesystem.GetObject(dst_file).contents) |
| 212 |
| 213 def testRaisesIfSourceAndDestAreTheSameFile(self): |
| 214 src_file = 'xyzzy' |
| 215 dst_file = src_file |
| 216 contents = 'contents of file' |
| 217 self.filesystem.CreateFile(src_file, contents=contents) |
| 218 self.assertTrue(self.filesystem.Exists(src_file)) |
| 219 self.assertRaises(self.shutil.Error, |
| 220 self.shutil.copyfile, src_file, dst_file) |
| 221 |
| 222 def testRaisesIfDestIsASymlinkToSrc(self): |
| 223 src_file = '/tmp/foo' |
| 224 dst_file = '/tmp/bar' |
| 225 contents = 'contents of file' |
| 226 self.filesystem.CreateFile(src_file, contents=contents) |
| 227 self.filesystem.CreateLink(dst_file, src_file) |
| 228 self.assertTrue(self.filesystem.Exists(src_file)) |
| 229 self.assertRaises(self.shutil.Error, |
| 230 self.shutil.copyfile, src_file, dst_file) |
| 231 |
| 232 def testSucceedsIfDestExistsAndIsWritable(self): |
| 233 src_file = 'xyzzy' |
| 234 dst_file = 'xyzzy_copy' |
| 235 src_contents = 'contents of source file' |
| 236 dst_contents = 'contents of dest file' |
| 237 self.filesystem.CreateFile(src_file, contents=src_contents) |
| 238 self.filesystem.CreateFile(dst_file, contents=dst_contents) |
| 239 self.assertTrue(self.filesystem.Exists(src_file)) |
| 240 self.assertTrue(self.filesystem.Exists(dst_file)) |
| 241 self.shutil.copyfile(src_file, dst_file) |
| 242 self.assertTrue(self.filesystem.Exists(dst_file)) |
| 243 self.assertEqual(src_contents, |
| 244 self.filesystem.GetObject(dst_file).contents) |
| 245 |
| 246 def testRaisesIfDestExistsAndIsNotWritable(self): |
| 247 src_file = 'xyzzy' |
| 248 dst_file = 'xyzzy_copy' |
| 249 src_contents = 'contents of source file' |
| 250 dst_contents = 'contents of dest file' |
| 251 self.filesystem.CreateFile(src_file, contents=src_contents) |
| 252 self.filesystem.CreateFile(dst_file, |
| 253 st_mode=stat.S_IFREG | 0o400, |
| 254 contents=dst_contents) |
| 255 self.assertTrue(self.filesystem.Exists(src_file)) |
| 256 self.assertTrue(self.filesystem.Exists(dst_file)) |
| 257 self.assertRaises(IOError, self.shutil.copyfile, src_file, dst_file) |
| 258 |
| 259 def testRaisesIfDestDirIsNotWritable(self): |
| 260 src_file = 'xyzzy' |
| 261 dst_dir = '/tmp/foo' |
| 262 dst_file = '%s/%s' % (dst_dir, src_file) |
| 263 src_contents = 'contents of source file' |
| 264 self.filesystem.CreateFile(src_file, contents=src_contents) |
| 265 self.filesystem.CreateDirectory(dst_dir, perm_bits=0o555) |
| 266 self.assertTrue(self.filesystem.Exists(src_file)) |
| 267 self.assertTrue(self.filesystem.Exists(dst_dir)) |
| 268 self.assertRaises(IOError, self.shutil.copyfile, src_file, dst_file) |
| 269 |
| 270 def testRaisesIfSrcDoesntExist(self): |
| 271 src_file = 'xyzzy' |
| 272 dst_file = 'xyzzy_copy' |
| 273 self.assertFalse(self.filesystem.Exists(src_file)) |
| 274 self.assertRaises(IOError, self.shutil.copyfile, src_file, dst_file) |
| 275 |
| 276 def testRaisesIfSrcNotReadable(self): |
| 277 src_file = 'xyzzy' |
| 278 dst_file = 'xyzzy_copy' |
| 279 src_contents = 'contents of source file' |
| 280 self.filesystem.CreateFile(src_file, |
| 281 st_mode=stat.S_IFREG | 0o000, |
| 282 contents=src_contents) |
| 283 self.assertTrue(self.filesystem.Exists(src_file)) |
| 284 self.assertRaises(IOError, self.shutil.copyfile, src_file, dst_file) |
| 285 |
| 286 def testRaisesIfSrcIsADirectory(self): |
| 287 src_file = 'xyzzy' |
| 288 dst_file = 'xyzzy_copy' |
| 289 self.filesystem.CreateDirectory(src_file) |
| 290 self.assertTrue(self.filesystem.Exists(src_file)) |
| 291 self.assertRaises(IOError, self.shutil.copyfile, src_file, dst_file) |
| 292 |
| 293 def testRaisesIfDestIsADirectory(self): |
| 294 src_file = 'xyzzy' |
| 295 dst_dir = '/tmp/foo' |
| 296 src_contents = 'contents of source file' |
| 297 self.filesystem.CreateFile(src_file, contents=src_contents) |
| 298 self.filesystem.CreateDirectory(dst_dir) |
| 299 self.assertTrue(self.filesystem.Exists(src_file)) |
| 300 self.assertTrue(self.filesystem.Exists(dst_dir)) |
| 301 self.assertRaises(IOError, self.shutil.copyfile, src_file, dst_dir) |
| 302 |
| 303 |
| 304 if __name__ == '__main__': |
| 305 unittest.main() |
OLD | NEW |