Skip to content

Commit bd32f49

Browse files
committed
rework utils
1 parent 7262817 commit bd32f49

File tree

2 files changed

+49
-43
lines changed

2 files changed

+49
-43
lines changed

static_precompiler/tests/test_utils.py

Lines changed: 37 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,13 @@
22
from mock import patch, MagicMock
33
from static_precompiler.compilers import CoffeeScript
44
from static_precompiler.exceptions import UnsupportedFile, CompilerNotFound
5-
from static_precompiler.utils import get_compilers, compile_static, compile_static_lazy, get_compiler_by_name
5+
from static_precompiler.utils import (
6+
get_compilers,
7+
get_compiler_by_name,
8+
get_compiler_by_path,
9+
compile_static,
10+
compile_static_lazy,
11+
)
612
import unittest
713

814

@@ -21,56 +27,59 @@ def test_get_compilers(self):
2127
with patch("static_precompiler.utils.COMPILERS", ["static_precompiler.compilers.CoffeeScript"]):
2228
compilers = get_compilers()
2329
self.assertEqual(len(compilers), 1)
24-
self.assertTrue(isinstance(compilers[0], CoffeeScript))
30+
self.assertTrue(CoffeeScript.name in compilers)
31+
self.assertTrue(isinstance(compilers[CoffeeScript.name], CoffeeScript))
2532

2633
def test_get_compiler_by_name(self):
27-
with patch("static_precompiler.utils.get_compilers_registry") as mocked_get_compilers_registry:
28-
mocked_get_compilers_registry.return_value = {CoffeeScript.name: CoffeeScript()}
34+
with patch("static_precompiler.utils.get_compilers") as mocked_get_compilers:
35+
mocked_get_compilers.return_value = {CoffeeScript.name: CoffeeScript()}
2936

3037
self.assertRaises(CompilerNotFound, get_compiler_by_name, 'non-existing compiler')
3138

3239
compiler = get_compiler_by_name(CoffeeScript.name)
3340
self.assertTrue(isinstance(compiler, CoffeeScript))
3441

35-
def test_compile_static(self):
42+
def test_get_compiler_by_path(self):
3643
mocked_coffeescript_compiler = MagicMock()
3744
mocked_coffeescript_compiler.is_supported.side_effect = lambda source_path: source_path.endswith(".coffee")
38-
mocked_coffeescript_compiler.compile.return_value = "compiled coffeescript"
3945
mocked_less_compiler = MagicMock()
4046
mocked_less_compiler.is_supported.side_effect = lambda source_path: source_path.endswith(".less")
41-
mocked_less_compiler.compile.return_value = "compiled less"
42-
4347
with patch("static_precompiler.utils.get_compilers") as mocked_get_compilers:
44-
mocked_get_compilers.return_value = [
45-
mocked_coffeescript_compiler,
46-
mocked_less_compiler
47-
]
48-
self.assertEquals(
49-
compile_static("test.coffee"),
50-
"compiled coffeescript"
51-
)
52-
self.assertEquals(
53-
compile_static("test.less"),
54-
"compiled less"
55-
)
48+
mocked_get_compilers.return_value = {
49+
"coffeescript": mocked_coffeescript_compiler,
50+
"less": mocked_less_compiler,
51+
}
52+
5653
self.assertRaises(
5754
UnsupportedFile,
58-
lambda: compile_static("test.sass")
55+
get_compiler_by_path,
56+
"test.sass"
5957
)
6058

61-
def test_compile_static_lazy(self):
59+
self.assertTrue(get_compiler_by_path("test.coffee") is mocked_coffeescript_compiler)
60+
self.assertTrue(get_compiler_by_path("test.less") is mocked_less_compiler)
61+
62+
def test_compile_static(self):
6263
mocked_compiler = MagicMock()
64+
mocked_compiler.compile.return_value = "compiled"
6365
mocked_compiler.compile_lazy.return_value = "compiled"
66+
source_filename = "test.coffee"
67+
with patch("static_precompiler.utils.get_compiler_by_path") as mocked_get_compiler_by_path:
68+
mocked_get_compiler_by_path.return_value = mocked_compiler
69+
self.assertEquals(
70+
compile_static(source_filename),
71+
"compiled"
72+
)
73+
mocked_get_compiler_by_path.assert_called_once_with(source_filename)
74+
mocked_compiler.compile.assert_called_once_with(source_filename)
6475

65-
with patch("static_precompiler.utils.get_compilers") as mocked_get_compilers:
66-
mocked_get_compilers.return_value = [
67-
mocked_compiler,
68-
]
76+
mocked_get_compiler_by_path.reset_mock()
6977
self.assertEquals(
70-
compile_static_lazy("source"),
78+
compile_static_lazy(source_filename),
7179
"compiled"
7280
)
73-
mocked_compiler.compile_lazy.assert_called_with("source")
81+
mocked_get_compiler_by_path.assert_called_once_with(source_filename)
82+
mocked_compiler.compile_lazy.assert_called_once_with(source_filename)
7483

7584

7685
def suite():

static_precompiler/utils.py

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import os
1212
import re
1313
import socket
14+
import six
1415
import subprocess
1516
from warnings import warn
1617

@@ -123,7 +124,7 @@ def convert_urls(content, path):
123124
compilers = None
124125

125126

126-
def get_compilers_registry():
127+
def get_compilers():
127128
global compilers
128129

129130
if compilers is None:
@@ -152,31 +153,27 @@ def get_compilers_registry():
152153
return compilers
153154

154155

155-
def get_compilers():
156-
return list(get_compilers_registry().values())
157-
158-
159156
def get_compiler_by_name(name):
160-
compilers = get_compilers_registry()
157+
compilers = get_compilers()
161158
try:
162159
return compilers[name]
163160
except KeyError:
164161
raise CompilerNotFound("There is no compiler with name '{0}'.".format(name))
165162

166163

167-
def compile_static(path):
168-
169-
for compiler in get_compilers():
164+
def get_compiler_by_path(path):
165+
for compiler in six.itervalues(get_compilers()):
170166
if compiler.is_supported(path):
171-
return compiler.compile(path)
167+
return compiler
172168

173169
raise UnsupportedFile("The source file '{0}' is not supported by any of available compilers.".format(path))
174170

175171

176-
def compile_static_lazy(path):
172+
def compile_static(path):
177173

178-
for compiler in get_compilers():
179-
if compiler.is_supported(path):
180-
return compiler.compile_lazy(path)
174+
return get_compiler_by_path(path).compile(path)
181175

182-
raise UnsupportedFile("The source file '{0}' is not supported by any of available compilers.".format(path))
176+
177+
def compile_static_lazy(path):
178+
179+
return get_compiler_by_path(path).compile_lazy(path)

0 commit comments

Comments
 (0)