[Zope-Checkins] CVS: Zope3/lib/python/Zope/Server/VFS/tests - PosixFilesystemTests.py:1.1.2.1 ReadFilesystemTests.py:1.1.2.1 WriteFilesystemTests.py:1.1.2.1 testOSFileSystem.py:1.1.2.10

Shane Hathaway shane@cvs.zope.org
Fri, 12 Apr 2002 15:04:30 -0400


Update of /cvs-repository/Zope3/lib/python/Zope/Server/VFS/tests
In directory cvs.zope.org:/tmp/cvs-serv4441

Modified Files:
      Tag: Zope3-Server-Branch
	testOSFileSystem.py 
Added Files:
      Tag: Zope3-Server-Branch
	PosixFilesystemTests.py ReadFilesystemTests.py 
	WriteFilesystemTests.py 
Log Message:
Split apart and expanded filesystem tests so they can be reused


=== Added File Zope3/lib/python/Zope/Server/VFS/tests/PosixFilesystemTests.py ===
##############################################################################
#
# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""

$Id: PosixFilesystemTests.py,v 1.1.2.1 2002/04/12 19:04:30 shane Exp $
"""


import stat

from Interface.Verify import verifyClass
from Zope.Server.VFS.IPosixFileSystem import IPosixFileSystem

from WriteFilesystemTests import WriteFilesystemTests


class PosixFilesystemTests (WriteFilesystemTests):
    """Tests of a writable and readable POSIX-compliant filesystem
    """

    def testChmod(self):
        old_mode = self.filesystem.stat(self.file_name)[stat.ST_MODE]
        new_mode = old_mode ^ 0444
        self.filesystem.chmod(self.file_name, new_mode)
        check_mode = self.filesystem.stat(self.file_name)[stat.ST_MODE]
        self.assertEqual(check_mode, new_mode)


    def testChown(self):
        self.filesystem.chown(self.file_name, 500, 500)
        s = self.filesystem.stat(self.file_name)
        self.assertEqual(s[stat.ST_UID], 500)
        self.assertEqual(s[stat.ST_GID], 500)


    def testMakeLink(self):
        self.filesystem.link(self.file_name, self.file_name + '.linked')
        self.failUnless(self.filesystem.exists(self.file_name + '.linked'))
        # Another test should test whether writing to one file
        # changes the other.


    def testMakeFifo(self):
        path = self.dir_name + '/fifo'
        self.filesystem.mkfifo(path)
        self.failUnless(self.filesystem.exists(path))
        # Another test should test the behavior of the fifo.


    def testMakeSymlink(self):
        self.filesystem.symlink(self.file_name, self.file_name + '.symlink')
        self.failUnless(self.filesystem.exists(self.file_name + '.symlink'))
        # Another test should test whether writing to one file
        # changes the other.


    def testPosixInterface(self):
        class_ = self.filesystem.__class__
        self.failUnless(
            IPosixFileSystem.isImplementedByInstancesOf(class_))
        self.failUnless(verifyClass(IPosixFileSystem, class_))


def test_suite():
    loader = unittest.TestLoader()
    return loader.loadTestsFromTestCase(OSFileSystemTest)

if __name__=='__main__':
    unittest.TextTestRunner().run( test_suite() )


=== Added File Zope3/lib/python/Zope/Server/VFS/tests/ReadFilesystemTests.py ===
##############################################################################
#
# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""

$Id: ReadFilesystemTests.py,v 1.1.2.1 2002/04/12 19:04:30 shane Exp $
"""


import stat
from StringIO import StringIO

from Interface.Verify import verifyClass
from Zope.Server.VFS.IReadFileSystem import IReadFileSystem


class ReadFilesystemTests:
    """Tests of a readable filesystem
    """

    filesystem = None
    dir_name  = '/dir'
    file_name = '/dir/file.txt'
    dir_contents = ['file.txt']
    file_contents = 'Lengthen your stride'


    def testExists(self):
        self.failUnless(self.filesystem.exists(self.dir_name))
        self.failUnless(self.filesystem.exists(self.file_name))


    def testIsDir(self):
        self.failUnless(self.filesystem.isdir(self.dir_name))
        self.failUnless(not self.filesystem.isdir(self.file_name))


    def testIsFile(self):
        self.failUnless(self.filesystem.isfile(self.file_name))
        self.failUnless(not self.filesystem.isfile(self.dir_name))


    def testListDir(self):
        lst = self.filesystem.listdir(self.dir_name, 0)
        lst.sort()
        self.assertEqual(lst, self.dir_contents)


    def testReadFile(self):
        s = StringIO()
        self.filesystem.readfile(self.file_name, 'rb', s)
        self.assertEqual(s.getvalue(), self.file_contents)


    def testReadPartOfFile(self):
        s = StringIO()
        self.filesystem.readfile(self.file_name, 'rb', s, 2)
        self.assertEqual(s.getvalue(), self.file_contents[2:])


    def testReadPartOfFile2(self):
        s = StringIO()
        self.filesystem.readfile(self.file_name, 'rb', s, 1, 5)
        self.assertEqual(s.getvalue(), self.file_contents[1:5])


    def testReadInterface(self):
        class_ = self.filesystem.__class__
        self.failUnless(
            IReadFileSystem.isImplementedByInstancesOf(class_))
        self.failUnless(verifyClass(IReadFileSystem, class_))



=== Added File Zope3/lib/python/Zope/Server/VFS/tests/WriteFilesystemTests.py ===
##############################################################################
#
# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""

$Id: WriteFilesystemTests.py,v 1.1.2.1 2002/04/12 19:04:30 shane Exp $
"""


from StringIO import StringIO

from Interface.Verify import verifyClass
from Zope.Server.VFS.IWriteFileSystem import IWriteFileSystem

from ReadFilesystemTests import ReadFilesystemTests


class WriteFilesystemTests (ReadFilesystemTests):
    """Tests of a writable and readable filesystem
    """

    def testRemove(self):
        self.failIf(not self.filesystem.exists(self.file_name))
        self.filesystem.remove(self.file_name)
        self.failIf(self.filesystem.exists(self.file_name))


    def testMkdir(self):
        path = self.dir_name + '/x'
        self.filesystem.mkdir(path)
        self.failUnless(self.filesystem.exists(path))
        self.failUnless(self.filesystem.isdir(path))


    def testRmdir(self):
        self.failIf(not self.filesystem.exists(self.dir_name))
        self.filesystem.remove(self.file_name)
        self.filesystem.rmdir(self.dir_name)
        self.failIf(self.filesystem.exists(self.dir_name))


    def testRename(self):
        self.filesystem.rename(self.file_name, self.file_name + '.bak')
        self.failIf(self.filesystem.exists(self.file_name))
        self.failIf(not self.filesystem.exists(self.file_name + '.bak'))


    def testWriteFile(self):
        s = StringIO()
        self.filesystem.readfile(self.file_name, 'rb', s)
        self.assertEqual(s.getvalue(), self.file_contents)

        data = 'Always ' + self.file_contents
        s = StringIO(data)
        self.filesystem.writefile(self.file_name, 'wb', s)

        s = StringIO()
        self.filesystem.readfile(self.file_name, 'rb', s)
        self.assertEqual(s.getvalue(), data)


    def testAppendToFile(self):
        data = ' again'
        s = StringIO(data)
        self.filesystem.writefile(self.file_name, 'ab', s)

        s = StringIO()
        self.filesystem.readfile(self.file_name, 'rb', s)
        self.assertEqual(s.getvalue(), self.file_contents + data)
        

    def testWritePartOfFile(self):
        data = '123'
        s = StringIO(data)
        self.filesystem.writefile(self.file_name, 'r+b', s, 3)

        expect = self.file_contents[:3] + data + self.file_contents[6:]

        s = StringIO()
        self.filesystem.readfile(self.file_name, 'rb', s)
        self.assertEqual(s.getvalue(), expect)


    def testWriteBeyondEndOfFile(self):
        partlen = len(self.file_contents) - 6
        data = 'daylight savings'
        s = StringIO(data)
        self.filesystem.writefile(self.file_name, 'r+b', s, partlen)

        expect = self.file_contents[:partlen] + data

        s = StringIO()
        self.filesystem.readfile(self.file_name, 'rb', s)
        self.assertEqual(s.getvalue(), expect)


    def testWriteNewFile(self):
        s = StringIO(self.file_contents)
        self.filesystem.writefile(self.file_name + '.new', 'wb', s)

        s = StringIO()
        self.filesystem.readfile(self.file_name, 'rb', s)
        self.assertEqual(s.getvalue(), self.file_contents)


    def testCheckWriteable(self):
        # Can't overwrite a directory.
        self.assertRaises(
            IOError, self.filesystem.check_writable, self.dir_name)
        # Can overwrite a file.
        try:
            self.filesystem.check_writable(self.file_name)
        except IOError, v:
            self.fail('%s should be writable. (%s)' % (self.file_name, v))


    def testWriteInterface(self):
        class_ = self.filesystem.__class__
        self.failUnless(
            IWriteFileSystem.isImplementedByInstancesOf(class_))
        self.failUnless(verifyClass(IWriteFileSystem, class_))


def test_suite():
    loader = unittest.TestLoader()
    return loader.loadTestsFromTestCase(OSFileSystemTest)

if __name__=='__main__':
    unittest.TextTestRunner().run( test_suite() )


=== Zope3/lib/python/Zope/Server/VFS/tests/testOSFileSystem.py 1.1.2.9 => 1.1.2.10 ===
 import unittest
 import os
-import stat
+import shutil
 import tempfile
 from StringIO import StringIO
 
-from Interface.Verify import verifyClass
-from Zope.Server.VFS.IReadFileSystem import IReadFileSystem
-from Zope.Server.VFS.IWriteFileSystem import IWriteFileSystem
-
 from Zope.Server.VFS.OSFileSystem import OSFileSystem
 
-class OSFileSystemTest(unittest.TestCase):
+from WriteFilesystemTests import WriteFilesystemTests
+
+
+def joinToRoot(root, name):
+    if name.startswith('/'):
+        name = name[1:]
+    return os.path.join(root, os.path.normpath(name))
+
+
+class OSFileSystemTests(unittest.TestCase, WriteFilesystemTests):
     """This test is constructed in a way that it builds up a directory
        structure, whcih is removed at the end.
     """
@@ -43,10 +48,15 @@
             self.filesystem = self.filesystem_class(self.root)
 
         os.mkdir(self.root)
+        os.mkdir(joinToRoot(self.root, self.dir_name))
+        f = open(joinToRoot(self.root, self.file_name), 'w')
+        f.write(self.file_contents)
+        f.close()
 
 
     def tearDown(self):
-        os.rmdir(self.root)
+
+        shutil.rmtree(self.root)
 
 
     def testNormalize(self):
@@ -75,175 +85,36 @@
         self.assertEqual(self.filesystem.root, self.root)
 
         self.assertEqual(self.filesystem.translate('/foo/'),
-                         self.filesystem.path_module.join(self.root, 'foo'))
+                         os.path.join(self.root, 'foo'))
         self.assertEqual(self.filesystem.translate('/foo/bar'),
-                         self.filesystem.path_module.join(self.root,
-                                                          'foo', 'bar'))
+                         os.path.join(self.root, 'foo', 'bar'))
         self.assertEqual(self.filesystem.translate('foo/bar'),
-                         self.filesystem.path_module.join(self.root,
-                                                          'foo', 'bar'))
+                         os.path.join(self.root, 'foo', 'bar'))
 
-    def testExists(self):
-        path = os.path.join(self.root, 'foo')
-        open(path, 'w').write('test')
-        self.failUnless(self.filesystem.exists('foo'))
-        os.remove(path)
-
-
-    def testIsDir(self):
-        path = os.path.join(self.root, 'foo')
-        os.mkdir(path)
-        self.failUnless(self.filesystem.isdir('foo'))
-        os.rmdir(path)
-
-
-    def testIsFile(self):
-        path = os.path.join(self.root, 'foo')
-        open(path, 'w').write('test')
-        self.failUnless(self.filesystem.isfile('foo'))
-        os.remove(path)
-
-
-    def testListDir(self):
-        path = os.path.join(self.root, 'foo')
-        open(path, 'w').write('test')
-        self.assertEqual(self.filesystem.listdir('/', 0),
-                         ['foo'])
-        os.remove(path)
-
-
-    def testReadfile(self):
-        path = os.path.join(self.root, 'foo')
-        open(path, 'w').write('writing test')
-        s = StringIO()
-        self.filesystem.readfile('foo', 'r', s)
-        self.assertEqual(s.getvalue(), 'writing test')
-        os.remove(path)
+    def testStat(self):
+        stat = os.stat(joinToRoot(self.root, self.file_name))
+        self.assertEqual(self.filesystem.stat(self.file_name), stat)
 
 
-    def testStat(self):
-        path = os.path.join(self.root, 'foo')
-        open(path, 'w').write('writing test')
-        self.assertEqual(self.filesystem.stat('foo'), os.stat(path))
-        os.remove(path)
-
-
-    def testChmod(self):
-        path = os.path.join(self.root, 'foo')
-        open(path, 'w').write('writing test')
-        mode = 6*2**6 + 4*2**3 + 4*2**0 # 644
-        self.filesystem.chmod('foo', mode)
-        self.assertEqual(('%o' %os.stat(path)[stat.ST_MODE])[-3:], '644')
-        os.remove(path)
-
-
-    def testChown(self):
-        # This test is disabled until we start using a RAM filesystem
-        # for testing.
-        return
-        path = os.path.join(self.root, 'foo')
-        open(path, 'w').write('writing test')
-        self.filesystem.chown('foo', 500, 500)
-        self.assertEqual(os.stat(path)[stat.ST_UID], 500)
-        self.assertEqual(os.stat(path)[stat.ST_GID], 500)
-        os.remove(path)
-
-
-    def testLink(self):
-        path = os.path.join(self.root, 'foo')
-        open(path, 'w').write('writing test')
-        self.filesystem.link('foo', 'foo1')
-        self.failUnless(os.path.exists(path+'1'))
-        os.remove(path)
-        os.remove(path+'1')
-
-
-    def testMkdir(self):
-        path = os.path.join(self.root, 'foo')
-        self.filesystem.mkdir('foo')
-        self.failUnless(os.path.exists(path))
-        self.failUnless(os.path.isdir(path))
-        os.rmdir(path)
-
-
-    def testFifo(self):
-        path = os.path.join(self.root, 'foo')
-        self.filesystem.mkfifo(path)
-        self.failUnless(os.path.exists(path))
-        os.remove(path)
-
-
-    def testRemove(self):
-        path = os.path.join(self.root, 'foo')
-        open(path, 'w').write('writing test')
-        self.filesystem.remove('foo')
-        self.failIf(os.path.exists(path))
-
-
-    def testRmdir(self):
-        path = os.path.join(self.root, 'foo')
-        os.mkdir(path)
-        self.filesystem.rmdir('foo')
-        self.failIf(os.path.exists(path))
-
-
-    def testRename(self):
-        path = os.path.join(self.root, 'foo')
-        open(path, 'w').write('writing test')
-        self.filesystem.rename('foo', 'foo1')
-        self.failUnless(os.path.exists(path+'1'))
-        os.remove(path+'1')
-
-
-    def testSymlink(self):
-        path = os.path.join(self.root, 'foo')
-        open(path, 'w').write('writing test')
-        self.filesystem.symlink('foo', 'foo1')
-        self.failUnless(os.path.exists(path+'1'))
-        os.remove(path)
-        os.remove(path+'1')
-
-
-    def testWritefile(self):
-        s = StringIO('foo bar')
-        self.filesystem.writefile('foo', 'w', s)
-        path = os.path.join(self.root, 'foo')
-        file = open(path, 'r')
-        self.assertEqual(file.read(), 'foo bar')
-        file.close()
-        os.remove(path)
-
-
-    def testCheckWriteable(self):
-        s = StringIO()
-        dirpath = os.path.join(self.root, 'somename')
-        os.mkdir(dirpath)
-        try:
-            # Can't overwrite a directory.
-            self.assertRaises(
-                IOError, self.filesystem.writefile, 'somename', 'w', s)
-        finally:
-            os.rmdir(dirpath)
-        try:
-            # Now it's ok.
-            self.filesystem.writefile('somename', 'w', s)
-        finally:
-            os.remove(dirpath)
-
-
-    def testInterface(self):
-        self.failUnless(
-            IReadFileSystem.isImplementedByInstancesOf(self.filesystem_class))
-        self.failUnless(verifyClass(IReadFileSystem, self.filesystem_class))
-
-        self.failUnless(
-            IWriteFileSystem.isImplementedByInstancesOf(self.filesystem_class))
-        self.failUnless(verifyClass(IWriteFileSystem, self.filesystem_class))
+
+
+if 0 and os.name == 'posix':
+
+    from PosixFilesystemTests import PosixFilesystemTests
+
+    class OSPosixFilesystemTests(OSFileSystemTests, PosixFilesystemTests):
+
+        def testChown(self):
+            # Disable this test, since it won't work unless you're root.
+            return
+
+    OSFileSystemTests = OSPosixFilesystemTests
+
 
 
 def test_suite():
     loader = unittest.TestLoader()
-    return loader.loadTestsFromTestCase(OSFileSystemTest)
+    return loader.loadTestsFromTestCase(OSFileSystemTests)
 
 if __name__=='__main__':
     unittest.TextTestRunner().run( test_suite() )