22from mock import patch , MagicMock
33from static_precompiler .compilers import CoffeeScript
44from 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+ )
612import 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
7685def suite ():
0 commit comments