Ticket #24826: fix-test-extended-length-storage.patch

File fix-test-extended-length-storage.patch, 2.4 KB (added by Raphaël Hertzog, 9 years ago)

Tested patch

  • django/core/files/storage.py

    Description: Fix failing test when AUFS is in use
     AUFS only supports filenames up to 242 characters and a test
     was wrongly assuming that a 255 characters filename would be possible.
    Bug: https://code.djangoproject.com/ticket/24826
    Author: Claude Paroz <claude@2xlibre.net> with changes by Raphaël Hertzog <hertzog@debian.org>
    
    a b from django.utils.crypto import get_rand  
    1313from django.utils.deconstruct import deconstructible
    1414from django.utils.deprecation import RemovedInDjango20Warning
    1515from django.utils.encoding import filepath_to_uri, force_text
    16 from django.utils.functional import LazyObject
     16from django.utils.functional import LazyObject, cached_property
    1717from django.utils.module_loading import import_string
    1818from django.utils.six.moves.urllib.parse import urljoin
    1919from django.utils.text import get_valid_filename
    class Storage(object):  
    2727    storage systems can inherit or override, as necessary.
    2828    """
    2929
     30    MAX_FILENAME_LENGTH = 255  # Should be safe on most backends
     31
    3032    # The following methods represent a public interface to private methods.
    3133    # These shouldn't be overridden by subclasses unless absolutely necessary.
    3234
    class FileSystemStorage(Storage):  
    198200            else settings.FILE_UPLOAD_DIRECTORY_PERMISSIONS
    199201        )
    200202
     203    @cached_property
     204    def MAX_FILENAME_LENGTH(self):
     205        dir_to_test = self.location
     206        while not os.path.exists(dir_to_test):
     207            dir_to_test = os.path.dirname(dir_to_test)
     208        try:
     209            return os.pathconf(dir_to_test, 'PC_NAME_MAX')
     210        except Exception:
     211            return Storage.MAX_FILENAME_LENGTH
     212
    201213    def _open(self, name, mode='rb'):
    202214        return File(open(self.path(name), mode))
    203215
  • tests/file_storage/tests.py

    a b class FileFieldStorageTests(SimpleTestCa  
    534534    def test_extended_length_storage(self):
    535535        # Testing FileField with max_length > 255. Most systems have filename
    536536        # length limitation of 255. Path takes extra chars.
    537         filename = 251 * 'a'  # 4 chars for extension.
     537        filename = (temp_storage.MAX_FILENAME_LENGTH - 4) * 'a'  # 4 chars for extension.
    538538        obj = Storage()
    539539        obj.extended_length.save('%s.txt' % filename, ContentFile('Same Content'))
    540540        self.assertEqual(obj.extended_length.name, 'tests/%s.txt' % filename)
Back to Top