Skip to content

Commit 610674f

Browse files
authored
Merge pull request #274 from asottile/sass_middleware_without_extension
Honor strip_extension in SassMiddleware
2 parents 2dfde14 + e51aa90 commit 610674f

File tree

4 files changed

+45
-28
lines changed

4 files changed

+45
-28
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ language: python
22
dist: trusty
33
matrix:
44
include:
5-
- python: pypy-5.4.1
5+
- python: pypy2.7-5.10.0
66
- python: 3.7
77
dist: xenial
88
sudo: required

sasstests.py

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
import tempfile
1515
import traceback
1616
import unittest
17-
import warnings
1817

1918
import pytest
2019
from six import StringIO, b, string_types, text_type
@@ -424,14 +423,11 @@ def test_compile_string_deprecated_source_comments_line_numbers(self):
424423
color: red;
425424
}'''
426425
expected = sass.compile(string=source, source_comments=True)
427-
with warnings.catch_warnings(record=True) as w:
428-
warnings.simplefilter('always')
426+
with pytest.warns(FutureWarning):
429427
actual = sass.compile(
430428
string=source,
431429
source_comments='line_numbers',
432430
)
433-
assert len(w) == 1
434-
assert issubclass(w[-1].category, FutureWarning)
435431
assert expected == actual
436432

437433
def test_compile_filename(self):
@@ -465,15 +461,12 @@ def test_compile_source_map_deprecated_source_comments_map(self):
465461
filename=filename,
466462
source_map_filename='a.scss.css.map',
467463
)
468-
with warnings.catch_warnings(record=True) as w:
469-
warnings.simplefilter('always')
464+
with pytest.warns(FutureWarning):
470465
actual, actual_map = sass.compile(
471466
filename=filename,
472467
source_comments='map',
473468
source_map_filename='a.scss.css.map',
474469
)
475-
assert len(w) == 1
476-
assert issubclass(w[-1].category, FutureWarning)
477470
assert expected == actual
478471
self.assert_source_map_equal(expected_map, actual_map)
479472

@@ -595,8 +588,7 @@ def test_output_style(self):
595588
class ManifestTestCase(BaseTestCase):
596589

597590
def test_normalize_manifests(self):
598-
with warnings.catch_warnings(record=True) as w:
599-
warnings.simplefilter('always')
591+
with pytest.warns(FutureWarning) as warninfo:
600592
manifests = Manifest.normalize_manifests({
601593
'package': 'sass/path',
602594
'package.name': ('sass/path', 'css/path'),
@@ -607,8 +599,7 @@ def test_normalize_manifests(self):
607599
'strip_extension': True,
608600
},
609601
})
610-
assert len(w) == 3
611-
assert all(issubclass(x.category, FutureWarning) for x in w)
602+
assert len(warninfo) == 3
612603
assert len(manifests) == 4
613604
assert isinstance(manifests['package'], Manifest)
614605
assert manifests['package'].sass_path == 'sass/path'
@@ -635,11 +626,8 @@ def replace_source_path(s, name):
635626
return s.replace('SOURCE', test_source_path(name))
636627

637628
shutil.copytree('test', src_path)
638-
with warnings.catch_warnings(record=True) as w:
639-
warnings.simplefilter('always')
629+
with pytest.warns(FutureWarning):
640630
m = Manifest(sass_path='test', css_path='css')
641-
assert len(w) == 1
642-
assert issubclass(w[-1].category, FutureWarning)
643631

644632
m.build_one(d, 'a.scss')
645633
with open(os.path.join(d, 'css', 'a.scss.css')) as f:
@@ -720,15 +708,12 @@ def test_wsgi_sass_middleware(self):
720708
with tempdir() as css_dir:
721709
src_dir = os.path.join(css_dir, 'src')
722710
shutil.copytree('test', src_dir)
723-
with warnings.catch_warnings(record=True) as w:
724-
warnings.simplefilter('always')
711+
with pytest.warns(FutureWarning):
725712
app = SassMiddleware(
726713
self.sample_wsgi_app, {
727714
__name__: (src_dir, css_dir, '/static'),
728715
},
729716
)
730-
assert len(w) == 1
731-
assert issubclass(w[-1].category, FutureWarning)
732717
client = Client(app, Response)
733718
r = client.get('/asdf')
734719
assert r.status_code == 200
@@ -746,6 +731,27 @@ def test_wsgi_sass_middleware(self):
746731
self.assertEqual(b'/static/not-exists.sass.css', r.data)
747732
assert r.mimetype == 'text/plain'
748733

734+
def test_wsgi_sass_middleware_without_extension(self):
735+
with tempdir() as css_dir:
736+
src_dir = os.path.join(css_dir, 'src')
737+
shutil.copytree('test', src_dir)
738+
app = SassMiddleware(
739+
self.sample_wsgi_app, {
740+
__name__: {
741+
'sass_path': src_dir,
742+
'css_path': css_dir,
743+
'wsgi_path': '/static',
744+
'strip_extension': True,
745+
},
746+
},
747+
)
748+
client = Client(app, Response)
749+
r = client.get('/static/a.css')
750+
assert r.status_code == 200
751+
expected = A_EXPECTED_CSS_WITH_MAP.replace('.scss.css', '.css')
752+
self.assertEqual(expected.encode(), r.data)
753+
assert r.mimetype == 'text/css'
754+
749755

750756
class DistutilsTestCase(BaseTestCase):
751757

@@ -828,15 +834,12 @@ def test_pysassc_stdout(self):
828834
assert A_EXPECTED_CSS.strip() == self.out.getvalue().strip()
829835

830836
def test_sassc_stdout(self):
831-
with warnings.catch_warnings(record=True) as w:
832-
warnings.simplefilter('always')
837+
with pytest.warns(FutureWarning) as warninfo:
833838
exit_code = sassc.main(
834839
['sassc', 'test/a.scss'],
835840
self.out, self.err,
836841
)
837-
assert len(w) == 1
838-
assert issubclass(w[-1].category, FutureWarning)
839-
assert 'use `pysassc`' in str(w[-1].message)
842+
assert 'use `pysassc`' in warninfo[0].message.args[0]
840843
assert exit_code == 0
841844
assert self.err.getvalue() == ''
842845
assert A_EXPECTED_CSS.strip() == self.out.getvalue().strip()

sassutils/builder.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,20 @@ def resolve_filename(self, package_dir, filename):
188188
css_path = os.path.join(package_dir, self.css_path, css_filename)
189189
return sass_path, css_path
190190

191+
def unresolve_filename(self, filename):
192+
"""Retrieves the probable source path from the output filename. Pass
193+
in a .css path to get out a .scss path.
194+
195+
:param filename: the css filename
196+
:type filename: :class:`str`
197+
:returns: the scss filename
198+
:rtype: :class:`str`
199+
"""
200+
filename, _ = os.path.splitext(filename)
201+
if self.strip_extension:
202+
filename = filename + '.scss'
203+
return filename
204+
191205
def build(self, package_dir, output_style='nested'):
192206
"""Builds the Sass/SCSS files in the specified :attr:`sass_path`.
193207
It finds :attr:`sass_path` and locates :attr:`css_path`

sassutils/wsgi.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ def __call__(self, environ, start_response):
125125
if not path.startswith(prefix):
126126
continue
127127
css_filename = path[len(prefix):]
128-
sass_filename = css_filename[:-4]
128+
sass_filename = manifest.unresolve_filename(css_filename)
129129
try:
130130
result = manifest.build_one(
131131
package_dir,

0 commit comments

Comments
 (0)