33import json
44from subprocess import call
55from threading import Thread
6+ from unittest import skipIf
67
78import django
89from django .conf import settings
910from django .test import TestCase , RequestFactory
1011from django_jinja .builtins import DEFAULT_EXTENSIONS
1112from django .views .generic .base import TemplateView
1213
13- from webpack_loader .utils import get_assets , get_bundle , WebpackException
14+ from webpack_loader .utils import get_config , get_assets , get_bundle , WebpackException
1415
1516
1617BUNDLE_PATH = os .path .join (settings .BASE_DIR , 'assets/bundles/' )
17-
18+ DEFAULT_CONFIG = 'DEFAULT'
1819
1920class LoaderTestCase (TestCase ):
2021 def setUp (self ):
@@ -23,16 +24,42 @@ def setUp(self):
2324 def clean_dir (self , directory ):
2425 if os .path .exists (BUNDLE_PATH ):
2526 [os .remove (os .path .join (BUNDLE_PATH , F )) for F in os .listdir (BUNDLE_PATH )]
26- os .remove (settings .WEBPACK_LOADER ['STATS_FILE' ])
27+ os .remove (settings .WEBPACK_LOADER [DEFAULT_CONFIG ][ 'STATS_FILE' ])
2728
2829 def compile_bundles (self , config , wait = None ):
2930 if wait :
3031 time .sleep (wait )
3132 call (['./node_modules/.bin/webpack' , '--config' , config ])
3233
34+ @skipIf (django .VERSION < (1 , 7 ),
35+ 'not supported in this django version' )
36+ def test_config_check (self ):
37+ from django .core .checks import Error
38+ from webpack_loader .apps import webpack_cfg_check
39+
40+ with self .settings (WEBPACK_LOADER = {
41+ 'BUNDLE_DIR_NAME' : 'bundles/' ,
42+ 'STATS_FILE' : 'webpack-stats.json' ,
43+ }):
44+ errors = webpack_cfg_check (None )
45+ expected_errors = [Error (
46+ 'Error while parsing WEBPACK_LOADER configuration' ,
47+ hint = 'Is WEBPACK_LOADER config compliant with the new format?' ,
48+ obj = 'django.conf.settings.WEBPACK_LOADER' ,
49+ id = 'django-webpack-loader.E001' ,
50+ )]
51+ self .assertEqual (errors , expected_errors )
52+
53+ with self .settings (WEBPACK_LOADER = {
54+ 'DEFAULT' : {}
55+ }):
56+ errors = webpack_cfg_check (None )
57+ expected_errors = []
58+ self .assertEqual (errors , expected_errors )
59+
3360 def test_simple_and_css_extract (self ):
3461 self .compile_bundles ('webpack.config.simple.js' )
35- assets = get_assets ()
62+ assets = get_assets (get_config ( DEFAULT_CONFIG ) )
3663 self .assertEqual (assets ['status' ], 'done' )
3764 self .assertIn ('chunks' , assets )
3865
@@ -46,13 +73,13 @@ def test_simple_and_css_extract(self):
4673
4774 def test_static_url (self ):
4875 self .compile_bundles ('webpack.config.publicPath.js' )
49- assets = get_assets ()
76+ assets = get_assets (get_config ( DEFAULT_CONFIG ) )
5077 self .assertEqual (assets ['status' ], 'done' )
5178 self .assertEqual (assets ['publicPath' ], 'http://custom-static-host.com/' )
5279
5380 def test_code_spliting (self ):
5481 self .compile_bundles ('webpack.config.split.js' )
55- assets = get_assets ()
82+ assets = get_assets (get_config ( DEFAULT_CONFIG ) )
5683 self .assertEqual (assets ['status' ], 'done' )
5784 self .assertIn ('chunks' , assets )
5885
@@ -68,12 +95,17 @@ def test_code_spliting(self):
6895
6996 def test_templatetags (self ):
7097 self .compile_bundles ('webpack.config.simple.js' )
98+ self .compile_bundles ('webpack.config.app2.js' )
7199 view = TemplateView .as_view (template_name = 'home.html' )
72100 request = self .factory .get ('/' )
73101 result = view (request )
74102 self .assertIn ('<link type="text/css" href="/static/bundles/styles.css" rel="stylesheet">' , result .rendered_content )
75103 self .assertIn ('<script type="text/javascript" src="/static/bundles/main.js"></script>' , result .rendered_content )
76104
105+ self .assertIn ('<link type="text/css" href="/static/bundles/styles-app2.css" rel="stylesheet">' , result .rendered_content )
106+ self .assertIn ('<script type="text/javascript" src="/static/bundles/app2.js"></script>' , result .rendered_content )
107+ self .assertIn ('<img src="/static/my-image.png"/>' , result .rendered_content )
108+
77109
78110 self .compile_bundles ('webpack.config.publicPath.js' )
79111 view = TemplateView .as_view (template_name = 'home.html' )
@@ -83,6 +115,7 @@ def test_templatetags(self):
83115
84116 def test_jinja2 (self ):
85117 self .compile_bundles ('webpack.config.simple.js' )
118+ self .compile_bundles ('webpack.config.app2.js' )
86119 view = TemplateView .as_view (template_name = 'home.jinja' )
87120
88121 if django .VERSION >= (1 , 8 ):
@@ -118,16 +151,16 @@ def test_reporting_errors(self):
118151 #TODO:
119152 self .compile_bundles ('webpack.config.error.js' )
120153 try :
121- get_bundle ('main' )
154+ get_bundle ('main' , DEFAULT_CONFIG )
122155 except WebpackException as e :
123156 self .assertIn ("Cannot resolve module 'the-library-that-did-not-exist'" , str (e ))
124157
125158 def test_missing_stats_file (self ):
126- os .remove (settings .WEBPACK_LOADER ['STATS_FILE' ])
159+ os .remove (settings .WEBPACK_LOADER [DEFAULT_CONFIG ][ 'STATS_FILE' ])
127160 try :
128- get_assets ()
161+ get_assets (get_config ( DEFAULT_CONFIG ) )
129162 except IOError as e :
130- expected = 'Error reading {}. Are you sure webpack has generated the file and the path is correct?' .format (settings .WEBPACK_LOADER ['STATS_FILE' ])
163+ expected = 'Error reading {}. Are you sure webpack has generated the file and the path is correct?' .format (settings .WEBPACK_LOADER [DEFAULT_CONFIG ][ 'STATS_FILE' ])
131164 self .assertIn (expected , str (e ))
132165
133166 def test_request_blocking (self ):
@@ -138,19 +171,23 @@ def test_request_blocking(self):
138171 view = TemplateView .as_view (template_name = 'home.html' )
139172
140173 with self .settings (DEBUG = True ):
141- open (settings .WEBPACK_LOADER ['STATS_FILE' ], 'w' ).write (json .dumps ({'status' : 'compiling' }))
174+ open (settings .WEBPACK_LOADER [DEFAULT_CONFIG ][ 'STATS_FILE' ], 'w' ).write (json .dumps ({'status' : 'compiling' }))
142175 then = time .time ()
143176 request = self .factory .get ('/' )
144177 result = view (request )
145178 t = Thread (target = self .compile_bundles , args = ('webpack.config.simple.js' , wait_for ))
179+ t2 = Thread (target = self .compile_bundles , args = ('webpack.config.app2.js' , wait_for ))
146180 t .start ()
181+ t2 .start ()
147182 result .rendered_content
148183 elapsed = time .time () - then
149184 t .join ()
185+ t2 .join ()
150186 self .assertTrue (elapsed > wait_for )
151187
152188 with self .settings (DEBUG = False ):
153189 self .compile_bundles ('webpack.config.simple.js' )
190+ self .compile_bundles ('webpack.config.app2.js' )
154191 then = time .time ()
155192 request = self .factory .get ('/' )
156193 result = view (request )
0 commit comments