Fixed #14961 -- Revised staticfiles's prefix handling to make sure it runs on Windows. Also revised staticfiles tests to pass on Windows.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@15119 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Jannis Leidel 2010-12-31 14:22:32 +00:00
parent f2c4c63cdc
commit 0de63c96f2
3 changed files with 22 additions and 25 deletions

View File

@ -79,7 +79,7 @@ class FileSystemFinder(BaseFinder):
absolute path (or ``None`` if no match). absolute path (or ``None`` if no match).
""" """
if prefix: if prefix:
prefix = '%s/' % prefix prefix = '%s%s' % (prefix, os.sep)
if not path.startswith(prefix): if not path.startswith(prefix):
return None return None
path = path[len(prefix):] path = path[len(prefix):]
@ -144,7 +144,7 @@ class AppDirectoriesFinder(BaseFinder):
storage = self.storages[app] storage = self.storages[app]
prefix = storage.get_prefix() prefix = storage.get_prefix()
if prefix: if prefix:
prefix = '%s/' % prefix prefix = '%s%s' % (prefix, os.sep)
if not path.startswith(prefix): if not path.startswith(prefix):
return None return None
path = path[len(prefix):] path = path[len(prefix):]

View File

@ -47,6 +47,7 @@ def serve(request, path, document_root=None, show_indexes=False, insecure=False)
"the --insecure option of 'runserver' is " "the --insecure option of 'runserver' is "
"used") "used")
if not document_root: if not document_root:
path = os.path.normpath(path)
absolute_path = finders.find(path) absolute_path = finders.find(path)
if not absolute_path: if not absolute_path:
raise Http404('"%s" could not be found' % path) raise Http404('"%s" could not be found' % path)

View File

@ -15,7 +15,8 @@ from django.template import Template, Context
from django.test import TestCase from django.test import TestCase
TEST_ROOT = os.path.dirname(__file__) TEST_ROOT = os.path.normcase(os.path.dirname(__file__))
class StaticFilesTestCase(TestCase): class StaticFilesTestCase(TestCase):
""" """
@ -51,8 +52,8 @@ class StaticFilesTestCase(TestCase):
'django.contrib.staticfiles.finders.DefaultStorageFinder', 'django.contrib.staticfiles.finders.DefaultStorageFinder',
) )
settings.INSTALLED_APPS = [ settings.INSTALLED_APPS = [
"django.contrib.staticfiles", 'django.contrib.staticfiles',
"regressiontests.staticfiles_tests", 'regressiontests.staticfiles_tests',
] ]
# Clear the cached default_storage out, this is because when it first # Clear the cached default_storage out, this is because when it first
@ -121,7 +122,6 @@ class TestDefaults(object):
def test_staticfiles_dirs(self): def test_staticfiles_dirs(self):
""" """
Can find a file in a STATICFILES_DIRS directory. Can find a file in a STATICFILES_DIRS directory.
""" """
self.assertFileContains('test.txt', 'Can we find') self.assertFileContains('test.txt', 'Can we find')
@ -129,21 +129,18 @@ class TestDefaults(object):
""" """
Can find a file in a subdirectory of a STATICFILES_DIRS Can find a file in a subdirectory of a STATICFILES_DIRS
directory. directory.
""" """
self.assertFileContains('subdir/test.txt', 'Can we find') self.assertFileContains('subdir/test.txt', 'Can we find')
def test_staticfiles_dirs_priority(self): def test_staticfiles_dirs_priority(self):
""" """
File in STATICFILES_DIRS has priority over file in app. File in STATICFILES_DIRS has priority over file in app.
""" """
self.assertFileContains('test/file.txt', 'STATICFILES_DIRS') self.assertFileContains('test/file.txt', 'STATICFILES_DIRS')
def test_app_files(self): def test_app_files(self):
""" """
Can find a file in an app media/ directory. Can find a file in an app media/ directory.
""" """
self.assertFileContains('test/file1.txt', 'file1 in the app dir') self.assertFileContains('test/file1.txt', 'file1 in the app dir')
@ -249,7 +246,6 @@ if sys.platform != 'win32':
def test_links_created(self): def test_links_created(self):
""" """
With ``--link``, symbolic links are created. With ``--link``, symbolic links are created.
""" """
self.assertTrue(os.path.islink(os.path.join(settings.STATIC_ROOT, 'test.txt'))) self.assertTrue(os.path.islink(os.path.join(settings.STATIC_ROOT, 'test.txt')))
@ -258,7 +254,7 @@ class TestServeStatic(StaticFilesTestCase):
""" """
Test static asset serving view. Test static asset serving view.
""" """
urls = "regressiontests.staticfiles_tests.urls.default" urls = 'regressiontests.staticfiles_tests.urls.default'
def _response(self, filepath): def _response(self, filepath):
return self.client.get( return self.client.get(
@ -280,8 +276,8 @@ class TestServeDisabled(TestServeStatic):
settings.DEBUG = False settings.DEBUG = False
def test_disabled_serving(self): def test_disabled_serving(self):
self.assertRaisesRegexp(ImproperlyConfigured, "The view to serve " self.assertRaisesRegexp(ImproperlyConfigured, 'The view to serve '
"static files can only be used if the DEBUG setting is True", 'static files can only be used if the DEBUG setting is True',
self._response, 'test.txt') self._response, 'test.txt')
@ -295,7 +291,7 @@ class TestServeStaticWithURLHelper(TestServeStatic, TestDefaults):
""" """
Test static asset serving view with staticfiles_urlpatterns helper. Test static asset serving view with staticfiles_urlpatterns helper.
""" """
urls = "regressiontests.staticfiles_tests.urls.helper" urls = 'regressiontests.staticfiles_tests.urls.helper'
class TestServeAdminMedia(TestServeStatic): class TestServeAdminMedia(TestServeStatic):
@ -330,9 +326,9 @@ class TestFileSystemFinder(StaticFilesTestCase, FinderTestCase):
def setUp(self): def setUp(self):
super(TestFileSystemFinder, self).setUp() super(TestFileSystemFinder, self).setUp()
self.finder = finders.FileSystemFinder() self.finder = finders.FileSystemFinder()
test_file_path = os.path.join(TEST_ROOT, 'project/documents/test/file.txt') test_file_path = os.path.join(TEST_ROOT, 'project', 'documents', 'test', 'file.txt')
self.find_first = ("test/file.txt", test_file_path) self.find_first = (os.path.join('test', 'file.txt'), test_file_path)
self.find_all = ("test/file.txt", [test_file_path]) self.find_all = (os.path.join('test', 'file.txt'), [test_file_path])
class TestAppDirectoriesFinder(StaticFilesTestCase, FinderTestCase): class TestAppDirectoriesFinder(StaticFilesTestCase, FinderTestCase):
@ -342,9 +338,9 @@ class TestAppDirectoriesFinder(StaticFilesTestCase, FinderTestCase):
def setUp(self): def setUp(self):
super(TestAppDirectoriesFinder, self).setUp() super(TestAppDirectoriesFinder, self).setUp()
self.finder = finders.AppDirectoriesFinder() self.finder = finders.AppDirectoriesFinder()
test_file_path = os.path.join(TEST_ROOT, 'apps/test/static/test/file1.txt') test_file_path = os.path.join(TEST_ROOT, 'apps', 'test', 'static', 'test', 'file1.txt')
self.find_first = ("test/file1.txt", test_file_path) self.find_first = (os.path.join('test', 'file1.txt'), test_file_path)
self.find_all = ("test/file1.txt", [test_file_path]) self.find_all = (os.path.join('test', 'file1.txt'), [test_file_path])
class TestDefaultStorageFinder(StaticFilesTestCase, FinderTestCase): class TestDefaultStorageFinder(StaticFilesTestCase, FinderTestCase):
@ -356,8 +352,8 @@ class TestDefaultStorageFinder(StaticFilesTestCase, FinderTestCase):
self.finder = finders.DefaultStorageFinder( self.finder = finders.DefaultStorageFinder(
storage=storage.StaticFilesStorage(location=settings.MEDIA_ROOT)) storage=storage.StaticFilesStorage(location=settings.MEDIA_ROOT))
test_file_path = os.path.join(settings.MEDIA_ROOT, 'media-file.txt') test_file_path = os.path.join(settings.MEDIA_ROOT, 'media-file.txt')
self.find_first = ("media-file.txt", test_file_path) self.find_first = ('media-file.txt', test_file_path)
self.find_all = ("media-file.txt", [test_file_path]) self.find_all = ('media-file.txt', [test_file_path])
class TestMiscFinder(TestCase): class TestMiscFinder(TestCase):
@ -366,9 +362,9 @@ class TestMiscFinder(TestCase):
""" """
def test_get_finder(self): def test_get_finder(self):
self.assertTrue(isinstance(finders.get_finder( self.assertTrue(isinstance(finders.get_finder(
"django.contrib.staticfiles.finders.FileSystemFinder"), 'django.contrib.staticfiles.finders.FileSystemFinder'),
finders.FileSystemFinder)) finders.FileSystemFinder))
self.assertRaises(ImproperlyConfigured, self.assertRaises(ImproperlyConfigured,
finders.get_finder, "django.contrib.staticfiles.finders.FooBarFinder") finders.get_finder, 'django.contrib.staticfiles.finders.FooBarFinder')
self.assertRaises(ImproperlyConfigured, self.assertRaises(ImproperlyConfigured,
finders.get_finder, "foo.bar.FooBarFinder") finders.get_finder, 'foo.bar.FooBarFinder')