Skip to content

Commit 89c7070

Browse files
author
Andrey Fedoseev
committed
Add tests for compile_static and compile_static_lazy
1 parent 2543d26 commit 89c7070

File tree

3 files changed

+83
-4
lines changed

3 files changed

+83
-4
lines changed

static_precompiler/tests/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
def suite():
55
from static_precompiler.tests import test_base_compiler
66
from static_precompiler.tests import test_url_converter
7+
from static_precompiler.tests import test_utils
78
from static_precompiler.tests import test_less
89
from static_precompiler.tests import test_coffeescript
910
from static_precompiler.tests import test_scss
@@ -13,6 +14,7 @@ def suite():
1314
test_suite = unittest.TestSuite()
1415
test_suite.addTests(test_base_compiler.suite())
1516
test_suite.addTests(test_url_converter.suite())
17+
test_suite.addTests(test_utils.suite())
1618
test_suite.addTests(test_less.suite())
1719
test_suite.addTests(test_coffeescript.suite())
1820
test_suite.addTests(test_scss.suite())
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
from django.core.exceptions import ImproperlyConfigured
2+
from mock import patch, MagicMock
3+
from static_precompiler.compilers import CoffeeScript
4+
from static_precompiler.exceptions import UnsupportedFile
5+
from static_precompiler.utils import get_compilers, compile_static, compile_static_lazy
6+
import unittest
7+
8+
9+
class UtilsTestCase(unittest.TestCase):
10+
11+
def test_get_compilers(self):
12+
with patch("static_precompiler.utils.COMPILERS", ["invalid_classpath"]):
13+
self.assertRaises(ImproperlyConfigured, get_compilers)
14+
15+
with patch("static_precompiler.utils.COMPILERS", ["non_existing_module.ClassName"]):
16+
self.assertRaises(ImproperlyConfigured, get_compilers)
17+
18+
with patch("static_precompiler.utils.COMPILERS", ["static_precompiler.NonExistingClass"]):
19+
self.assertRaises(ImproperlyConfigured, get_compilers)
20+
21+
with patch("static_precompiler.utils.COMPILERS", ["static_precompiler.compilers.CoffeeScript"]):
22+
compilers = get_compilers()
23+
self.assertEqual(len(compilers), 1)
24+
self.assertTrue(isinstance(compilers[0], CoffeeScript))
25+
26+
def test_compile_static(self):
27+
mocked_coffeescript_compiler = MagicMock()
28+
mocked_coffeescript_compiler.is_supported.side_effect = lambda source_path: source_path.endswith(".coffee")
29+
mocked_coffeescript_compiler.compile.return_value = "compiled coffeescript"
30+
mocked_less_compiler = MagicMock()
31+
mocked_less_compiler.is_supported.side_effect = lambda source_path: source_path.endswith(".less")
32+
mocked_less_compiler.compile.return_value = "compiled less"
33+
34+
with patch("static_precompiler.utils.get_compilers") as mocked_get_compilers:
35+
mocked_get_compilers.return_value = [
36+
mocked_coffeescript_compiler,
37+
mocked_less_compiler
38+
]
39+
self.assertEquals(
40+
compile_static("test.coffee"),
41+
"compiled coffeescript"
42+
)
43+
self.assertEquals(
44+
compile_static("test.less"),
45+
"compiled less"
46+
)
47+
self.assertRaises(
48+
UnsupportedFile,
49+
lambda: compile_static("test.sass")
50+
)
51+
52+
def test_compile_static_lazy(self):
53+
mocked_compiler = MagicMock()
54+
mocked_compiler.compile_lazy.return_value = "compiled"
55+
56+
with patch("static_precompiler.utils.get_compilers") as mocked_get_compilers:
57+
mocked_get_compilers.return_value = [
58+
mocked_compiler,
59+
]
60+
self.assertEquals(
61+
compile_static_lazy("source"),
62+
"compiled"
63+
)
64+
mocked_compiler.compile_lazy.assert_called_with("source")
65+
66+
67+
def suite():
68+
loader = unittest.TestLoader()
69+
test_suite = unittest.TestSuite()
70+
test_suite.addTest(loader.loadTestsFromTestCase(UtilsTestCase))
71+
return test_suite
72+
73+
74+
if __name__ == '__main__':
75+
unittest.TextTestRunner(verbosity=2).run(suite())

static_precompiler/utils.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
from hashlib import md5
22
from django.core.cache import cache
33
from django.core.exceptions import ImproperlyConfigured
4-
from django.templatetags.static import static
54
from django.utils.encoding import smart_str, smart_bytes
5+
from django.utils.functional import lazy
66
from django.utils.importlib import import_module
77
# noinspection PyUnresolvedReferences
88
from six.moves.urllib import parse as urllib_parse
99
from static_precompiler.exceptions import UnsupportedFile
1010
from static_precompiler.settings import MTIME_DELAY, POSIX_COMPATIBLE, COMPILERS, \
11-
STATIC_URL, PREPEND_STATIC_URL
11+
STATIC_URL
1212
import os
1313
import re
1414
import socket
@@ -120,7 +120,7 @@ def get_compilers():
120120
global compilers
121121

122122
if compilers is None:
123-
compilers = []
123+
compilers_temp = []
124124
for compiler_path in COMPILERS:
125125
try:
126126
compiler_module, compiler_classname = compiler_path.rsplit('.', 1)
@@ -135,7 +135,9 @@ def get_compilers():
135135
except AttributeError:
136136
raise ImproperlyConfigured('Compiler module "{0}" does not define a "{1}" class'.format(compiler_module, compiler_classname))
137137

138-
compilers.append(compiler_class())
138+
compilers_temp.append(compiler_class())
139+
140+
compilers = compilers_temp
139141

140142
return compilers
141143

0 commit comments

Comments
 (0)