| 352 | def temp_staticfiles_dirs(*files): |
| 353 | """Copy `files` from STATICFILES_DIRS to a temporary directory |
| 354 | |
| 355 | Also, override settings to use that STATICFILES_DIRS""" |
| 356 | |
| 357 | tmp_static_dir = tempfile.mkdtemp(dir=os.environ['DJANGO_TEST_TEMP_DIR']) |
| 358 | |
| 359 | def my_decorator(testcase): |
| 360 | @override_settings(**dict(TEST_SETTINGS, |
| 361 | STATICFILES_STORAGE='django.contrib.staticfiles.storage.CachedStaticFilesStorage', |
| 362 | STATICFILES_DIRS = (tmp_static_dir,), |
| 363 | DEBUG=False)) |
| 364 | def wrapped(self): |
| 365 | self._tmp_static_dir = tmp_static_dir |
| 366 | self.addCleanup(shutil.rmtree, tmp_static_dir, |
| 367 | ignore_errors=True, onerror=rmtree_errorhandler) |
| 368 | for fn in files: |
| 369 | new_dst = os.path.join(tmp_static_dir, fn) |
| 370 | try: |
| 371 | os.makedirs(os.path.dirname(new_dst)) |
| 372 | except OSError: |
| 373 | # it's fine if directory already exists |
| 374 | pass |
| 375 | shutil.copy(finders.find(fn), new_dst) |
| 376 | return testcase(self) |
| 377 | return wrapped |
| 378 | return my_decorator |
| 379 | |
| 420 | def add_trailing_whitespace(self, fn): |
| 421 | filename = os.path.join(TEST_ROOT, 'project', 'documents', fn) |
| 422 | orig_contents = open(filename, 'r').read() |
| 423 | def cleanup_fun(): |
| 424 | with open(filename, 'w') as f: |
| 425 | f.write(orig_contents) |
| 426 | self.addCleanup(cleanup_fun) |
| 427 | #filename = os.path.join(settings.STATICFILES_DIRS[0], fn) |
| 428 | with open(filename, 'a') as f: |
| 429 | f.write(b' ') |
| 430 | |
| 431 | #@temp_staticfiles_dirs("cached/styles.css", "cached/other.css") |
| 432 | def test_both_changed(self): |
| 433 | relpath1 = self.cached_file_path("cached/styles.css") |
| 434 | self.assertEqual("cached/styles.93b1147e8552.css", relpath1) |
| 435 | |
| 436 | self.add_trailing_whitespace("cached/styles.css") |
| 437 | self.add_trailing_whitespace("cached/other.css") |
| 438 | |
| 439 | # if you uncomment this, test passes |
| 440 | #self.run_collectstatic() |
| 441 | self.run_collectstatic() |
| 442 | |
| 443 | relpath2 = self.cached_file_path("cached/styles.css") |
| 444 | self.assertNotEqual(relpath1, relpath2) |
| 445 | with storage.staticfiles_storage.open(relpath2) as relfile: |
| 446 | content = relfile.read() |
| 447 | self.assertNotIn(b"other.css", content) |
| 448 | self.assertNotIn(b"other.d41d8cd98f00.css", content) |
| 449 | self.assertIn(b"other.7215ee9c7d9d.css", content) # md5(' ')[:12] |
| 450 | |
| 451 | #@temp_staticfiles_dirs("cached/styles.css", "cached/other.css") |
| 452 | def test_content_changed(self): |
| 453 | """Change dependent file and test if ancestor changes properly. |
| 454 | |
| 455 | styles.css depends on other.css, which is initially empty. |
| 456 | 1. Generate styles.css |
| 457 | 2. Modify other.css |
| 458 | 3. Re-generate styles.css and verify link to other.css has changed |
| 459 | """ |
| 460 | relpath1 = self.cached_file_path("cached/styles.css") |
| 461 | self.assertEqual(relpath1, "cached/styles.93b1147e8552.css") |
| 462 | |
| 463 | self.add_trailing_whitespace("cached/other.css") |
| 464 | |
| 465 | self.run_collectstatic() |
| 466 | |
| 467 | relpath2 = self.cached_file_path("cached/styles.css") |
| 468 | self.assertNotEqual(relpath1, relpath2) |
| 469 | with storage.staticfiles_storage.open(relpath2) as other: |
| 470 | content = relfile.read() |
| 471 | self.assertNotIn(b"other.css", content) |
| 472 | self.assertNotIn(b"other.d41d8cd98f00.css", content) |
| 473 | self.assertIn(b"other.9dd4e461268c.css", content) |
| 474 | |