def test_moveToSizeCache(self, hook=lambda : None): """ L{FilePath.moveTo} clears its destination's status cache, such that calls to L{FilePath.getsize} after the call to C{moveTo} will report the new size, not the old one. This is a separate test from C{test_moveToExistsCache} because it is intended to cover the fact that the destination's cache is dropped; test_moveToExistsCache doesn't cover this case because (currently) a file that doesn't exist yet does not cache the fact of its non- existence. """ fp = filepath.FilePath(self.mktemp()) fp2 = filepath.FilePath(self.mktemp()) fp.setContent("1234") fp2.setContent("1234567890") hook() # Sanity check / kick off caching. self.assertEqual(fp.getsize(), 4) self.assertEqual(fp2.getsize(), 10) # Actually attempting to replace a file on Windows would fail with # ERROR_ALREADY_EXISTS, but we don't need to test that, just the cached # metadata, so, delete the file ... os.remove(fp2.path) # ... but don't clear the status cache, as fp2.remove() would. self.assertEqual(fp2.getsize(), 10) fp.moveTo(fp2) self.assertEqual(fp2.getsize(), 4)
def test_alwaysCatchOSError(self): """ Verify that in the normal case where a directory does not exist, we will get an OSError. """ fp = filepath.FilePath(self.mktemp()) self.assertRaises(OSError, fp.children)
def test_moveToExistsCache(self): """ A L{FilePath} that has been moved aside with L{FilePath.moveTo} no longer registers as existing. Its previously non-existent target exists, though, as it was created by the call to C{moveTo}. """ fp = filepath.FilePath(self.mktemp()) fp2 = filepath.FilePath(self.mktemp()) fp.touch() # Both a sanity check (make sure the file status looks right) and an # enticement for stat-caching logic to kick in and remember that these # exist / don't exist. self.assertEquals(fp.exists(), True) self.assertEquals(fp2.exists(), False) fp.moveTo(fp2) self.assertEqual(fp.exists(), False) self.assertEqual(fp2.exists(), True)
def test_copyToWithoutSymlink(self): """ Verify that copying with followLinks=False copies symlinks as symlinks """ self.symlink("sub1", self.path.child("link1").path) fp = filepath.FilePath(self.mktemp()) self.path.copyTo(fp, followLinks=False) self.assertTrue(fp.child("link1").islink()) self.assertEquals(os.readlink(self.path.child("link1").path), os.readlink(fp.child("link1").path))
def test_existsCache(self): """ Check that C{filepath.FilePath.exists} correctly restat the object if an operation has occurred in the mean time. """ fp = filepath.FilePath(self.mktemp()) self.assertEquals(fp.exists(), False) fp.makedirs() self.assertEquals(fp.exists(), True)
def test_copyToWithSymlink(self): """ Verify that copying with followLinks=True copies symlink targets instead of symlinks """ self.symlink(self.path.child("sub1").path, self.path.child("link1").path) fp = filepath.FilePath(self.mktemp()) self.path.copyTo(fp) self.assertFalse(fp.child("link1").islink()) self.assertEquals([x.basename() for x in fp.child("sub1").children()], [x.basename() for x in fp.child("link1").children()])
def setUp(self): self.now = time.time() cmn = self.cmn = os.path.abspath(self.mktemp()) self.all = [cmn] os.mkdir(cmn) self.subdir("sub1") f = self.subfile("file1") f.write(self.f1content) f.close() f = self.subfile("sub1", "file2") f.write(self.f2content) f.close() self.subdir('sub3') f = self.subfile("sub3", "file3.ext1") f.close() f = self.subfile("sub3", "file3.ext2") f.close() f = self.subfile("sub3", "file3.ext3") f.close() self.path = filepath.FilePath(cmn) self.root = filepath.FilePath("/")
def test_moveTo(self): """ Verify that moving an entire directory results into another directory with the same content. """ oldPaths = list(self.path.walk()) # Record initial state fp = filepath.FilePath(self.mktemp()) self.path.moveTo(fp) fp.moveTo(self.path) newPaths = list(self.path.walk()) # Record double-move state newPaths.sort() oldPaths.sort() self.assertEquals(newPaths, oldPaths)
def test_keepOriginalAttributes(self): """ Verify that the Unlistable exception raised will preserve the attributes of the previously-raised exception. """ fp = filepath.FilePath(self.mktemp()) ose = self.assertRaises(OSError, fp.children) d1 = ose.__dict__.keys() d1.remove('originalException') d2 = ose.originalException.__dict__.keys() d1.sort() d2.sort() self.assertEquals(d1, d2)
def test_copyToDirectory(self): """ L{FilePath.copyTo} makes a copy of all the contents of the directory named by that L{FilePath} if it is able to do so. """ oldPaths = list(self.path.walk()) # Record initial state fp = filepath.FilePath(self.mktemp()) self.path.copyTo(fp) self.path.remove() fp.copyTo(self.path) newPaths = list(self.path.walk()) # Record double-copy state newPaths.sort() oldPaths.sort() self.assertEquals(newPaths, oldPaths)
def test_createBinaryMode(self): """ L{FilePath.create} should always open (and write to) files in binary mode; line-feed octets should be unmodified. (While this test should pass on all platforms, it is only really interesting on platforms which have the concept of binary mode, i.e. Windows platforms.) """ path = filepath.FilePath(self.mktemp()) f = path.create() self.failUnless("b" in f.mode) f.write("\n") f.close() read = open(path.path, "rb").read() self.assertEqual(read, "\n")
def test_changed(self): """ L{FilePath.changed} indicates that the L{FilePath} has changed, but does not re-read the status information from the filesystem until it is queried again via another method, such as C{getsize}. """ fp = filepath.FilePath(self.mktemp()) fp.setContent("12345") self.assertEquals(fp.getsize(), 5) # Someone else comes along and changes the file. fObj = open(fp.path, 'wb') fObj.write("12345678") fObj.close() # Sanity check for caching: size should still be 5. self.assertEquals(fp.getsize(), 5) fp.changed() # This path should look like we don't know what status it's in, not that # we know that it didn't exist when last we checked. self.assertEqual(fp.statinfo, None) self.assertEquals(fp.getsize(), 8)
def testComparison(self): self.assertEquals(filepath.FilePath('a'), filepath.FilePath('a')) self.failUnless(filepath.FilePath('z') > filepath.FilePath('a')) self.failUnless(filepath.FilePath('z') >= filepath.FilePath('a')) self.failUnless(filepath.FilePath('a') >= filepath.FilePath('a')) self.failUnless(filepath.FilePath('a') <= filepath.FilePath('a')) self.failUnless(filepath.FilePath('a') < filepath.FilePath('z')) self.failUnless(filepath.FilePath('a') <= filepath.FilePath('z')) self.failUnless(filepath.FilePath('a') != filepath.FilePath('z')) self.failUnless(filepath.FilePath('z') != filepath.FilePath('a')) self.failIf(filepath.FilePath('z') != filepath.FilePath('z'))
def testPreauthChild(self): fp = filepath.FilePath('.') fp.preauthChild('foo/bar') self.assertRaises(filepath.InsecurePath, fp.child, '/foo')