1
# Copyright (c) 2001-2005 Twisted Matrix Laboratories.
2
# See LICENSE for details.
8
from twisted.trial import unittest
10
from twisted.vfs.backends import osfs, inmem
11
from twisted.vfs.ivfs import IFileSystemContainer, IFileSystemLeaf, VFSError
12
from twisted.vfs import pathutils
15
class OSVFSTest(unittest.TestCase):
18
self.tmpdir = self.mktemp()
20
os.mkdir(os.path.join(self.tmpdir, 'somedir'))
23
shutil.rmtree( self.tmpdir )
25
def test_subclassing(self):
26
# The children of a subclassed OSDirectory will also be instances of
27
# the subclass (unless childDirFactory is explicitly overridden).
29
# Define a subclass of OSDirectory
30
class OSDirSubclass(osfs.OSDirectory):
33
# Subdirectories, both existing and newly created, will be instances of
35
osdir = OSDirSubclass(self.tmpdir)
36
self.assert_(isinstance(osdir.child('somedir'), OSDirSubclass))
37
self.assert_(isinstance(osdir.createDirectory('new'), OSDirSubclass))
39
def test_childDirFactory(self):
40
# The class of subdirectories can be overridden using childDirFactory
42
# Define a subclass of OSDirectory that overrides childDirFactory
43
class OSDirSubclass(osfs.OSDirectory):
44
def childDirFactory(self):
45
return osfs.OSDirectory
47
# Subdirectories, both existing and newly created, will be instances of
48
# osfs.OSDirectory, but not OSDirSubclass.
49
osdir = OSDirSubclass(self.tmpdir)
50
self.assert_(isinstance(osdir.child('somedir'), osfs.OSDirectory))
51
self.assertNot(isinstance(osdir.child('somedir'), OSDirSubclass))
52
self.assert_(isinstance(osdir.createDirectory('new'), osfs.OSDirectory))
53
self.assertNot(isinstance(osdir.createDirectory('new2'), OSDirSubclass))
55
def test_createFileExclusive(self):
56
osdir = osfs.OSDirectory(self.tmpdir)
58
# Creating a new file with exclusivity should pass
59
child = osdir.createFile('foo', exclusive=True)
60
self.failUnless(IFileSystemLeaf.providedBy(child))
61
self.assertIn('foo', [name for name, child in osdir.children()])
63
# Creating an existing file with exclusivity should fail.
64
self.assertRaises(VFSError, osdir.createFile, 'foo', exclusive=True)
66
# Creating an existing file unexclusively should pass.
67
child = osdir.createFile('foo', exclusive=False)
68
self.failUnless(IFileSystemLeaf.providedBy(child))
69
self.assertIn('foo', [name for name, child in osdir.children()])