| 1 |
import os |
| 2 |
|
| 3 |
from django.conf import settings |
| 4 |
from django.contrib.staticfiles import finders, storage |
| 5 |
from django.core.exceptions import ImproperlyConfigured |
| 6 |
from django.test import SimpleTestCase, override_settings |
| 7 |
from django.utils.deprecation import RemovedInDjango61Warning |
| 8 |
|
| 9 |
from .cases import StaticFilesTestCase |
| 10 |
from .settings import TEST_ROOT |
| 11 |
|
| 12 |
DEPRECATION_MSG = ( |
| 13 |
"Passing the `all` argument to find() is deprecated. Use `find_all` instead." |
| 14 |
) |
| 15 |
|
| 16 |
|
| 17 |
class TestFinders: |
| 18 |
""" |
| 19 |
Base finder test mixin. |
| 20 |
|
| 21 |
On Windows, sometimes the case of the path we ask the finders for and the |
| 22 |
path(s) they find can differ. Compare them using os.path.normcase() to |
| 23 |
avoid false negatives. |
| 24 |
""" |
| 25 |
|
| 26 |
def test_find_first(self): |
| 27 |
src, dst = self.find_first |
| 28 |
found = self.finder.find(src) |
| 29 |
self.assertEqual(os.path.normcase(found), os.path.normcase(dst)) |
| 30 |
|
| 31 |
def test_find_all(self): |
| 32 |
src, dst = self.find_all |
| 33 |
found = self.finder.find(src, find_all=True) |
| 34 |
found = [os.path.normcase(f) for f in found] |
| 35 |
dst = [os.path.normcase(d) for d in dst] |
| 36 |
self.assertEqual(found, dst) |
| 37 |
|
| 38 |
def test_find_all_deprecated_param(self): |
| 39 |
src, dst = self.find_all |
| 40 |
with self.assertWarnsMessage(RemovedInDjango61Warning, DEPRECATION_MSG) as ctx: |
| 41 |
found = self.finder.find(src, all=True) |
| 42 |
found = [os.path.normcase(f) for f in found] |
| 43 |
dst = [os.path.normcase(d) for d in dst] |
| 44 |
self.assertEqual(found, dst) |
| 45 |
self.assertEqual(ctx.filename, __file__) |
| 46 |
|
| 47 |
def test_find_all_conflicting_params(self): |
| 48 |
src, dst = self.find_all |
| 49 |
msg = ( |
| 50 |
f"{self.finder.__class__.__qualname__}.find() got multiple values for " |
| 51 |
"argument 'find_all'" |
| 52 |
) |
| 53 |
with ( |
| 54 |
self.assertWarnsMessage(RemovedInDjango61Warning, DEPRECATION_MSG) as ctx, |
| 55 |
self.assertRaisesMessage(TypeError, msg), |
| 56 |
): |
| 57 |
self.finder.find(src, find_all=True, all=True) |
| 58 |
self.assertEqual(ctx.filename, __file__) |
| 59 |
|
| 60 |
def test_find_all_unexpected_params(self): |
| 61 |
src, dst = self.find_all |
| 62 |
msg = ( |
| 63 |
f"{self.finder.__class__.__qualname__}.find() got an unexpected keyword " |
| 64 |
"argument 'wrong'" |
| 65 |
) |
| 66 |
with ( |
| 67 |
self.assertWarnsMessage(RemovedInDjango61Warning, DEPRECATION_MSG) as ctx, |
| 68 |
self.assertRaisesMessage(TypeError, msg), |
| 69 |
): |
| 70 |
self.finder.find(src, all=True, wrong=1) |
| 71 |
self.assertEqual(ctx.filename, __file__) |
| 72 |
|
| 73 |
with self.assertRaisesMessage(TypeError, msg): |
| 74 |
self.finder.find(src, find_all=True, wrong=1) |
| 75 |
|
| 76 |
with self.assertRaisesMessage(TypeError, msg): |
| 77 |
self.finder.find(src, wrong=1) |
| 78 |
|
| 79 |
|
| 80 |
class TestFileSystemFinder(TestFinders, StaticFilesTestCase): |
| 81 |
""" |
| 82 |
Test FileSystemFinder. |
| 83 |
""" |
| 84 |
|
| 85 |
def setUp(self): |
| 86 |
super().setUp() |
| 87 |
self.finder = finders.FileSystemFinder() |
| 88 |
test_file_path = os.path.join( |
| 89 |
TEST_ROOT, "project", "documents", "test", "file.txt" |
| 90 |
) |
| 91 |
self.find_first = (os.path.join("test", "file.txt"), test_file_path) |
| 92 |
self.find_all = (os.path.join("test", "file.txt"), [test_file_path]) |
| 93 |
|
| 94 |
|
| 95 |
class TestAppDirectoriesFinder(TestFinders, StaticFilesTestCase): |
| 96 |
""" |
| 97 |
Test AppDirectoriesFinder. |
| 98 |
""" |
| 99 |
|
| 100 |
def setUp(self): |
| 101 |
super().setUp() |
| 102 |
self.finder = finders.AppDirectoriesFinder() |
| 103 |
test_file_path = os.path.join( |
| 104 |
TEST_ROOT, "apps", "test", "static", "test", "file1.txt" |
| 105 |
) |
| 106 |
self.find_first = (os.path.join("test", "file1.txt"), test_file_path) |
| 107 |
self.find_all = (os.path.join("test", "file1.txt"), [test_file_path]) |
| 108 |
|
| 109 |
|
| 110 |
class TestDefaultStorageFinder(TestFinders, StaticFilesTestCase): |
| 111 |
""" |
| 112 |
Test DefaultStorageFinder. |
| 113 |
""" |
| 114 |
|
| 115 |
def setUp(self): |
| 116 |
super().setUp() |
| 117 |
self.finder = finders.DefaultStorageFinder( |
| 118 |
storage=storage.StaticFilesStorage(location=settings.MEDIA_ROOT) |
| 119 |
) |
| 120 |
test_file_path = os.path.join(settings.MEDIA_ROOT, "media-file.txt") |
| 121 |
self.find_first = ("media-file.txt", test_file_path) |
| 122 |
self.find_all = ("media-file.txt", [test_file_path]) |
| 123 |
|
| 124 |
|
| 125 |
@override_settings( |
| 126 |
STATICFILES_FINDERS=["django.contrib.staticfiles.finders.FileSystemFinder"], |
| 127 |
STATICFILES_DIRS=[os.path.join(TEST_ROOT, "project", "documents")], |
| 128 |
) |
| 129 |
class TestMiscFinder(SimpleTestCase): |
| 130 |
""" |
| 131 |
A few misc finder tests. |
| 132 |
""" |
| 133 |
|
| 134 |
def test_get_finder(self): |
| 135 |
self.assertIsInstance( |
| 136 |
finders.get_finder("django.contrib.staticfiles.finders.FileSystemFinder"), |
| 137 |
finders.FileSystemFinder, |
| 138 |
) |
| 139 |
|
| 140 |
def test_get_finder_bad_classname(self): |
| 141 |
with self.assertRaises(ImportError): |
| 142 |
finders.get_finder("django.contrib.staticfiles.finders.FooBarFinder") |
| 143 |
|
| 144 |
def test_get_finder_bad_module(self): |
| 145 |
with self.assertRaises(ImportError): |
| 146 |
finders.get_finder("foo.bar.FooBarFinder") |
| 147 |
|
| 148 |
def test_cache(self): |
| 149 |
finders.get_finder.cache_clear() |
| 150 |
for n in range(10): |
| 151 |
finders.get_finder("django.contrib.staticfiles.finders.FileSystemFinder") |
| 152 |
cache_info = finders.get_finder.cache_info() |
| 153 |
self.assertEqual(cache_info.hits, 9) |
| 154 |
self.assertEqual(cache_info.currsize, 1) |
| 155 |
|
| 156 |
def test_searched_locations(self): |
| 157 |
finders.find("spam") |
| 158 |
self.assertEqual( |
| 159 |
finders.searched_locations, |
| 160 |
[os.path.join(TEST_ROOT, "project", "documents")], |
| 161 |
) |
| 162 |
|
| 163 |
def test_searched_locations_find_all(self): |
| 164 |
finders.find("spam", find_all=True) |
| 165 |
self.assertEqual( |
| 166 |
finders.searched_locations, |
| 167 |
[os.path.join(TEST_ROOT, "project", "documents")], |
| 168 |
) |
| 169 |
|
| 170 |
def test_searched_locations_deprecated_all(self): |
| 171 |
with self.assertWarnsMessage(RemovedInDjango61Warning, DEPRECATION_MSG) as ctx: |
| 172 |
finders.find("spam", all=True) |
| 173 |
self.assertEqual( |
| 174 |
finders.searched_locations, |
| 175 |
[os.path.join(TEST_ROOT, "project", "documents")], |
| 176 |
) |
| 177 |
self.assertEqual(ctx.filename, __file__) |
| 178 |
|
| 179 |
def test_searched_locations_conflicting_params(self): |
| 180 |
msg = "find() got multiple values for argument 'find_all'" |
| 181 |
with ( |
| 182 |
self.assertWarnsMessage(RemovedInDjango61Warning, DEPRECATION_MSG) as ctx, |
| 183 |
self.assertRaisesMessage(TypeError, msg), |
| 184 |
): |
| 185 |
finders.find("spam", find_all=True, all=True) |
| 186 |
self.assertEqual(ctx.filename, __file__) |
| 187 |
|
| 188 |
def test_searched_locations_unexpected_params(self): |
| 189 |
msg = "find() got an unexpected keyword argument 'wrong'" |
| 190 |
with ( |
| 191 |
self.assertWarnsMessage(RemovedInDjango61Warning, DEPRECATION_MSG) as ctx, |
| 192 |
self.assertRaisesMessage(TypeError, msg), |
| 193 |
): |
| 194 |
finders.find("spam", all=True, wrong=1) |
| 195 |
self.assertEqual(ctx.filename, __file__) |
| 196 |
|
| 197 |
with self.assertRaisesMessage(TypeError, msg): |
| 198 |
finders.find("spam", find_all=True, wrong=1) |
| 199 |
|
| 200 |
with self.assertRaisesMessage(TypeError, msg): |
| 201 |
finders.find("spam", wrong=1) |
| 202 |
|
| 203 |
@override_settings(MEDIA_ROOT="") |
| 204 |
def test_location_empty(self): |
| 205 |
msg = ( |
| 206 |
"The storage backend of the staticfiles finder " |
| 207 |
"<class 'django.contrib.staticfiles.finders.DefaultStorageFinder'> " |
| 208 |
"doesn't have a valid location." |
| 209 |
) |
| 210 |
with self.assertRaisesMessage(ImproperlyConfigured, msg): |
| 211 |
finders.DefaultStorageFinder() |