| 1 |
import datetime |
| 2 |
import os |
| 3 |
import shutil |
| 4 |
import tempfile |
| 5 |
import unittest |
| 6 |
from io import StringIO |
| 7 |
from pathlib import Path |
| 8 |
from unittest import mock |
| 9 |
|
| 10 |
from admin_scripts.tests import AdminScriptTestCase |
| 11 |
|
| 12 |
from django.conf import STATICFILES_STORAGE_ALIAS, settings |
| 13 |
from django.contrib.staticfiles import storage |
| 14 |
from django.contrib.staticfiles.management.commands import collectstatic, runserver |
| 15 |
from django.core.exceptions import ImproperlyConfigured |
| 16 |
from django.core.management import CommandError, call_command |
| 17 |
from django.core.management.base import SystemCheckError |
| 18 |
from django.test import RequestFactory, override_settings |
| 19 |
from django.test.utils import extend_sys_path |
| 20 |
from django.utils._os import symlinks_supported |
| 21 |
from django.utils.functional import empty |
| 22 |
|
| 23 |
from .cases import CollectionTestCase, StaticFilesTestCase, TestDefaults |
| 24 |
from .settings import TEST_ROOT, TEST_SETTINGS |
| 25 |
from .storage import DummyStorage |
| 26 |
|
| 27 |
|
| 28 |
class TestNoFilesCreated: |
| 29 |
def test_no_files_created(self): |
| 30 |
""" |
| 31 |
Make sure no files were create in the destination directory. |
| 32 |
""" |
| 33 |
self.assertEqual(os.listdir(settings.STATIC_ROOT), []) |
| 34 |
|
| 35 |
|
| 36 |
class TestRunserver(StaticFilesTestCase): |
| 37 |
@override_settings(MIDDLEWARE=["django.middleware.common.CommonMiddleware"]) |
| 38 |
def test_middleware_loaded_only_once(self): |
| 39 |
command = runserver.Command() |
| 40 |
with mock.patch("django.middleware.common.CommonMiddleware") as mocked: |
| 41 |
command.get_handler(use_static_handler=True, insecure_serving=True) |
| 42 |
self.assertEqual(mocked.call_count, 1) |
| 43 |
|
| 44 |
def test_404_response(self): |
| 45 |
command = runserver.Command() |
| 46 |
handler = command.get_handler(use_static_handler=True, insecure_serving=True) |
| 47 |
missing_static_file = os.path.join(settings.STATIC_URL, "unknown.css") |
| 48 |
req = RequestFactory().get(missing_static_file) |
| 49 |
with override_settings(DEBUG=False): |
| 50 |
response = handler.get_response(req) |
| 51 |
self.assertEqual(response.status_code, 404) |
| 52 |
with override_settings(DEBUG=True): |
| 53 |
response = handler.get_response(req) |
| 54 |
self.assertEqual(response.status_code, 404) |
| 55 |
|
| 56 |
|
| 57 |
class TestFindStatic(TestDefaults, CollectionTestCase): |
| 58 |
""" |
| 59 |
Test ``findstatic`` management command. |
| 60 |
""" |
| 61 |
|
| 62 |
def _get_file(self, filepath): |
| 63 |
path = call_command( |
| 64 |
"findstatic", filepath, all=False, verbosity=0, stdout=StringIO() |
| 65 |
) |
| 66 |
with open(path, encoding="utf-8") as f: |
| 67 |
return f.read() |
| 68 |
|
| 69 |
def test_all_files(self): |
| 70 |
""" |
| 71 |
findstatic returns all candidate files if run without --first and -v1. |
| 72 |
""" |
| 73 |
result = call_command( |
| 74 |
"findstatic", "test/file.txt", verbosity=1, stdout=StringIO() |
| 75 |
) |
| 76 |
lines = [line.strip() for line in result.split("\n")] |
| 77 |
self.assertEqual( |
| 78 |
len(lines), 3 |
| 79 |
) # three because there is also the "Found <file> here" line |
| 80 |
self.assertIn("project", lines[1]) |
| 81 |
self.assertIn("apps", lines[2]) |
| 82 |
|
| 83 |
def test_all_files_less_verbose(self): |
| 84 |
""" |
| 85 |
findstatic returns all candidate files if run without --first and -v0. |
| 86 |
""" |
| 87 |
result = call_command( |
| 88 |
"findstatic", "test/file.txt", verbosity=0, stdout=StringIO() |
| 89 |
) |
| 90 |
lines = [line.strip() for line in result.split("\n")] |
| 91 |
self.assertEqual(len(lines), 2) |
| 92 |
self.assertIn("project", lines[0]) |
| 93 |
self.assertIn("apps", lines[1]) |
| 94 |
|
| 95 |
def test_all_files_more_verbose(self): |
| 96 |
""" |
| 97 |
findstatic returns all candidate files if run without --first and -v2. |
| 98 |
Also, test that findstatic returns the searched locations with -v2. |
| 99 |
""" |
| 100 |
result = call_command( |
| 101 |
"findstatic", "test/file.txt", verbosity=2, stdout=StringIO() |
| 102 |
) |
| 103 |
lines = [line.strip() for line in result.split("\n")] |
| 104 |
self.assertIn("project", lines[1]) |
| 105 |
self.assertIn("apps", lines[2]) |
| 106 |
self.assertIn("Looking in the following locations:", lines[3]) |
| 107 |
searched_locations = ", ".join(lines[4:]) |
| 108 |
# AppDirectoriesFinder searched locations |
| 109 |
self.assertIn( |
| 110 |
os.path.join("staticfiles_tests", "apps", "test", "static"), |
| 111 |
searched_locations, |
| 112 |
) |
| 113 |
self.assertIn( |
| 114 |
os.path.join("staticfiles_tests", "apps", "no_label", "static"), |
| 115 |
searched_locations, |
| 116 |
) |
| 117 |
# FileSystemFinder searched locations |
| 118 |
self.assertIn(TEST_SETTINGS["STATICFILES_DIRS"][1][1], searched_locations) |
| 119 |
self.assertIn(TEST_SETTINGS["STATICFILES_DIRS"][0], searched_locations) |
| 120 |
self.assertIn(str(TEST_SETTINGS["STATICFILES_DIRS"][2]), searched_locations) |
| 121 |
# DefaultStorageFinder searched locations |
| 122 |
self.assertIn( |
| 123 |
os.path.join("staticfiles_tests", "project", "site_media", "media"), |
| 124 |
searched_locations, |
| 125 |
) |
| 126 |
|
| 127 |
def test_missing_args_message(self): |
| 128 |
msg = "Enter at least one staticfile." |
| 129 |
with self.assertRaisesMessage(CommandError, msg): |
| 130 |
call_command("findstatic") |
| 131 |
|
| 132 |
|
| 133 |
class TestConfiguration(StaticFilesTestCase): |
| 134 |
def test_location_empty(self): |
| 135 |
msg = "without having set the STATIC_ROOT setting to a filesystem path" |
| 136 |
err = StringIO() |
| 137 |
for root in ["", None]: |
| 138 |
with override_settings(STATIC_ROOT=root): |
| 139 |
with self.assertRaisesMessage(ImproperlyConfigured, msg): |
| 140 |
call_command( |
| 141 |
"collectstatic", interactive=False, verbosity=0, stderr=err |
| 142 |
) |
| 143 |
|
| 144 |
def test_local_storage_detection_helper(self): |
| 145 |
staticfiles_storage = storage.staticfiles_storage |
| 146 |
try: |
| 147 |
storage.staticfiles_storage._wrapped = empty |
| 148 |
with self.settings( |
| 149 |
STORAGES={ |
| 150 |
**settings.STORAGES, |
| 151 |
STATICFILES_STORAGE_ALIAS: { |
| 152 |
"BACKEND": ( |
| 153 |
"django.contrib.staticfiles.storage.StaticFilesStorage" |
| 154 |
) |
| 155 |
}, |
| 156 |
} |
| 157 |
): |
| 158 |
command = collectstatic.Command() |
| 159 |
self.assertTrue(command.is_local_storage()) |
| 160 |
|
| 161 |
storage.staticfiles_storage._wrapped = empty |
| 162 |
with self.settings( |
| 163 |
STORAGES={ |
| 164 |
**settings.STORAGES, |
| 165 |
STATICFILES_STORAGE_ALIAS: { |
| 166 |
"BACKEND": "staticfiles_tests.storage.DummyStorage" |
| 167 |
}, |
| 168 |
} |
| 169 |
): |
| 170 |
command = collectstatic.Command() |
| 171 |
self.assertFalse(command.is_local_storage()) |
| 172 |
|
| 173 |
collectstatic.staticfiles_storage = storage.FileSystemStorage() |
| 174 |
command = collectstatic.Command() |
| 175 |
self.assertTrue(command.is_local_storage()) |
| 176 |
|
| 177 |
collectstatic.staticfiles_storage = DummyStorage() |
| 178 |
command = collectstatic.Command() |
| 179 |
self.assertFalse(command.is_local_storage()) |
| 180 |
finally: |
| 181 |
staticfiles_storage._wrapped = empty |
| 182 |
collectstatic.staticfiles_storage = staticfiles_storage |
| 183 |
storage.staticfiles_storage = staticfiles_storage |
| 184 |
|
| 185 |
@override_settings(STATICFILES_DIRS=("test")) |
| 186 |
def test_collectstatis_check(self): |
| 187 |
msg = "The STATICFILES_DIRS setting is not a tuple or list." |
| 188 |
with self.assertRaisesMessage(SystemCheckError, msg): |
| 189 |
call_command("collectstatic", skip_checks=False) |
| 190 |
|
| 191 |
|
| 192 |
class TestCollectionHelpSubcommand(AdminScriptTestCase): |
| 193 |
@override_settings(STATIC_ROOT=None) |
| 194 |
def test_missing_settings_dont_prevent_help(self): |
| 195 |
""" |
| 196 |
Even if the STATIC_ROOT setting is not set, one can still call the |
| 197 |
`manage.py help collectstatic` command. |
| 198 |
""" |
| 199 |
self.write_settings("settings.py", apps=["django.contrib.staticfiles"]) |
| 200 |
out, err = self.run_manage(["help", "collectstatic"]) |
| 201 |
self.assertNoOutput(err) |
| 202 |
|
| 203 |
|
| 204 |
class TestCollection(TestDefaults, CollectionTestCase): |
| 205 |
""" |
| 206 |
Test ``collectstatic`` management command. |
| 207 |
""" |
| 208 |
|
| 209 |
def test_ignore(self): |
| 210 |
""" |
| 211 |
-i patterns are ignored. |
| 212 |
""" |
| 213 |
self.assertFileNotFound("test/test.ignoreme") |
| 214 |
|
| 215 |
def test_common_ignore_patterns(self): |
| 216 |
""" |
| 217 |
Common ignore patterns (*~, .*, CVS) are ignored. |
| 218 |
""" |
| 219 |
self.assertFileNotFound("test/.hidden") |
| 220 |
self.assertFileNotFound("test/backup~") |
| 221 |
self.assertFileNotFound("test/CVS") |
| 222 |
|
| 223 |
def test_pathlib(self): |
| 224 |
self.assertFileContains("pathlib.txt", "pathlib") |
| 225 |
|
| 226 |
|
| 227 |
class TestCollectionPathLib(TestCollection): |
| 228 |
def mkdtemp(self): |
| 229 |
tmp_dir = super().mkdtemp() |
| 230 |
return Path(tmp_dir) |
| 231 |
|
| 232 |
|
| 233 |
class TestCollectionVerbosity(CollectionTestCase): |
| 234 |
copying_msg = "Copying " |
| 235 |
run_collectstatic_in_setUp = False |
| 236 |
post_process_msg = "Post-processed" |
| 237 |
staticfiles_copied_msg = "static files copied to" |
| 238 |
|
| 239 |
def test_verbosity_0(self): |
| 240 |
stdout = StringIO() |
| 241 |
self.run_collectstatic(verbosity=0, stdout=stdout) |
| 242 |
self.assertEqual(stdout.getvalue(), "") |
| 243 |
|
| 244 |
def test_verbosity_1(self): |
| 245 |
stdout = StringIO() |
| 246 |
self.run_collectstatic(verbosity=1, stdout=stdout) |
| 247 |
output = stdout.getvalue() |
| 248 |
self.assertIn(self.staticfiles_copied_msg, output) |
| 249 |
self.assertNotIn(self.copying_msg, output) |
| 250 |
|
| 251 |
def test_verbosity_2(self): |
| 252 |
stdout = StringIO() |
| 253 |
self.run_collectstatic(verbosity=2, stdout=stdout) |
| 254 |
output = stdout.getvalue() |
| 255 |
self.assertIn(self.staticfiles_copied_msg, output) |
| 256 |
self.assertIn(self.copying_msg, output) |
| 257 |
|
| 258 |
@override_settings( |
| 259 |
STORAGES={ |
| 260 |
**settings.STORAGES, |
| 261 |
STATICFILES_STORAGE_ALIAS: { |
| 262 |
"BACKEND": ( |
| 263 |
"django.contrib.staticfiles.storage.ManifestStaticFilesStorage" |
| 264 |
) |
| 265 |
}, |
| 266 |
} |
| 267 |
) |
| 268 |
def test_verbosity_1_with_post_process(self): |
| 269 |
stdout = StringIO() |
| 270 |
self.run_collectstatic(verbosity=1, stdout=stdout, post_process=True) |
| 271 |
self.assertNotIn(self.post_process_msg, stdout.getvalue()) |
| 272 |
|
| 273 |
@override_settings( |
| 274 |
STORAGES={ |
| 275 |
**settings.STORAGES, |
| 276 |
STATICFILES_STORAGE_ALIAS: { |
| 277 |
"BACKEND": ( |
| 278 |
"django.contrib.staticfiles.storage.ManifestStaticFilesStorage" |
| 279 |
) |
| 280 |
}, |
| 281 |
} |
| 282 |
) |
| 283 |
def test_verbosity_2_with_post_process(self): |
| 284 |
stdout = StringIO() |
| 285 |
self.run_collectstatic(verbosity=2, stdout=stdout, post_process=True) |
| 286 |
self.assertIn(self.post_process_msg, stdout.getvalue()) |
| 287 |
|
| 288 |
|
| 289 |
class TestCollectionClear(CollectionTestCase): |
| 290 |
""" |
| 291 |
Test the ``--clear`` option of the ``collectstatic`` management command. |
| 292 |
""" |
| 293 |
|
| 294 |
def run_collectstatic(self, **kwargs): |
| 295 |
clear_filepath = os.path.join(settings.STATIC_ROOT, "cleared.txt") |
| 296 |
with open(clear_filepath, "w") as f: |
| 297 |
f.write("should be cleared") |
| 298 |
super().run_collectstatic(clear=True) |
| 299 |
|
| 300 |
def test_cleared_not_found(self): |
| 301 |
self.assertFileNotFound("cleared.txt") |
| 302 |
|
| 303 |
def test_dir_not_exists(self, **kwargs): |
| 304 |
shutil.rmtree(settings.STATIC_ROOT) |
| 305 |
super().run_collectstatic(clear=True) |
| 306 |
|
| 307 |
@override_settings( |
| 308 |
STORAGES={ |
| 309 |
**settings.STORAGES, |
| 310 |
STATICFILES_STORAGE_ALIAS: { |
| 311 |
"BACKEND": "staticfiles_tests.storage.PathNotImplementedStorage" |
| 312 |
}, |
| 313 |
} |
| 314 |
) |
| 315 |
def test_handle_path_notimplemented(self): |
| 316 |
self.run_collectstatic() |
| 317 |
self.assertFileNotFound("cleared.txt") |
| 318 |
|
| 319 |
|
| 320 |
class TestInteractiveMessages(CollectionTestCase): |
| 321 |
overwrite_warning_msg = "This will overwrite existing files!" |
| 322 |
delete_warning_msg = "This will DELETE ALL FILES in this location!" |
| 323 |
files_copied_msg = "static files copied" |
| 324 |
|
| 325 |
@staticmethod |
| 326 |
def mock_input(stdout): |
| 327 |
def _input(msg): |
| 328 |
stdout.write(msg) |
| 329 |
return "yes" |
| 330 |
|
| 331 |
return _input |
| 332 |
|
| 333 |
def test_warning_when_clearing_staticdir(self): |
| 334 |
stdout = StringIO() |
| 335 |
self.run_collectstatic() |
| 336 |
with mock.patch("builtins.input", side_effect=self.mock_input(stdout)): |
| 337 |
call_command("collectstatic", interactive=True, clear=True, stdout=stdout) |
| 338 |
|
| 339 |
output = stdout.getvalue() |
| 340 |
self.assertNotIn(self.overwrite_warning_msg, output) |
| 341 |
self.assertIn(self.delete_warning_msg, output) |
| 342 |
|
| 343 |
def test_warning_when_overwriting_files_in_staticdir(self): |
| 344 |
stdout = StringIO() |
| 345 |
self.run_collectstatic() |
| 346 |
with mock.patch("builtins.input", side_effect=self.mock_input(stdout)): |
| 347 |
call_command("collectstatic", interactive=True, stdout=stdout) |
| 348 |
output = stdout.getvalue() |
| 349 |
self.assertIn(self.overwrite_warning_msg, output) |
| 350 |
self.assertNotIn(self.delete_warning_msg, output) |
| 351 |
|
| 352 |
def test_no_warning_when_staticdir_does_not_exist(self): |
| 353 |
stdout = StringIO() |
| 354 |
shutil.rmtree(settings.STATIC_ROOT) |
| 355 |
call_command("collectstatic", interactive=True, stdout=stdout) |
| 356 |
output = stdout.getvalue() |
| 357 |
self.assertNotIn(self.overwrite_warning_msg, output) |
| 358 |
self.assertNotIn(self.delete_warning_msg, output) |
| 359 |
self.assertIn(self.files_copied_msg, output) |
| 360 |
|
| 361 |
def test_no_warning_for_empty_staticdir(self): |
| 362 |
stdout = StringIO() |
| 363 |
with tempfile.TemporaryDirectory( |
| 364 |
prefix="collectstatic_empty_staticdir_test" |
| 365 |
) as static_dir: |
| 366 |
with override_settings(STATIC_ROOT=static_dir): |
| 367 |
call_command("collectstatic", interactive=True, stdout=stdout) |
| 368 |
output = stdout.getvalue() |
| 369 |
self.assertNotIn(self.overwrite_warning_msg, output) |
| 370 |
self.assertNotIn(self.delete_warning_msg, output) |
| 371 |
self.assertIn(self.files_copied_msg, output) |
| 372 |
|
| 373 |
def test_cancelled(self): |
| 374 |
self.run_collectstatic() |
| 375 |
with mock.patch("builtins.input", side_effect=lambda _: "no"): |
| 376 |
with self.assertRaisesMessage( |
| 377 |
CommandError, "Collecting static files cancelled" |
| 378 |
): |
| 379 |
call_command("collectstatic", interactive=True) |
| 380 |
|
| 381 |
|
| 382 |
class TestCollectionNoDefaultIgnore(TestDefaults, CollectionTestCase): |
| 383 |
""" |
| 384 |
The ``--no-default-ignore`` option of the ``collectstatic`` management |
| 385 |
command. |
| 386 |
""" |
| 387 |
|
| 388 |
def run_collectstatic(self): |
| 389 |
super().run_collectstatic(use_default_ignore_patterns=False) |
| 390 |
|
| 391 |
def test_no_common_ignore_patterns(self): |
| 392 |
""" |
| 393 |
With --no-default-ignore, common ignore patterns (*~, .*, CVS) |
| 394 |
are not ignored. |
| 395 |
""" |
| 396 |
self.assertFileContains("test/.hidden", "should be ignored") |
| 397 |
self.assertFileContains("test/backup~", "should be ignored") |
| 398 |
self.assertFileContains("test/CVS", "should be ignored") |
| 399 |
|
| 400 |
|
| 401 |
@override_settings( |
| 402 |
INSTALLED_APPS=[ |
| 403 |
"staticfiles_tests.apps.staticfiles_config.IgnorePatternsAppConfig", |
| 404 |
"staticfiles_tests.apps.test", |
| 405 |
] |
| 406 |
) |
| 407 |
class TestCollectionCustomIgnorePatterns(CollectionTestCase): |
| 408 |
def test_custom_ignore_patterns(self): |
| 409 |
""" |
| 410 |
A custom ignore_patterns list, ['*.css', '*/vendor/*.js'] in this case, |
| 411 |
can be specified in an AppConfig definition. |
| 412 |
""" |
| 413 |
self.assertFileNotFound("test/nonascii.css") |
| 414 |
self.assertFileContains("test/.hidden", "should be ignored") |
| 415 |
self.assertFileNotFound(os.path.join("test", "vendor", "module.js")) |
| 416 |
|
| 417 |
|
| 418 |
class TestCollectionDryRun(TestNoFilesCreated, CollectionTestCase): |
| 419 |
""" |
| 420 |
Test ``--dry-run`` option for ``collectstatic`` management command. |
| 421 |
""" |
| 422 |
|
| 423 |
def run_collectstatic(self): |
| 424 |
super().run_collectstatic(dry_run=True) |
| 425 |
|
| 426 |
|
| 427 |
@override_settings( |
| 428 |
STORAGES={ |
| 429 |
**settings.STORAGES, |
| 430 |
STATICFILES_STORAGE_ALIAS: { |
| 431 |
"BACKEND": "django.contrib.staticfiles.storage.ManifestStaticFilesStorage" |
| 432 |
}, |
| 433 |
} |
| 434 |
) |
| 435 |
class TestCollectionDryRunManifestStaticFilesStorage(TestCollectionDryRun): |
| 436 |
pass |
| 437 |
|
| 438 |
|
| 439 |
class TestCollectionFilesOverride(CollectionTestCase): |
| 440 |
""" |
| 441 |
Test overriding duplicated files by ``collectstatic`` management command. |
| 442 |
Check for proper handling of apps order in installed apps even if file modification |
| 443 |
dates are in different order: |
| 444 |
'staticfiles_test_app', |
| 445 |
'staticfiles_tests.apps.no_label', |
| 446 |
""" |
| 447 |
|
| 448 |
def setUp(self): |
| 449 |
self.temp_dir = tempfile.mkdtemp() |
| 450 |
self.addCleanup(shutil.rmtree, self.temp_dir) |
| 451 |
|
| 452 |
# get modification and access times for no_label/static/file2.txt |
| 453 |
self.orig_path = os.path.join( |
| 454 |
TEST_ROOT, "apps", "no_label", "static", "file2.txt" |
| 455 |
) |
| 456 |
self.orig_mtime = os.path.getmtime(self.orig_path) |
| 457 |
self.orig_atime = os.path.getatime(self.orig_path) |
| 458 |
|
| 459 |
# prepare duplicate of file2.txt from a temporary app |
| 460 |
# this file will have modification time older than no_label/static/file2.txt |
| 461 |
# anyway it should be taken to STATIC_ROOT because the temporary app is before |
| 462 |
# 'no_label' app in installed apps |
| 463 |
self.temp_app_path = os.path.join(self.temp_dir, "staticfiles_test_app") |
| 464 |
self.testfile_path = os.path.join(self.temp_app_path, "static", "file2.txt") |
| 465 |
|
| 466 |
os.makedirs(self.temp_app_path) |
| 467 |
with open(os.path.join(self.temp_app_path, "__init__.py"), "w+"): |
| 468 |
pass |
| 469 |
|
| 470 |
os.makedirs(os.path.dirname(self.testfile_path)) |
| 471 |
with open(self.testfile_path, "w+") as f: |
| 472 |
f.write("duplicate of file2.txt") |
| 473 |
|
| 474 |
os.utime(self.testfile_path, (self.orig_atime - 1, self.orig_mtime - 1)) |
| 475 |
|
| 476 |
settings_with_test_app = self.modify_settings( |
| 477 |
INSTALLED_APPS={"prepend": "staticfiles_test_app"}, |
| 478 |
) |
| 479 |
with extend_sys_path(self.temp_dir): |
| 480 |
settings_with_test_app.enable() |
| 481 |
self.addCleanup(settings_with_test_app.disable) |
| 482 |
super().setUp() |
| 483 |
|
| 484 |
def test_ordering_override(self): |
| 485 |
""" |
| 486 |
Test if collectstatic takes files in proper order |
| 487 |
""" |
| 488 |
self.assertFileContains("file2.txt", "duplicate of file2.txt") |
| 489 |
|
| 490 |
# run collectstatic again |
| 491 |
self.run_collectstatic() |
| 492 |
|
| 493 |
self.assertFileContains("file2.txt", "duplicate of file2.txt") |
| 494 |
|
| 495 |
|
| 496 |
# The collectstatic test suite already has conflicting files since both |
| 497 |
# project/test/file.txt and apps/test/static/test/file.txt are collected. To |
| 498 |
# properly test for the warning not happening unless we tell it to explicitly, |
| 499 |
# we remove the project directory and will add back a conflicting file later. |
| 500 |
@override_settings(STATICFILES_DIRS=[]) |
| 501 |
class TestCollectionOverwriteWarning(CollectionTestCase): |
| 502 |
""" |
| 503 |
Test warning in ``collectstatic`` output when a file is skipped because a |
| 504 |
previous file was already written to the same path. |
| 505 |
""" |
| 506 |
|
| 507 |
# If this string is in the collectstatic output, it means the warning we're |
| 508 |
# looking for was emitted. |
| 509 |
warning_string = "Found another file" |
| 510 |
|
| 511 |
def _collectstatic_output(self, **kwargs): |
| 512 |
""" |
| 513 |
Run collectstatic, and capture and return the output. We want to run |
| 514 |
the command at highest verbosity, which is why we can't |
| 515 |
just call e.g. BaseCollectionTestCase.run_collectstatic() |
| 516 |
""" |
| 517 |
out = StringIO() |
| 518 |
call_command( |
| 519 |
"collectstatic", interactive=False, verbosity=3, stdout=out, **kwargs |
| 520 |
) |
| 521 |
return out.getvalue() |
| 522 |
|
| 523 |
def test_no_warning(self): |
| 524 |
""" |
| 525 |
There isn't a warning if there isn't a duplicate destination. |
| 526 |
""" |
| 527 |
output = self._collectstatic_output(clear=True) |
| 528 |
self.assertNotIn(self.warning_string, output) |
| 529 |
|
| 530 |
def test_warning(self): |
| 531 |
""" |
| 532 |
There is a warning when there are duplicate destinations. |
| 533 |
""" |
| 534 |
with tempfile.TemporaryDirectory() as static_dir: |
| 535 |
duplicate = os.path.join(static_dir, "test", "file.txt") |
| 536 |
os.mkdir(os.path.dirname(duplicate)) |
| 537 |
with open(duplicate, "w+") as f: |
| 538 |
f.write("duplicate of file.txt") |
| 539 |
|
| 540 |
with self.settings(STATICFILES_DIRS=[static_dir]): |
| 541 |
output = self._collectstatic_output(clear=True) |
| 542 |
self.assertIn(self.warning_string, output) |
| 543 |
|
| 544 |
os.remove(duplicate) |
| 545 |
|
| 546 |
# Make sure the warning went away again. |
| 547 |
with self.settings(STATICFILES_DIRS=[static_dir]): |
| 548 |
output = self._collectstatic_output(clear=True) |
| 549 |
self.assertNotIn(self.warning_string, output) |
| 550 |
|
| 551 |
|
| 552 |
@override_settings( |
| 553 |
STORAGES={ |
| 554 |
**settings.STORAGES, |
| 555 |
STATICFILES_STORAGE_ALIAS: { |
| 556 |
"BACKEND": "staticfiles_tests.storage.DummyStorage" |
| 557 |
}, |
| 558 |
} |
| 559 |
) |
| 560 |
class TestCollectionNonLocalStorage(TestNoFilesCreated, CollectionTestCase): |
| 561 |
""" |
| 562 |
Tests for a Storage that implements get_modified_time() but not path() |
| 563 |
(#15035). |
| 564 |
""" |
| 565 |
|
| 566 |
def test_storage_properties(self): |
| 567 |
# Properties of the Storage as described in the ticket. |
| 568 |
storage = DummyStorage() |
| 569 |
self.assertEqual( |
| 570 |
storage.get_modified_time("name"), |
| 571 |
datetime.datetime(1970, 1, 1, tzinfo=datetime.timezone.utc), |
| 572 |
) |
| 573 |
with self.assertRaisesMessage( |
| 574 |
NotImplementedError, "This backend doesn't support absolute paths." |
| 575 |
): |
| 576 |
storage.path("name") |
| 577 |
|
| 578 |
|
| 579 |
class TestCollectionNeverCopyStorage(CollectionTestCase): |
| 580 |
@override_settings( |
| 581 |
STORAGES={ |
| 582 |
**settings.STORAGES, |
| 583 |
STATICFILES_STORAGE_ALIAS: { |
| 584 |
"BACKEND": "staticfiles_tests.storage.NeverCopyRemoteStorage" |
| 585 |
}, |
| 586 |
} |
| 587 |
) |
| 588 |
def test_skips_newer_files_in_remote_storage(self): |
| 589 |
""" |
| 590 |
collectstatic skips newer files in a remote storage. |
| 591 |
run_collectstatic() in setUp() copies the static files, then files are |
| 592 |
always skipped after NeverCopyRemoteStorage is activated since |
| 593 |
NeverCopyRemoteStorage.get_modified_time() returns a datetime in the |
| 594 |
future to simulate an unmodified file. |
| 595 |
""" |
| 596 |
stdout = StringIO() |
| 597 |
self.run_collectstatic(stdout=stdout, verbosity=2) |
| 598 |
output = stdout.getvalue() |
| 599 |
self.assertIn("Skipping 'test.txt' (not modified)", output) |
| 600 |
|
| 601 |
|
| 602 |
@unittest.skipUnless(symlinks_supported(), "Must be able to symlink to run this test.") |
| 603 |
class TestCollectionLinks(TestDefaults, CollectionTestCase): |
| 604 |
""" |
| 605 |
Test ``--link`` option for ``collectstatic`` management command. |
| 606 |
|
| 607 |
Note that by inheriting ``TestDefaults`` we repeat all |
| 608 |
the standard file resolving tests here, to make sure using |
| 609 |
``--link`` does not change the file-selection semantics. |
| 610 |
""" |
| 611 |
|
| 612 |
def run_collectstatic(self, clear=False, link=True, **kwargs): |
| 613 |
super().run_collectstatic(link=link, clear=clear, **kwargs) |
| 614 |
|
| 615 |
def test_links_created(self): |
| 616 |
""" |
| 617 |
With ``--link``, symbolic links are created. |
| 618 |
""" |
| 619 |
self.assertTrue(os.path.islink(os.path.join(settings.STATIC_ROOT, "test.txt"))) |
| 620 |
|
| 621 |
def test_broken_symlink(self): |
| 622 |
""" |
| 623 |
Test broken symlink gets deleted. |
| 624 |
""" |
| 625 |
path = os.path.join(settings.STATIC_ROOT, "test.txt") |
| 626 |
os.unlink(path) |
| 627 |
self.run_collectstatic() |
| 628 |
self.assertTrue(os.path.islink(path)) |
| 629 |
|
| 630 |
def test_symlinks_and_files_replaced(self): |
| 631 |
""" |
| 632 |
Running collectstatic in non-symlink mode replaces symlinks with files, |
| 633 |
while symlink mode replaces files with symlinks. |
| 634 |
""" |
| 635 |
path = os.path.join(settings.STATIC_ROOT, "test.txt") |
| 636 |
self.assertTrue(os.path.islink(path)) |
| 637 |
self.run_collectstatic(link=False) |
| 638 |
self.assertFalse(os.path.islink(path)) |
| 639 |
self.run_collectstatic(link=True) |
| 640 |
self.assertTrue(os.path.islink(path)) |
| 641 |
|
| 642 |
def test_clear_broken_symlink(self): |
| 643 |
""" |
| 644 |
With ``--clear``, broken symbolic links are deleted. |
| 645 |
""" |
| 646 |
nonexistent_file_path = os.path.join(settings.STATIC_ROOT, "nonexistent.txt") |
| 647 |
broken_symlink_path = os.path.join(settings.STATIC_ROOT, "symlink.txt") |
| 648 |
os.symlink(nonexistent_file_path, broken_symlink_path) |
| 649 |
self.run_collectstatic(clear=True) |
| 650 |
self.assertFalse(os.path.lexists(broken_symlink_path)) |
| 651 |
|
| 652 |
@override_settings( |
| 653 |
STORAGES={ |
| 654 |
**settings.STORAGES, |
| 655 |
STATICFILES_STORAGE_ALIAS: { |
| 656 |
"BACKEND": "staticfiles_tests.storage.PathNotImplementedStorage" |
| 657 |
}, |
| 658 |
} |
| 659 |
) |
| 660 |
def test_no_remote_link(self): |
| 661 |
with self.assertRaisesMessage( |
| 662 |
CommandError, "Can't symlink to a remote destination." |
| 663 |
): |
| 664 |
self.run_collectstatic() |