Ian Lewis avatar Ian Lewis committed 83fa2f0

[s3boto] Fixed the listdir() method. fixes #57

Comments (0)

Files changed (2)

storages/backends/s3boto.py

 
     def listdir(self, name):
         name = self._normalize_name(self._clean_name(name))
+        # for the bucket.list and logic below name needs to end in /
+        # But for the root path "" we leave it as an empty string
+        if name:
+            name += '/'
+
         dirlist = self.bucket.list(self._encode_name(name))
         files = []
         dirs = set()
-        base_parts = name.split("/") if name else []
+        base_parts = name.split("/")[:-1]
         for item in dirlist:
             parts = item.name.split("/")
             parts = parts[len(base_parts):]

storages/tests/s3boto.py

 from django.conf import settings
 from django.core.files.storage import FileSystemStorage
 
+from boto.s3.key import Key
+
 from storages.backends import s3boto
 
 __all__ = (
     #    
     #    # show file does not exist
     #    self.assertFalse(self.storage.exists(name))
-    #
-    #def test_storage_listdir(self):
-    #    content = 'not blank'
-    #    file_names = ["1.txt", "2.txt", "3.txt", "4.txt"]
-    #    for name in file_names:
-    #        file = self.storage.open(self.prefix_path(name), 'w')
-    #        file.write(content)
-    #        file.close()
-    #    dir_names = ["a", "b", "c"]
-    #    for name in dir_names:
-    #        file = self.storage.open(self.prefix_path('%s/bar.txt' % name), 'w')
-    #        file.write(content)
-    #        file.close()
-    #    dirs, files = self.storage.listdir(self.path_prefix)
-    #    for name in file_names:
-    #        self.assertTrue(name in files)
-    #    for name in dir_names:
-    #        self.assertTrue(name in dirs)
-    #    
+
+    def test_storage_listdir_base(self):
+        file_names = ["some/path/1.txt", "2.txt", "other/path/3.txt", "4.txt"]
+
+        self.storage.bucket.list.return_value = []
+        for p in file_names:
+            key = mock.MagicMock(spec=Key)
+            key.name = p
+            self.storage.bucket.list.return_value.append(key)
+
+        dirs, files = self.storage.listdir("")
+
+        self.assertEqual(len(dirs), 2)
+        for directory in ["some", "other"]:
+            self.assertTrue(directory in dirs, 
+                            """ "%s" not in directory list "%s".""" % (
+                                directory, dirs))
+            
+        self.assertEqual(len(files), 2)
+        for filename in ["2.txt", "4.txt"]:
+            self.assertTrue(filename in files, 
+                            """ "%s" not in file list "%s".""" % (
+                                filename, files))
+
+    def test_storage_listdir_subdir(self):
+        file_names = ["some/path/1.txt", "some/2.txt"]
+
+        self.storage.bucket.list.return_value = []
+        for p in file_names:
+            key = mock.MagicMock(spec=Key)
+            key.name = p
+            self.storage.bucket.list.return_value.append(key)
+
+        dirs, files = self.storage.listdir("some/")
+        self.assertEqual(len(dirs), 1)
+        self.assertTrue('path' in dirs, 
+                        """ "path" not in directory list "%s".""" % (dirs,))
+            
+        self.assertEqual(len(files), 1)
+        self.assertTrue('2.txt' in files, 
+                        """ "2.txt" not in files list "%s".""" % (files,))
+
     #def test_storage_size(self):
     #    name = self.prefix_path('test_storage_size.txt')
     #    content = 'new content'
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.