Skip to content
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

Fix Custom Filters and Tests on Windows #42

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
19 changes: 12 additions & 7 deletions jinja_to_js/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,13 @@ def temp_var_names_generator():
x += 1


def clean_path(file_path):
# Jinja2 doesn't accept Windows filepaths (but does output them!)
if os.name == 'nt':
return file_path.replace(os.path.sep, '/')
return file_path


class JinjaToJS(object):

def __init__(self,
Expand Down Expand Up @@ -227,9 +234,7 @@ def __init__(self,

self._add_dependency(self.runtime_path, 'jinjaToJS')

# Jinja2 doesn't accept Windows filepaths
if os.name == 'nt':
self.template_name = self.template_name.replace(os.pathsep, '/')
self.template_name = clean_path(self.template_name)

template_string, template_path, _ = self.environment.loader.get_source(
self.environment, self.template_name
Expand Down Expand Up @@ -298,6 +303,7 @@ def _add_dependency(self, dependency, var_name=None):
Returns:
str
"""
dependency = clean_path(dependency)
if var_name is None:
var_name = next(self.temp_var_names)
# Don't add duplicate dependencies
Expand Down Expand Up @@ -342,7 +348,8 @@ def _process_extends(self, node, **kwargs):
include_prefix=self.include_prefix,
include_ext=self.include_ext,
child_blocks=self.child_blocks,
dependencies=self.dependencies)
dependencies=self.dependencies,
custom_filters=self.custom_filters)

# add the parent templates output to the current output
self.output.write(parent_template.output.getvalue())
Expand Down Expand Up @@ -976,9 +983,7 @@ def _process_include(self, node, **kwargs):
if not include_path.startswith('.'):
include_path = './' + include_path

# Jinja2 doesn't accept Windows filepaths (but does output them!)
if os.name == 'nt':
include_path = include_path.replace(os.pathsep, '/')
include_path = clean_path(include_path)

include_path = path.splitext(include_path)[0] + self.include_ext
include_var_name = self._get_depencency_var_name(include_path)
Expand Down
1 change: 1 addition & 0 deletions tests/templates/extends_custom_filters.jinja
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{% extends 'custom_filters.jinja' %}
18 changes: 13 additions & 5 deletions tests/test_jinja_to_js.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import pytest

from jinja_to_js import JinjaToJS, is_method_call
from jinja_to_js import JinjaToJS, is_method_call, clean_path

if "check_output" not in dir(subprocess):
def check_output(*popenargs, **kwargs):
Expand Down Expand Up @@ -334,6 +334,14 @@ def unicode_snowmen(value):

self._run_test('custom_filters.jinja')

def test_extends_custom_filters(self):
def unicode_snowmen(value):
return ''.join(['☃' for x in value])

self.env.filters['unicode_snowmen'] = unicode_snowmen

self._run_test('extends_custom_filters.jinja')

def test_custom_global(self):
def convert_to_uppercase(value):
return value.upper()
Expand All @@ -357,11 +365,11 @@ def _run_test(self, name, additional=None, **kwargs):
jinja_result = self.env.get_template(name).render(**kwargs).strip()

# create the main template
path = self._compile_js_template(name)
path = clean_path(self._compile_js_template(name))
template_args = [path]

# create a temp file containing the data
data_file_path = self._write_to_temp_file(json.dumps(kwargs, cls=Encoder))
data_file_path = clean_path(self._write_to_temp_file(json.dumps(kwargs, cls=Encoder)))

# if additional template are required e.g. for includes then create those too
if additional:
Expand All @@ -372,7 +380,7 @@ def _run_test(self, name, additional=None, **kwargs):
try:
js_result = check_output(
['node',
self.NODE_SCRIPT_PATH]
clean_path(self.NODE_SCRIPT_PATH)]
+ template_args +
[data_file_path]
)
Expand Down Expand Up @@ -401,7 +409,7 @@ def _compile_js_template(self, name):
custom_filters=['unicode_snowmen']
).get_output()

target = self.temp_dir + '/' + os.path.splitext(name)[0] + '.js'
target = os.path.join(self.temp_dir, os.path.splitext(name)[0] + '.js')

if not os.path.exists(os.path.dirname(target)):
os.makedirs(os.path.dirname(target))
Expand Down