Skip to content

Commit 4d81d75

Browse files
committed
Covered 100% code with tests and moved to codecov
1 parent 883bc52 commit 4d81d75

File tree

10 files changed

+148
-41
lines changed

10 files changed

+148
-41
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ examples/**/assets/bundles/
6565
examples/**/assets/webpack-stats.json
6666

6767
tests/ve/
68+
tests/ve3/
6869
tests/node_modules/
6970
tests/assets/bundles/
7071
tests/assets/webpack-stats.json

.travis.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
language: python
22
python:
33
- "2.7"
4-
- "3.2"
54
- "3.3"
65
- "3.4"
76
# command to install dependencies
@@ -10,7 +9,9 @@ install: "./install.sh"
109
# command to run tests
1110
script: "coverage run --source=webpack_loader manage.py test"
1211
after_success:
13-
coveralls
12+
- coverage xml
13+
- cp coverage.xml ../
14+
- codecov --token=78880239-ce1b-4fbe-b2df-950e2609238b
1415
env:
1516
- DJANGO_VERSION=1.6
1617
- DJANGO_VERSION=1.7

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[![Join the chat at https://gitter.im/owais/django-webpack-loader](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/owais/django-webpack-loader?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
44
[![Build Status](https://travis-ci.org/owais/django-webpack-loader.svg?branch=master)](https://travis-ci.org/owais/django-webpack-loader)
5-
<a href='https://coveralls.io/r/owais/django-webpack-loader?branch=master'><img src='https://coveralls.io/repos/owais/django-webpack-loader/badge.svg?branch=master' alt='Coverage Status' /></a>
5+
[![codecov.io](http://codecov.io/github/owais/django-webpack-loader/coverage.svg?branch=master)](http://codecov.io/github/owais/django-webpack-loader?branch=master)
66

77
<br>
88
Read http://owaislone.org/blog/webpack-plus-reactjs-and-django/ for a detailed step by step guide on setting up webpack with django using this library.

tests/app/settings.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
'django.contrib.staticfiles',
4040
'app',
4141
'webpack_loader',
42+
'django_jinja',
4243
)
4344

4445
MIDDLEWARE_CLASSES = (
@@ -111,3 +112,10 @@
111112
'BUNDLE_DIR_NAME': 'bundles/',
112113
'STATS_FILE': os.path.join(BASE_DIR, 'assets/webpack-stats.json'),
113114
}
115+
116+
from django_jinja.builtins import DEFAULT_EXTENSIONS
117+
118+
JINJA2_EXTENSIONS = DEFAULT_EXTENSIONS + [
119+
"webpack_loader.contrib.jinja2ext.WebpackExtension",
120+
]
121+
DEFAULT_JINJA2_TEMPLATE_EXTENSION = '.jinja'

tests/app/templates/home.jinja

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Example</title>
6+
{{ render_bundle('main', 'css') }}
7+
</head>
8+
9+
<body>
10+
<div id="react-app"></div>
11+
{{ render_bundle('main', 'js') }}
12+
</body>
13+
</html>

tests/app/tests/test_webpack.py

Lines changed: 82 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
from subprocess import call
55
from threading import Thread
66

7+
import django
78
from django.conf import settings
89
from django.test import TestCase, RequestFactory
9-
from webpack_loader.utils import get_assets
10+
from django_jinja.builtins import DEFAULT_EXTENSIONS
1011
from django.views.generic.base import TemplateView
1112

12-
13-
view = TemplateView.as_view(template_name='home.html')
13+
from webpack_loader.utils import get_assets, get_bundle, WebpackException
1414

1515

1616
BUNDLE_PATH = os.path.join(settings.BASE_DIR, 'assets/bundles/')
@@ -30,38 +30,7 @@ def compile_bundles(self, config, wait=None):
3030
time.sleep(wait)
3131
call(['./node_modules/.bin/webpack', '--config', os.path.join('assets/', config)])
3232

33-
def test_request_blocking(self):
34-
# FIXME: This will work 99% time but there is no garauntee with the
35-
# 4 second thing. Need a better way to detect if request was blocked on
36-
# not.
37-
wait_for = 3
38-
39-
with self.settings(DEBUG=True):
40-
open(settings.WEBPACK_LOADER['STATS_FILE'], 'w').write(json.dumps({'status': 'compiling'}))
41-
then = time.time()
42-
request = self.factory.get('/')
43-
result = view(request)
44-
t = Thread(target=self.compile_bundles, args=('webpack.config.simple.js', wait_for))
45-
t.start()
46-
result.rendered_content
47-
elapsed = time.time() - then
48-
t.join()
49-
self.assertTrue(elapsed > wait_for)
50-
51-
with self.settings(DEBUG=False):
52-
self.compile_bundles('webpack.config.simple.js')
53-
then = time.time()
54-
request = self.factory.get('/')
55-
result = view(request)
56-
result.rendered_content
57-
elapsed = time.time() - then
58-
self.assertTrue(elapsed < wait_for)
59-
60-
def test_reporting_errors(self):
61-
#TODO:
62-
pass
63-
64-
def test_simple(self):
33+
def test_simple_and_css_extract(self):
6534
self.compile_bundles('webpack.config.simple.js')
6635
assets = get_assets()
6736
self.assertEqual(assets['status'], 'done')
@@ -75,7 +44,7 @@ def test_simple(self):
7544
self.assertEqual(main[0]['path'], os.path.join(settings.BASE_DIR, 'assets/bundles/main.js'))
7645
self.assertEqual(main[1]['path'], os.path.join(settings.BASE_DIR, 'assets/bundles/styles.css'))
7746

78-
def test_multiple_files(self):
47+
def test_code_spliting(self):
7948
self.compile_bundles('webpack.config.split.js')
8049
assets = get_assets()
8150
self.assertEqual(assets['status'], 'done')
@@ -90,3 +59,80 @@ def test_multiple_files(self):
9059

9160
vendor = chunks['vendor']
9261
self.assertEqual(vendor[0]['path'], os.path.join(settings.BASE_DIR, 'assets/bundles/vendor.js'))
62+
63+
def test_jinja2(self):
64+
self.compile_bundles('webpack.config.simple.js')
65+
view = TemplateView.as_view(template_name='home.jinja')
66+
67+
if django.VERSION >= (1, 8):
68+
settings = {
69+
'TEMPLATES': [
70+
{
71+
"BACKEND": "django_jinja.backend.Jinja2",
72+
"APP_DIRS": True,
73+
"OPTIONS": {
74+
"match_extension": ".jinja",
75+
"extensions": DEFAULT_EXTENSIONS + [
76+
"webpack_loader.contrib.jinja2ext.WebpackExtension",
77+
]
78+
79+
}
80+
},
81+
]
82+
}
83+
else:
84+
settings = {
85+
'TEMPLATE_LOADERS': (
86+
'django_jinja.loaders.FileSystemLoader',
87+
'django_jinja.loaders.AppLoader',
88+
),
89+
}
90+
with self.settings(**settings):
91+
request = self.factory.get('/')
92+
result = view(request)
93+
self.assertIn('<link type="text/css" href="/static/bundles/styles.css" rel="stylesheet">', result.rendered_content)
94+
self.assertIn('<script type="text/javascript" src="/static/bundles/main.js"></script>', result.rendered_content)
95+
96+
def test_reporting_errors(self):
97+
#TODO:
98+
self.compile_bundles('webpack.config.error.js')
99+
try:
100+
get_bundle('main')
101+
except WebpackException as e:
102+
self.assertIn("Cannot resolve module 'the-library-that-did-not-exist'", e.message)
103+
104+
def test_missing_stats_file(self):
105+
os.remove(settings.WEBPACK_LOADER['STATS_FILE'])
106+
try:
107+
get_assets()
108+
except IOError as e:
109+
expected = 'Error reading {}. Are you sure webpack has generated the file and the path is correct?'.format(settings.WEBPACK_LOADER['STATS_FILE'])
110+
self.assertIn(expected, str(e))
111+
112+
def test_request_blocking(self):
113+
# FIXME: This will work 99% time but there is no garauntee with the
114+
# 4 second thing. Need a better way to detect if request was blocked on
115+
# not.
116+
wait_for = 3
117+
view = TemplateView.as_view(template_name='home.html')
118+
119+
with self.settings(DEBUG=True):
120+
open(settings.WEBPACK_LOADER['STATS_FILE'], 'w').write(json.dumps({'status': 'compiling'}))
121+
then = time.time()
122+
request = self.factory.get('/')
123+
result = view(request)
124+
t = Thread(target=self.compile_bundles, args=('webpack.config.simple.js', wait_for))
125+
t.start()
126+
result.rendered_content
127+
elapsed = time.time() - then
128+
t.join()
129+
self.assertTrue(elapsed > wait_for)
130+
131+
with self.settings(DEBUG=False):
132+
self.compile_bundles('webpack.config.simple.js')
133+
then = time.time()
134+
request = self.factory.get('/')
135+
result = view(request)
136+
result.rendered_content
137+
elapsed = time.time() - then
138+
self.assertTrue(elapsed < wait_for)

tests/assets/js/bad_index.jsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
var React = require('the-library-that-did-not-exist');
2+
var App = require('./app');
3+
var style = require('./style.css');
4+
5+
React.render(<App/>, document.getElementById('react-app'));
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
var path = require("path");
2+
var webpack = require('webpack');
3+
var BundleTracker = require('webpack-bundle-tracker');
4+
var ExtractTextPlugin = require("extract-text-webpack-plugin");
5+
6+
7+
module.exports = {
8+
context: __dirname,
9+
entry: './js/bad_index',
10+
output: {
11+
path: path.resolve('./assets/bundles/'),
12+
filename: "[name].js",
13+
},
14+
15+
plugins: [
16+
new ExtractTextPlugin("styles.css"),
17+
new BundleTracker({filename: './assets/webpack-stats.json'}),
18+
],
19+
20+
module: {
21+
loaders: [
22+
// we pass the output from babel loader to react-hot loader
23+
{ test: /\.jsx?$/, exclude: /node_modules/, loaders: ['babel'], },
24+
{ test: /\.css$/, loader: ExtractTextPlugin.extract("style-loader", "css-loader") }
25+
],
26+
},
27+
28+
resolve: {
29+
modulesDirectories: ['node_modules', 'bower_components'],
30+
extensions: ['', '.js', '.jsx']
31+
},
32+
}

tests/requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1-
coveralls
21
coverage
2+
codecov
3+
django-jinja

webpack_loader/contrib/jinja2ext.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@
66
class WebpackExtension(jinja2.ext.Extension):
77
def __init__(self, environment):
88
super(WebpackExtension, self).__init__(environment)
9-
environment.globals["render_bundle"] = lambda name: jinja2.Markup(render_bundle(name))
9+
environment.globals["render_bundle"] = lambda *a, **k: jinja2.Markup(render_bundle(*a, **k))

0 commit comments

Comments
 (0)