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 module.""" |
| 18 |
| 19 import errno |
| 20 import os |
| 21 import re |
| 22 import stat |
| 23 import sys |
| 24 import time |
| 25 if sys.version_info < (2, 7): |
| 26 import unittest2 as unittest |
| 27 else: |
| 28 import unittest |
| 29 |
| 30 import fake_filesystem |
| 31 |
| 32 |
| 33 def _GetDummyTime(start_time, increment): |
| 34 def _DummyTime(): |
| 35 _DummyTime._curr_time += increment |
| 36 return _DummyTime._curr_time |
| 37 _DummyTime._curr_time = start_time - increment # pylint: disable-msg=W0612 |
| 38 return _DummyTime |
| 39 |
| 40 |
| 41 class TestCase(unittest.TestCase): |
| 42 is_windows = sys.platform.startswith('win') |
| 43 is_cygwin = sys.platform == 'cygwin' |
| 44 |
| 45 def assertModeEqual(self, expected, actual): |
| 46 return self.assertEqual(stat.S_IMODE(expected), stat.S_IMODE(actual)) |
| 47 |
| 48 |
| 49 class FakeDirectoryUnitTest(TestCase): |
| 50 def setUp(self): |
| 51 self.orig_time = time.time |
| 52 time.time = _GetDummyTime(10, 1) |
| 53 self.fake_file = fake_filesystem.FakeFile('foobar', contents='dummy_file') |
| 54 self.fake_dir = fake_filesystem.FakeDirectory('somedir') |
| 55 |
| 56 def tearDown(self): |
| 57 time.time = self.orig_time |
| 58 |
| 59 def testNewFileAndDirectory(self): |
| 60 self.assertTrue(stat.S_IFREG & self.fake_file.st_mode) |
| 61 self.assertTrue(stat.S_IFDIR & self.fake_dir.st_mode) |
| 62 self.assertEqual({}, self.fake_dir.contents) |
| 63 self.assertEqual(10, self.fake_file.st_ctime) |
| 64 |
| 65 def testAddEntry(self): |
| 66 self.fake_dir.AddEntry(self.fake_file) |
| 67 self.assertEqual({'foobar': self.fake_file}, self.fake_dir.contents) |
| 68 |
| 69 def testGetEntry(self): |
| 70 self.fake_dir.AddEntry(self.fake_file) |
| 71 self.assertEqual(self.fake_file, self.fake_dir.GetEntry('foobar')) |
| 72 |
| 73 def testRemoveEntry(self): |
| 74 self.fake_dir.AddEntry(self.fake_file) |
| 75 self.assertEqual(self.fake_file, self.fake_dir.GetEntry('foobar')) |
| 76 self.fake_dir.RemoveEntry('foobar') |
| 77 self.assertRaises(KeyError, self.fake_dir.GetEntry, 'foobar') |
| 78 |
| 79 def testShouldThrowIfSetSizeIsNotInteger(self): |
| 80 self.assertRaises(IOError, self.fake_file.SetSize, 0.1) |
| 81 |
| 82 def testShouldThrowIfSetSizeIsNegative(self): |
| 83 self.assertRaises(IOError, self.fake_file.SetSize, -1) |
| 84 |
| 85 def testProduceEmptyFileIfSetSizeIsZero(self): |
| 86 self.fake_file.SetSize(0) |
| 87 self.assertEqual('', self.fake_file.contents) |
| 88 |
| 89 def testSetsContentEmptyIfSetSizeIsZero(self): |
| 90 self.fake_file.SetSize(0) |
| 91 self.assertEqual('', self.fake_file.contents) |
| 92 |
| 93 def testTruncateFileIfSizeIsSmallerThanCurrentSize(self): |
| 94 self.fake_file.SetSize(6) |
| 95 self.assertEqual('dummy_', self.fake_file.contents) |
| 96 |
| 97 def testLeaveFileUnchangedIfSizeIsEqualToCurrentSize(self): |
| 98 self.fake_file.SetSize(10) |
| 99 self.assertEqual('dummy_file', self.fake_file.contents) |
| 100 |
| 101 def testPadsFileContentWithNullBytesIfSizeIsGreaterThanCurrentSize(self): |
| 102 self.fake_file.SetSize(13) |
| 103 self.assertEqual('dummy_file\0\0\0', self.fake_file.contents) |
| 104 |
| 105 def testSetMTime(self): |
| 106 self.assertEqual(10, self.fake_file.st_mtime) |
| 107 self.fake_file.SetMTime(13) |
| 108 self.assertEqual(13, self.fake_file.st_mtime) |
| 109 self.fake_file.SetMTime(131) |
| 110 self.assertEqual(131, self.fake_file.st_mtime) |
| 111 |
| 112 def testFileInode(self): |
| 113 filesystem = fake_filesystem.FakeFilesystem(path_separator='/') |
| 114 fake_os = fake_filesystem.FakeOsModule(filesystem) |
| 115 file_path = 'some_file1' |
| 116 filesystem.CreateFile(file_path, contents='contents here1', inode=42) |
| 117 self.assertEqual(42, fake_os.stat(file_path)[stat.ST_INO]) |
| 118 |
| 119 file_obj = filesystem.GetObject(file_path) |
| 120 file_obj.SetIno(43) |
| 121 self.assertEqual(43, fake_os.stat(file_path)[stat.ST_INO]) |
| 122 |
| 123 def testDirectoryInode(self): |
| 124 filesystem = fake_filesystem.FakeFilesystem(path_separator='/') |
| 125 fake_os = fake_filesystem.FakeOsModule(filesystem) |
| 126 dirpath = 'testdir' |
| 127 filesystem.CreateDirectory(dirpath, inode=42) |
| 128 self.assertEqual(42, fake_os.stat(dirpath)[stat.ST_INO]) |
| 129 |
| 130 dir_obj = filesystem.GetObject(dirpath) |
| 131 dir_obj.SetIno(43) |
| 132 self.assertEqual(43, fake_os.stat(dirpath)[stat.ST_INO]) |
| 133 |
| 134 |
| 135 class SetLargeFileSizeTest(FakeDirectoryUnitTest): |
| 136 |
| 137 def testShouldThrowIfSizeIsNotInteger(self): |
| 138 self.assertRaises(IOError, self.fake_file.SetLargeFileSize, 0.1) |
| 139 |
| 140 def testShouldThrowIfSizeIsNegative(self): |
| 141 self.assertRaises(IOError, self.fake_file.SetLargeFileSize, -1) |
| 142 |
| 143 def testSetsContentNoneIfSizeIsNonNegativeInteger(self): |
| 144 self.fake_file.SetLargeFileSize(1000000000) |
| 145 self.assertEqual(None, self.fake_file.contents) |
| 146 self.assertEqual(1000000000, self.fake_file.st_size) |
| 147 |
| 148 |
| 149 class NormalizePathTest(TestCase): |
| 150 def setUp(self): |
| 151 self.filesystem = fake_filesystem.FakeFilesystem(path_separator='/') |
| 152 self.root_name = '/' |
| 153 |
| 154 def testEmptyPathShouldGetNormalizedToRootPath(self): |
| 155 self.assertEqual(self.root_name, self.filesystem.NormalizePath('')) |
| 156 |
| 157 def testRootPathRemainsUnchanged(self): |
| 158 self.assertEqual(self.root_name, |
| 159 self.filesystem.NormalizePath(self.root_name)) |
| 160 |
| 161 def testRelativePathForcedToCwd(self): |
| 162 path = 'bar' |
| 163 self.filesystem.cwd = '/foo' |
| 164 self.assertEqual('/foo/bar', self.filesystem.NormalizePath(path)) |
| 165 |
| 166 def testAbsolutePathRemainsUnchanged(self): |
| 167 path = '/foo/bar' |
| 168 self.assertEqual(path, self.filesystem.NormalizePath(path)) |
| 169 |
| 170 def testDottedPathIsNormalized(self): |
| 171 path = '/foo/..' |
| 172 self.assertEqual('/', self.filesystem.NormalizePath(path)) |
| 173 path = 'foo/../bar' |
| 174 self.assertEqual('/bar', self.filesystem.NormalizePath(path)) |
| 175 |
| 176 def testDotPathIsNormalized(self): |
| 177 path = '.' |
| 178 self.assertEqual('/', self.filesystem.NormalizePath(path)) |
| 179 |
| 180 |
| 181 class GetPathComponentsTest(TestCase): |
| 182 def setUp(self): |
| 183 self.filesystem = fake_filesystem.FakeFilesystem(path_separator='/') |
| 184 self.root_name = '/' |
| 185 |
| 186 def testRootPathShouldReturnEmptyList(self): |
| 187 self.assertEqual([], self.filesystem.GetPathComponents(self.root_name)) |
| 188 |
| 189 def testEmptyPathShouldReturnEmptyList(self): |
| 190 self.assertEqual([], self.filesystem.GetPathComponents('')) |
| 191 |
| 192 def testRelativePathWithOneComponentShouldReturnComponent(self): |
| 193 self.assertEqual(['foo'], self.filesystem.GetPathComponents('foo')) |
| 194 |
| 195 def testAbsolutePathWithOneComponentShouldReturnComponent(self): |
| 196 self.assertEqual(['foo'], self.filesystem.GetPathComponents('/foo')) |
| 197 |
| 198 def testTwoLevelRelativePathShouldReturnComponents(self): |
| 199 self.assertEqual(['foo', 'bar'], |
| 200 self.filesystem.GetPathComponents('foo/bar')) |
| 201 |
| 202 def testTwoLevelAbsolutePathShouldReturnComponents(self): |
| 203 self.assertEqual(['foo', 'bar'], |
| 204 self.filesystem.GetPathComponents('/foo/bar')) |
| 205 |
| 206 |
| 207 class FakeFilesystemUnitTest(TestCase): |
| 208 def setUp(self): |
| 209 self.filesystem = fake_filesystem.FakeFilesystem(path_separator='/') |
| 210 self.root_name = '/' |
| 211 self.fake_file = fake_filesystem.FakeFile('foobar') |
| 212 self.fake_child = fake_filesystem.FakeDirectory('foobaz') |
| 213 self.fake_grandchild = fake_filesystem.FakeDirectory('quux') |
| 214 |
| 215 def testNewFilesystem(self): |
| 216 self.assertEqual('/', self.filesystem.path_separator) |
| 217 self.assertTrue(stat.S_IFDIR & self.filesystem.root.st_mode) |
| 218 self.assertEqual(self.root_name, self.filesystem.root.name) |
| 219 self.assertEqual({}, self.filesystem.root.contents) |
| 220 |
| 221 def testNoneRaisesTypeError(self): |
| 222 self.assertRaises(TypeError, self.filesystem.Exists, None) |
| 223 |
| 224 def testEmptyStringDoesNotExist(self): |
| 225 self.assertFalse(self.filesystem.Exists('')) |
| 226 |
| 227 def testExistsRoot(self): |
| 228 self.assertTrue(self.filesystem.Exists(self.root_name)) |
| 229 |
| 230 def testExistsUnaddedFile(self): |
| 231 self.assertFalse(self.filesystem.Exists(self.fake_file.name)) |
| 232 |
| 233 def testGetRootObject(self): |
| 234 self.assertEqual(self.filesystem.root, |
| 235 self.filesystem.GetObject(self.root_name)) |
| 236 |
| 237 def testAddObjectToRoot(self): |
| 238 self.filesystem.AddObject(self.root_name, self.fake_file) |
| 239 self.assertEqual({'foobar': self.fake_file}, self.filesystem.root.contents) |
| 240 |
| 241 def testExistsAddedFile(self): |
| 242 self.filesystem.AddObject(self.root_name, self.fake_file) |
| 243 self.assertTrue(self.filesystem.Exists(self.fake_file.name)) |
| 244 |
| 245 def testExistsRelativePath(self): |
| 246 self.filesystem.CreateFile('/a/b/file_one') |
| 247 self.filesystem.CreateFile('/a/c/file_two') |
| 248 self.assertTrue(self.filesystem.Exists('a/b/../c/file_two')) |
| 249 self.assertTrue(self.filesystem.Exists('/a/c/../b/file_one')) |
| 250 self.assertTrue(self.filesystem.Exists('/a/c/../../a/b/file_one')) |
| 251 self.assertFalse(self.filesystem.Exists('a/b/../z/d')) |
| 252 self.assertFalse(self.filesystem.Exists('a/b/../z/../c/file_two')) |
| 253 self.filesystem.cwd = '/a/c' |
| 254 self.assertTrue(self.filesystem.Exists('../b/file_one')) |
| 255 self.assertTrue(self.filesystem.Exists('../../a/b/file_one')) |
| 256 self.assertTrue(self.filesystem.Exists('../../a/b/../../a/c/file_two')) |
| 257 self.assertFalse(self.filesystem.Exists('../z/file_one')) |
| 258 self.assertFalse(self.filesystem.Exists('../z/../c/file_two')) |
| 259 |
| 260 def testGetObjectFromRoot(self): |
| 261 self.filesystem.AddObject(self.root_name, self.fake_file) |
| 262 self.assertEqual(self.fake_file, self.filesystem.GetObject('foobar')) |
| 263 |
| 264 def testGetNonexistentObjectFromRootError(self): |
| 265 self.filesystem.AddObject(self.root_name, self.fake_file) |
| 266 self.assertEqual(self.fake_file, self.filesystem.GetObject('foobar')) |
| 267 self.assertRaises(IOError, self.filesystem.GetObject, |
| 268 'some_bogus_filename') |
| 269 |
| 270 def testRemoveObjectFromRoot(self): |
| 271 self.filesystem.AddObject(self.root_name, self.fake_file) |
| 272 self.filesystem.RemoveObject(self.fake_file.name) |
| 273 self.assertRaises(IOError, self.filesystem.GetObject, self.fake_file.name) |
| 274 |
| 275 def testRemoveNonexistenObjectFromRootError(self): |
| 276 self.assertRaises(IOError, self.filesystem.RemoveObject, |
| 277 'some_bogus_filename') |
| 278 |
| 279 def testExistsRemovedFile(self): |
| 280 self.filesystem.AddObject(self.root_name, self.fake_file) |
| 281 self.filesystem.RemoveObject(self.fake_file.name) |
| 282 self.assertFalse(self.filesystem.Exists(self.fake_file.name)) |
| 283 |
| 284 def testAddObjectToChild(self): |
| 285 self.filesystem.AddObject(self.root_name, self.fake_child) |
| 286 self.filesystem.AddObject(self.fake_child.name, self.fake_file) |
| 287 self.assertEqual( |
| 288 {self.fake_file.name: self.fake_file}, |
| 289 self.filesystem.root.GetEntry(self.fake_child.name).contents) |
| 290 |
| 291 def testAddObjectToRegularFileError(self): |
| 292 self.filesystem.AddObject(self.root_name, self.fake_file) |
| 293 self.assertRaises(IOError, self.filesystem.AddObject, |
| 294 self.fake_file.name, self.fake_file) |
| 295 |
| 296 def testExistsFileAddedToChild(self): |
| 297 self.filesystem.AddObject(self.root_name, self.fake_child) |
| 298 self.filesystem.AddObject(self.fake_child.name, self.fake_file) |
| 299 path = self.filesystem.JoinPaths(self.fake_child.name, |
| 300 self.fake_file.name) |
| 301 self.assertTrue(self.filesystem.Exists(path)) |
| 302 |
| 303 def testGetObjectFromChild(self): |
| 304 self.filesystem.AddObject(self.root_name, self.fake_child) |
| 305 self.filesystem.AddObject(self.fake_child.name, self.fake_file) |
| 306 self.assertEqual(self.fake_file, |
| 307 self.filesystem.GetObject( |
| 308 self.filesystem.JoinPaths(self.fake_child.name, |
| 309 self.fake_file.name))) |
| 310 |
| 311 def testGetNonexistentObjectFromChildError(self): |
| 312 self.filesystem.AddObject(self.root_name, self.fake_child) |
| 313 self.filesystem.AddObject(self.fake_child.name, self.fake_file) |
| 314 self.assertRaises(IOError, self.filesystem.GetObject, |
| 315 self.filesystem.JoinPaths(self.fake_child.name, |
| 316 'some_bogus_filename')) |
| 317 |
| 318 def testRemoveObjectFromChild(self): |
| 319 self.filesystem.AddObject(self.root_name, self.fake_child) |
| 320 self.filesystem.AddObject(self.fake_child.name, self.fake_file) |
| 321 target_path = self.filesystem.JoinPaths(self.fake_child.name, |
| 322 self.fake_file.name) |
| 323 self.filesystem.RemoveObject(target_path) |
| 324 self.assertRaises(IOError, self.filesystem.GetObject, target_path) |
| 325 |
| 326 def testRemoveObjectFromChildError(self): |
| 327 self.filesystem.AddObject(self.root_name, self.fake_child) |
| 328 self.assertRaises(IOError, self.filesystem.RemoveObject, |
| 329 self.filesystem.JoinPaths(self.fake_child.name, |
| 330 'some_bogus_filename')) |
| 331 |
| 332 def testRemoveObjectFromNonDirectoryError(self): |
| 333 self.filesystem.AddObject(self.root_name, self.fake_file) |
| 334 self.assertRaises( |
| 335 IOError, self.filesystem.RemoveObject, |
| 336 self.filesystem.JoinPaths( |
| 337 '%s' % self.fake_file.name, |
| 338 'file_does_not_matter_since_parent_not_a_directory')) |
| 339 |
| 340 def testExistsFileRemovedFromChild(self): |
| 341 self.filesystem.AddObject(self.root_name, self.fake_child) |
| 342 self.filesystem.AddObject(self.fake_child.name, self.fake_file) |
| 343 path = self.filesystem.JoinPaths(self.fake_child.name, |
| 344 self.fake_file.name) |
| 345 self.filesystem.RemoveObject(path) |
| 346 self.assertFalse(self.filesystem.Exists(path)) |
| 347 |
| 348 def testOperateOnGrandchildDirectory(self): |
| 349 self.filesystem.AddObject(self.root_name, self.fake_child) |
| 350 self.filesystem.AddObject(self.fake_child.name, self.fake_grandchild) |
| 351 grandchild_directory = self.filesystem.JoinPaths(self.fake_child.name, |
| 352 self.fake_grandchild.name) |
| 353 grandchild_file = self.filesystem.JoinPaths(grandchild_directory, |
| 354 self.fake_file.name) |
| 355 self.assertRaises(IOError, self.filesystem.GetObject, grandchild_file) |
| 356 self.filesystem.AddObject(grandchild_directory, self.fake_file) |
| 357 self.assertEqual(self.fake_file, |
| 358 self.filesystem.GetObject(grandchild_file)) |
| 359 self.assertTrue(self.filesystem.Exists(grandchild_file)) |
| 360 self.filesystem.RemoveObject(grandchild_file) |
| 361 self.assertRaises(IOError, self.filesystem.GetObject, grandchild_file) |
| 362 self.assertFalse(self.filesystem.Exists(grandchild_file)) |
| 363 |
| 364 def testCreateDirectoryInRootDirectory(self): |
| 365 path = 'foo' |
| 366 self.filesystem.CreateDirectory(path) |
| 367 new_dir = self.filesystem.GetObject(path) |
| 368 self.assertEqual(os.path.basename(path), new_dir.name) |
| 369 self.assertTrue(stat.S_IFDIR & new_dir.st_mode) |
| 370 |
| 371 def testCreateDirectoryInRootDirectoryAlreadyExistsError(self): |
| 372 path = 'foo' |
| 373 self.filesystem.CreateDirectory(path) |
| 374 self.assertRaises(OSError, self.filesystem.CreateDirectory, path) |
| 375 |
| 376 def testCreateDirectory(self): |
| 377 path = 'foo/bar/baz' |
| 378 self.filesystem.CreateDirectory(path) |
| 379 new_dir = self.filesystem.GetObject(path) |
| 380 self.assertEqual(os.path.basename(path), new_dir.name) |
| 381 self.assertTrue(stat.S_IFDIR & new_dir.st_mode) |
| 382 |
| 383 # Create second directory to make sure first is OK. |
| 384 path = '%s/quux' % path |
| 385 self.filesystem.CreateDirectory(path) |
| 386 new_dir = self.filesystem.GetObject(path) |
| 387 self.assertEqual(os.path.basename(path), new_dir.name) |
| 388 self.assertTrue(stat.S_IFDIR & new_dir.st_mode) |
| 389 |
| 390 def testCreateDirectoryAlreadyExistsError(self): |
| 391 path = 'foo/bar/baz' |
| 392 self.filesystem.CreateDirectory(path) |
| 393 self.assertRaises(OSError, self.filesystem.CreateDirectory, path) |
| 394 |
| 395 def testCreateFileInCurrentDirectory(self): |
| 396 path = 'foo' |
| 397 contents = 'dummy data' |
| 398 self.filesystem.CreateFile(path, contents=contents) |
| 399 self.assertTrue(self.filesystem.Exists(path)) |
| 400 self.assertFalse(self.filesystem.Exists(os.path.dirname(path))) |
| 401 path = './%s' % path |
| 402 self.assertTrue(self.filesystem.Exists(os.path.dirname(path))) |
| 403 |
| 404 def testCreateFileInRootDirectory(self): |
| 405 path = '/foo' |
| 406 contents = 'dummy data' |
| 407 self.filesystem.CreateFile(path, contents=contents) |
| 408 new_file = self.filesystem.GetObject(path) |
| 409 self.assertTrue(self.filesystem.Exists(path)) |
| 410 self.assertTrue(self.filesystem.Exists(os.path.dirname(path))) |
| 411 self.assertEqual(os.path.basename(path), new_file.name) |
| 412 self.assertTrue(stat.S_IFREG & new_file.st_mode) |
| 413 self.assertEqual(contents, new_file.contents) |
| 414 |
| 415 def testCreateFileWithSizeButNoContentCreatesLargeFile(self): |
| 416 path = 'large_foo_bar' |
| 417 self.filesystem.CreateFile(path, st_size=100000000) |
| 418 new_file = self.filesystem.GetObject(path) |
| 419 self.assertEqual(None, new_file.contents) |
| 420 self.assertEqual(100000000, new_file.st_size) |
| 421 |
| 422 def testCreateFileInRootDirectoryAlreadyExistsError(self): |
| 423 path = 'foo' |
| 424 self.filesystem.CreateFile(path) |
| 425 self.assertRaises(IOError, self.filesystem.CreateFile, path) |
| 426 |
| 427 def testCreateFile(self): |
| 428 path = 'foo/bar/baz' |
| 429 retval = self.filesystem.CreateFile(path, contents='dummy_data') |
| 430 self.assertTrue(self.filesystem.Exists(path)) |
| 431 self.assertTrue(self.filesystem.Exists(os.path.dirname(path))) |
| 432 new_file = self.filesystem.GetObject(path) |
| 433 self.assertEqual(os.path.basename(path), new_file.name) |
| 434 self.assertTrue(stat.S_IFREG & new_file.st_mode) |
| 435 self.assertEqual(new_file, retval) |
| 436 |
| 437 def testCreateFileAlreadyExistsError(self): |
| 438 path = 'foo/bar/baz' |
| 439 self.filesystem.CreateFile(path, contents='dummy_data') |
| 440 self.assertRaises(IOError, self.filesystem.CreateFile, path) |
| 441 |
| 442 def testCreateLink(self): |
| 443 path = 'foo/bar/baz' |
| 444 target_path = 'foo/bar/quux' |
| 445 new_file = self.filesystem.CreateLink(path, 'quux') |
| 446 # Neither the path not the final target exists before we actually write to |
| 447 # one of them, even though the link appears in the file system. |
| 448 self.assertFalse(self.filesystem.Exists(path)) |
| 449 self.assertFalse(self.filesystem.Exists(target_path)) |
| 450 self.assertTrue(stat.S_IFLNK & new_file.st_mode) |
| 451 |
| 452 # but once we write the linked to file, they both will exist. |
| 453 self.filesystem.CreateFile(target_path) |
| 454 self.assertTrue(self.filesystem.Exists(path)) |
| 455 self.assertTrue(self.filesystem.Exists(target_path)) |
| 456 |
| 457 def testResolveObject(self): |
| 458 target_path = 'dir/target' |
| 459 target_contents = '0123456789ABCDEF' |
| 460 link_name = 'x' |
| 461 self.filesystem.CreateDirectory('dir') |
| 462 self.filesystem.CreateFile('dir/target', contents=target_contents) |
| 463 self.filesystem.CreateLink(link_name, target_path) |
| 464 obj = self.filesystem.ResolveObject(link_name) |
| 465 self.assertEqual('target', obj.name) |
| 466 self.assertEqual(target_contents, obj.contents) |
| 467 |
| 468 def testLresolveObject(self): |
| 469 target_path = 'dir/target' |
| 470 target_contents = '0123456789ABCDEF' |
| 471 link_name = 'x' |
| 472 self.filesystem.CreateDirectory('dir') |
| 473 self.filesystem.CreateFile('dir/target', contents=target_contents) |
| 474 self.filesystem.CreateLink(link_name, target_path) |
| 475 obj = self.filesystem.LResolveObject(link_name) |
| 476 self.assertEqual(link_name, obj.name) |
| 477 self.assertEqual(target_path, obj.contents) |
| 478 |
| 479 def testDirectoryAccessOnFile(self): |
| 480 self.filesystem.CreateFile('not_a_dir') |
| 481 self.assertRaises(IOError, self.filesystem.ResolveObject, 'not_a_dir/foo') |
| 482 self.assertRaises(IOError, self.filesystem.ResolveObject, |
| 483 'not_a_dir/foo/bar') |
| 484 self.assertRaises(IOError, self.filesystem.LResolveObject, 'not_a_dir/foo') |
| 485 self.assertRaises(IOError, self.filesystem.LResolveObject, |
| 486 'not_a_dir/foo/bar') |
| 487 |
| 488 |
| 489 class FakeOsModuleTest(TestCase): |
| 490 |
| 491 def setUp(self): |
| 492 self.filesystem = fake_filesystem.FakeFilesystem(path_separator='/') |
| 493 self.os = fake_filesystem.FakeOsModule(self.filesystem) |
| 494 self.rwx = self.os.R_OK | self.os.W_OK | self.os.X_OK |
| 495 self.rw = self.os.R_OK | self.os.W_OK |
| 496 self.orig_time = time.time |
| 497 time.time = _GetDummyTime(200, 20) |
| 498 |
| 499 def tearDown(self): |
| 500 time.time = self.orig_time |
| 501 |
| 502 def assertRaisesWithRegexpMatch(self, expected_exception, expected_regexp, |
| 503 callable_obj, *args, **kwargs): |
| 504 """Asserts that the message in a raised exception matches the given regexp. |
| 505 |
| 506 Args: |
| 507 expected_exception: Exception class expected to be raised. |
| 508 expected_regexp: Regexp (re pattern object or string) expected to be |
| 509 found in error message. |
| 510 callable_obj: Function to be called. |
| 511 *args: Extra args. |
| 512 **kwargs: Extra kwargs. |
| 513 """ |
| 514 try: |
| 515 callable_obj(*args, **kwargs) |
| 516 except expected_exception as err: |
| 517 if isinstance(expected_regexp, str): |
| 518 expected_regexp = re.compile(expected_regexp) |
| 519 self.assertTrue( |
| 520 expected_regexp.search(str(err)), |
| 521 '"%s" does not match "%s"' % (expected_regexp.pattern, str(err))) |
| 522 else: |
| 523 self.fail(expected_exception.__name__ + ' not raised') |
| 524 |
| 525 def testChdir(self): |
| 526 """chdir should work on a directory.""" |
| 527 directory = '/foo' |
| 528 self.filesystem.CreateDirectory(directory) |
| 529 self.os.chdir(directory) |
| 530 |
| 531 def testChdirFailsNonExist(self): |
| 532 """chdir should raise OSError if the target does not exist.""" |
| 533 directory = '/no/such/directory' |
| 534 self.assertRaises(OSError, self.os.chdir, directory) |
| 535 |
| 536 def testChdirFailsNonDirectory(self): |
| 537 """chdir should raies OSError if the target is not a directory.""" |
| 538 filename = '/foo/bar' |
| 539 self.filesystem.CreateFile(filename) |
| 540 self.assertRaises(OSError, self.os.chdir, filename) |
| 541 |
| 542 def testConsecutiveChdir(self): |
| 543 """Consecutive relative chdir calls should work.""" |
| 544 dir1 = 'foo' |
| 545 dir2 = 'bar' |
| 546 full_dirname = self.os.path.join(dir1, dir2) |
| 547 self.filesystem.CreateDirectory(full_dirname) |
| 548 self.os.chdir(dir1) |
| 549 self.os.chdir(dir2) |
| 550 self.assertEqual(self.os.getcwd(), self.os.path.sep + full_dirname) |
| 551 |
| 552 def testBackwardsChdir(self): |
| 553 """chdir into '..' should behave appropriately.""" |
| 554 rootdir = self.os.getcwd() |
| 555 dirname = 'foo' |
| 556 abs_dirname = self.os.path.abspath(dirname) |
| 557 self.filesystem.CreateDirectory(dirname) |
| 558 self.os.chdir(dirname) |
| 559 self.assertEqual(abs_dirname, self.os.getcwd()) |
| 560 self.os.chdir('..') |
| 561 self.assertEqual(rootdir, self.os.getcwd()) |
| 562 self.os.chdir(self.os.path.join(dirname, '..')) |
| 563 self.assertEqual(rootdir, self.os.getcwd()) |
| 564 |
| 565 def testGetCwd(self): |
| 566 dirname = '/foo/bar' |
| 567 self.filesystem.CreateDirectory(dirname) |
| 568 self.assertEqual(self.os.getcwd(), self.os.path.sep) |
| 569 self.os.chdir(dirname) |
| 570 self.assertEqual(self.os.getcwd(), dirname) |
| 571 |
| 572 def testListdir(self): |
| 573 directory = 'xyzzy/plugh' |
| 574 files = ['foo', 'bar', 'baz'] |
| 575 for f in files: |
| 576 self.filesystem.CreateFile('%s/%s' % (directory, f)) |
| 577 files.sort() |
| 578 self.assertEqual(files, self.os.listdir(directory)) |
| 579 |
| 580 def testListdirOnSymlink(self): |
| 581 directory = 'xyzzy' |
| 582 files = ['foo', 'bar', 'baz'] |
| 583 for f in files: |
| 584 self.filesystem.CreateFile('%s/%s' % (directory, f)) |
| 585 self.filesystem.CreateLink('symlink', 'xyzzy') |
| 586 files.sort() |
| 587 self.assertEqual(files, self.os.listdir('symlink')) |
| 588 |
| 589 def testListdirError(self): |
| 590 file_path = 'foo/bar/baz' |
| 591 self.filesystem.CreateFile(file_path) |
| 592 self.assertRaises(OSError, self.os.listdir, file_path) |
| 593 |
| 594 def testExistsCurrentDir(self): |
| 595 self.assertTrue(self.filesystem.Exists('.')) |
| 596 |
| 597 def testListdirCurrent(self): |
| 598 files = ['foo', 'bar', 'baz'] |
| 599 for f in files: |
| 600 self.filesystem.CreateFile('%s' % f) |
| 601 files.sort() |
| 602 self.assertEqual(files, self.os.listdir('.')) |
| 603 |
| 604 def testFdopen(self): |
| 605 fake_open = fake_filesystem.FakeFileOpen(self.filesystem) |
| 606 file_path1 = 'some_file1' |
| 607 self.filesystem.CreateFile(file_path1, contents='contents here1') |
| 608 fake_file1 = fake_open(file_path1, 'r') |
| 609 self.assertEqual(0, fake_file1.fileno()) |
| 610 |
| 611 self.assertFalse(self.os.fdopen(0) is fake_file1) |
| 612 |
| 613 self.assertRaises(TypeError, self.os.fdopen, None) |
| 614 self.assertRaises(TypeError, self.os.fdopen, 'a string') |
| 615 |
| 616 def testOutOfRangeFdopen(self): |
| 617 # We haven't created any files, so even 0 is out of range. |
| 618 self.assertRaises(OSError, self.os.fdopen, 0) |
| 619 |
| 620 def testClosedFileDescriptor(self): |
| 621 fake_open = fake_filesystem.FakeFileOpen(self.filesystem) |
| 622 first_path = 'some_file1' |
| 623 second_path = 'some_file2' |
| 624 third_path = 'some_file3' |
| 625 self.filesystem.CreateFile(first_path, contents='contents here1') |
| 626 self.filesystem.CreateFile(second_path, contents='contents here2') |
| 627 self.filesystem.CreateFile(third_path, contents='contents here3') |
| 628 |
| 629 fake_file1 = fake_open(first_path, 'r') |
| 630 fake_file2 = fake_open(second_path, 'r') |
| 631 fake_file3 = fake_open(third_path, 'r') |
| 632 self.assertEqual(0, fake_file1.fileno()) |
| 633 self.assertEqual(1, fake_file2.fileno()) |
| 634 self.assertEqual(2, fake_file3.fileno()) |
| 635 |
| 636 fileno2 = fake_file2.fileno() |
| 637 self.os.close(fileno2) |
| 638 self.assertRaises(OSError, self.os.close, fileno2) |
| 639 self.assertEqual(0, fake_file1.fileno()) |
| 640 self.assertEqual(2, fake_file3.fileno()) |
| 641 |
| 642 self.assertFalse(self.os.fdopen(0) is fake_file1) |
| 643 self.assertFalse(self.os.fdopen(2) is fake_file3) |
| 644 self.assertRaises(OSError, self.os.fdopen, 1) |
| 645 |
| 646 def testFdopenMode(self): |
| 647 fake_open = fake_filesystem.FakeFileOpen(self.filesystem) |
| 648 file_path1 = 'some_file1' |
| 649 self.filesystem.CreateFile(file_path1, contents='contents here1', |
| 650 st_mode=((stat.S_IFREG | 0o666) ^ stat.S_IWRITE)) |
| 651 |
| 652 fake_file1 = fake_open(file_path1, 'r') |
| 653 self.assertEqual(0, fake_file1.fileno()) |
| 654 self.os.fdopen(0) |
| 655 self.os.fdopen(0, mode='r') |
| 656 exception = OSError if sys.version_info < (3, 0) else IOError |
| 657 self.assertRaises(exception, self.os.fdopen, 0, 'w') |
| 658 |
| 659 def testLowLevelOpenCreate(self): |
| 660 file_path = 'file1' |
| 661 # this is the low-level open, not FakeFileOpen |
| 662 fileno = self.os.open(file_path, self.os.O_CREAT) |
| 663 self.assertEqual(0, fileno) |
| 664 self.assertTrue(self.os.path.exists(file_path)) |
| 665 |
| 666 def testLowLevelOpenCreateMode(self): |
| 667 file_path = 'file1' |
| 668 fileno = self.os.open(file_path, self.os.O_CREAT, 0o700) |
| 669 self.assertEqual(0, fileno) |
| 670 self.assertTrue(self.os.path.exists(file_path)) |
| 671 self.assertModeEqual(0o700, self.os.stat(file_path).st_mode) |
| 672 |
| 673 def testLowLevelOpenCreateModeUnsupported(self): |
| 674 file_path = 'file1' |
| 675 fake_flag = 0b100000000000000000000000 |
| 676 self.assertRaises(NotImplementedError, self.os.open, file_path, fake_flag) |
| 677 |
| 678 def testLowLevelWriteRead(self): |
| 679 file_path = 'file1' |
| 680 self.filesystem.CreateFile(file_path, contents='orig contents') |
| 681 new_contents = '1234567890abcdef' |
| 682 fake_open = fake_filesystem.FakeFileOpen(self.filesystem) |
| 683 |
| 684 fh = fake_open(file_path, 'w') |
| 685 fileno = fh.fileno() |
| 686 |
| 687 self.assertEqual(len(new_contents), self.os.write(fileno, new_contents)) |
| 688 self.assertEqual(new_contents, |
| 689 self.filesystem.GetObject(file_path).contents) |
| 690 self.os.close(fileno) |
| 691 |
| 692 fh = fake_open(file_path, 'r') |
| 693 fileno = fh.fileno() |
| 694 self.assertEqual('', self.os.read(fileno, 0)) |
| 695 self.assertEqual(new_contents[0:2], self.os.read(fileno, 2)) |
| 696 self.assertEqual(new_contents[2:10], self.os.read(fileno, 8)) |
| 697 self.assertEqual(new_contents[10:], self.os.read(fileno, 100)) |
| 698 self.assertEqual('', self.os.read(fileno, 10)) |
| 699 self.os.close(fileno) |
| 700 |
| 701 self.assertRaises(OSError, self.os.write, fileno, new_contents) |
| 702 self.assertRaises(OSError, self.os.read, fileno, 10) |
| 703 |
| 704 def testFstat(self): |
| 705 directory = 'xyzzy' |
| 706 file_path = '%s/plugh' % directory |
| 707 self.filesystem.CreateFile(file_path, contents='ABCDE') |
| 708 fake_open = fake_filesystem.FakeFileOpen(self.filesystem) |
| 709 file_obj = fake_open(file_path) |
| 710 fileno = file_obj.fileno() |
| 711 self.assertTrue(stat.S_IFREG & self.os.fstat(fileno)[stat.ST_MODE]) |
| 712 self.assertTrue(stat.S_IFREG & self.os.fstat(fileno).st_mode) |
| 713 self.assertEqual(5, self.os.fstat(fileno)[stat.ST_SIZE]) |
| 714 |
| 715 def testStat(self): |
| 716 directory = 'xyzzy' |
| 717 file_path = '%s/plugh' % directory |
| 718 self.filesystem.CreateFile(file_path, contents='ABCDE') |
| 719 self.assertTrue(stat.S_IFDIR & self.os.stat(directory)[stat.ST_MODE]) |
| 720 self.assertTrue(stat.S_IFREG & self.os.stat(file_path)[stat.ST_MODE]) |
| 721 self.assertTrue(stat.S_IFREG & self.os.stat(file_path).st_mode) |
| 722 self.assertEqual(5, self.os.stat(file_path)[stat.ST_SIZE]) |
| 723 |
| 724 def testLstat(self): |
| 725 directory = 'xyzzy' |
| 726 base_name = 'plugh' |
| 727 file_contents = 'frobozz' |
| 728 # Just make sure we didn't accidentally make our test data meaningless. |
| 729 self.assertNotEqual(len(base_name), len(file_contents)) |
| 730 file_path = '%s/%s' % (directory, base_name) |
| 731 link_path = '%s/link' % directory |
| 732 self.filesystem.CreateFile(file_path, contents=file_contents) |
| 733 self.filesystem.CreateLink(link_path, base_name) |
| 734 self.assertEqual(len(file_contents), self.os.lstat(file_path)[stat.ST_SIZE]) |
| 735 self.assertEqual(len(base_name), self.os.lstat(link_path)[stat.ST_SIZE]) |
| 736 |
| 737 def testStatNonExistentFile(self): |
| 738 # set up |
| 739 file_path = '/non/existent/file' |
| 740 self.assertFalse(self.filesystem.Exists(file_path)) |
| 741 # actual tests |
| 742 try: |
| 743 # Use try-catch to check exception attributes. |
| 744 self.os.stat(file_path) |
| 745 self.fail('Exception is expected.') # COV_NF_LINE |
| 746 except OSError as os_error: |
| 747 self.assertEqual(errno.ENOENT, os_error.errno) |
| 748 self.assertEqual(file_path, os_error.filename) |
| 749 |
| 750 def testReadlink(self): |
| 751 link_path = 'foo/bar/baz' |
| 752 target = 'tarJAY' |
| 753 self.filesystem.CreateLink(link_path, target) |
| 754 self.assertEqual(self.os.readlink(link_path), target) |
| 755 |
| 756 def testReadlinkRaisesIfPathIsNotALink(self): |
| 757 file_path = 'foo/bar/eleventyone' |
| 758 self.filesystem.CreateFile(file_path) |
| 759 self.assertRaises(OSError, self.os.readlink, file_path) |
| 760 |
| 761 def testReadlinkRaisesIfPathDoesNotExist(self): |
| 762 self.assertRaises(OSError, self.os.readlink, '/this/path/does/not/exist') |
| 763 |
| 764 def testReadlinkRaisesIfPathIsNone(self): |
| 765 self.assertRaises(TypeError, self.os.readlink, None) |
| 766 |
| 767 def testReadlinkWithLinksInPath(self): |
| 768 self.filesystem.CreateLink('/meyer/lemon/pie', 'yum') |
| 769 self.filesystem.CreateLink('/geo/metro', '/meyer') |
| 770 self.assertEqual('yum', self.os.readlink('/geo/metro/lemon/pie')) |
| 771 |
| 772 def testReadlinkWithChainedLinksInPath(self): |
| 773 self.filesystem.CreateLink('/eastern/european/wolfhounds/chase', 'cats') |
| 774 self.filesystem.CreateLink('/russian', '/eastern/european') |
| 775 self.filesystem.CreateLink('/dogs', '/russian/wolfhounds') |
| 776 self.assertEqual('cats', self.os.readlink('/dogs/chase')) |
| 777 |
| 778 def testRemoveDir(self): |
| 779 directory = 'xyzzy' |
| 780 dir_path = '/%s/plugh' % directory |
| 781 self.filesystem.CreateDirectory(dir_path) |
| 782 self.assertTrue(self.filesystem.Exists(dir_path)) |
| 783 self.assertRaises(OSError, self.os.remove, dir_path) |
| 784 self.assertTrue(self.filesystem.Exists(dir_path)) |
| 785 self.os.chdir(directory) |
| 786 self.assertRaises(OSError, self.os.remove, 'plugh') |
| 787 self.assertTrue(self.filesystem.Exists(dir_path)) |
| 788 self.assertRaises(OSError, self.os.remove, '/plugh') |
| 789 |
| 790 def testRemoveFile(self): |
| 791 directory = 'zzy' |
| 792 file_path = '%s/plugh' % directory |
| 793 self.filesystem.CreateFile(file_path) |
| 794 self.assertTrue(self.filesystem.Exists(file_path)) |
| 795 self.os.remove(file_path) |
| 796 self.assertFalse(self.filesystem.Exists(file_path)) |
| 797 |
| 798 def testRemoveFileNoDirectory(self): |
| 799 directory = 'zzy' |
| 800 file_name = 'plugh' |
| 801 file_path = '%s/%s' % (directory, file_name) |
| 802 self.filesystem.CreateFile(file_path) |
| 803 self.assertTrue(self.filesystem.Exists(file_path)) |
| 804 self.os.chdir(directory) |
| 805 self.os.remove(file_name) |
| 806 self.assertFalse(self.filesystem.Exists(file_path)) |
| 807 |
| 808 def testRemoveFileRelativePath(self): |
| 809 original_dir = self.os.getcwd() |
| 810 directory = 'zzy' |
| 811 subdirectory = self.os.path.join(directory, directory) |
| 812 file_name = 'plugh' |
| 813 file_path = '%s/%s' % (directory, file_name) |
| 814 file_path_relative = self.os.path.join('..', file_name) |
| 815 self.filesystem.CreateFile(file_path) |
| 816 self.assertTrue(self.filesystem.Exists(file_path)) |
| 817 self.filesystem.CreateDirectory(subdirectory) |
| 818 self.assertTrue(self.filesystem.Exists(subdirectory)) |
| 819 self.os.chdir(subdirectory) |
| 820 self.os.remove(file_path_relative) |
| 821 self.assertFalse(self.filesystem.Exists(file_path_relative)) |
| 822 self.os.chdir(original_dir) |
| 823 self.assertFalse(self.filesystem.Exists(file_path)) |
| 824 |
| 825 def testRemoveDirRaisesError(self): |
| 826 directory = 'zzy' |
| 827 self.filesystem.CreateDirectory(directory) |
| 828 self.assertRaises(OSError, |
| 829 self.os.remove, |
| 830 directory) |
| 831 |
| 832 def testRemoveSymlinkToDir(self): |
| 833 directory = 'zzy' |
| 834 link = 'link_to_dir' |
| 835 self.filesystem.CreateDirectory(directory) |
| 836 self.os.symlink(directory, link) |
| 837 self.assertTrue(self.filesystem.Exists(directory)) |
| 838 self.assertTrue(self.filesystem.Exists(link)) |
| 839 self.os.remove(link) |
| 840 self.assertTrue(self.filesystem.Exists(directory)) |
| 841 self.assertFalse(self.filesystem.Exists(link)) |
| 842 |
| 843 def testUnlink(self): |
| 844 self.assertTrue(self.os.unlink == self.os.remove) |
| 845 |
| 846 def testUnlinkRaisesIfNotExist(self): |
| 847 file_path = '/file/does/not/exist' |
| 848 self.assertFalse(self.filesystem.Exists(file_path)) |
| 849 self.assertRaises(OSError, self.os.unlink, file_path) |
| 850 |
| 851 def testRenameToNonexistentFile(self): |
| 852 """Can rename a file to an unused name.""" |
| 853 directory = 'xyzzy' |
| 854 old_file_path = '%s/plugh_old' % directory |
| 855 new_file_path = '%s/plugh_new' % directory |
| 856 self.filesystem.CreateFile(old_file_path, contents='test contents') |
| 857 self.assertTrue(self.filesystem.Exists(old_file_path)) |
| 858 self.assertFalse(self.filesystem.Exists(new_file_path)) |
| 859 self.os.rename(old_file_path, new_file_path) |
| 860 self.assertFalse(self.filesystem.Exists(old_file_path)) |
| 861 self.assertTrue(self.filesystem.Exists(new_file_path)) |
| 862 self.assertEqual('test contents', |
| 863 self.filesystem.GetObject(new_file_path).contents) |
| 864 |
| 865 def testRenameDirectory(self): |
| 866 """Can rename a directory to an unused name.""" |
| 867 for old_path, new_path in [('wxyyw', 'xyzzy'), ('/abccb', 'cdeed')]: |
| 868 self.filesystem.CreateFile('%s/plugh' % old_path, contents='test') |
| 869 self.assertTrue(self.filesystem.Exists(old_path)) |
| 870 self.assertFalse(self.filesystem.Exists(new_path)) |
| 871 self.os.rename(old_path, new_path) |
| 872 self.assertFalse(self.filesystem.Exists(old_path)) |
| 873 self.assertTrue(self.filesystem.Exists(new_path)) |
| 874 self.assertEqual( |
| 875 'test', self.filesystem.GetObject('%s/plugh' % new_path).contents) |
| 876 |
| 877 def testRenameToExistentFile(self): |
| 878 """Can rename a file to a used name.""" |
| 879 directory = 'xyzzy' |
| 880 old_file_path = '%s/plugh_old' % directory |
| 881 new_file_path = '%s/plugh_new' % directory |
| 882 self.filesystem.CreateFile(old_file_path, contents='test contents 1') |
| 883 self.filesystem.CreateFile(new_file_path, contents='test contents 2') |
| 884 self.assertTrue(self.filesystem.Exists(old_file_path)) |
| 885 self.assertTrue(self.filesystem.Exists(new_file_path)) |
| 886 self.os.rename(old_file_path, new_file_path) |
| 887 self.assertFalse(self.filesystem.Exists(old_file_path)) |
| 888 self.assertTrue(self.filesystem.Exists(new_file_path)) |
| 889 self.assertEqual('test contents 1', |
| 890 self.filesystem.GetObject(new_file_path).contents) |
| 891 |
| 892 def testRenameToNonexistentDir(self): |
| 893 """Can rename a file to a name in a nonexistent dir.""" |
| 894 directory = 'xyzzy' |
| 895 old_file_path = '%s/plugh_old' % directory |
| 896 new_file_path = '%s/no_such_path/plugh_new' % directory |
| 897 self.filesystem.CreateFile(old_file_path, contents='test contents') |
| 898 self.assertTrue(self.filesystem.Exists(old_file_path)) |
| 899 self.assertFalse(self.filesystem.Exists(new_file_path)) |
| 900 self.assertRaises(IOError, self.os.rename, old_file_path, new_file_path) |
| 901 self.assertTrue(self.filesystem.Exists(old_file_path)) |
| 902 self.assertFalse(self.filesystem.Exists(new_file_path)) |
| 903 self.assertEqual('test contents', |
| 904 self.filesystem.GetObject(old_file_path).contents) |
| 905 |
| 906 def testRenameNonexistentFileShouldRaiseError(self): |
| 907 """Can't rename a file that doesn't exist.""" |
| 908 self.assertRaises(OSError, |
| 909 self.os.rename, |
| 910 'nonexistent-foo', |
| 911 'doesn\'t-matter-bar') |
| 912 |
| 913 def testRenameEmptyDir(self): |
| 914 """Test a rename of an empty directory.""" |
| 915 directory = 'xyzzy' |
| 916 before_dir = '%s/empty' % directory |
| 917 after_dir = '%s/unused' % directory |
| 918 self.filesystem.CreateDirectory(before_dir) |
| 919 self.assertTrue(self.filesystem.Exists('%s/.' % before_dir)) |
| 920 self.assertFalse(self.filesystem.Exists(after_dir)) |
| 921 self.os.rename(before_dir, after_dir) |
| 922 self.assertFalse(self.filesystem.Exists(before_dir)) |
| 923 self.assertTrue(self.filesystem.Exists('%s/.' % after_dir)) |
| 924 |
| 925 def testRenameDir(self): |
| 926 """Test a rename of a directory.""" |
| 927 directory = 'xyzzy' |
| 928 before_dir = '%s/before' % directory |
| 929 before_file = '%s/before/file' % directory |
| 930 after_dir = '%s/after' % directory |
| 931 after_file = '%s/after/file' % directory |
| 932 self.filesystem.CreateDirectory(before_dir) |
| 933 self.filesystem.CreateFile(before_file, contents='payload') |
| 934 self.assertTrue(self.filesystem.Exists(before_dir)) |
| 935 self.assertTrue(self.filesystem.Exists(before_file)) |
| 936 self.assertFalse(self.filesystem.Exists(after_dir)) |
| 937 self.assertFalse(self.filesystem.Exists(after_file)) |
| 938 self.os.rename(before_dir, after_dir) |
| 939 self.assertFalse(self.filesystem.Exists(before_dir)) |
| 940 self.assertFalse(self.filesystem.Exists(before_file)) |
| 941 self.assertTrue(self.filesystem.Exists(after_dir)) |
| 942 self.assertTrue(self.filesystem.Exists(after_file)) |
| 943 self.assertEqual('payload', |
| 944 self.filesystem.GetObject(after_file).contents) |
| 945 |
| 946 def testRenamePreservesStat(self): |
| 947 """Test if rename preserves mtime.""" |
| 948 directory = 'xyzzy' |
| 949 old_file_path = '%s/plugh_old' % directory |
| 950 new_file_path = '%s/plugh_new' % directory |
| 951 old_file = self.filesystem.CreateFile(old_file_path) |
| 952 old_file.SetMTime(old_file.st_mtime - 3600) |
| 953 self.os.chown(old_file_path, 200, 200) |
| 954 self.os.chmod(old_file_path, 0o222) |
| 955 new_file = self.filesystem.CreateFile(new_file_path) |
| 956 self.assertNotEqual(new_file.st_mtime, old_file.st_mtime) |
| 957 self.os.rename(old_file_path, new_file_path) |
| 958 new_file = self.filesystem.GetObject(new_file_path) |
| 959 self.assertEqual(new_file.st_mtime, old_file.st_mtime) |
| 960 self.assertEqual(new_file.st_mode, old_file.st_mode) |
| 961 self.assertEqual(new_file.st_uid, old_file.st_uid) |
| 962 self.assertEqual(new_file.st_gid, old_file.st_gid) |
| 963 |
| 964 def testRenameSameFilenames(self): |
| 965 """Test renaming when old and new names are the same.""" |
| 966 directory = 'xyzzy' |
| 967 file_contents = 'Spam eggs' |
| 968 file_path = '%s/eggs' % directory |
| 969 self.filesystem.CreateFile(file_path, contents=file_contents) |
| 970 self.os.rename(file_path, file_path) |
| 971 self.assertEqual(file_contents, |
| 972 self.filesystem.GetObject(file_path).contents) |
| 973 |
| 974 def testRmdir(self): |
| 975 """Can remove a directory.""" |
| 976 directory = 'xyzzy' |
| 977 sub_dir = '/xyzzy/abccd' |
| 978 other_dir = '/xyzzy/cdeed' |
| 979 self.filesystem.CreateDirectory(directory) |
| 980 self.assertTrue(self.filesystem.Exists(directory)) |
| 981 self.os.rmdir(directory) |
| 982 self.assertFalse(self.filesystem.Exists(directory)) |
| 983 self.filesystem.CreateDirectory(sub_dir) |
| 984 self.filesystem.CreateDirectory(other_dir) |
| 985 self.os.chdir(sub_dir) |
| 986 self.os.rmdir('../cdeed') |
| 987 self.assertFalse(self.filesystem.Exists(other_dir)) |
| 988 self.os.chdir('..') |
| 989 self.os.rmdir('abccd') |
| 990 self.assertFalse(self.filesystem.Exists(sub_dir)) |
| 991 |
| 992 def testRmdirRaisesIfNotEmpty(self): |
| 993 """Raises an exception if the target directory is not empty.""" |
| 994 directory = 'xyzzy' |
| 995 file_path = '%s/plugh' % directory |
| 996 self.filesystem.CreateFile(file_path) |
| 997 self.assertTrue(self.filesystem.Exists(file_path)) |
| 998 self.assertRaises(OSError, self.os.rmdir, directory) |
| 999 |
| 1000 def testRmdirRaisesIfNotDirectory(self): |
| 1001 """Raises an exception if the target is not a directory.""" |
| 1002 directory = 'xyzzy' |
| 1003 file_path = '%s/plugh' % directory |
| 1004 self.filesystem.CreateFile(file_path) |
| 1005 self.assertTrue(self.filesystem.Exists(file_path)) |
| 1006 self.assertRaises(OSError, self.os.rmdir, file_path) |
| 1007 self.assertRaises(OSError, self.os.rmdir, '.') |
| 1008 |
| 1009 def testRmdirRaisesIfNotExist(self): |
| 1010 """Raises an exception if the target does not exist.""" |
| 1011 directory = 'xyzzy' |
| 1012 self.assertFalse(self.filesystem.Exists(directory)) |
| 1013 self.assertRaises(OSError, self.os.rmdir, directory) |
| 1014 |
| 1015 def RemovedirsCheck(self, directory): |
| 1016 self.assertTrue(self.filesystem.Exists(directory)) |
| 1017 self.os.removedirs(directory) |
| 1018 return not self.filesystem.Exists(directory) |
| 1019 |
| 1020 def testRemovedirs(self): |
| 1021 data = ['test1', 'test1/test2', 'test1/extra', 'test1/test2/test3'] |
| 1022 for directory in data: |
| 1023 self.filesystem.CreateDirectory(directory) |
| 1024 self.assertTrue(self.filesystem.Exists(directory)) |
| 1025 self.assertRaises(OSError, self.RemovedirsCheck, data[0]) |
| 1026 self.assertRaises(OSError, self.RemovedirsCheck, data[1]) |
| 1027 |
| 1028 self.assertTrue(self.RemovedirsCheck(data[3])) |
| 1029 self.assertTrue(self.filesystem.Exists(data[0])) |
| 1030 self.assertFalse(self.filesystem.Exists(data[1])) |
| 1031 self.assertTrue(self.filesystem.Exists(data[2])) |
| 1032 |
| 1033 # Should raise because '/test1/extra' is all that is left, and |
| 1034 # removedirs('/test1/extra') will eventually try to rmdir('/'). |
| 1035 self.assertRaises(OSError, self.RemovedirsCheck, data[2]) |
| 1036 |
| 1037 # However, it will still delete '/test1') in the process. |
| 1038 self.assertFalse(self.filesystem.Exists(data[0])) |
| 1039 |
| 1040 self.filesystem.CreateDirectory('test1/test2') |
| 1041 # Add this to the root directory to avoid raising an exception. |
| 1042 self.filesystem.CreateDirectory('test3') |
| 1043 self.assertTrue(self.RemovedirsCheck('test1/test2')) |
| 1044 self.assertFalse(self.filesystem.Exists('test1/test2')) |
| 1045 self.assertFalse(self.filesystem.Exists('test1')) |
| 1046 |
| 1047 def testRemovedirsRaisesIfRemovingRoot(self): |
| 1048 """Raises exception if asked to remove '/'.""" |
| 1049 directory = '/' |
| 1050 self.assertTrue(self.filesystem.Exists(directory)) |
| 1051 self.assertRaises(OSError, self.os.removedirs, directory) |
| 1052 |
| 1053 def testRemovedirsRaisesIfCascadeRemovingRoot(self): |
| 1054 """Raises exception if asked to remove '/' as part of a larger operation. |
| 1055 |
| 1056 All of other directories should still be removed, though. |
| 1057 """ |
| 1058 directory = '/foo/bar/' |
| 1059 self.filesystem.CreateDirectory(directory) |
| 1060 self.assertTrue(self.filesystem.Exists(directory)) |
| 1061 self.assertRaises(OSError, self.os.removedirs, directory) |
| 1062 head, unused_tail = self.os.path.split(directory) |
| 1063 while head != '/': |
| 1064 self.assertFalse(self.filesystem.Exists(directory)) |
| 1065 head, unused_tail = self.os.path.split(head) |
| 1066 |
| 1067 def testRemovedirsWithTrailingSlash(self): |
| 1068 """removedirs works on directory names with trailing slashes.""" |
| 1069 # separate this case from the removing-root-directory case |
| 1070 self.filesystem.CreateDirectory('/baz') |
| 1071 directory = '/foo/bar/' |
| 1072 self.filesystem.CreateDirectory(directory) |
| 1073 self.assertTrue(self.filesystem.Exists(directory)) |
| 1074 self.os.removedirs(directory) |
| 1075 self.assertFalse(self.filesystem.Exists(directory)) |
| 1076 |
| 1077 def testMkdir(self): |
| 1078 """mkdir can create a relative directory.""" |
| 1079 directory = 'xyzzy' |
| 1080 self.assertFalse(self.filesystem.Exists(directory)) |
| 1081 self.os.mkdir(directory) |
| 1082 self.assertTrue(self.filesystem.Exists('/%s' % directory)) |
| 1083 self.os.chdir(directory) |
| 1084 self.os.mkdir(directory) |
| 1085 self.assertTrue(self.filesystem.Exists('/%s/%s' % (directory, directory))) |
| 1086 self.os.chdir(directory) |
| 1087 self.os.mkdir('../abccb') |
| 1088 self.assertTrue(self.filesystem.Exists('/%s/abccb' % directory)) |
| 1089 |
| 1090 def testMkdirWithTrailingSlash(self): |
| 1091 """mkdir can create a directory named with a trailing slash.""" |
| 1092 directory = '/foo/' |
| 1093 self.assertFalse(self.filesystem.Exists(directory)) |
| 1094 self.os.mkdir(directory) |
| 1095 self.assertTrue(self.filesystem.Exists(directory)) |
| 1096 self.assertTrue(self.filesystem.Exists('/foo')) |
| 1097 |
| 1098 def testMkdirRaisesIfEmptyDirectoryName(self): |
| 1099 """mkdir raises exeption if creating directory named ''.""" |
| 1100 directory = '' |
| 1101 self.assertRaises(OSError, self.os.mkdir, directory) |
| 1102 |
| 1103 def testMkdirRaisesIfNoParent(self): |
| 1104 """mkdir raises exception if parent directory does not exist.""" |
| 1105 parent = 'xyzzy' |
| 1106 directory = '%s/foo' % (parent,) |
| 1107 self.assertFalse(self.filesystem.Exists(parent)) |
| 1108 self.assertRaises(Exception, self.os.mkdir, directory) |
| 1109 |
| 1110 def testMkdirRaisesIfDirectoryExists(self): |
| 1111 """mkdir raises exception if directory already exists.""" |
| 1112 directory = 'xyzzy' |
| 1113 self.filesystem.CreateDirectory(directory) |
| 1114 self.assertTrue(self.filesystem.Exists(directory)) |
| 1115 self.assertRaises(Exception, self.os.mkdir, directory) |
| 1116 |
| 1117 def testMkdirRaisesIfFileExists(self): |
| 1118 """mkdir raises exception if name already exists as a file.""" |
| 1119 directory = 'xyzzy' |
| 1120 file_path = '%s/plugh' % directory |
| 1121 self.filesystem.CreateFile(file_path) |
| 1122 self.assertTrue(self.filesystem.Exists(file_path)) |
| 1123 self.assertRaises(Exception, self.os.mkdir, file_path) |
| 1124 |
| 1125 def testMkdirRaisesWithSlashDot(self): |
| 1126 """mkdir raises exception if mkdir foo/. (trailing /.).""" |
| 1127 self.assertRaises(Exception, self.os.mkdir, '/.') |
| 1128 directory = '/xyzzy/.' |
| 1129 self.assertRaises(Exception, self.os.mkdir, directory) |
| 1130 self.filesystem.CreateDirectory('/xyzzy') |
| 1131 self.assertRaises(Exception, self.os.mkdir, directory) |
| 1132 |
| 1133 def testMkdirRaisesWithDoubleDots(self): |
| 1134 """mkdir raises exception if mkdir foo/foo2/../foo3.""" |
| 1135 self.assertRaises(Exception, self.os.mkdir, '/..') |
| 1136 directory = '/xyzzy/dir1/dir2/../../dir3' |
| 1137 self.assertRaises(Exception, self.os.mkdir, directory) |
| 1138 self.filesystem.CreateDirectory('/xyzzy') |
| 1139 self.assertRaises(Exception, self.os.mkdir, directory) |
| 1140 self.filesystem.CreateDirectory('/xyzzy/dir1') |
| 1141 self.assertRaises(Exception, self.os.mkdir, directory) |
| 1142 self.filesystem.CreateDirectory('/xyzzy/dir1/dir2') |
| 1143 self.os.mkdir(directory) |
| 1144 self.assertTrue(self.filesystem.Exists(directory)) |
| 1145 directory = '/xyzzy/dir1/..' |
| 1146 self.assertRaises(Exception, self.os.mkdir, directory) |
| 1147 |
| 1148 def testMkdirRaisesIfParentIsReadOnly(self): |
| 1149 """mkdir raises exception if parent is read only.""" |
| 1150 directory = '/a' |
| 1151 self.os.mkdir(directory) |
| 1152 |
| 1153 # Change directory permissions to be read only. |
| 1154 self.os.chmod(directory, 0o400) |
| 1155 |
| 1156 directory = '/a/b' |
| 1157 self.assertRaises(Exception, self.os.mkdir, directory) |
| 1158 |
| 1159 def testMakedirs(self): |
| 1160 """makedirs can create a directory even in parent does not exist.""" |
| 1161 parent = 'xyzzy' |
| 1162 directory = '%s/foo' % (parent,) |
| 1163 self.assertFalse(self.filesystem.Exists(parent)) |
| 1164 self.os.makedirs(directory) |
| 1165 self.assertTrue(self.filesystem.Exists(parent)) |
| 1166 |
| 1167 def testMakedirsRaisesIfParentIsFile(self): |
| 1168 """makedirs raises exception if a parent component exists as a file.""" |
| 1169 file_path = 'xyzzy' |
| 1170 directory = '%s/plugh' % file_path |
| 1171 self.filesystem.CreateFile(file_path) |
| 1172 self.assertTrue(self.filesystem.Exists(file_path)) |
| 1173 self.assertRaises(Exception, self.os.makedirs, directory) |
| 1174 |
| 1175 def testMakedirsRaisesIfAccessDenied(self): |
| 1176 """makedirs raises exception if access denied.""" |
| 1177 directory = '/a' |
| 1178 self.os.mkdir(directory) |
| 1179 |
| 1180 # Change directory permissions to be read only. |
| 1181 self.os.chmod(directory, 0o400) |
| 1182 |
| 1183 directory = '/a/b' |
| 1184 self.assertRaises(Exception, self.os.makedirs, directory) |
| 1185 |
| 1186 def _CreateTestFile(self, path): |
| 1187 self.filesystem.CreateFile(path) |
| 1188 self.assertTrue(self.filesystem.Exists(path)) |
| 1189 st = self.os.stat(path) |
| 1190 self.assertEqual(0o666, stat.S_IMODE(st.st_mode)) |
| 1191 self.assertTrue(st.st_mode & stat.S_IFREG) |
| 1192 self.assertFalse(st.st_mode & stat.S_IFDIR) |
| 1193 |
| 1194 def _CreateTestDirectory(self, path): |
| 1195 self.filesystem.CreateDirectory(path) |
| 1196 self.assertTrue(self.filesystem.Exists(path)) |
| 1197 st = self.os.stat(path) |
| 1198 self.assertEqual(0o777, stat.S_IMODE(st.st_mode)) |
| 1199 self.assertFalse(st.st_mode & stat.S_IFREG) |
| 1200 self.assertTrue(st.st_mode & stat.S_IFDIR) |
| 1201 |
| 1202 def testAccess700(self): |
| 1203 # set up |
| 1204 path = '/some_file' |
| 1205 self._CreateTestFile(path) |
| 1206 self.os.chmod(path, 0o700) |
| 1207 self.assertModeEqual(0o700, self.os.stat(path).st_mode) |
| 1208 # actual tests |
| 1209 self.assertTrue(self.os.access(path, self.os.F_OK)) |
| 1210 self.assertTrue(self.os.access(path, self.os.R_OK)) |
| 1211 self.assertTrue(self.os.access(path, self.os.W_OK)) |
| 1212 self.assertTrue(self.os.access(path, self.os.X_OK)) |
| 1213 self.assertTrue(self.os.access(path, self.rwx)) |
| 1214 |
| 1215 def testAccess600(self): |
| 1216 # set up |
| 1217 path = '/some_file' |
| 1218 self._CreateTestFile(path) |
| 1219 self.os.chmod(path, 0o600) |
| 1220 self.assertModeEqual(0o600, self.os.stat(path).st_mode) |
| 1221 # actual tests |
| 1222 self.assertTrue(self.os.access(path, self.os.F_OK)) |
| 1223 self.assertTrue(self.os.access(path, self.os.R_OK)) |
| 1224 self.assertTrue(self.os.access(path, self.os.W_OK)) |
| 1225 self.assertFalse(self.os.access(path, self.os.X_OK)) |
| 1226 self.assertFalse(self.os.access(path, self.rwx)) |
| 1227 self.assertTrue(self.os.access(path, self.rw)) |
| 1228 |
| 1229 def testAccess400(self): |
| 1230 # set up |
| 1231 path = '/some_file' |
| 1232 self._CreateTestFile(path) |
| 1233 self.os.chmod(path, 0o400) |
| 1234 self.assertModeEqual(0o400, self.os.stat(path).st_mode) |
| 1235 # actual tests |
| 1236 self.assertTrue(self.os.access(path, self.os.F_OK)) |
| 1237 self.assertTrue(self.os.access(path, self.os.R_OK)) |
| 1238 self.assertFalse(self.os.access(path, self.os.W_OK)) |
| 1239 self.assertFalse(self.os.access(path, self.os.X_OK)) |
| 1240 self.assertFalse(self.os.access(path, self.rwx)) |
| 1241 self.assertFalse(self.os.access(path, self.rw)) |
| 1242 |
| 1243 def testAccessNonExistentFile(self): |
| 1244 # set up |
| 1245 path = '/non/existent/file' |
| 1246 self.assertFalse(self.filesystem.Exists(path)) |
| 1247 # actual tests |
| 1248 self.assertFalse(self.os.access(path, self.os.F_OK)) |
| 1249 self.assertFalse(self.os.access(path, self.os.R_OK)) |
| 1250 self.assertFalse(self.os.access(path, self.os.W_OK)) |
| 1251 self.assertFalse(self.os.access(path, self.os.X_OK)) |
| 1252 self.assertFalse(self.os.access(path, self.rwx)) |
| 1253 self.assertFalse(self.os.access(path, self.rw)) |
| 1254 |
| 1255 def testChmod(self): |
| 1256 # set up |
| 1257 path = '/some_file' |
| 1258 self._CreateTestFile(path) |
| 1259 # actual tests |
| 1260 self.os.chmod(path, 0o6543) |
| 1261 st = self.os.stat(path) |
| 1262 self.assertModeEqual(0o6543, st.st_mode) |
| 1263 self.assertTrue(st.st_mode & stat.S_IFREG) |
| 1264 self.assertFalse(st.st_mode & stat.S_IFDIR) |
| 1265 |
| 1266 def testChmodDir(self): |
| 1267 # set up |
| 1268 path = '/some_dir' |
| 1269 self._CreateTestDirectory(path) |
| 1270 # actual tests |
| 1271 self.os.chmod(path, 0o1234) |
| 1272 st = self.os.stat(path) |
| 1273 self.assertModeEqual(0o1234, st.st_mode) |
| 1274 self.assertFalse(st.st_mode & stat.S_IFREG) |
| 1275 self.assertTrue(st.st_mode & stat.S_IFDIR) |
| 1276 |
| 1277 def testChmodNonExistent(self): |
| 1278 # set up |
| 1279 path = '/non/existent/file' |
| 1280 self.assertFalse(self.filesystem.Exists(path)) |
| 1281 # actual tests |
| 1282 try: |
| 1283 # Use try-catch to check exception attributes. |
| 1284 self.os.chmod(path, 0o777) |
| 1285 self.fail('Exception is expected.') # COV_NF_LINE |
| 1286 except OSError as os_error: |
| 1287 self.assertEqual(errno.ENOENT, os_error.errno) |
| 1288 self.assertEqual(path, os_error.filename) |
| 1289 |
| 1290 def testChmodStCtime(self): |
| 1291 # set up |
| 1292 file_path = 'some_file' |
| 1293 self.filesystem.CreateFile(file_path) |
| 1294 self.assertTrue(self.filesystem.Exists(file_path)) |
| 1295 st = self.os.stat(file_path) |
| 1296 self.assertEqual(200, st.st_ctime) |
| 1297 # tests |
| 1298 self.os.chmod(file_path, 0o765) |
| 1299 st = self.os.stat(file_path) |
| 1300 self.assertEqual(220, st.st_ctime) |
| 1301 |
| 1302 def testUtimeSetsCurrentTimeIfArgsIsNone(self): |
| 1303 # set up |
| 1304 path = '/some_file' |
| 1305 self._CreateTestFile(path) |
| 1306 st = self.os.stat(path) |
| 1307 # 200 is the current time established in setUp(). |
| 1308 self.assertEqual(200, st.st_atime) |
| 1309 self.assertEqual(200, st.st_mtime) |
| 1310 # actual tests |
| 1311 self.os.utime(path, None) |
| 1312 st = self.os.stat(path) |
| 1313 self.assertEqual(220, st.st_atime) |
| 1314 self.assertEqual(240, st.st_mtime) |
| 1315 |
| 1316 def testUtimeSetsCurrentTimeIfArgsIsNoneWithFloats(self): |
| 1317 # set up |
| 1318 # time.time can report back floats, but it should be converted to ints |
| 1319 # since atime/ctime/mtime are all defined as seconds since epoch. |
| 1320 time.time = _GetDummyTime(200.0123, 20) |
| 1321 path = '/some_file' |
| 1322 self._CreateTestFile(path) |
| 1323 st = self.os.stat(path) |
| 1324 # 200 is the current time established above (if converted to int). |
| 1325 self.assertEqual(200, st.st_atime) |
| 1326 self.assertEqual(200, st.st_mtime) |
| 1327 # actual tests |
| 1328 self.os.utime(path, None) |
| 1329 st = self.os.stat(path) |
| 1330 self.assertEqual(220, st.st_atime) |
| 1331 self.assertEqual(240, st.st_mtime) |
| 1332 |
| 1333 def testUtimeSetsSpecifiedTime(self): |
| 1334 # set up |
| 1335 path = '/some_file' |
| 1336 self._CreateTestFile(path) |
| 1337 st = self.os.stat(path) |
| 1338 # actual tests |
| 1339 self.os.utime(path, (1, 2)) |
| 1340 st = self.os.stat(path) |
| 1341 self.assertEqual(1, st.st_atime) |
| 1342 self.assertEqual(2, st.st_mtime) |
| 1343 |
| 1344 def testUtimeDir(self): |
| 1345 # set up |
| 1346 path = '/some_dir' |
| 1347 self._CreateTestDirectory(path) |
| 1348 # actual tests |
| 1349 self.os.utime(path, (1.0, 2.0)) |
| 1350 st = self.os.stat(path) |
| 1351 self.assertEqual(1.0, st.st_atime) |
| 1352 self.assertEqual(2.0, st.st_mtime) |
| 1353 |
| 1354 def testUtimeNonExistent(self): |
| 1355 # set up |
| 1356 path = '/non/existent/file' |
| 1357 self.assertFalse(self.filesystem.Exists(path)) |
| 1358 # actual tests |
| 1359 try: |
| 1360 # Use try-catch to check exception attributes. |
| 1361 self.os.utime(path, (1, 2)) |
| 1362 self.fail('Exception is expected.') # COV_NF_LINE |
| 1363 except OSError as os_error: |
| 1364 self.assertEqual(errno.ENOENT, os_error.errno) |
| 1365 self.assertEqual(path, os_error.filename) |
| 1366 |
| 1367 def testUtimeTupleArgIsOfIncorrectLength(self): |
| 1368 # set up |
| 1369 path = '/some_dir' |
| 1370 self._CreateTestDirectory(path) |
| 1371 # actual tests |
| 1372 self.assertRaisesWithRegexpMatch( |
| 1373 TypeError, r'utime\(\) arg 2 must be a tuple \(atime, mtime\)', |
| 1374 self.os.utime, path, (1, 2, 3)) |
| 1375 |
| 1376 def testUtimeTupleArgContainsIncorrectType(self): |
| 1377 # set up |
| 1378 path = '/some_dir' |
| 1379 self._CreateTestDirectory(path) |
| 1380 # actual tests |
| 1381 self.assertRaisesWithRegexpMatch( |
| 1382 TypeError, 'an integer is required', |
| 1383 self.os.utime, path, (1, 'str')) |
| 1384 |
| 1385 def testChownExistingFile(self): |
| 1386 # set up |
| 1387 file_path = 'some_file' |
| 1388 self.filesystem.CreateFile(file_path) |
| 1389 # first set it make sure it's set |
| 1390 self.os.chown(file_path, 100, 100) |
| 1391 st = self.os.stat(file_path) |
| 1392 self.assertEqual(st[stat.ST_UID], 100) |
| 1393 self.assertEqual(st[stat.ST_GID], 100) |
| 1394 # we can make sure it changed |
| 1395 self.os.chown(file_path, 200, 200) |
| 1396 st = self.os.stat(file_path) |
| 1397 self.assertEqual(st[stat.ST_UID], 200) |
| 1398 self.assertEqual(st[stat.ST_GID], 200) |
| 1399 # setting a value to -1 leaves it unchanged |
| 1400 self.os.chown(file_path, -1, -1) |
| 1401 st = self.os.stat(file_path) |
| 1402 self.assertEqual(st[stat.ST_UID], 200) |
| 1403 self.assertEqual(st[stat.ST_GID], 200) |
| 1404 |
| 1405 def testChownNonexistingFileShouldRaiseOsError(self): |
| 1406 file_path = 'some_file' |
| 1407 self.assertFalse(self.filesystem.Exists(file_path)) |
| 1408 self.assertRaises(OSError, self.os.chown, file_path, 100, 100) |
| 1409 |
| 1410 def testClassifyDirectoryContents(self): |
| 1411 """Directory classification should work correctly.""" |
| 1412 root_directory = '/foo' |
| 1413 test_directories = ['bar1', 'baz2'] |
| 1414 test_files = ['baz1', 'bar2', 'baz3'] |
| 1415 self.filesystem.CreateDirectory(root_directory) |
| 1416 for directory in test_directories: |
| 1417 directory = self.os.path.join(root_directory, directory) |
| 1418 self.filesystem.CreateDirectory(directory) |
| 1419 for test_file in test_files: |
| 1420 test_file = self.os.path.join(root_directory, test_file) |
| 1421 self.filesystem.CreateFile(test_file) |
| 1422 |
| 1423 test_directories.sort() |
| 1424 test_files.sort() |
| 1425 generator = self.os.walk(root_directory) |
| 1426 root, dirs, files = next(generator) |
| 1427 dirs.sort() |
| 1428 files.sort() |
| 1429 self.assertEqual(root_directory, root) |
| 1430 self.assertEqual(test_directories, dirs) |
| 1431 self.assertEqual(test_files, files) |
| 1432 |
| 1433 def testClassifyDoesNotHideExceptions(self): |
| 1434 """_ClassifyDirectoryContents should not hide exceptions.""" |
| 1435 directory = '/foo' |
| 1436 self.assertEqual(False, self.filesystem.Exists(directory)) |
| 1437 self.assertRaises(OSError, self.os._ClassifyDirectoryContents, directory) |
| 1438 |
| 1439 def testWalkTopDown(self): |
| 1440 """Walk down ordering is correct.""" |
| 1441 self.filesystem.CreateFile('foo/1.txt') |
| 1442 self.filesystem.CreateFile('foo/bar1/2.txt') |
| 1443 self.filesystem.CreateFile('foo/bar1/baz/3.txt') |
| 1444 self.filesystem.CreateFile('foo/bar2/4.txt') |
| 1445 expected = [ |
| 1446 ('foo', ['bar1', 'bar2'], ['1.txt']), |
| 1447 ('foo/bar1', ['baz'], ['2.txt']), |
| 1448 ('foo/bar1/baz', [], ['3.txt']), |
| 1449 ('foo/bar2', [], ['4.txt']), |
| 1450 ] |
| 1451 self.assertEqual(expected, [step for step in self.os.walk('foo')]) |
| 1452 |
| 1453 def testWalkBottomUp(self): |
| 1454 """Walk up ordering is correct.""" |
| 1455 self.filesystem.CreateFile('foo/bar1/baz/1.txt') |
| 1456 self.filesystem.CreateFile('foo/bar1/2.txt') |
| 1457 self.filesystem.CreateFile('foo/bar2/3.txt') |
| 1458 self.filesystem.CreateFile('foo/4.txt') |
| 1459 |
| 1460 expected = [ |
| 1461 ('foo/bar1/baz', [], ['1.txt']), |
| 1462 ('foo/bar1', ['baz'], ['2.txt']), |
| 1463 ('foo/bar2', [], ['3.txt']), |
| 1464 ('foo', ['bar1', 'bar2'], ['4.txt']), |
| 1465 ] |
| 1466 self.assertEqual(expected, |
| 1467 [step for step in self.os.walk('foo', topdown=False)]) |
| 1468 |
| 1469 def testWalkRaisesIfNonExistent(self): |
| 1470 """Raises an exception when attempting to walk non-existent directory.""" |
| 1471 directory = '/foo/bar' |
| 1472 self.assertEqual(False, self.filesystem.Exists(directory)) |
| 1473 generator = self.os.walk(directory) |
| 1474 self.assertRaises(StopIteration, next, generator) |
| 1475 |
| 1476 def testWalkRaisesIfNotDirectory(self): |
| 1477 """Raises an exception when attempting to walk a non-directory.""" |
| 1478 filename = '/foo/bar' |
| 1479 self.filesystem.CreateFile(filename) |
| 1480 generator = self.os.walk(filename) |
| 1481 self.assertRaises(StopIteration, next, generator) |
| 1482 |
| 1483 def testMkNodeCanCreateAFile(self): |
| 1484 filename = 'foo' |
| 1485 self.assertFalse(self.filesystem.Exists(filename)) |
| 1486 self.os.mknod(filename) |
| 1487 self.assertTrue(self.filesystem.Exists(filename)) |
| 1488 |
| 1489 def testMkNodeRaisesIfEmptyFileName(self): |
| 1490 filename = '' |
| 1491 self.assertRaises(OSError, self.os.mknod, filename) |
| 1492 |
| 1493 def testMkNodeRaisesIfParentDirDoesntExist(self): |
| 1494 parent = 'xyzzy' |
| 1495 filename = '%s/foo' % (parent,) |
| 1496 self.assertFalse(self.filesystem.Exists(parent)) |
| 1497 self.assertRaises(OSError, self.os.mknod, filename) |
| 1498 |
| 1499 def testMkNodeRaisesIfFileExists(self): |
| 1500 filename = '/tmp/foo' |
| 1501 self.filesystem.CreateFile(filename) |
| 1502 self.assertTrue(self.filesystem.Exists(filename)) |
| 1503 self.assertRaises(OSError, self.os.mknod, filename) |
| 1504 |
| 1505 def testMkNodeRaisesIfFilenameIsDot(self): |
| 1506 filename = '/tmp/.' |
| 1507 self.assertRaises(OSError, self.os.mknod, filename) |
| 1508 |
| 1509 def testMkNodeRaisesIfFilenameIsDoubleDot(self): |
| 1510 filename = '/tmp/..' |
| 1511 self.assertRaises(OSError, self.os.mknod, filename) |
| 1512 |
| 1513 def testMknodEmptyTailForExistingFileRaises(self): |
| 1514 filename = '/tmp/foo' |
| 1515 self.filesystem.CreateFile(filename) |
| 1516 self.assertTrue(self.filesystem.Exists(filename)) |
| 1517 self.assertRaises(OSError, self.os.mknod, filename) |
| 1518 |
| 1519 def testMknodEmptyTailForNonexistentFileRaises(self): |
| 1520 filename = '/tmp/foo' |
| 1521 self.assertRaises(OSError, self.os.mknod, filename) |
| 1522 |
| 1523 def testMknodRaisesIfFilenameIsEmptyString(self): |
| 1524 filename = '' |
| 1525 self.assertRaises(OSError, self.os.mknod, filename) |
| 1526 |
| 1527 def testMknodeRaisesIfUnsupportedOptions(self): |
| 1528 filename = 'abcde' |
| 1529 self.assertRaises(OSError, self.os.mknod, filename, |
| 1530 mode=stat.S_IFCHR) |
| 1531 |
| 1532 def testMknodeRaisesIfParentIsNotADirectory(self): |
| 1533 filename1 = '/tmp/foo' |
| 1534 self.filesystem.CreateFile(filename1) |
| 1535 self.assertTrue(self.filesystem.Exists(filename1)) |
| 1536 filename2 = '/tmp/foo/bar' |
| 1537 self.assertRaises(OSError, self.os.mknod, filename2) |
| 1538 |
| 1539 def ResetErrno(self): |
| 1540 """Reset the last seen errno.""" |
| 1541 self.last_errno = False |
| 1542 |
| 1543 def StoreErrno(self, os_error): |
| 1544 """Store the last errno we saw.""" |
| 1545 self.last_errno = os_error.errno |
| 1546 |
| 1547 def GetErrno(self): |
| 1548 """Return the last errno we saw.""" |
| 1549 return self.last_errno |
| 1550 |
| 1551 def testWalkCallsOnErrorIfNonExistent(self): |
| 1552 """Calls onerror with correct errno when walking non-existent directory.""" |
| 1553 self.ResetErrno() |
| 1554 directory = '/foo/bar' |
| 1555 self.assertEqual(False, self.filesystem.Exists(directory)) |
| 1556 # Calling os.walk on a non-existent directory should trigger a call to the |
| 1557 # onerror method. We do not actually care what, if anything, is returned. |
| 1558 for unused_entry in self.os.walk(directory, onerror=self.StoreErrno): |
| 1559 pass |
| 1560 self.assertTrue(self.GetErrno() in (errno.ENOTDIR, errno.ENOENT)) |
| 1561 |
| 1562 def testWalkCallsOnErrorIfNotDirectory(self): |
| 1563 """Calls onerror with correct errno when walking non-directory.""" |
| 1564 self.ResetErrno() |
| 1565 filename = '/foo/bar' |
| 1566 self.filesystem.CreateFile(filename) |
| 1567 self.assertEqual(True, self.filesystem.Exists(filename)) |
| 1568 # Calling os.walk on a file should trigger a call to the onerror method. |
| 1569 # We do not actually care what, if anything, is returned. |
| 1570 for unused_entry in self.os.walk(filename, onerror=self.StoreErrno): |
| 1571 pass |
| 1572 self.assertTrue(self.GetErrno() in (errno.ENOTDIR, errno.EACCES)) |
| 1573 |
| 1574 def testWalkSkipsRemovedDirectories(self): |
| 1575 """Caller can modify list of directories to visit while walking.""" |
| 1576 root = '/foo' |
| 1577 visit = 'visit' |
| 1578 no_visit = 'no_visit' |
| 1579 self.filesystem.CreateFile('%s/bar' % (root,)) |
| 1580 self.filesystem.CreateFile('%s/%s/1.txt' % (root, visit)) |
| 1581 self.filesystem.CreateFile('%s/%s/2.txt' % (root, visit)) |
| 1582 self.filesystem.CreateFile('%s/%s/3.txt' % (root, no_visit)) |
| 1583 self.filesystem.CreateFile('%s/%s/4.txt' % (root, no_visit)) |
| 1584 |
| 1585 generator = self.os.walk('/foo') |
| 1586 root_contents = next(generator) |
| 1587 root_contents[1].remove(no_visit) |
| 1588 |
| 1589 visited_visit_directory = False |
| 1590 |
| 1591 for root, unused_dirs, unused_files in iter(generator): |
| 1592 self.assertEqual(False, root.endswith('/%s' % (no_visit))) |
| 1593 if root.endswith('/%s' % (visit)): |
| 1594 visited_visit_directory = True |
| 1595 |
| 1596 self.assertEqual(True, visited_visit_directory) |
| 1597 |
| 1598 def testSymlink(self): |
| 1599 file_path = 'foo/bar/baz' |
| 1600 self.os.symlink('bogus', file_path) |
| 1601 self.assertTrue(self.os.path.lexists(file_path)) |
| 1602 self.assertFalse(self.os.path.exists(file_path)) |
| 1603 self.filesystem.CreateFile('foo/bar/bogus') |
| 1604 self.assertTrue(self.os.path.lexists(file_path)) |
| 1605 self.assertTrue(self.os.path.exists(file_path)) |
| 1606 |
| 1607 def testUMask(self): |
| 1608 umask = os.umask(0o22) |
| 1609 os.umask(umask) |
| 1610 self.assertEqual(umask, self.os.umask(0o22)) |
| 1611 |
| 1612 def testMkdirUmaskApplied(self): |
| 1613 """mkdir creates a directory with umask applied.""" |
| 1614 self.os.umask(0o22) |
| 1615 self.os.mkdir('dir1') |
| 1616 self.assertModeEqual(0o755, self.os.stat('dir1').st_mode) |
| 1617 self.os.umask(0o67) |
| 1618 self.os.mkdir('dir2') |
| 1619 self.assertModeEqual(0o710, self.os.stat('dir2').st_mode) |
| 1620 |
| 1621 def testMakedirsUmaskApplied(self): |
| 1622 """makedirs creates a directories with umask applied.""" |
| 1623 self.os.umask(0o22) |
| 1624 self.os.makedirs('/p1/dir1') |
| 1625 self.assertModeEqual(0o755, self.os.stat('/p1').st_mode) |
| 1626 self.assertModeEqual(0o755, self.os.stat('/p1/dir1').st_mode) |
| 1627 self.os.umask(0o67) |
| 1628 self.os.makedirs('/p2/dir2') |
| 1629 self.assertModeEqual(0o710, self.os.stat('/p2').st_mode) |
| 1630 self.assertModeEqual(0o710, self.os.stat('/p2/dir2').st_mode) |
| 1631 |
| 1632 def testMknodeUmaskApplied(self): |
| 1633 """mkdir creates a device with umask applied.""" |
| 1634 self.os.umask(0o22) |
| 1635 self.os.mknod('nod1') |
| 1636 self.assertModeEqual(0o644, self.os.stat('nod1').st_mode) |
| 1637 self.os.umask(0o27) |
| 1638 self.os.mknod('nod2') |
| 1639 self.assertModeEqual(0o640, self.os.stat('nod2').st_mode) |
| 1640 |
| 1641 def testOpenUmaskApplied(self): |
| 1642 """open creates a file with umask applied.""" |
| 1643 fake_open = fake_filesystem.FakeFileOpen(self.filesystem) |
| 1644 self.os.umask(0o22) |
| 1645 fake_open('file1', 'w').close() |
| 1646 self.assertModeEqual(0o644, self.os.stat('file1').st_mode) |
| 1647 self.os.umask(0o27) |
| 1648 fake_open('file2', 'w').close() |
| 1649 self.assertModeEqual(0o640, self.os.stat('file2').st_mode) |
| 1650 |
| 1651 |
| 1652 class StatPropagationTest(TestCase): |
| 1653 |
| 1654 def setUp(self): |
| 1655 self.filesystem = fake_filesystem.FakeFilesystem(path_separator='/') |
| 1656 self.os = fake_filesystem.FakeOsModule(self.filesystem) |
| 1657 self.open = fake_filesystem.FakeFileOpen(self.filesystem) |
| 1658 |
| 1659 def testFileSizeUpdatedViaClose(self): |
| 1660 """test that file size gets updated via close().""" |
| 1661 file_dir = 'xyzzy' |
| 1662 file_path = 'xyzzy/close' |
| 1663 content = 'This is a test.' |
| 1664 self.os.mkdir(file_dir) |
| 1665 fh = self.open(file_path, 'w') |
| 1666 self.assertEqual(0, self.os.stat(file_path)[stat.ST_SIZE]) |
| 1667 self.assertEqual('', self.filesystem.GetObject(file_path).contents) |
| 1668 fh.write(content) |
| 1669 self.assertEqual(0, self.os.stat(file_path)[stat.ST_SIZE]) |
| 1670 self.assertEqual('', self.filesystem.GetObject(file_path).contents) |
| 1671 fh.close() |
| 1672 self.assertEqual(len(content), self.os.stat(file_path)[stat.ST_SIZE]) |
| 1673 self.assertEqual(content, self.filesystem.GetObject(file_path).contents) |
| 1674 |
| 1675 def testFileSizeNotResetAfterClose(self): |
| 1676 file_dir = 'xyzzy' |
| 1677 file_path = 'xyzzy/close' |
| 1678 self.os.mkdir(file_dir) |
| 1679 size = 1234 |
| 1680 # The file has size, but no content. When the file is opened for reading, |
| 1681 # its size should be preserved. |
| 1682 self.filesystem.CreateFile(file_path, st_size=size) |
| 1683 fh = self.open(file_path, 'r') |
| 1684 fh.close() |
| 1685 self.assertEqual(size, self.open(file_path, 'r').Size()) |
| 1686 |
| 1687 def testFileSizeAfterWrite(self): |
| 1688 file_path = 'test_file' |
| 1689 original_content = 'abcdef' |
| 1690 original_size = len(original_content) |
| 1691 self.filesystem.CreateFile(file_path, contents=original_content) |
| 1692 added_content = 'foo bar' |
| 1693 expected_size = original_size + len(added_content) |
| 1694 fh = self.open(file_path, 'a') |
| 1695 fh.write(added_content) |
| 1696 self.assertEqual(expected_size, fh.Size()) |
| 1697 fh.close() |
| 1698 self.assertEqual(expected_size, self.open(file_path, 'r').Size()) |
| 1699 |
| 1700 def testLargeFileSizeAfterWrite(self): |
| 1701 file_path = 'test_file' |
| 1702 original_content = 'abcdef' |
| 1703 original_size = len(original_content) |
| 1704 self.filesystem.CreateFile(file_path, st_size=original_size) |
| 1705 added_content = 'foo bar' |
| 1706 fh = self.open(file_path, 'a') |
| 1707 # We can't use assertRaises, because the exception is thrown |
| 1708 # in __getattr__, so just saying 'fh.write' causes the exception. |
| 1709 try: |
| 1710 fh.write(added_content) |
| 1711 except fake_filesystem.FakeLargeFileIoException: |
| 1712 return |
| 1713 self.fail('Writing to a large file should not be allowed') |
| 1714 |
| 1715 def testFileSizeUpdatedViaFlush(self): |
| 1716 """test that file size gets updated via flush().""" |
| 1717 file_dir = 'xyzzy' |
| 1718 file_name = 'flush' |
| 1719 file_path = self.os.path.join(file_dir, file_name) |
| 1720 content = 'This might be a test.' |
| 1721 self.os.mkdir(file_dir) |
| 1722 fh = self.open(file_path, 'w') |
| 1723 self.assertEqual(0, self.os.stat(file_path)[stat.ST_SIZE]) |
| 1724 self.assertEqual('', self.filesystem.GetObject(file_path).contents) |
| 1725 fh.write(content) |
| 1726 self.assertEqual(0, self.os.stat(file_path)[stat.ST_SIZE]) |
| 1727 self.assertEqual('', self.filesystem.GetObject(file_path).contents) |
| 1728 fh.flush() |
| 1729 self.assertEqual(len(content), self.os.stat(file_path)[stat.ST_SIZE]) |
| 1730 self.assertEqual(content, self.filesystem.GetObject(file_path).contents) |
| 1731 fh.close() |
| 1732 self.assertEqual(len(content), self.os.stat(file_path)[stat.ST_SIZE]) |
| 1733 self.assertEqual(content, self.filesystem.GetObject(file_path).contents) |
| 1734 |
| 1735 def testFileSizeTruncation(self): |
| 1736 """test that file size gets updated via open().""" |
| 1737 file_dir = 'xyzzy' |
| 1738 file_path = 'xyzzy/truncation' |
| 1739 content = 'AAA content.' |
| 1740 |
| 1741 # pre-create file with content |
| 1742 self.os.mkdir(file_dir) |
| 1743 fh = self.open(file_path, 'w') |
| 1744 fh.write(content) |
| 1745 fh.close() |
| 1746 self.assertEqual(len(content), self.os.stat(file_path)[stat.ST_SIZE]) |
| 1747 self.assertEqual(content, self.filesystem.GetObject(file_path).contents) |
| 1748 |
| 1749 # test file truncation |
| 1750 fh = self.open(file_path, 'w') |
| 1751 self.assertEqual(0, self.os.stat(file_path)[stat.ST_SIZE]) |
| 1752 self.assertEqual('', self.filesystem.GetObject(file_path).contents) |
| 1753 fh.close() |
| 1754 |
| 1755 |
| 1756 class OsPathInjectionRegressionTest(TestCase): |
| 1757 """Test faking os.path before calling os.walk. |
| 1758 |
| 1759 Found when investigating a problem with |
| 1760 gws/tools/labrat/rat_utils_unittest, which was faking out os.path |
| 1761 before calling os.walk. |
| 1762 """ |
| 1763 |
| 1764 def setUp(self): |
| 1765 self.filesystem = fake_filesystem.FakeFilesystem(path_separator='/') |
| 1766 self.os_path = os.path |
| 1767 # The bug was that when os.path gets faked, the FakePathModule doesn't get |
| 1768 # called in self.os.walk(). FakePathModule now insists that it is created |
| 1769 # as part of FakeOsModule. |
| 1770 self.os = fake_filesystem.FakeOsModule(self.filesystem) |
| 1771 |
| 1772 def tearDown(self): |
| 1773 os.path = self.os_path |
| 1774 |
| 1775 def testCreateTopLevelDirectory(self): |
| 1776 top_level_dir = '/x' |
| 1777 self.assertFalse(self.filesystem.Exists(top_level_dir)) |
| 1778 self.filesystem.CreateDirectory(top_level_dir) |
| 1779 self.assertTrue(self.filesystem.Exists('/')) |
| 1780 self.assertTrue(self.filesystem.Exists(top_level_dir)) |
| 1781 self.filesystem.CreateDirectory('%s/po' % top_level_dir) |
| 1782 self.filesystem.CreateFile('%s/po/control' % top_level_dir) |
| 1783 self.filesystem.CreateFile('%s/po/experiment' % top_level_dir) |
| 1784 self.filesystem.CreateDirectory('%s/gv' % top_level_dir) |
| 1785 self.filesystem.CreateFile('%s/gv/control' % top_level_dir) |
| 1786 |
| 1787 expected = [ |
| 1788 ('/', ['x'], []), |
| 1789 ('/x', ['gv', 'po'], []), |
| 1790 ('/x/gv', [], ['control']), |
| 1791 ('/x/po', [], ['control', 'experiment']), |
| 1792 ] |
| 1793 self.assertEqual(expected, [step for step in self.os.walk('/')]) |
| 1794 |
| 1795 |
| 1796 class FakePathModuleTest(TestCase): |
| 1797 def setUp(self): |
| 1798 self.orig_time = time.time |
| 1799 time.time = _GetDummyTime(10, 1) |
| 1800 self.filesystem = fake_filesystem.FakeFilesystem(path_separator='/') |
| 1801 self.os = fake_filesystem.FakeOsModule(self.filesystem) |
| 1802 self.path = self.os.path |
| 1803 |
| 1804 def tearDown(self): |
| 1805 time.time = self.orig_time |
| 1806 |
| 1807 def testAbspath(self): |
| 1808 """abspath should return a consistent representation of a file.""" |
| 1809 filename = 'foo' |
| 1810 abspath = '/%s' % filename |
| 1811 self.filesystem.CreateFile(abspath) |
| 1812 self.assertEqual(abspath, self.path.abspath(abspath)) |
| 1813 self.assertEqual(abspath, self.path.abspath(filename)) |
| 1814 self.assertEqual(abspath, self.path.abspath('../%s' % filename)) |
| 1815 |
| 1816 def testAbspathDealsWithRelativeNonRootPath(self): |
| 1817 """abspath should correctly handle relative paths from a non-/ directory. |
| 1818 |
| 1819 This test is distinct from the basic functionality test because |
| 1820 fake_filesystem has historically been based in /. |
| 1821 """ |
| 1822 filename = '/foo/bar/baz' |
| 1823 file_components = filename.split(self.path.sep) |
| 1824 basedir = '/%s' % (file_components[0],) |
| 1825 self.filesystem.CreateFile(filename) |
| 1826 self.os.chdir(basedir) |
| 1827 self.assertEqual(basedir, self.path.abspath(self.path.curdir)) |
| 1828 self.assertEqual('/', self.path.abspath('..')) |
| 1829 self.assertEqual(self.path.join(basedir, file_components[1]), |
| 1830 self.path.abspath(file_components[1])) |
| 1831 |
| 1832 def testRelpath(self): |
| 1833 path_foo = '/path/to/foo' |
| 1834 path_bar = '/path/to/bar' |
| 1835 path_other = '/some/where/else' |
| 1836 self.assertRaises(ValueError, self.path.relpath, None) |
| 1837 self.assertRaises(ValueError, self.path.relpath, '') |
| 1838 self.assertEqual(path_foo[1:], |
| 1839 self.path.relpath(path_foo)) |
| 1840 self.assertEqual('../foo', |
| 1841 self.path.relpath(path_foo, path_bar)) |
| 1842 self.assertEqual('../../..%s' % path_other, |
| 1843 self.path.relpath(path_other, path_bar)) |
| 1844 self.assertEqual('.', |
| 1845 self.path.relpath(path_bar, path_bar)) |
| 1846 |
| 1847 @unittest.skipIf(TestCase.is_windows, 'realpath does not follow symlinks in wi
n32') |
| 1848 def testRealpathVsAbspath(self): |
| 1849 self.filesystem.CreateFile('/george/washington/bridge') |
| 1850 self.filesystem.CreateLink('/first/president', '/george/washington') |
| 1851 self.assertEqual('/first/president/bridge', |
| 1852 self.os.path.abspath('/first/president/bridge')) |
| 1853 self.assertEqual('/george/washington/bridge', |
| 1854 self.os.path.realpath('/first/president/bridge')) |
| 1855 self.os.chdir('/first/president') |
| 1856 self.assertEqual('/george/washington/bridge', |
| 1857 self.os.path.realpath('bridge')) |
| 1858 |
| 1859 def testExists(self): |
| 1860 file_path = 'foo/bar/baz' |
| 1861 self.filesystem.CreateFile(file_path) |
| 1862 self.assertTrue(self.path.exists(file_path)) |
| 1863 self.assertFalse(self.path.exists('/some/other/bogus/path')) |
| 1864 |
| 1865 def testLexists(self): |
| 1866 file_path = 'foo/bar/baz' |
| 1867 self.filesystem.CreateDirectory('foo/bar') |
| 1868 self.filesystem.CreateLink(file_path, 'bogus') |
| 1869 self.assertTrue(self.path.lexists(file_path)) |
| 1870 self.assertFalse(self.path.exists(file_path)) |
| 1871 self.filesystem.CreateFile('foo/bar/bogus') |
| 1872 self.assertTrue(self.path.exists(file_path)) |
| 1873 |
| 1874 def testDirname(self): |
| 1875 dirname = 'foo/bar' |
| 1876 self.assertEqual(dirname, self.path.dirname('%s/baz' % dirname)) |
| 1877 |
| 1878 def testJoin(self): |
| 1879 components = ['foo', 'bar', 'baz'] |
| 1880 self.assertEqual('foo/bar/baz', self.path.join(*components)) |
| 1881 |
| 1882 def testExpandUser(self): |
| 1883 if self.is_windows: |
| 1884 self.assertEqual(self.path.expanduser('~'), |
| 1885 self.os.environ['USERPROFILE'].replace('\\', '/')) |
| 1886 else: |
| 1887 self.assertEqual(self.path.expanduser('~'), |
| 1888 self.os.environ['HOME']) |
| 1889 |
| 1890 @unittest.skipIf(TestCase.is_windows or TestCase.is_cygwin, |
| 1891 'only tested on unix systems') |
| 1892 def testExpandRoot(self): |
| 1893 self.assertEqual('/root', self.path.expanduser('~root')) |
| 1894 |
| 1895 def testGetsizePathNonexistent(self): |
| 1896 file_path = 'foo/bar/baz' |
| 1897 self.assertRaises(IOError, self.path.getsize, file_path) |
| 1898 |
| 1899 def testGetsizeFileEmpty(self): |
| 1900 file_path = 'foo/bar/baz' |
| 1901 self.filesystem.CreateFile(file_path) |
| 1902 self.assertEqual(0, self.path.getsize(file_path)) |
| 1903 |
| 1904 def testGetsizeFileNonZeroSize(self): |
| 1905 file_path = 'foo/bar/baz' |
| 1906 self.filesystem.CreateFile(file_path, contents='1234567') |
| 1907 self.assertEqual(7, self.path.getsize(file_path)) |
| 1908 |
| 1909 def testGetsizeDirEmpty(self): |
| 1910 # For directories, only require that the size is non-negative. |
| 1911 dir_path = 'foo/bar' |
| 1912 self.filesystem.CreateDirectory(dir_path) |
| 1913 size = self.path.getsize(dir_path) |
| 1914 self.assertFalse(int(size) < 0, |
| 1915 'expected non-negative size; actual: %s' % size) |
| 1916 |
| 1917 def testGetsizeDirNonZeroSize(self): |
| 1918 # For directories, only require that the size is non-negative. |
| 1919 dir_path = 'foo/bar' |
| 1920 self.filesystem.CreateFile(self.filesystem.JoinPaths(dir_path, 'baz')) |
| 1921 size = self.path.getsize(dir_path) |
| 1922 self.assertFalse(int(size) < 0, |
| 1923 'expected non-negative size; actual: %s' % size) |
| 1924 |
| 1925 def testIsdir(self): |
| 1926 self.filesystem.CreateFile('foo/bar') |
| 1927 self.assertTrue(self.path.isdir('foo')) |
| 1928 self.assertFalse(self.path.isdir('foo/bar')) |
| 1929 self.assertFalse(self.path.isdir('it_dont_exist')) |
| 1930 |
| 1931 def testIsdirWithCwdChange(self): |
| 1932 self.filesystem.CreateFile('/foo/bar/baz') |
| 1933 self.assertTrue(self.path.isdir('/foo')) |
| 1934 self.assertTrue(self.path.isdir('/foo/bar')) |
| 1935 self.assertTrue(self.path.isdir('foo')) |
| 1936 self.assertTrue(self.path.isdir('foo/bar')) |
| 1937 self.filesystem.cwd = '/foo' |
| 1938 self.assertTrue(self.path.isdir('/foo')) |
| 1939 self.assertTrue(self.path.isdir('/foo/bar')) |
| 1940 self.assertTrue(self.path.isdir('bar')) |
| 1941 |
| 1942 def testIsfile(self): |
| 1943 self.filesystem.CreateFile('foo/bar') |
| 1944 self.assertFalse(self.path.isfile('foo')) |
| 1945 self.assertTrue(self.path.isfile('foo/bar')) |
| 1946 self.assertFalse(self.path.isfile('it_dont_exist')) |
| 1947 |
| 1948 def testGetMtime(self): |
| 1949 test_file = self.filesystem.CreateFile('foo/bar1.txt') |
| 1950 # The root directory ('', effectively '/') is created at time 10, |
| 1951 # the parent directory ('foo') at time 11, and the file at time 12. |
| 1952 self.assertEqual(12, test_file.st_mtime) |
| 1953 test_file.SetMTime(24) |
| 1954 self.assertEqual(24, self.path.getmtime('foo/bar1.txt')) |
| 1955 |
| 1956 def testGetMtimeRaisesOSError(self): |
| 1957 self.assertFalse(self.path.exists('it_dont_exist')) |
| 1958 self.assertRaises(OSError, self.path.getmtime, 'it_dont_exist') |
| 1959 |
| 1960 def testIslink(self): |
| 1961 self.filesystem.CreateDirectory('foo') |
| 1962 self.filesystem.CreateFile('foo/regular_file') |
| 1963 self.filesystem.CreateLink('foo/link_to_file', 'regular_file') |
| 1964 self.assertFalse(self.path.islink('foo')) |
| 1965 |
| 1966 # An object can be both a link and a file or file, according to the |
| 1967 # comments in Python/Lib/posixpath.py. |
| 1968 self.assertTrue(self.path.islink('foo/link_to_file')) |
| 1969 self.assertTrue(self.path.isfile('foo/link_to_file')) |
| 1970 |
| 1971 self.assertTrue(self.path.isfile('foo/regular_file')) |
| 1972 self.assertFalse(self.path.islink('foo/regular_file')) |
| 1973 |
| 1974 self.assertFalse(self.path.islink('it_dont_exist')) |
| 1975 |
| 1976 @unittest.skipIf(sys.version_info >= (3, 0) or TestCase.is_windows, |
| 1977 'os.path.walk deprecrated in Python 3, cannot be properly ' |
| 1978 'tested in win32') |
| 1979 def testWalk(self): |
| 1980 self.filesystem.CreateFile('/foo/bar/baz') |
| 1981 self.filesystem.CreateFile('/foo/bar/xyzzy/plugh') |
| 1982 visited_nodes = [] |
| 1983 |
| 1984 def RecordVisitedNodes(visited, dirname, fnames): |
| 1985 visited.extend(((dirname, fname) for fname in fnames)) |
| 1986 |
| 1987 self.path.walk('/foo', RecordVisitedNodes, visited_nodes) |
| 1988 expected = [('/foo', 'bar'), |
| 1989 ('/foo/bar', 'baz'), |
| 1990 ('/foo/bar', 'xyzzy'), |
| 1991 ('/foo/bar/xyzzy', 'plugh')] |
| 1992 self.assertEqual(expected, visited_nodes) |
| 1993 |
| 1994 @unittest.skipIf(sys.version_info >= (3, 0) or TestCase.is_windows, |
| 1995 'os.path.walk deprecrated in Python 3, cannot be properly ' |
| 1996 'tested in win32') |
| 1997 def testWalkFromNonexistentTopDoesNotThrow(self): |
| 1998 visited_nodes = [] |
| 1999 |
| 2000 def RecordVisitedNodes(visited, dirname, fnames): |
| 2001 visited.extend(((dirname, fname) for fname in fnames)) |
| 2002 |
| 2003 self.path.walk('/foo', RecordVisitedNodes, visited_nodes) |
| 2004 self.assertEqual([], visited_nodes) |
| 2005 |
| 2006 |
| 2007 class FakeFileOpenTestBase(TestCase): |
| 2008 def setUp(self): |
| 2009 self.filesystem = fake_filesystem.FakeFilesystem(path_separator='/') |
| 2010 self.file = fake_filesystem.FakeFileOpen(self.filesystem) |
| 2011 self.open = self.file |
| 2012 self.os = fake_filesystem.FakeOsModule(self.filesystem) |
| 2013 self.orig_time = time.time |
| 2014 time.time = _GetDummyTime(100, 10) |
| 2015 |
| 2016 def tearDown(self): |
| 2017 time.time = self.orig_time |
| 2018 |
| 2019 |
| 2020 class FakeFileOpenTest(FakeFileOpenTestBase): |
| 2021 def testOpenNoParentDir(self): |
| 2022 """Expect raise when open'ing a file in a missing directory.""" |
| 2023 file_path = 'foo/bar.txt' |
| 2024 self.assertRaises(IOError, self.file, file_path, 'w') |
| 2025 |
| 2026 def testDeleteOnClose(self): |
| 2027 file_dir = 'boo' |
| 2028 file_path = 'boo/far' |
| 2029 self.os.mkdir(file_dir) |
| 2030 self.file = fake_filesystem.FakeFileOpen(self.filesystem, |
| 2031 delete_on_close=True) |
| 2032 fh = self.file(file_path, 'w') |
| 2033 self.assertTrue(self.filesystem.Exists(file_path)) |
| 2034 fh.close() |
| 2035 self.assertFalse(self.filesystem.Exists(file_path)) |
| 2036 |
| 2037 def testNoDeleteOnCloseByDefault(self): |
| 2038 file_dir = 'boo' |
| 2039 file_path = 'boo/czar' |
| 2040 self.file = fake_filesystem.FakeFileOpen(self.filesystem) |
| 2041 self.os.mkdir(file_dir) |
| 2042 fh = self.file(file_path, 'w') |
| 2043 self.assertTrue(self.filesystem.Exists(file_path)) |
| 2044 fh.close() |
| 2045 self.assertTrue(self.filesystem.Exists(file_path)) |
| 2046 |
| 2047 def testCompatibilityOfWithStatement(self): |
| 2048 self.file = fake_filesystem.FakeFileOpen(self.filesystem, |
| 2049 delete_on_close=True) |
| 2050 file_path = 'foo' |
| 2051 self.assertFalse(self.filesystem.Exists(file_path)) |
| 2052 with self.file(file_path, 'w') as _: |
| 2053 self.assertTrue(self.filesystem.Exists(file_path)) |
| 2054 # After the 'with' statement, the close() method should have been called. |
| 2055 self.assertFalse(self.filesystem.Exists(file_path)) |
| 2056 |
| 2057 def testOpenValidFile(self): |
| 2058 contents = [ |
| 2059 'I am he as\n', |
| 2060 'you are he as\n', |
| 2061 'you are me and\n', |
| 2062 'we are all together\n' |
| 2063 ] |
| 2064 file_path = 'foo/bar.txt' |
| 2065 self.filesystem.CreateFile(file_path, contents=''.join(contents)) |
| 2066 self.assertEqual(contents, self.file(file_path).readlines()) |
| 2067 |
| 2068 def testOpenValidArgs(self): |
| 2069 contents = [ |
| 2070 "Bang bang Maxwell's silver hammer\n", |
| 2071 'Came down on her head', |
| 2072 ] |
| 2073 file_path = 'abbey_road/maxwell' |
| 2074 self.filesystem.CreateFile(file_path, contents=''.join(contents)) |
| 2075 self.assertEqual( |
| 2076 contents, self.open(file_path, mode='r', buffering=1).readlines()) |
| 2077 if sys.version_info >= (3, 0): |
| 2078 self.assertEqual( |
| 2079 contents, self.open(file_path, mode='r', buffering=1, |
| 2080 encoding='utf-8', errors='strict', newline='\n', |
| 2081 closefd=False, opener=False).readlines()) |
| 2082 |
| 2083 @unittest.skipIf(sys.version_info < (3, 0), 'only tested on 3.0 or greater') |
| 2084 def testOpenNewlineArg(self): |
| 2085 file_path = 'some_file' |
| 2086 file_contents = 'two\r\nlines' |
| 2087 self.filesystem.CreateFile(file_path, contents=file_contents) |
| 2088 fake_file = self.open(file_path, mode='r', newline=None) |
| 2089 self.assertEqual(['two\n', 'lines'], fake_file.readlines()) |
| 2090 fake_file = self.open(file_path, mode='r', newline='') |
| 2091 self.assertEqual(['two\r\n', 'lines'], fake_file.readlines()) |
| 2092 fake_file = self.open(file_path, mode='r', newline='\r') |
| 2093 self.assertEqual(['two\r', '\r', 'lines'], fake_file.readlines()) |
| 2094 fake_file = self.open(file_path, mode='r', newline='\n') |
| 2095 self.assertEqual(['two\r\n', 'lines'], fake_file.readlines()) |
| 2096 fake_file = self.open(file_path, mode='r', newline='\r\n') |
| 2097 self.assertEqual(['two\r\r\n', 'lines'], fake_file.readlines()) |
| 2098 |
| 2099 def testOpenValidFileWithCwd(self): |
| 2100 contents = [ |
| 2101 'I am he as\n', |
| 2102 'you are he as\n', |
| 2103 'you are me and\n', |
| 2104 'we are all together\n' |
| 2105 ] |
| 2106 file_path = '/foo/bar.txt' |
| 2107 self.filesystem.CreateFile(file_path, contents=''.join(contents)) |
| 2108 self.filesystem.cwd = '/foo' |
| 2109 self.assertEqual(contents, self.file(file_path).readlines()) |
| 2110 |
| 2111 def testIterateOverFile(self): |
| 2112 contents = [ |
| 2113 "Bang bang Maxwell's silver hammer", |
| 2114 'Came down on her head', |
| 2115 ] |
| 2116 file_path = 'abbey_road/maxwell' |
| 2117 self.filesystem.CreateFile(file_path, contents='\n'.join(contents)) |
| 2118 result = [line.rstrip() for line in self.file(file_path)] |
| 2119 self.assertEqual(contents, result) |
| 2120 |
| 2121 def testOpenDirectoryError(self): |
| 2122 directory_path = 'foo/bar' |
| 2123 self.filesystem.CreateDirectory(directory_path) |
| 2124 self.assertRaises(IOError, self.file.__call__, directory_path) |
| 2125 |
| 2126 def testCreateFileWithWrite(self): |
| 2127 contents = [ |
| 2128 "Here comes the sun, little darlin'", |
| 2129 'Here comes the sun, and I say,', |
| 2130 "It's alright", |
| 2131 ] |
| 2132 file_dir = 'abbey_road' |
| 2133 file_path = 'abbey_road/here_comes_the_sun' |
| 2134 self.os.mkdir(file_dir) |
| 2135 fake_file = self.file(file_path, 'w') |
| 2136 for line in contents: |
| 2137 fake_file.write(line + '\n') |
| 2138 fake_file.close() |
| 2139 result = [line.rstrip() for line in self.file(file_path)] |
| 2140 self.assertEqual(contents, result) |
| 2141 |
| 2142 def testCreateFileWithAppend(self): |
| 2143 contents = [ |
| 2144 "Here comes the sun, little darlin'", |
| 2145 'Here comes the sun, and I say,', |
| 2146 "It's alright", |
| 2147 ] |
| 2148 file_dir = 'abbey_road' |
| 2149 file_path = 'abbey_road/here_comes_the_sun' |
| 2150 self.os.mkdir(file_dir) |
| 2151 fake_file = self.file(file_path, 'a') |
| 2152 for line in contents: |
| 2153 fake_file.write(line + '\n') |
| 2154 fake_file.close() |
| 2155 result = [line.rstrip() for line in self.file(file_path)] |
| 2156 self.assertEqual(contents, result) |
| 2157 |
| 2158 def testOverwriteExistingFile(self): |
| 2159 file_path = 'overwrite/this/file' |
| 2160 self.filesystem.CreateFile(file_path, contents='To disappear') |
| 2161 new_contents = [ |
| 2162 'Only these lines', |
| 2163 'should be in the file.', |
| 2164 ] |
| 2165 fake_file = self.file(file_path, 'w') |
| 2166 for line in new_contents: |
| 2167 fake_file.write(line + '\n') |
| 2168 fake_file.close() |
| 2169 result = [line.rstrip() for line in self.file(file_path)] |
| 2170 self.assertEqual(new_contents, result) |
| 2171 |
| 2172 def testAppendExistingFile(self): |
| 2173 file_path = 'append/this/file' |
| 2174 contents = [ |
| 2175 'Contents of original file' |
| 2176 'Appended contents', |
| 2177 ] |
| 2178 self.filesystem.CreateFile(file_path, contents=contents[0]) |
| 2179 fake_file = self.file(file_path, 'a') |
| 2180 for line in contents[1:]: |
| 2181 fake_file.write(line + '\n') |
| 2182 fake_file.close() |
| 2183 result = [line.rstrip() for line in self.file(file_path)] |
| 2184 self.assertEqual(contents, result) |
| 2185 |
| 2186 def testOpenWithWplus(self): |
| 2187 # set up |
| 2188 file_path = 'wplus_file' |
| 2189 self.filesystem.CreateFile(file_path, contents='old contents') |
| 2190 self.assertTrue(self.filesystem.Exists(file_path)) |
| 2191 fake_file = self.file(file_path, 'r') |
| 2192 self.assertEqual('old contents', fake_file.read()) |
| 2193 fake_file.close() |
| 2194 # actual tests |
| 2195 fake_file = self.file(file_path, 'w+') |
| 2196 fake_file.write('new contents') |
| 2197 fake_file.seek(0) |
| 2198 self.assertTrue('new contents', fake_file.read()) |
| 2199 fake_file.close() |
| 2200 |
| 2201 def testOpenWithWplusTruncation(self): |
| 2202 # set up |
| 2203 file_path = 'wplus_file' |
| 2204 self.filesystem.CreateFile(file_path, contents='old contents') |
| 2205 self.assertTrue(self.filesystem.Exists(file_path)) |
| 2206 fake_file = self.file(file_path, 'r') |
| 2207 self.assertEqual('old contents', fake_file.read()) |
| 2208 fake_file.close() |
| 2209 # actual tests |
| 2210 fake_file = self.file(file_path, 'w+') |
| 2211 fake_file.seek(0) |
| 2212 self.assertEqual('', fake_file.read()) |
| 2213 fake_file.close() |
| 2214 |
| 2215 def testOpenWithAppendFlag(self): |
| 2216 contents = [ |
| 2217 'I am he as\n', |
| 2218 'you are he as\n', |
| 2219 'you are me and\n', |
| 2220 'we are all together\n' |
| 2221 ] |
| 2222 additional_contents = [ |
| 2223 'These new lines\n', |
| 2224 'like you a lot.\n' |
| 2225 ] |
| 2226 file_path = 'append/this/file' |
| 2227 self.filesystem.CreateFile(file_path, contents=''.join(contents)) |
| 2228 fake_file = self.file(file_path, 'a') |
| 2229 self.assertRaises(IOError, fake_file.read) |
| 2230 self.assertEqual('', fake_file.read(0)) |
| 2231 self.assertEqual('', fake_file.readline(0)) |
| 2232 self.assertEqual(len(''.join(contents)), fake_file.tell()) |
| 2233 fake_file.seek(0) |
| 2234 self.assertEqual(0, fake_file.tell()) |
| 2235 fake_file.writelines(additional_contents) |
| 2236 fake_file.close() |
| 2237 result = self.file(file_path).readlines() |
| 2238 self.assertEqual(contents + additional_contents, result) |
| 2239 |
| 2240 def testAppendWithAplus(self): |
| 2241 # set up |
| 2242 file_path = 'aplus_file' |
| 2243 self.filesystem.CreateFile(file_path, contents='old contents') |
| 2244 self.assertTrue(self.filesystem.Exists(file_path)) |
| 2245 fake_file = self.file(file_path, 'r') |
| 2246 self.assertEqual('old contents', fake_file.read()) |
| 2247 fake_file.close() |
| 2248 # actual tests |
| 2249 fake_file = self.file(file_path, 'a+') |
| 2250 self.assertEqual(0, fake_file.tell()) |
| 2251 fake_file.seek(6, 1) |
| 2252 fake_file.write('new contents') |
| 2253 self.assertEqual(24, fake_file.tell()) |
| 2254 fake_file.seek(0) |
| 2255 self.assertEqual('old contentsnew contents', fake_file.read()) |
| 2256 fake_file.close() |
| 2257 |
| 2258 def testAppendWithAplusReadWithLoop(self): |
| 2259 # set up |
| 2260 file_path = 'aplus_file' |
| 2261 self.filesystem.CreateFile(file_path, contents='old contents') |
| 2262 self.assertTrue(self.filesystem.Exists(file_path)) |
| 2263 fake_file = self.file(file_path, 'r') |
| 2264 self.assertEqual('old contents', fake_file.read()) |
| 2265 fake_file.close() |
| 2266 # actual tests |
| 2267 fake_file = self.file(file_path, 'a+') |
| 2268 fake_file.seek(0) |
| 2269 fake_file.write('new contents') |
| 2270 fake_file.seek(0) |
| 2271 for line in fake_file: |
| 2272 self.assertEqual('old contentsnew contents', line) |
| 2273 fake_file.close() |
| 2274 |
| 2275 def testReadEmptyFileWithAplus(self): |
| 2276 file_path = 'aplus_file' |
| 2277 fake_file = self.file(file_path, 'a+') |
| 2278 self.assertEqual('', fake_file.read()) |
| 2279 fake_file.close() |
| 2280 |
| 2281 def testReadWithRplus(self): |
| 2282 # set up |
| 2283 file_path = 'rplus_file' |
| 2284 self.filesystem.CreateFile(file_path, contents='old contents here') |
| 2285 self.assertTrue(self.filesystem.Exists(file_path)) |
| 2286 fake_file = self.file(file_path, 'r') |
| 2287 self.assertEqual('old contents here', fake_file.read()) |
| 2288 fake_file.close() |
| 2289 # actual tests |
| 2290 fake_file = self.file(file_path, 'r+') |
| 2291 self.assertEqual('old contents here', fake_file.read()) |
| 2292 fake_file.seek(0) |
| 2293 fake_file.write('new contents') |
| 2294 fake_file.seek(0) |
| 2295 self.assertEqual('new contents here', fake_file.read()) |
| 2296 fake_file.close() |
| 2297 |
| 2298 def testOpenStCtime(self): |
| 2299 # set up |
| 2300 file_path = 'some_file' |
| 2301 self.assertFalse(self.filesystem.Exists(file_path)) |
| 2302 # tests |
| 2303 fake_file = self.file(file_path, 'w') |
| 2304 fake_file.close() |
| 2305 st = self.os.stat(file_path) |
| 2306 self.assertEqual(100, st.st_ctime) |
| 2307 |
| 2308 fake_file = self.file(file_path, 'w') |
| 2309 fake_file.close() |
| 2310 st = self.os.stat(file_path) |
| 2311 self.assertEqual(110, st.st_ctime) |
| 2312 |
| 2313 fake_file = self.file(file_path, 'w+') |
| 2314 fake_file.close() |
| 2315 st = self.os.stat(file_path) |
| 2316 self.assertEqual(120, st.st_ctime) |
| 2317 |
| 2318 fake_file = self.file(file_path, 'r') |
| 2319 fake_file.close() |
| 2320 st = self.os.stat(file_path) |
| 2321 self.assertEqual(120, st.st_ctime) |
| 2322 |
| 2323 def _CreateWithPermission(self, file_path, perm_bits): |
| 2324 self.filesystem.CreateFile(file_path) |
| 2325 self.os.chmod(file_path, perm_bits) |
| 2326 st = self.os.stat(file_path) |
| 2327 self.assertModeEqual(perm_bits, st.st_mode) |
| 2328 self.assertTrue(st.st_mode & stat.S_IFREG) |
| 2329 self.assertFalse(st.st_mode & stat.S_IFDIR) |
| 2330 |
| 2331 def testOpenFlags700(self): |
| 2332 # set up |
| 2333 file_path = 'target_file' |
| 2334 self._CreateWithPermission(file_path, 0o700) |
| 2335 # actual tests |
| 2336 self.file(file_path, 'r').close() |
| 2337 self.file(file_path, 'w').close() |
| 2338 self.file(file_path, 'w+').close() |
| 2339 self.assertRaises(IOError, self.file, file_path, 'INV') |
| 2340 |
| 2341 def testOpenFlags400(self): |
| 2342 # set up |
| 2343 file_path = 'target_file' |
| 2344 self._CreateWithPermission(file_path, 0o400) |
| 2345 # actual tests |
| 2346 self.file(file_path, 'r').close() |
| 2347 self.assertRaises(IOError, self.file, file_path, 'w') |
| 2348 self.assertRaises(IOError, self.file, file_path, 'w+') |
| 2349 |
| 2350 def testOpenFlags200(self): |
| 2351 # set up |
| 2352 file_path = 'target_file' |
| 2353 self._CreateWithPermission(file_path, 0o200) |
| 2354 # actual tests |
| 2355 self.assertRaises(IOError, self.file, file_path, 'r') |
| 2356 self.file(file_path, 'w').close() |
| 2357 self.assertRaises(IOError, self.file, file_path, 'w+') |
| 2358 |
| 2359 def testOpenFlags100(self): |
| 2360 # set up |
| 2361 file_path = 'target_file' |
| 2362 self._CreateWithPermission(file_path, 0o100) |
| 2363 # actual tests 4 |
| 2364 self.assertRaises(IOError, self.file, file_path, 'r') |
| 2365 self.assertRaises(IOError, self.file, file_path, 'w') |
| 2366 self.assertRaises(IOError, self.file, file_path, 'w+') |
| 2367 |
| 2368 def testFollowLinkRead(self): |
| 2369 link_path = '/foo/bar/baz' |
| 2370 target = '/tarJAY' |
| 2371 target_contents = 'real baz contents' |
| 2372 self.filesystem.CreateFile(target, contents=target_contents) |
| 2373 self.filesystem.CreateLink(link_path, target) |
| 2374 self.assertEqual(target, self.os.readlink(link_path)) |
| 2375 fh = self.open(link_path, 'r') |
| 2376 got_contents = fh.read() |
| 2377 fh.close() |
| 2378 self.assertEqual(target_contents, got_contents) |
| 2379 |
| 2380 def testFollowLinkWrite(self): |
| 2381 link_path = '/foo/bar/TBD' |
| 2382 target = '/tarJAY' |
| 2383 target_contents = 'real baz contents' |
| 2384 self.filesystem.CreateLink(link_path, target) |
| 2385 self.assertFalse(self.filesystem.Exists(target)) |
| 2386 |
| 2387 fh = self.open(link_path, 'w') |
| 2388 fh.write(target_contents) |
| 2389 fh.close() |
| 2390 fh = self.open(target, 'r') |
| 2391 got_contents = fh.read() |
| 2392 fh.close() |
| 2393 self.assertEqual(target_contents, got_contents) |
| 2394 |
| 2395 def testFollowIntraPathLinkWrite(self): |
| 2396 # Test a link in the middle of of a file path. |
| 2397 link_path = '/foo/build/local_machine/output/1' |
| 2398 target = '/tmp/output/1' |
| 2399 self.filesystem.CreateDirectory('/tmp/output') |
| 2400 self.filesystem.CreateLink('/foo/build/local_machine', '/tmp') |
| 2401 self.assertFalse(self.filesystem.Exists(link_path)) |
| 2402 self.assertFalse(self.filesystem.Exists(target)) |
| 2403 |
| 2404 target_contents = 'real baz contents' |
| 2405 fh = self.open(link_path, 'w') |
| 2406 fh.write(target_contents) |
| 2407 fh.close() |
| 2408 fh = self.open(target, 'r') |
| 2409 got_contents = fh.read() |
| 2410 fh.close() |
| 2411 self.assertEqual(target_contents, got_contents) |
| 2412 |
| 2413 def testFileDescriptorsForDifferentFiles(self): |
| 2414 first_path = 'some_file1' |
| 2415 second_path = 'some_file2' |
| 2416 third_path = 'some_file3' |
| 2417 self.filesystem.CreateFile(first_path, contents='contents here1') |
| 2418 self.filesystem.CreateFile(second_path, contents='contents here2') |
| 2419 self.filesystem.CreateFile(third_path, contents='contents here3') |
| 2420 |
| 2421 fake_file1 = self.open(first_path, 'r') |
| 2422 fake_file2 = self.open(second_path, 'r') |
| 2423 fake_file3 = self.open(third_path, 'r') |
| 2424 self.assertEqual(0, fake_file1.fileno()) |
| 2425 self.assertEqual(1, fake_file2.fileno()) |
| 2426 self.assertEqual(2, fake_file3.fileno()) |
| 2427 |
| 2428 def testFileDescriptorsForTheSameFileAreDifferent(self): |
| 2429 first_path = 'some_file1' |
| 2430 second_path = 'some_file2' |
| 2431 self.filesystem.CreateFile(first_path, contents='contents here1') |
| 2432 self.filesystem.CreateFile(second_path, contents='contents here2') |
| 2433 |
| 2434 fake_file1 = self.open(first_path, 'r') |
| 2435 fake_file2 = self.open(second_path, 'r') |
| 2436 fake_file1a = self.open(first_path, 'r') |
| 2437 self.assertEqual(0, fake_file1.fileno()) |
| 2438 self.assertEqual(1, fake_file2.fileno()) |
| 2439 self.assertEqual(2, fake_file1a.fileno()) |
| 2440 |
| 2441 def testReusedFileDescriptorsDoNotAffectOthers(self): |
| 2442 first_path = 'some_file1' |
| 2443 second_path = 'some_file2' |
| 2444 third_path = 'some_file3' |
| 2445 self.filesystem.CreateFile(first_path, contents='contents here1') |
| 2446 self.filesystem.CreateFile(second_path, contents='contents here2') |
| 2447 self.filesystem.CreateFile(third_path, contents='contents here3') |
| 2448 |
| 2449 fake_file1 = self.open(first_path, 'r') |
| 2450 fake_file2 = self.open(second_path, 'r') |
| 2451 fake_file3 = self.open(third_path, 'r') |
| 2452 fake_file1a = self.open(first_path, 'r') |
| 2453 self.assertEqual(0, fake_file1.fileno()) |
| 2454 self.assertEqual(1, fake_file2.fileno()) |
| 2455 self.assertEqual(2, fake_file3.fileno()) |
| 2456 self.assertEqual(3, fake_file1a.fileno()) |
| 2457 |
| 2458 fake_file1.close() |
| 2459 fake_file2.close() |
| 2460 fake_file2 = self.open(second_path, 'r') |
| 2461 fake_file1b = self.open(first_path, 'r') |
| 2462 self.assertEqual(0, fake_file2.fileno()) |
| 2463 self.assertEqual(1, fake_file1b.fileno()) |
| 2464 self.assertEqual(2, fake_file3.fileno()) |
| 2465 self.assertEqual(3, fake_file1a.fileno()) |
| 2466 |
| 2467 def testIntertwinedReadWrite(self): |
| 2468 file_path = 'some_file' |
| 2469 self.filesystem.CreateFile(file_path) |
| 2470 with self.open(file_path, 'a') as writer: |
| 2471 with self.open(file_path, 'r') as reader: |
| 2472 writes = ['hello', 'world\n', 'somewhere\nover', 'the\n', 'rainbow'] |
| 2473 reads = [] |
| 2474 # when writes are flushes, they are piped to the reader |
| 2475 for write in writes: |
| 2476 writer.write(write) |
| 2477 writer.flush() |
| 2478 reads.append(reader.read()) |
| 2479 reader.flush() |
| 2480 self.assertEqual(writes, reads) |
| 2481 writes = ['nothing', 'to\nsee', 'here'] |
| 2482 reads = [] |
| 2483 # when writes are not flushed, the reader doesn't read anything new |
| 2484 for write in writes: |
| 2485 writer.write(write) |
| 2486 reads.append(reader.read()) |
| 2487 self.assertEqual(['' for _ in writes], reads) |
| 2488 |
| 2489 def testOpenIoErrors(self): |
| 2490 file_path = 'some_file' |
| 2491 self.filesystem.CreateFile(file_path) |
| 2492 |
| 2493 with self.open(file_path, 'a') as fh: |
| 2494 self.assertRaises(IOError, fh.read) |
| 2495 self.assertRaises(IOError, fh.readlines) |
| 2496 with self.open(file_path, 'w') as fh: |
| 2497 self.assertRaises(IOError, fh.read) |
| 2498 self.assertRaises(IOError, fh.readlines) |
| 2499 with self.open(file_path, 'r') as fh: |
| 2500 self.assertRaises(IOError, fh.truncate) |
| 2501 self.assertRaises(IOError, fh.write, 'contents') |
| 2502 self.assertRaises(IOError, fh.writelines, ['con', 'tents']) |
| 2503 |
| 2504 def _IteratorOpen(file_path, mode): |
| 2505 for _ in self.open(file_path, mode): |
| 2506 pass |
| 2507 self.assertRaises(IOError, _IteratorOpen, file_path, 'w') |
| 2508 self.assertRaises(IOError, _IteratorOpen, file_path, 'a') |
| 2509 |
| 2510 |
| 2511 class OpenWithFileDescriptorTest(FakeFileOpenTestBase): |
| 2512 |
| 2513 @unittest.skipIf(sys.version_info < (3, 0), 'only tested on 3.0 or greater') |
| 2514 def testOpenWithFileDescriptor(self): |
| 2515 file_path = 'this/file' |
| 2516 self.filesystem.CreateFile(file_path) |
| 2517 fd = self.os.open(file_path, os.O_CREAT) |
| 2518 self.assertEqual(fd, self.open(fd, 'r').fileno()) |
| 2519 |
| 2520 @unittest.skipIf(sys.version_info < (3, 0), 'only tested on 3.0 or greater') |
| 2521 def testClosefdWithFileDescriptor(self): |
| 2522 file_path = 'this/file' |
| 2523 self.filesystem.CreateFile(file_path) |
| 2524 fd = self.os.open(file_path, os.O_CREAT) |
| 2525 fh = self.open(fd, 'r', closefd=False) |
| 2526 fh.close() |
| 2527 self.assertIsNotNone(self.filesystem.open_files[fd]) |
| 2528 fh = self.open(fd, 'r', closefd=True) |
| 2529 fh.close() |
| 2530 self.assertIsNone(self.filesystem.open_files[fd]) |
| 2531 |
| 2532 |
| 2533 class OpenWithBinaryFlagsTest(TestCase): |
| 2534 |
| 2535 def setUp(self): |
| 2536 self.filesystem = fake_filesystem.FakeFilesystem(path_separator='/') |
| 2537 self.file = fake_filesystem.FakeFileOpen(self.filesystem) |
| 2538 self.os = fake_filesystem.FakeOsModule(self.filesystem) |
| 2539 self.file_path = 'some_file' |
| 2540 self.file_contents = b'binary contents' |
| 2541 self.filesystem.CreateFile(self.file_path, contents=self.file_contents) |
| 2542 |
| 2543 def OpenFakeFile(self, mode): |
| 2544 return self.file(self.file_path, mode=mode) |
| 2545 |
| 2546 def OpenFileAndSeek(self, mode): |
| 2547 fake_file = self.file(self.file_path, mode=mode) |
| 2548 fake_file.seek(0, 2) |
| 2549 return fake_file |
| 2550 |
| 2551 def WriteAndReopenFile(self, fake_file, mode='rb'): |
| 2552 fake_file.write(self.file_contents) |
| 2553 fake_file.close() |
| 2554 return self.file(self.file_path, mode=mode) |
| 2555 |
| 2556 def testReadBinary(self): |
| 2557 fake_file = self.OpenFakeFile('rb') |
| 2558 self.assertEqual(self.file_contents, fake_file.read()) |
| 2559 |
| 2560 def testWriteBinary(self): |
| 2561 fake_file = self.OpenFileAndSeek('wb') |
| 2562 self.assertEqual(0, fake_file.tell()) |
| 2563 fake_file = self.WriteAndReopenFile(fake_file, mode='rb') |
| 2564 self.assertEqual(self.file_contents, fake_file.read()) |
| 2565 # reopen the file in text mode |
| 2566 fake_file = self.OpenFakeFile('wb') |
| 2567 fake_file = self.WriteAndReopenFile(fake_file, mode='r') |
| 2568 self.assertEqual(self.file_contents.decode('ascii'), fake_file.read()) |
| 2569 |
| 2570 def testWriteAndReadBinary(self): |
| 2571 fake_file = self.OpenFileAndSeek('w+b') |
| 2572 self.assertEqual(0, fake_file.tell()) |
| 2573 fake_file = self.WriteAndReopenFile(fake_file, mode='rb') |
| 2574 self.assertEqual(self.file_contents, fake_file.read()) |
| 2575 |
| 2576 |
| 2577 class OpenWithIgnoredFlagsTest(TestCase): |
| 2578 |
| 2579 def setUp(self): |
| 2580 self.filesystem = fake_filesystem.FakeFilesystem(path_separator='/') |
| 2581 self.file = fake_filesystem.FakeFileOpen(self.filesystem) |
| 2582 self.os = fake_filesystem.FakeOsModule(self.filesystem) |
| 2583 self.file_path = 'some_file' |
| 2584 self.read_contents = self.file_contents = 'two\r\nlines' |
| 2585 # For python 3.x, text file newlines are converted to \n |
| 2586 if sys.version_info >= (3, 0): |
| 2587 self.read_contents = 'two\nlines' |
| 2588 self.filesystem.CreateFile(self.file_path, contents=self.file_contents) |
| 2589 # It's resonable to assume the file exists at this point |
| 2590 |
| 2591 def OpenFakeFile(self, mode): |
| 2592 return self.file(self.file_path, mode=mode) |
| 2593 |
| 2594 def OpenFileAndSeek(self, mode): |
| 2595 fake_file = self.file(self.file_path, mode=mode) |
| 2596 fake_file.seek(0, 2) |
| 2597 return fake_file |
| 2598 |
| 2599 def WriteAndReopenFile(self, fake_file, mode='r'): |
| 2600 fake_file.write(self.file_contents) |
| 2601 fake_file.close() |
| 2602 return self.file(self.file_path, mode=mode) |
| 2603 |
| 2604 def testReadText(self): |
| 2605 fake_file = self.OpenFakeFile('rt') |
| 2606 self.assertEqual(self.read_contents, fake_file.read()) |
| 2607 |
| 2608 def testReadUniversalNewlines(self): |
| 2609 fake_file = self.OpenFakeFile('rU') |
| 2610 self.assertEqual(self.read_contents, fake_file.read()) |
| 2611 |
| 2612 def testUniversalNewlines(self): |
| 2613 fake_file = self.OpenFakeFile('U') |
| 2614 self.assertEqual(self.read_contents, fake_file.read()) |
| 2615 |
| 2616 def testWriteText(self): |
| 2617 fake_file = self.OpenFileAndSeek('wt') |
| 2618 self.assertEqual(0, fake_file.tell()) |
| 2619 fake_file = self.WriteAndReopenFile(fake_file) |
| 2620 self.assertEqual(self.read_contents, fake_file.read()) |
| 2621 |
| 2622 def testWriteAndReadTextBinary(self): |
| 2623 fake_file = self.OpenFileAndSeek('w+bt') |
| 2624 self.assertEqual(0, fake_file.tell()) |
| 2625 if sys.version_info >= (3, 0): |
| 2626 self.assertRaises(TypeError, fake_file.write, self.file_contents) |
| 2627 else: |
| 2628 fake_file = self.WriteAndReopenFile(fake_file, mode='rb') |
| 2629 self.assertEqual(self.file_contents, fake_file.read()) |
| 2630 |
| 2631 |
| 2632 class OpenWithInvalidFlagsTest(FakeFileOpenTestBase): |
| 2633 |
| 2634 def testCapitalR(self): |
| 2635 self.assertRaises(IOError, self.file, 'some_file', 'R') |
| 2636 |
| 2637 def testCapitalW(self): |
| 2638 self.assertRaises(IOError, self.file, 'some_file', 'W') |
| 2639 |
| 2640 def testCapitalA(self): |
| 2641 self.assertRaises(IOError, self.file, 'some_file', 'A') |
| 2642 |
| 2643 def testLowerU(self): |
| 2644 self.assertRaises(IOError, self.file, 'some_file', 'u') |
| 2645 |
| 2646 def testLowerRw(self): |
| 2647 self.assertRaises(IOError, self.file, 'some_file', 'rw') |
| 2648 |
| 2649 |
| 2650 class ResolvePathTest(FakeFileOpenTestBase): |
| 2651 |
| 2652 def __WriteToFile(self, file_name): |
| 2653 fh = self.open(file_name, 'w') |
| 2654 fh.write('x') |
| 2655 fh.close() |
| 2656 |
| 2657 def testNoneFilepathRaisesTypeError(self): |
| 2658 self.assertRaises(TypeError, self.open, None, 'w') |
| 2659 |
| 2660 def testEmptyFilepathRaisesIOError(self): |
| 2661 self.assertRaises(IOError, self.open, '', 'w') |
| 2662 |
| 2663 def testNormalPath(self): |
| 2664 self.__WriteToFile('foo') |
| 2665 self.assertTrue(self.filesystem.Exists('foo')) |
| 2666 |
| 2667 def testLinkWithinSameDirectory(self): |
| 2668 final_target = '/foo/baz' |
| 2669 self.filesystem.CreateLink('/foo/bar', 'baz') |
| 2670 self.__WriteToFile('/foo/bar') |
| 2671 self.assertTrue(self.filesystem.Exists(final_target)) |
| 2672 self.assertEqual(1, self.os.stat(final_target)[stat.ST_SIZE]) |
| 2673 |
| 2674 def testLinkToSubDirectory(self): |
| 2675 final_target = '/foo/baz/bip' |
| 2676 self.filesystem.CreateDirectory('/foo/baz') |
| 2677 self.filesystem.CreateLink('/foo/bar', 'baz/bip') |
| 2678 self.__WriteToFile('/foo/bar') |
| 2679 self.assertTrue(self.filesystem.Exists(final_target)) |
| 2680 self.assertEqual(1, self.os.stat(final_target)[stat.ST_SIZE]) |
| 2681 self.assertTrue(self.filesystem.Exists('/foo/baz')) |
| 2682 # Make sure that intermediate directory got created. |
| 2683 new_dir = self.filesystem.GetObject('/foo/baz') |
| 2684 self.assertTrue(stat.S_IFDIR & new_dir.st_mode) |
| 2685 |
| 2686 def testLinkToParentDirectory(self): |
| 2687 final_target = '/baz/bip' |
| 2688 self.filesystem.CreateDirectory('/foo') |
| 2689 self.filesystem.CreateDirectory('/baz') |
| 2690 self.filesystem.CreateLink('/foo/bar', '../baz') |
| 2691 self.__WriteToFile('/foo/bar/bip') |
| 2692 self.assertTrue(self.filesystem.Exists(final_target)) |
| 2693 self.assertEqual(1, self.os.stat(final_target)[stat.ST_SIZE]) |
| 2694 self.assertTrue(self.filesystem.Exists('/foo/bar')) |
| 2695 |
| 2696 def testLinkToAbsolutePath(self): |
| 2697 final_target = '/foo/baz/bip' |
| 2698 self.filesystem.CreateDirectory('/foo/baz') |
| 2699 self.filesystem.CreateLink('/foo/bar', final_target) |
| 2700 self.__WriteToFile('/foo/bar') |
| 2701 self.assertTrue(self.filesystem.Exists(final_target)) |
| 2702 |
| 2703 def testRelativeLinksWorkAfterChdir(self): |
| 2704 final_target = '/foo/baz/bip' |
| 2705 self.filesystem.CreateDirectory('/foo/baz') |
| 2706 self.filesystem.CreateLink('/foo/bar', './baz/bip') |
| 2707 self.assertEqual(final_target, |
| 2708 self.filesystem.ResolvePath('/foo/bar')) |
| 2709 |
| 2710 os_module = fake_filesystem.FakeOsModule(self.filesystem) |
| 2711 self.assertTrue(os_module.path.islink('/foo/bar')) |
| 2712 os_module.chdir('/foo') |
| 2713 self.assertEqual('/foo', os_module.getcwd()) |
| 2714 self.assertTrue(os_module.path.islink('bar')) |
| 2715 |
| 2716 self.assertEqual('/foo/baz/bip', |
| 2717 self.filesystem.ResolvePath('bar')) |
| 2718 |
| 2719 self.__WriteToFile('/foo/bar') |
| 2720 self.assertTrue(self.filesystem.Exists(final_target)) |
| 2721 |
| 2722 def testAbsoluteLinksWorkAfterChdir(self): |
| 2723 final_target = '/foo/baz/bip' |
| 2724 self.filesystem.CreateDirectory('/foo/baz') |
| 2725 self.filesystem.CreateLink('/foo/bar', final_target) |
| 2726 self.assertEqual(final_target, |
| 2727 self.filesystem.ResolvePath('/foo/bar')) |
| 2728 |
| 2729 os_module = fake_filesystem.FakeOsModule(self.filesystem) |
| 2730 self.assertTrue(os_module.path.islink('/foo/bar')) |
| 2731 os_module.chdir('/foo') |
| 2732 self.assertEqual('/foo', os_module.getcwd()) |
| 2733 self.assertTrue(os_module.path.islink('bar')) |
| 2734 |
| 2735 self.assertEqual('/foo/baz/bip', |
| 2736 self.filesystem.ResolvePath('bar')) |
| 2737 |
| 2738 self.__WriteToFile('/foo/bar') |
| 2739 self.assertTrue(self.filesystem.Exists(final_target)) |
| 2740 |
| 2741 def testChdirThroughRelativeLink(self): |
| 2742 self.filesystem.CreateDirectory('/x/foo') |
| 2743 self.filesystem.CreateDirectory('/x/bar') |
| 2744 self.filesystem.CreateLink('/x/foo/bar', '../bar') |
| 2745 self.assertEqual('/x/bar', self.filesystem.ResolvePath('/x/foo/bar')) |
| 2746 |
| 2747 os_module = fake_filesystem.FakeOsModule(self.filesystem) |
| 2748 os_module.chdir('/x/foo') |
| 2749 self.assertEqual('/x/foo', os_module.getcwd()) |
| 2750 self.assertEqual('/x/bar', self.filesystem.ResolvePath('bar')) |
| 2751 |
| 2752 os_module.chdir('bar') |
| 2753 self.assertEqual('/x/bar', os_module.getcwd()) |
| 2754 |
| 2755 def testReadLinkToLink(self): |
| 2756 # Write into the final link target and read back from a file which will |
| 2757 # point to that. |
| 2758 self.filesystem.CreateLink('/foo/bar', 'link') |
| 2759 self.filesystem.CreateLink('/foo/link', 'baz') |
| 2760 self.__WriteToFile('/foo/baz') |
| 2761 fh = self.open('/foo/bar', 'r') |
| 2762 self.assertEqual('x', fh.read()) |
| 2763 |
| 2764 def testWriteLinkToLink(self): |
| 2765 final_target = '/foo/baz' |
| 2766 self.filesystem.CreateLink('/foo/bar', 'link') |
| 2767 self.filesystem.CreateLink('/foo/link', 'baz') |
| 2768 self.__WriteToFile('/foo/bar') |
| 2769 self.assertTrue(self.filesystem.Exists(final_target)) |
| 2770 |
| 2771 def testMultipleLinks(self): |
| 2772 final_target = '/a/link1/c/link2/e' |
| 2773 self.os.makedirs('/a/link1/c/link2') |
| 2774 |
| 2775 self.filesystem.CreateLink('/a/b', 'link1') |
| 2776 self.assertEqual('/a/link1', self.filesystem.ResolvePath('/a/b')) |
| 2777 self.assertEqual('/a/link1/c', self.filesystem.ResolvePath('/a/b/c')) |
| 2778 |
| 2779 self.filesystem.CreateLink('/a/link1/c/d', 'link2') |
| 2780 self.assertTrue(self.filesystem.Exists('/a/link1/c/d')) |
| 2781 self.assertTrue(self.filesystem.Exists('/a/b/c/d')) |
| 2782 |
| 2783 final_target = '/a/link1/c/link2/e' |
| 2784 self.assertFalse(self.filesystem.Exists(final_target)) |
| 2785 self.__WriteToFile('/a/b/c/d/e') |
| 2786 self.assertTrue(self.filesystem.Exists(final_target)) |
| 2787 |
| 2788 def testTooManyLinks(self): |
| 2789 self.filesystem.CreateLink('/a/loop', 'loop') |
| 2790 self.assertFalse(self.filesystem.Exists('/a/loop')) |
| 2791 |
| 2792 |
| 2793 class PathManipulationTests(TestCase): |
| 2794 def setUp(self): |
| 2795 self.filesystem = fake_filesystem.FakeFilesystem(path_separator='|') |
| 2796 |
| 2797 |
| 2798 class CollapsePathPipeSeparatorTest(PathManipulationTests): |
| 2799 """Tests CollapsePath (mimics os.path.normpath) using | as path separator.""" |
| 2800 |
| 2801 def testEmptyPathBecomesDotPath(self): |
| 2802 self.assertEqual('.', self.filesystem.CollapsePath('')) |
| 2803 |
| 2804 def testDotPathUnchanged(self): |
| 2805 self.assertEqual('.', self.filesystem.CollapsePath('.')) |
| 2806 |
| 2807 def testSlashesAreNotCollapsed(self): |
| 2808 """Tests that '/' is not treated specially if the path separator is '|'. |
| 2809 |
| 2810 In particular, multiple slashes should not be collapsed. |
| 2811 """ |
| 2812 self.assertEqual('/', self.filesystem.CollapsePath('/')) |
| 2813 self.assertEqual('/////', self.filesystem.CollapsePath('/////')) |
| 2814 |
| 2815 def testRootPath(self): |
| 2816 self.assertEqual('|', self.filesystem.CollapsePath('|')) |
| 2817 |
| 2818 def testMultipleSeparatorsCollapsedIntoRootPath(self): |
| 2819 self.assertEqual('|', self.filesystem.CollapsePath('|||||')) |
| 2820 |
| 2821 def testAllDotPathsRemovedButOne(self): |
| 2822 self.assertEqual('.', self.filesystem.CollapsePath('.|.|.|.')) |
| 2823 |
| 2824 def testAllDotPathsRemovedIfAnotherPathComponentExists(self): |
| 2825 self.assertEqual('|', self.filesystem.CollapsePath('|.|.|.|')) |
| 2826 self.assertEqual('foo|bar', self.filesystem.CollapsePath('foo|.|.|.|bar')) |
| 2827 |
| 2828 def testIgnoresUpLevelReferencesStartingFromRoot(self): |
| 2829 self.assertEqual('|', self.filesystem.CollapsePath('|..|..|..|')) |
| 2830 self.assertEqual('|', self.filesystem.CollapsePath('||..|.|..||')) |
| 2831 self.assertEqual( |
| 2832 '|', self.filesystem.CollapsePath('|..|..|foo|bar|..|..|')) |
| 2833 |
| 2834 def testConservesUpLevelReferencesStartingFromCurrentDirectory(self): |
| 2835 self.assertEqual( |
| 2836 '..|..', self.filesystem.CollapsePath('..|foo|bar|..|..|..')) |
| 2837 |
| 2838 def testCombineDotAndUpLevelReferencesInAbsolutePath(self): |
| 2839 self.assertEqual( |
| 2840 '|yes', self.filesystem.CollapsePath('|||||.|..|||yes|no|..|.|||')) |
| 2841 |
| 2842 def testDotsInPathCollapsesToLastPath(self): |
| 2843 self.assertEqual( |
| 2844 'bar', self.filesystem.CollapsePath('foo|..|bar')) |
| 2845 self.assertEqual( |
| 2846 'bar', self.filesystem.CollapsePath('foo|..|yes|..|no|..|bar')) |
| 2847 |
| 2848 |
| 2849 class SplitPathTest(PathManipulationTests): |
| 2850 """Tests SplitPath (which mimics os.path.split) using | as path separator.""" |
| 2851 |
| 2852 def testEmptyPath(self): |
| 2853 self.assertEqual(('', ''), self.filesystem.SplitPath('')) |
| 2854 |
| 2855 def testNoSeparators(self): |
| 2856 self.assertEqual(('', 'ab'), self.filesystem.SplitPath('ab')) |
| 2857 |
| 2858 def testSlashesDoNotSplit(self): |
| 2859 """Tests that '/' is not treated specially if the path separator is '|'.""" |
| 2860 self.assertEqual(('', 'a/b'), self.filesystem.SplitPath('a/b')) |
| 2861 |
| 2862 def testEliminateTrailingSeparatorsFromHead(self): |
| 2863 self.assertEqual(('a', 'b'), self.filesystem.SplitPath('a|b')) |
| 2864 self.assertEqual(('a', 'b'), self.filesystem.SplitPath('a|||b')) |
| 2865 self.assertEqual(('|a', 'b'), self.filesystem.SplitPath('|a||b')) |
| 2866 self.assertEqual(('a|b', 'c'), self.filesystem.SplitPath('a|b|c')) |
| 2867 self.assertEqual(('|a|b', 'c'), self.filesystem.SplitPath('|a|b|c')) |
| 2868 |
| 2869 def testRootSeparatorIsNotStripped(self): |
| 2870 self.assertEqual(('|', ''), self.filesystem.SplitPath('|||')) |
| 2871 self.assertEqual(('|', 'a'), self.filesystem.SplitPath('|a')) |
| 2872 self.assertEqual(('|', 'a'), self.filesystem.SplitPath('|||a')) |
| 2873 |
| 2874 def testEmptyTailIfPathEndsInSeparator(self): |
| 2875 self.assertEqual(('a|b', ''), self.filesystem.SplitPath('a|b|')) |
| 2876 |
| 2877 def testEmptyPathComponentsArePreservedInHead(self): |
| 2878 self.assertEqual(('|a||b', 'c'), self.filesystem.SplitPath('|a||b||c')) |
| 2879 |
| 2880 |
| 2881 class JoinPathTest(PathManipulationTests): |
| 2882 """Tests JoinPath (which mimics os.path.join) using | as path separator.""" |
| 2883 |
| 2884 def testOneEmptyComponent(self): |
| 2885 self.assertEqual('', self.filesystem.JoinPaths('')) |
| 2886 |
| 2887 def testMultipleEmptyComponents(self): |
| 2888 self.assertEqual('', self.filesystem.JoinPaths('', '', '')) |
| 2889 |
| 2890 def testSeparatorsNotStrippedFromSingleComponent(self): |
| 2891 self.assertEqual('||a||', self.filesystem.JoinPaths('||a||')) |
| 2892 |
| 2893 def testOneSeparatorAddedBetweenComponents(self): |
| 2894 self.assertEqual('a|b|c|d', self.filesystem.JoinPaths('a', 'b', 'c', 'd')) |
| 2895 |
| 2896 def testNoSeparatorAddedForComponentsEndingInSeparator(self): |
| 2897 self.assertEqual('a|b|c', self.filesystem.JoinPaths('a|', 'b|', 'c')) |
| 2898 self.assertEqual('a|||b|||c', |
| 2899 self.filesystem.JoinPaths('a|||', 'b|||', 'c')) |
| 2900 |
| 2901 def testComponentsPrecedingAbsoluteComponentAreIgnored(self): |
| 2902 self.assertEqual('|c|d', self.filesystem.JoinPaths('a', '|b', '|c', 'd')) |
| 2903 |
| 2904 def testOneSeparatorAddedForTrailingEmptyComponents(self): |
| 2905 self.assertEqual('a|', self.filesystem.JoinPaths('a', '')) |
| 2906 self.assertEqual('a|', self.filesystem.JoinPaths('a', '', '')) |
| 2907 |
| 2908 def testNoSeparatorAddedForLeadingEmptyComponents(self): |
| 2909 self.assertEqual('a', self.filesystem.JoinPaths('', 'a')) |
| 2910 |
| 2911 def testInternalEmptyComponentsIgnored(self): |
| 2912 self.assertEqual('a|b', self.filesystem.JoinPaths('a', '', 'b')) |
| 2913 self.assertEqual('a|b|', self.filesystem.JoinPaths('a|', '', 'b|')) |
| 2914 |
| 2915 |
| 2916 class PathSeparatorTest(TestCase): |
| 2917 def testOsPathSepMatchesFakeFilesystemSeparator(self): |
| 2918 filesystem = fake_filesystem.FakeFilesystem(path_separator='!') |
| 2919 fake_os = fake_filesystem.FakeOsModule(filesystem) |
| 2920 self.assertEqual('!', fake_os.sep) |
| 2921 self.assertEqual('!', fake_os.path.sep) |
| 2922 |
| 2923 |
| 2924 if __name__ == '__main__': |
| 2925 main() |
OLD | NEW |