gump-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From leosim...@apache.org
Subject svn commit: r156263 - in gump/branches/Gump3/pygump/python/gump: test/testUtilIO.py util/io.py
Date Sat, 05 Mar 2005 16:25:21 GMT
Author: leosimons
Date: Sat Mar  5 08:25:19 2005
New Revision: 156263

URL: http://svn.apache.org/viewcvs?view=rev&rev=156263
Log:
Working VFS code and tests to accompany it.

Added:
    gump/branches/Gump3/pygump/python/gump/test/testUtilIO.py
      - copied, changed from r156236, gump/branches/Gump3/pygump/python/gump/test/testExample.py
Modified:
    gump/branches/Gump3/pygump/python/gump/util/io.py

Copied: gump/branches/Gump3/pygump/python/gump/test/testUtilIO.py (from r156236, gump/branches/Gump3/pygump/python/gump/test/testExample.py)
URL: http://svn.apache.org/viewcvs/gump/branches/Gump3/pygump/python/gump/test/testUtilIO.py?view=diff&rev=156263&p1=gump/branches/Gump3/pygump/python/gump/test/testExample.py&r1=156236&p2=gump/branches/Gump3/pygump/python/gump/test/testUtilIO.py&r2=156263
==============================================================================
--- gump/branches/Gump3/pygump/python/gump/test/testExample.py (original)
+++ gump/branches/Gump3/pygump/python/gump/test/testUtilIO.py Sat Mar  5 08:25:19 2005
@@ -17,39 +17,119 @@
 __copyright__ = "Copyright (c) 2005 The Apache Software Foundation"
 __license__   = "http://www.apache.org/licenses/LICENSE-2.0"
 
-"""
-    This is an example of a testcase. Simply copy and rename this file
-    (the filename has to start with "test"), then rename the class below,
-    update the reference to the classname in the test_suite() method,
-    and write your test methods.
-    
-    See the documentation for the unittest package for more help with
-    tests. You can run all tests from the commandline using "./gump test".
-"""
-
 import unittest
 from unittest import TestCase
 
-class ExampleTestCase(TestCase):
-    def setUp(self):
-        # initialize tests here
-        pass
+import os
+import StringIO
+
+from gump.util.io import open_file_or_stream
+from gump.util.io import VFS
+
+class IOUtilTestCase(TestCase):
+    def test_open_file_or_stream(self):
+        contents = "blahblah"
+        filename = os.path.join(os.getcwd(), "gump-ioutiltestcase-tmp.txt")
+        
+        try:
+            file = open_file_or_stream(filename, 'w')
+            file.write(contents)
+            file.close()
+            file = open_file_or_stream(filename, mode='r')
+            result = file.read()
+            file.close()
+            self.assertEqual(contents, result)
+        finally:
+            try:
+                os.remove(filename)
+            except:
+                pass
+        
+        stream = StringIO.StringIO(contents)
+        file = open_file_or_stream(stream)
+        result = file.read()
+        file.close()
+        self.assertEqual(contents, result)
+    
+    def test_error(self):
+        from gump.util.io import Error
+        try:
+            raise Error
+        except:
+            pass
     
-    def tearDown(self):
-        # clean up after tests here
-        pass
-        
-    def test_something(self):
-        # you can do anything inside a test
-        # use the assertXXX methods on TestCase
-        # to check conditions
-        self.assert_( True )
-        self.assertEquals( type({}), type({}) )
+    def test_vfs(self):
+        base = os.path.join(os.getcwd(), "gump-ioutiltestcase-base-tmp")
+        cache = os.path.join(os.getcwd(), "..", "gump-ioutiltestcase-cache-tmp")
+        try:
+            os.rmdir(base)
+        except:
+            pass
+        try:
+            os.mkdir(base)
+        except:
+            pass
+
+        try:
+            os.rmdir(cache)
+        except:
+            pass
+        try:
+            os.mkdir(cache)
+        except:
+            pass
+        
+        vfs = VFS(False, False)
+        self.assertEqual(None, vfs.filesystem_root)
+        self.assertEqual(None, vfs.cachedir)
+
+        vfs = VFS(base, cache)
+        self.assertEqual(os.path.normpath(os.path.abspath(base)), vfs.filesystem_root)
+        self.assertEqual(os.path.normpath(os.path.abspath(cache)), vfs.cachedir)
+        vfs = VFS(base, None)
+        
+        contents = "blahblah"
+        filename = os.path.join(base, "gump-ioutiltestcase-tmp.txt")
+        
+        try:
+            file = open_file_or_stream(filename, 'w')
+            file.write(contents)
+            file.close()
+            file = vfs.get_as_stream("gump-ioutiltestcase-tmp.txt")
+            result = file.read()
+            file.close()
+            self.assertEqual(contents, result)
+        finally:
+            try:
+                os.remove(filename)
+            except:
+                pass
+        
+        try:
+            file = vfs.get_as_stream("http://gump.apache.org/")
+            result = file.read()
+            file.close()
+            self.assert_( result.index("<html>") > -1 )
+        except:
+            print """
+The testcase for the VFS utility needs a live internet connection. It seems
+that is currently not available. This is likely a non-critical problem.
+"""
+            raise
+        
+        try:
+            os.rmdir(base)
+        except:
+            pass
+        try:
+            os.rmdir(cache)
+        except:
+            pass
 
 # this is used by testrunner.py to determine what tests to run
 def test_suite():
     # be sure to change the referenceto the TestCase class you create above!
-    return unittest.makeSuite(ExampleTestCase,'test')
+    return unittest.makeSuite(IOUtilTestCase,'test')
 
 # this allows us to run this test by itself from the commandline
 if __name__ == '__main__':

Modified: gump/branches/Gump3/pygump/python/gump/util/io.py
URL: http://svn.apache.org/viewcvs/gump/branches/Gump3/pygump/python/gump/util/io.py?view=diff&r1=156262&r2=156263
==============================================================================
--- gump/branches/Gump3/pygump/python/gump/util/io.py (original)
+++ gump/branches/Gump3/pygump/python/gump/util/io.py Sat Mar  5 08:25:19 2005
@@ -74,7 +74,7 @@
             identified by the argument and returns it as a file-like object.
             Note that the returned file must be closed as normal!
     """
-    def __init__(self, filesystem_root = None, cachedir = None):
+    def __init__(self, filesystem_root=None, cachedir=None):
         """
         Create a new VFS. Arguments:
             
@@ -87,12 +87,12 @@
                           be used as the cache for downloaded files.
         """
         if filesystem_root:
-            self.filesystem_root = os.path.abspath(filesystem_root)
+            self.filesystem_root = os.path.normpath(os.path.abspath(filesystem_root))
         else:
             self.filesystem_root = None
             
         if cachedir:
-            self.cachedir = os.path.abspath(cachedir)
+            self.cachedir = os.path.normpath(os.path.abspath(cachedir))
         else:
             self.cachedir = None
     
@@ -128,7 +128,7 @@
             urllib.urlretrieve(url, path)
             return open(path)
         else:
-            return urllib.urlopen(path)
+            return urllib.urlopen(url)
 
     def _get_file_as_stream(self, path):
         """Get access to a local file as a stream for reading.
@@ -169,7 +169,7 @@
         if not os.path.exists(serverdir): os.mkdir(serverdir)
 
         filebase = string.replace(path, '/', '_')
-        filename = self._safe_filename(filebase + '___' + query + '___' + fragment_idenfitier)
+        filename = self._safe_filename(filebase + '___' + query + '___' + fragment_identifier)
         
         # we can be *reasonably* sure there's no two urls that result in
         # this same path...



Mime
View raw message