2015-07-01 19:43:25 +08:00
|
|
|
import os
|
|
|
|
import shutil
|
|
|
|
import tempfile
|
|
|
|
|
|
|
|
from django.conf import settings
|
|
|
|
from django.core.management import call_command
|
|
|
|
from django.template import Context, Template
|
|
|
|
from django.test import SimpleTestCase, override_settings
|
|
|
|
|
|
|
|
from .settings import TEST_SETTINGS
|
|
|
|
|
|
|
|
|
2017-01-19 15:39:46 +08:00
|
|
|
class BaseStaticFilesMixin:
|
2015-07-01 19:43:25 +08:00
|
|
|
"""
|
|
|
|
Test case with a couple utility assertions.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def assertFileContains(self, filepath, text):
|
|
|
|
self.assertIn(
|
|
|
|
text,
|
2017-01-24 19:22:42 +08:00
|
|
|
self._get_file(filepath),
|
2015-07-01 19:43:25 +08:00
|
|
|
"'%s' not in '%s'" % (text, filepath),
|
|
|
|
)
|
|
|
|
|
|
|
|
def assertFileNotFound(self, filepath):
|
2019-01-28 23:01:35 +08:00
|
|
|
with self.assertRaises(OSError):
|
2016-01-17 19:26:39 +08:00
|
|
|
self._get_file(filepath)
|
2015-07-01 19:43:25 +08:00
|
|
|
|
|
|
|
def render_template(self, template, **kwargs):
|
2016-12-29 23:27:49 +08:00
|
|
|
if isinstance(template, str):
|
2015-07-01 19:43:25 +08:00
|
|
|
template = Template(template)
|
2016-12-17 23:03:44 +08:00
|
|
|
return template.render(Context(**kwargs)).strip()
|
2015-07-01 19:43:25 +08:00
|
|
|
|
|
|
|
def static_template_snippet(self, path, asvar=False):
|
|
|
|
if asvar:
|
2018-01-22 05:38:52 +08:00
|
|
|
return "{%% load static from static %%}{%% static '%s' as var %%}{{ var }}" % path
|
|
|
|
return "{%% load static from static %%}{%% static '%s' %%}" % path
|
2015-07-01 19:43:25 +08:00
|
|
|
|
|
|
|
def assertStaticRenders(self, path, result, asvar=False, **kwargs):
|
|
|
|
template = self.static_template_snippet(path, asvar)
|
|
|
|
self.assertEqual(self.render_template(template, **kwargs), result)
|
|
|
|
|
|
|
|
def assertStaticRaises(self, exc, path, result, asvar=False, **kwargs):
|
2016-01-17 19:26:39 +08:00
|
|
|
with self.assertRaises(exc):
|
|
|
|
self.assertStaticRenders(path, result, **kwargs)
|
2015-07-01 19:43:25 +08:00
|
|
|
|
|
|
|
|
|
|
|
@override_settings(**TEST_SETTINGS)
|
2016-03-26 23:17:06 +08:00
|
|
|
class StaticFilesTestCase(BaseStaticFilesMixin, SimpleTestCase):
|
2015-07-01 19:43:25 +08:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
2016-03-26 23:17:06 +08:00
|
|
|
@override_settings(**TEST_SETTINGS)
|
|
|
|
class CollectionTestCase(BaseStaticFilesMixin, SimpleTestCase):
|
2015-07-01 19:43:25 +08:00
|
|
|
"""
|
|
|
|
Tests shared by all file finding features (collectstatic,
|
|
|
|
findstatic, and static serve view).
|
|
|
|
|
|
|
|
This relies on the asserts defined in BaseStaticFilesTestCase, but
|
|
|
|
is separated because some test cases need those asserts without
|
|
|
|
all these tests.
|
|
|
|
"""
|
2018-04-24 11:04:02 +08:00
|
|
|
run_collectstatic_in_setUp = True
|
|
|
|
|
2015-07-01 19:43:25 +08:00
|
|
|
def setUp(self):
|
2017-01-21 21:13:44 +08:00
|
|
|
super().setUp()
|
2015-07-01 19:43:25 +08:00
|
|
|
temp_dir = tempfile.mkdtemp()
|
|
|
|
# Override the STATIC_ROOT for all tests from setUp to tearDown
|
|
|
|
# rather than as a context manager
|
|
|
|
self.patched_settings = self.settings(STATIC_ROOT=temp_dir)
|
|
|
|
self.patched_settings.enable()
|
2018-04-24 11:04:02 +08:00
|
|
|
if self.run_collectstatic_in_setUp:
|
|
|
|
self.run_collectstatic()
|
2015-07-01 19:43:25 +08:00
|
|
|
# Same comment as in runtests.teardown.
|
2016-12-29 23:27:49 +08:00
|
|
|
self.addCleanup(shutil.rmtree, temp_dir)
|
2015-07-01 19:43:25 +08:00
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
self.patched_settings.disable()
|
2017-01-21 21:13:44 +08:00
|
|
|
super().tearDown()
|
2015-07-01 19:43:25 +08:00
|
|
|
|
2017-02-02 00:41:56 +08:00
|
|
|
def run_collectstatic(self, *, verbosity=0, **kwargs):
|
2017-01-04 08:03:08 +08:00
|
|
|
call_command('collectstatic', interactive=False, verbosity=verbosity,
|
2015-07-01 19:43:25 +08:00
|
|
|
ignore_patterns=['*.ignoreme'], **kwargs)
|
|
|
|
|
|
|
|
def _get_file(self, filepath):
|
|
|
|
assert filepath, 'filepath is empty.'
|
|
|
|
filepath = os.path.join(settings.STATIC_ROOT, filepath)
|
2019-01-28 00:30:20 +08:00
|
|
|
with open(filepath, encoding='utf-8') as f:
|
2015-07-01 19:43:25 +08:00
|
|
|
return f.read()
|
|
|
|
|
|
|
|
|
2017-01-19 15:39:46 +08:00
|
|
|
class TestDefaults:
|
2015-07-01 19:43:25 +08:00
|
|
|
"""
|
|
|
|
A few standard test cases.
|
|
|
|
"""
|
|
|
|
def test_staticfiles_dirs(self):
|
|
|
|
"""
|
|
|
|
Can find a file in a STATICFILES_DIRS directory.
|
|
|
|
"""
|
|
|
|
self.assertFileContains('test.txt', 'Can we find')
|
|
|
|
self.assertFileContains(os.path.join('prefix', 'test.txt'), 'Prefix')
|
|
|
|
|
|
|
|
def test_staticfiles_dirs_subdir(self):
|
|
|
|
"""
|
|
|
|
Can find a file in a subdirectory of a STATICFILES_DIRS
|
|
|
|
directory.
|
|
|
|
"""
|
|
|
|
self.assertFileContains('subdir/test.txt', 'Can we find')
|
|
|
|
|
|
|
|
def test_staticfiles_dirs_priority(self):
|
|
|
|
"""
|
|
|
|
File in STATICFILES_DIRS has priority over file in app.
|
|
|
|
"""
|
|
|
|
self.assertFileContains('test/file.txt', 'STATICFILES_DIRS')
|
|
|
|
|
|
|
|
def test_app_files(self):
|
|
|
|
"""
|
|
|
|
Can find a file in an app static/ directory.
|
|
|
|
"""
|
|
|
|
self.assertFileContains('test/file1.txt', 'file1 in the app dir')
|
|
|
|
|
|
|
|
def test_nonascii_filenames(self):
|
|
|
|
"""
|
|
|
|
Can find a file with non-ASCII character in an app static/ directory.
|
|
|
|
"""
|
|
|
|
self.assertFileContains('test/⊗.txt', '⊗ in the app dir')
|
|
|
|
|
|
|
|
def test_camelcase_filenames(self):
|
|
|
|
"""
|
|
|
|
Can find a file with capital letters.
|
|
|
|
"""
|
|
|
|
self.assertFileContains('test/camelCase.txt', 'camelCase')
|
2017-03-17 19:55:00 +08:00
|
|
|
|
|
|
|
def test_filename_with_percent_sign(self):
|
|
|
|
self.assertFileContains('test/%2F.txt', '%2F content')
|