Skip to content

Commit

Permalink
Merge pull request #111 from asottile/disallow_arbitrary_kwargs
Browse files Browse the repository at this point in the history
Disallow arbitrary kwargs in compile()
  • Loading branch information
asottile committed Dec 10, 2015
2 parents 86e9fc2 + 0aa67c6 commit 797a571
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 6 deletions.
15 changes: 14 additions & 1 deletion sass.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,16 @@ def compile_dirname(
return True, None


def _check_no_remaining_kwargs(func, kwargs):
if kwargs:
raise TypeError(
'{0}() got unexpected keyword argument(s) {1}'.format(
func.__name__,
', '.join("'{0}'".format(arg) for arg in sorted(kwargs)),
)
)


def compile(**kwargs):
"""There are three modes of parameters :func:`compile()` can take:
``string``, ``filename``, and ``dirname``.
Expand Down Expand Up @@ -456,9 +466,10 @@ def func_name(a, b):
if not isinstance(indented, bool):
raise TypeError('indented must be bool, not ' +
repr(source_comments))
_check_no_remaining_kwargs(compile, kwargs)
s, v = compile_string(
string, output_style, source_comments, include_paths, precision,
custom_functions, indented
custom_functions, indented,
)
if s:
return v.decode('utf-8')
Expand All @@ -470,6 +481,7 @@ def func_name(a, b):
raise IOError('{0!r} seems not a file'.format(filename))
elif isinstance(filename, text_type):
filename = filename.encode(fs_encoding)
_check_no_remaining_kwargs(compile, kwargs)
s, v, source_map = compile_filename(
filename, output_style, source_comments, include_paths, precision,
source_map_filename, custom_functions,
Expand Down Expand Up @@ -515,6 +527,7 @@ def func_name(a, b):
except ValueError:
raise ValueError('dirname must be a pair of (source_dir, '
'output_dir)')
_check_no_remaining_kwargs(compile, kwargs)
s, v = compile_dirname(
search_path, output_path, output_style, source_comments,
include_paths, precision, custom_functions,
Expand Down
34 changes: 29 additions & 5 deletions sasstests.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,19 @@ def test_compile_invalid_source_comments(self):
string='a { color: blue; }',
source_comments='invalid')

def test_compile_disallows_arbitrary_arguments(self):
for args in (
{'string': 'a{b:c}'},
{'filename': 'test/a.scss'},
{'dirname': ('test', '/dev/null')},
):
with assert_raises(TypeError) as excinfo:
sass.compile(herp='derp', harp='darp', **args)
msg, = excinfo.value.args
assert msg == (
"compile() got unexpected keyword argument(s) 'harp', 'herp'"
)

def test_compile_string(self):
actual = sass.compile(string='a { b { color: blue; } }')
assert actual == 'a b {\n color: blue; }\n'
Expand Down Expand Up @@ -1023,13 +1036,24 @@ def compile_with_func(s):


@contextlib.contextmanager
def assert_raises_compile_error(expected):
def assert_raises(exctype):
# I want pytest.raises, this'll have to do for now
class C:
pass

try:
yield
yield C
assert False, 'Expected to raise!'
except sass.CompileError as e:
msg, = e.args
assert msg.decode('UTF-8') == expected, (msg, expected)
except exctype as e:
C.value = e


@contextlib.contextmanager
def assert_raises_compile_error(expected):
with assert_raises(sass.CompileError) as excinfo:
yield
msg, = excinfo.value.args
assert msg.decode('UTF-8') == expected, (msg, expected)


class RegexMatcher(object):
Expand Down

0 comments on commit 797a571

Please sign in to comment.