Skip to content

Honor strip_extension in SassMiddleware #274

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ language: python
dist: trusty
matrix:
include:
- python: pypy-5.4.1
- python: pypy2.7-5.10.0
- python: 3.7
dist: xenial
sudo: required
Expand Down
55 changes: 29 additions & 26 deletions sasstests.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import tempfile
import traceback
import unittest
import warnings

import pytest
from six import StringIO, b, string_types, text_type
Expand Down Expand Up @@ -424,14 +423,11 @@ def test_compile_string_deprecated_source_comments_line_numbers(self):
color: red;
}'''
expected = sass.compile(string=source, source_comments=True)
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter('always')
with pytest.warns(FutureWarning):
actual = sass.compile(
string=source,
source_comments='line_numbers',
)
assert len(w) == 1
assert issubclass(w[-1].category, FutureWarning)
assert expected == actual

def test_compile_filename(self):
Expand Down Expand Up @@ -465,15 +461,12 @@ def test_compile_source_map_deprecated_source_comments_map(self):
filename=filename,
source_map_filename='a.scss.css.map',
)
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter('always')
with pytest.warns(FutureWarning):
actual, actual_map = sass.compile(
filename=filename,
source_comments='map',
source_map_filename='a.scss.css.map',
)
assert len(w) == 1
assert issubclass(w[-1].category, FutureWarning)
assert expected == actual
self.assert_source_map_equal(expected_map, actual_map)

Expand Down Expand Up @@ -595,8 +588,7 @@ def test_output_style(self):
class ManifestTestCase(BaseTestCase):

def test_normalize_manifests(self):
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter('always')
with pytest.warns(FutureWarning) as warninfo:
manifests = Manifest.normalize_manifests({
'package': 'sass/path',
'package.name': ('sass/path', 'css/path'),
Expand All @@ -607,8 +599,7 @@ def test_normalize_manifests(self):
'strip_extension': True,
},
})
assert len(w) == 3
assert all(issubclass(x.category, FutureWarning) for x in w)
assert len(warninfo) == 3
assert len(manifests) == 4
assert isinstance(manifests['package'], Manifest)
assert manifests['package'].sass_path == 'sass/path'
Expand All @@ -635,11 +626,8 @@ def replace_source_path(s, name):
return s.replace('SOURCE', test_source_path(name))

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

m.build_one(d, 'a.scss')
with open(os.path.join(d, 'css', 'a.scss.css')) as f:
Expand Down Expand Up @@ -720,15 +708,12 @@ def test_wsgi_sass_middleware(self):
with tempdir() as css_dir:
src_dir = os.path.join(css_dir, 'src')
shutil.copytree('test', src_dir)
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter('always')
with pytest.warns(FutureWarning):
app = SassMiddleware(
self.sample_wsgi_app, {
__name__: (src_dir, css_dir, '/static'),
},
)
assert len(w) == 1
assert issubclass(w[-1].category, FutureWarning)
client = Client(app, Response)
r = client.get('/asdf')
assert r.status_code == 200
Expand All @@ -746,6 +731,27 @@ def test_wsgi_sass_middleware(self):
self.assertEqual(b'/static/not-exists.sass.css', r.data)
assert r.mimetype == 'text/plain'

def test_wsgi_sass_middleware_without_extension(self):
with tempdir() as css_dir:
src_dir = os.path.join(css_dir, 'src')
shutil.copytree('test', src_dir)
app = SassMiddleware(
self.sample_wsgi_app, {
__name__: {
'sass_path': src_dir,
'css_path': css_dir,
'wsgi_path': '/static',
'strip_extension': True,
},
},
)
client = Client(app, Response)
r = client.get('/static/a.css')
assert r.status_code == 200
expected = A_EXPECTED_CSS_WITH_MAP.replace('.scss.css', '.css')
self.assertEqual(expected.encode(), r.data)
assert r.mimetype == 'text/css'


class DistutilsTestCase(BaseTestCase):

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

def test_sassc_stdout(self):
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter('always')
with pytest.warns(FutureWarning) as warninfo:
exit_code = sassc.main(
['sassc', 'test/a.scss'],
self.out, self.err,
)
assert len(w) == 1
assert issubclass(w[-1].category, FutureWarning)
assert 'use `pysassc`' in str(w[-1].message)
assert 'use `pysassc`' in warninfo[0].message.args[0]
assert exit_code == 0
assert self.err.getvalue() == ''
assert A_EXPECTED_CSS.strip() == self.out.getvalue().strip()
Expand Down
14 changes: 14 additions & 0 deletions sassutils/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,20 @@ def resolve_filename(self, package_dir, filename):
css_path = os.path.join(package_dir, self.css_path, css_filename)
return sass_path, css_path

def unresolve_filename(self, filename):
"""Retrieves the probable source path from the output filename. Pass
in a .css path to get out a .scss path.
:param filename: the css filename
:type filename: :class:`str`
:returns: the scss filename
:rtype: :class:`str`
"""
filename, _ = os.path.splitext(filename)
if self.strip_extension:
filename = filename + '.scss'
return filename

def build(self, package_dir, output_style='nested'):
"""Builds the Sass/SCSS files in the specified :attr:`sass_path`.
It finds :attr:`sass_path` and locates :attr:`css_path`
Expand Down
2 changes: 1 addition & 1 deletion sassutils/wsgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def __call__(self, environ, start_response):
if not path.startswith(prefix):
continue
css_filename = path[len(prefix):]
sass_filename = css_filename[:-4]
sass_filename = manifest.unresolve_filename(css_filename)
try:
result = manifest.build_one(
package_dir,
Expand Down