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

« back to all changes in this revision

Viewing changes to twisted/vfs/test/test_vfs.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
 
 
2
import os
 
3
import os.path
 
4
import shutil
 
5
 
 
6
from twisted.trial import unittest
 
7
 
 
8
from twisted.vfs.backends import osfs, inmem
 
9
from twisted.vfs.ivfs import IFileSystemContainer, IFileSystemLeaf
 
10
 
 
11
 
 
12
class OSVFSTest(unittest.TestCase):
 
13
 
 
14
    def setUp(self):
 
15
        self.tmpdir = self.mktemp()
 
16
        os.mkdir(self.tmpdir)
 
17
        os.mkdir(os.path.join(self.tmpdir, 'ned'))
 
18
        open(os.path.join(self.tmpdir, 'file.txt'), 'w').write('wobble\n')
 
19
        self.root = osfs.OSDirectory(self.tmpdir)
 
20
 
 
21
    def tearDown(self):
 
22
        shutil.rmtree(self.tmpdir)
 
23
 
 
24
    def test_listdir(self):
 
25
        nodes = self.root.children()
 
26
        paths = [path for (path, node) in nodes]
 
27
        paths.sort()
 
28
        self.assertEquals(paths, ['.', '..', 'file.txt', 'ned'])
 
29
 
 
30
    def test_mkdir(self):
 
31
        new = self.root.createDirectory('fred')
 
32
        nodes = new.children()
 
33
        self.assertEquals([path for (path, node) in nodes], ['.', '..'])
 
34
 
 
35
    def test_rmdir(self):
 
36
        self.root.child('ned').remove()
 
37
        nodes = self.root.children()
 
38
        paths = [path for (path, node) in nodes]
 
39
        paths.sort()
 
40
        self.assertEquals(paths, ['.', '..', 'file.txt'])
 
41
 
 
42
    def test_rmfile(self):
 
43
        self.root.child('file.txt').remove()
 
44
        nodes = self.root.children()
 
45
        paths = [path for (path, node) in nodes]
 
46
        paths.sort()
 
47
        self.assertEquals(paths, ['.', '..', 'ned'])
 
48
 
 
49
    def test_rename(self):
 
50
        self.root.child('ned').rename('sed')
 
51
        nodes = self.root.children()
 
52
        paths = [path for (path, node) in nodes]
 
53
        paths.sort()
 
54
        self.assertEquals(paths, ['.', '..', 'file.txt', 'sed'])
 
55
 
 
56
    def test_mkfile(self):
 
57
        new = self.root.createFile('fred.txt')
 
58
        nodes = self.root.children()
 
59
        paths = [path for (path, node) in nodes]
 
60
        paths.sort()
 
61
        self.assertEquals(paths, ['.', '..', 'file.txt', 'fred.txt', 'ned'])
 
62
 
 
63
    def test_writefile(self):
 
64
        new = self.root.createFile('fred.txt')
 
65
        new.open(os.O_WRONLY)
 
66
        new.writeChunk(0, 'roar')
 
67
        new.close()
 
68
        new.open(os.O_RDONLY)
 
69
        text = new.readChunk(0, 100)
 
70
        new.close()
 
71
        self.assertEquals(text, 'roar')
 
72
 
 
73
    def test_readfile(self):
 
74
        fh = self.root.child('file.txt')
 
75
        fh.open(os.O_RDONLY)
 
76
        text = fh.readChunk(0, 100)
 
77
        fh.close()
 
78
        self.assertEquals(text, 'wobble\n')
 
79
 
 
80
    def test_exists(self):
 
81
        self.failUnless(self.root.exists('file.txt'))
 
82
        self.failIf(self.root.exists('noodle'))
 
83
 
 
84
 
 
85
 
 
86
class InMemVFSTest(OSVFSTest):
 
87
 
 
88
    def setUp(self):
 
89
        root = inmem.FakeDirectory()
 
90
        ned = inmem.FakeDirectory('ned', root)
 
91
        f = inmem.FakeFile('file.txt', root, 'wobble\n')
 
92
        root._children = { 'ned' : ned, 'file.txt' : f }
 
93
        self.root = root
 
94
 
 
95
    def tearDown(self):
 
96
        pass
 
97