Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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
  • Loading branch information...
commit 0de63c96f2a230d8e768ea876b6eb4bc3c8f80dd 1 parent f2c4c63
Jannis Leidel authored December 31, 2010
4  django/contrib/staticfiles/finders.py
@@ -79,7 +79,7 @@ def find_location(self, root, path, prefix=None):
79 79
         absolute path (or ``None`` if no match).
80 80
         """
81 81
         if prefix:
82  
-            prefix = '%s/' % prefix
  82
+            prefix = '%s%s' % (prefix, os.sep)
83 83
             if not path.startswith(prefix):
84 84
                 return None
85 85
             path = path[len(prefix):]
@@ -144,7 +144,7 @@ def find_in_app(self, app, path):
144 144
         storage = self.storages[app]
145 145
         prefix = storage.get_prefix()
146 146
         if prefix:
147  
-            prefix = '%s/' % prefix
  147
+            prefix = '%s%s' % (prefix, os.sep)
148 148
             if not path.startswith(prefix):
149 149
                 return None
150 150
             path = path[len(prefix):]
1  django/contrib/staticfiles/views.py
@@ -47,6 +47,7 @@ def serve(request, path, document_root=None, show_indexes=False, insecure=False)
47 47
                                    "the --insecure option of 'runserver' is "
48 48
                                    "used")
49 49
     if not document_root:
  50
+        path = os.path.normpath(path)
50 51
         absolute_path = finders.find(path)
51 52
         if not absolute_path:
52 53
             raise Http404('"%s" could not be found' % path)
42  tests/regressiontests/staticfiles_tests/tests.py
@@ -15,7 +15,8 @@
15 15
 from django.test import TestCase
16 16
 
17 17
 
18  
-TEST_ROOT = os.path.dirname(__file__)
  18
+TEST_ROOT = os.path.normcase(os.path.dirname(__file__))
  19
+
19 20
 
20 21
 class StaticFilesTestCase(TestCase):
21 22
     """
@@ -51,8 +52,8 @@ def setUp(self):
51 52
             'django.contrib.staticfiles.finders.DefaultStorageFinder',
52 53
         )
53 54
         settings.INSTALLED_APPS = [
54  
-            "django.contrib.staticfiles",
55  
-            "regressiontests.staticfiles_tests",
  55
+            'django.contrib.staticfiles',
  56
+            'regressiontests.staticfiles_tests',
56 57
         ]
57 58
 
58 59
         # Clear the cached default_storage out, this is because when it first
@@ -121,7 +122,6 @@ class TestDefaults(object):
121 122
     def test_staticfiles_dirs(self):
122 123
         """
123 124
         Can find a file in a STATICFILES_DIRS directory.
124  
-
125 125
         """
126 126
         self.assertFileContains('test.txt', 'Can we find')
127 127
 
@@ -129,21 +129,18 @@ def test_staticfiles_dirs_subdir(self):
129 129
         """
130 130
         Can find a file in a subdirectory of a STATICFILES_DIRS
131 131
         directory.
132  
-
133 132
         """
134 133
         self.assertFileContains('subdir/test.txt', 'Can we find')
135 134
 
136 135
     def test_staticfiles_dirs_priority(self):
137 136
         """
138 137
         File in STATICFILES_DIRS has priority over file in app.
139  
-
140 138
         """
141 139
         self.assertFileContains('test/file.txt', 'STATICFILES_DIRS')
142 140
 
143 141
     def test_app_files(self):
144 142
         """
145 143
         Can find a file in an app media/ directory.
146  
-
147 144
         """
148 145
         self.assertFileContains('test/file1.txt', 'file1 in the app dir')
149 146
 
@@ -249,7 +246,6 @@ def run_collectstatic(self):
249 246
         def test_links_created(self):
250 247
             """
251 248
             With ``--link``, symbolic links are created.
252  
-
253 249
             """
254 250
             self.assertTrue(os.path.islink(os.path.join(settings.STATIC_ROOT, 'test.txt')))
255 251
 
@@ -258,7 +254,7 @@ class TestServeStatic(StaticFilesTestCase):
258 254
     """
259 255
     Test static asset serving view.
260 256
     """
261  
-    urls = "regressiontests.staticfiles_tests.urls.default"
  257
+    urls = 'regressiontests.staticfiles_tests.urls.default'
262 258
 
263 259
     def _response(self, filepath):
264 260
         return self.client.get(
@@ -280,8 +276,8 @@ def setUp(self):
280 276
         settings.DEBUG = False
281 277
 
282 278
     def test_disabled_serving(self):
283  
-        self.assertRaisesRegexp(ImproperlyConfigured, "The view to serve "
284  
-            "static files can only be used if the DEBUG setting is True",
  279
+        self.assertRaisesRegexp(ImproperlyConfigured, 'The view to serve '
  280
+            'static files can only be used if the DEBUG setting is True',
285 281
             self._response, 'test.txt')
286 282
 
287 283
 
@@ -295,7 +291,7 @@ class TestServeStaticWithURLHelper(TestServeStatic, TestDefaults):
295 291
     """
296 292
     Test static asset serving view with staticfiles_urlpatterns helper.
297 293
     """
298  
-    urls = "regressiontests.staticfiles_tests.urls.helper"
  294
+    urls = 'regressiontests.staticfiles_tests.urls.helper'
299 295
 
300 296
 
301 297
 class TestServeAdminMedia(TestServeStatic):
@@ -330,9 +326,9 @@ class TestFileSystemFinder(StaticFilesTestCase, FinderTestCase):
330 326
     def setUp(self):
331 327
         super(TestFileSystemFinder, self).setUp()
332 328
         self.finder = finders.FileSystemFinder()
333  
-        test_file_path = os.path.join(TEST_ROOT, 'project/documents/test/file.txt')
334  
-        self.find_first = ("test/file.txt", test_file_path)
335  
-        self.find_all = ("test/file.txt", [test_file_path])
  329
+        test_file_path = os.path.join(TEST_ROOT, 'project', 'documents', 'test', 'file.txt')
  330
+        self.find_first = (os.path.join('test', 'file.txt'), test_file_path)
  331
+        self.find_all = (os.path.join('test', 'file.txt'), [test_file_path])
336 332
 
337 333
 
338 334
 class TestAppDirectoriesFinder(StaticFilesTestCase, FinderTestCase):
@@ -342,9 +338,9 @@ class TestAppDirectoriesFinder(StaticFilesTestCase, FinderTestCase):
342 338
     def setUp(self):
343 339
         super(TestAppDirectoriesFinder, self).setUp()
344 340
         self.finder = finders.AppDirectoriesFinder()
345  
-        test_file_path = os.path.join(TEST_ROOT, 'apps/test/static/test/file1.txt')
346  
-        self.find_first = ("test/file1.txt", test_file_path)
347  
-        self.find_all = ("test/file1.txt", [test_file_path])
  341
+        test_file_path = os.path.join(TEST_ROOT, 'apps', 'test', 'static', 'test', 'file1.txt')
  342
+        self.find_first = (os.path.join('test', 'file1.txt'), test_file_path)
  343
+        self.find_all = (os.path.join('test', 'file1.txt'), [test_file_path])
348 344
 
349 345
 
350 346
 class TestDefaultStorageFinder(StaticFilesTestCase, FinderTestCase):
@@ -356,8 +352,8 @@ def setUp(self):
356 352
         self.finder = finders.DefaultStorageFinder(
357 353
             storage=storage.StaticFilesStorage(location=settings.MEDIA_ROOT))
358 354
         test_file_path = os.path.join(settings.MEDIA_ROOT, 'media-file.txt')
359  
-        self.find_first = ("media-file.txt", test_file_path)
360  
-        self.find_all = ("media-file.txt", [test_file_path])
  355
+        self.find_first = ('media-file.txt', test_file_path)
  356
+        self.find_all = ('media-file.txt', [test_file_path])
361 357
 
362 358
 
363 359
 class TestMiscFinder(TestCase):
@@ -366,9 +362,9 @@ class TestMiscFinder(TestCase):
366 362
     """
367 363
     def test_get_finder(self):
368 364
         self.assertTrue(isinstance(finders.get_finder(
369  
-            "django.contrib.staticfiles.finders.FileSystemFinder"),
  365
+            'django.contrib.staticfiles.finders.FileSystemFinder'),
370 366
             finders.FileSystemFinder))
371 367
         self.assertRaises(ImproperlyConfigured,
372  
-            finders.get_finder, "django.contrib.staticfiles.finders.FooBarFinder")
  368
+            finders.get_finder, 'django.contrib.staticfiles.finders.FooBarFinder')
373 369
         self.assertRaises(ImproperlyConfigured,
374  
-            finders.get_finder, "foo.bar.FooBarFinder")
  370
+            finders.get_finder, 'foo.bar.FooBarFinder')

0 notes on commit 0de63c9

Please sign in to comment.
Something went wrong with that request. Please try again.