Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 32 additions & 28 deletions static_precompiler/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
from mock import patch, MagicMock
from static_precompiler.compilers import CoffeeScript
from static_precompiler.exceptions import UnsupportedFile, CompilerNotFound
from static_precompiler.utils import get_compilers, compile_static, compile_static_lazy, get_compiler_by_name
from static_precompiler.utils import get_compilers, get_compiler_by_name, \
get_compiler_by_path, compile_static, compile_static_lazy
import unittest


Expand All @@ -21,56 +22,59 @@ def test_get_compilers(self):
with patch("static_precompiler.utils.COMPILERS", ["static_precompiler.compilers.CoffeeScript"]):
compilers = get_compilers()
self.assertEqual(len(compilers), 1)
self.assertTrue(isinstance(compilers[0], CoffeeScript))
self.assertTrue(CoffeeScript.name in compilers)
self.assertTrue(isinstance(compilers[CoffeeScript.name], CoffeeScript))

def test_get_compiler_by_name(self):
with patch("static_precompiler.utils.get_compilers_registry") as mocked_get_compilers_registry:
mocked_get_compilers_registry.return_value = {CoffeeScript.name: CoffeeScript()}
with patch("static_precompiler.utils.get_compilers") as mocked_get_compilers:
mocked_get_compilers.return_value = {CoffeeScript.name: CoffeeScript()}

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

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

def test_compile_static(self):
def test_get_compiler_by_path(self):
mocked_coffeescript_compiler = MagicMock()
mocked_coffeescript_compiler.is_supported.side_effect = lambda source_path: source_path.endswith(".coffee")
mocked_coffeescript_compiler.compile.return_value = "compiled coffeescript"
mocked_less_compiler = MagicMock()
mocked_less_compiler.is_supported.side_effect = lambda source_path: source_path.endswith(".less")
mocked_less_compiler.compile.return_value = "compiled less"

with patch("static_precompiler.utils.get_compilers") as mocked_get_compilers:
mocked_get_compilers.return_value = [
mocked_coffeescript_compiler,
mocked_less_compiler
]
self.assertEquals(
compile_static("test.coffee"),
"compiled coffeescript"
)
self.assertEquals(
compile_static("test.less"),
"compiled less"
)
mocked_get_compilers.return_value = {
"coffeescript": mocked_coffeescript_compiler,
"less": mocked_less_compiler,
}

self.assertRaises(
UnsupportedFile,
lambda: compile_static("test.sass")
get_compiler_by_path,
"test.sass"
)

def test_compile_static_lazy(self):
self.assertTrue(get_compiler_by_path("test.coffee") is mocked_coffeescript_compiler)
self.assertTrue(get_compiler_by_path("test.less") is mocked_less_compiler)

def test_compile_static(self):
mocked_compiler = MagicMock()
mocked_compiler.compile.return_value = "compiled"
mocked_compiler.compile_lazy.return_value = "compiled"
source_filename = "test.coffee"
with patch("static_precompiler.utils.get_compiler_by_path") as mocked_get_compiler_by_path:
mocked_get_compiler_by_path.return_value = mocked_compiler
self.assertEquals(
compile_static(source_filename),
"compiled"
)
mocked_get_compiler_by_path.assert_called_once_with(source_filename)
mocked_compiler.compile.assert_called_once_with(source_filename)

with patch("static_precompiler.utils.get_compilers") as mocked_get_compilers:
mocked_get_compilers.return_value = [
mocked_compiler,
]
mocked_get_compiler_by_path.reset_mock()
self.assertEquals(
compile_static_lazy("source"),
compile_static_lazy(source_filename),
"compiled"
)
mocked_compiler.compile_lazy.assert_called_with("source")
mocked_get_compiler_by_path.assert_called_once_with(source_filename)
mocked_compiler.compile_lazy.assert_called_once_with(source_filename)


def suite():
Expand Down
27 changes: 12 additions & 15 deletions static_precompiler/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import os
import re
import socket
import six
import subprocess
from warnings import warn

Expand Down Expand Up @@ -123,7 +124,7 @@ def convert_urls(content, path):
compilers = None


def get_compilers_registry():
def get_compilers():
global compilers

if compilers is None:
Expand Down Expand Up @@ -152,31 +153,27 @@ def get_compilers_registry():
return compilers


def get_compilers():
return list(get_compilers_registry().values())


def get_compiler_by_name(name):
compilers = get_compilers_registry()
compilers = get_compilers()
try:
return compilers[name]
except KeyError:
raise CompilerNotFound("There is no compiler with name '{0}'.".format(name))


def compile_static(path):

for compiler in get_compilers():
def get_compiler_by_path(path):
for compiler in six.itervalues(get_compilers()):
if compiler.is_supported(path):
return compiler.compile(path)
return compiler

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


def compile_static_lazy(path):
def compile_static(path):

for compiler in get_compilers():
if compiler.is_supported(path):
return compiler.compile_lazy(path)
return get_compiler_by_path(path).compile(path)

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

def compile_static_lazy(path):

return get_compiler_by_path(path).compile_lazy(path)