Skip to content

Commit 6346a20

Browse files
committed
Merge pull request jazzband#531 from theatlantic/pr/compiler-compressor-unit-tests
Add unit tests for the bundled compiler and compressor classes
2 parents 0ed51f7 + c17fd82 commit 6346a20

36 files changed

+429
-10
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ docs/_build/
1313
coverage/
1414
tests/static/
1515
tests/assets/js/dummy.js
16+
tests/node_modules/
1617
.tox/
1718
.DS_Store
1819
.idea
@@ -21,3 +22,6 @@ tests/assets/js/dummy.js
2122
.pydevproject
2223
.ropeproject
2324
__pycache__
25+
npm-debug.log
26+
tests/npm-cache
27+
django-pipeline-*/

MANIFEST.in

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,8 @@ recursive-include pipeline/jinja2 *.html *.jinja
33
include AUTHORS LICENSE README.rst HISTORY.rst
44
recursive-include tests *
55
recursive-exclude tests *.pyc *.pyo
6+
recursive-exclude tests/node_modules *
7+
recursive-exclude tests/npm-cache *
8+
recursive-exclude tests/npm *
69
include docs/Makefile docs/make.bat docs/conf.py
710
recursive-include docs *.rst

pipeline/compilers/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
from pipeline.conf import settings
1414
from pipeline.exceptions import CompilerError
15-
from pipeline.utils import to_class
15+
from pipeline.utils import to_class, set_std_streams_blocking
1616

1717

1818
class Compiler(object):
@@ -120,6 +120,7 @@ def execute_command(self, command, cwd=None, stdout_captured=None):
120120
stdout=stdout,
121121
stderr=subprocess.PIPE)
122122
_, stderr = compiling.communicate()
123+
set_std_streams_blocking()
123124

124125
if compiling.returncode != 0:
125126
stdout_captured = None # Don't save erroneous result.

pipeline/compressors/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
from pipeline.conf import settings
1616
from pipeline.exceptions import CompressorError
17-
from pipeline.utils import to_class, relpath
17+
from pipeline.utils import to_class, relpath, set_std_streams_blocking
1818

1919
URL_DETECTOR = r"""url\((['"]){0,1}\s*(.*?)["']{0,1}\)"""
2020
URL_REPLACER = r"""url\(__EMBED__(.+?)(\?\d+)?\)"""
@@ -248,6 +248,7 @@ def execute_command(self, command, content):
248248
if content:
249249
content = smart_bytes(content)
250250
stdout, stderr = pipe.communicate(content)
251+
set_std_streams_blocking()
251252
if stderr.strip() and pipe.returncode != 0:
252253
raise CompressorError(stderr)
253254
elif self.verbose:

pipeline/conf.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# -*- coding: utf-8 -*-
22
from __future__ import unicode_literals
33

4+
import os
45
import collections
56
import shlex
67

@@ -101,7 +102,7 @@ def __getitem__(self, key):
101102
value = self.settings[key]
102103
if key.endswith(("_BINARY", "_ARGUMENTS")):
103104
if isinstance(value, string_types):
104-
return tuple(shlex.split(value))
105+
return tuple(shlex.split(value, posix=(os.name == 'posix')))
105106
return tuple(value)
106107
return value
107108

pipeline/utils.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
11
from __future__ import unicode_literals
22

3+
try:
4+
import fcntl
5+
except ImportError:
6+
# windows
7+
fcntl = None
8+
39
import importlib
410
import mimetypes
511
import posixpath
12+
import os
13+
import sys
614

715
try:
816
from urllib.parse import quote
@@ -54,3 +62,19 @@ def relpath(path, start=posixpath.curdir):
5462
if not rel_list:
5563
return posixpath.curdir
5664
return posixpath.join(*rel_list)
65+
66+
67+
def set_std_streams_blocking():
68+
"""
69+
Set stdout and stderr to be blocking.
70+
71+
This is called after Popen.communicate() to revert stdout and stderr back
72+
to be blocking (the default) in the event that the process to which they
73+
were passed manipulated one or both file descriptors to be non-blocking.
74+
"""
75+
if not fcntl:
76+
return
77+
for f in (sys.__stdout__, sys.__stderr__):
78+
fileno = f.fileno()
79+
flags = fcntl.fcntl(fileno, fcntl.F_GETFL)
80+
fcntl.fcntl(fileno, fcntl.F_SETFL, flags & ~os.O_NONBLOCK)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
(function() {
2+
var cube, square;
3+
4+
square = function(x) {
5+
return x * x;
6+
};
7+
8+
cube = function(x) {
9+
return square(x) * x;
10+
};
11+
12+
}).call(this);
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
square = (x) -> x * x
2+
cube = (x) -> square(x) * x
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
"use strict";
2+
3+
// Expression bodies
4+
var odds = evens.map(function (v) {
5+
return v + 1;
6+
});
7+
var nums = evens.map(function (v, i) {
8+
return v + i;
9+
});
10+
11+
// Statement bodies
12+
nums.forEach(function (v) {
13+
if (v % 5 === 0) fives.push(v);
14+
});
15+
16+
// Lexical this
17+
var bob = {
18+
_name: "Bob",
19+
_friends: [],
20+
printFriends: function printFriends() {
21+
var _this = this;
22+
23+
this._friends.forEach(function (f) {
24+
return console.log(_this._name + " knows " + f);
25+
});
26+
}
27+
};

tests/assets/compilers/es6/input.es6

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Expression bodies
2+
var odds = evens.map(v => v + 1);
3+
var nums = evens.map((v, i) => v + i);
4+
5+
// Statement bodies
6+
nums.forEach(v => {
7+
if (v % 5 === 0)
8+
fives.push(v);
9+
});
10+
11+
// Lexical this
12+
var bob = {
13+
_name: "Bob",
14+
_friends: [],
15+
printFriends() {
16+
this._friends.forEach(f =>
17+
console.log(this._name + " knows " + f));
18+
}
19+
};

0 commit comments

Comments
 (0)