~certify-web-dev/twisted/certify-trunk

« back to all changes in this revision

Viewing changes to twisted/vfs/test/test_osfs.py

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2007-01-17 14:52:35 UTC
  • mfrom: (1.1.5 upstream) (2.1.2 etch)
  • Revision ID: james.westby@ubuntu.com-20070117145235-btmig6qfmqfen0om
Tags: 2.5.0-0ubuntu1
New upstream version, compatible with python2.5.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (c) 2001-2005 Twisted Matrix Laboratories.
 
2
# See LICENSE for details.
 
3
 
 
4
import os
 
5
import os.path
 
6
import shutil
 
7
 
 
8
from twisted.trial import unittest
 
9
 
 
10
from twisted.vfs.backends import osfs, inmem
 
11
from twisted.vfs.ivfs import IFileSystemContainer, IFileSystemLeaf, VFSError
 
12
from twisted.vfs import pathutils
 
13
 
 
14
 
 
15
class OSVFSTest(unittest.TestCase):
 
16
 
 
17
    def setUp(self):
 
18
        self.tmpdir = self.mktemp()
 
19
        os.mkdir(self.tmpdir)
 
20
        os.mkdir(os.path.join(self.tmpdir, 'somedir'))
 
21
 
 
22
    def tearDown(self):
 
23
        shutil.rmtree( self.tmpdir )
 
24
 
 
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).
 
28
        
 
29
        # Define a subclass of OSDirectory
 
30
        class OSDirSubclass(osfs.OSDirectory):
 
31
            pass
 
32
 
 
33
        # Subdirectories, both existing and newly created, will be instances of
 
34
        # the subclass.
 
35
        osdir = OSDirSubclass(self.tmpdir)
 
36
        self.assert_(isinstance(osdir.child('somedir'), OSDirSubclass))
 
37
        self.assert_(isinstance(osdir.createDirectory('new'), OSDirSubclass))
 
38
 
 
39
    def test_childDirFactory(self):
 
40
        # The class of subdirectories can be overridden using childDirFactory
 
41
        
 
42
        # Define a subclass of OSDirectory that overrides childDirFactory
 
43
        class OSDirSubclass(osfs.OSDirectory):
 
44
            def childDirFactory(self):
 
45
                return osfs.OSDirectory
 
46
 
 
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))
 
54
        
 
55
    def test_createFileExclusive(self):
 
56
        osdir = osfs.OSDirectory(self.tmpdir)
 
57
 
 
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()])
 
62
 
 
63
        # Creating an existing file with exclusivity should fail.
 
64
        self.assertRaises(VFSError, osdir.createFile, 'foo', exclusive=True)
 
65
 
 
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()])
 
70