fix utils.template.render_templatefile() bug +test #1212
Conversation
@Digenis - a good catch about rstrip, +1 to fix it. I'm not a fan of mock tests though - they are unreadable, and they may require a rewrite if the internal implementation changes. But some tests may be better than no tests :) Maybe we should just switch to https://github.com/audreyr/cookiecutter to reduce maintenance burden. It is well-supported, provide more features and already works in Python 3. |
@@ -10,7 +10,7 @@ def render_templatefile(path, **kwargs): | |||
|
|||
content = string.Template(raw).substitute(**kwargs) | |||
|
|||
with open(path.rstrip('.tmpl'), 'wb') as file: | |||
with open(re.sub(r'\.tmpl$', '', path), 'wb') as file: |
dangra
May 7, 2015
Member
I prefer to use simple string functions instead of regex whenever I can, my suggestion is to use path.rpartition('.')[0]
if this is about striping the template extension only.
In any case it looks good to me.
I prefer to use simple string functions instead of regex whenever I can, my suggestion is to use path.rpartition('.')[0]
if this is about striping the template extension only.
In any case it looks good to me.
cookiecutter brings several dependencies https://github.com/audreyr/cookiecutter/blob/master/setup.py#L26-L31 |
8b8c052
to
85b80b6
@dangra I would use .rpartition if there was some assurance about the filename .tmpl extension. What I would do in order of preference is: Should I go for the first one? |
@@ -10,7 +10,8 @@ def render_templatefile(path, **kwargs): | |||
|
|||
content = string.Template(raw).substitute(**kwargs) | |||
|
|||
with open(path.rstrip('.tmpl'), 'wb') as file: | |||
render_path = path[:len('.tmpl')] if path.endswith('.tmpl') else path | |||
with open(render_path) as file: |
dangra
May 7, 2015
Member
missing w
mode.
missing w
mode.
@@ -10,7 +10,8 @@ def render_templatefile(path, **kwargs): | |||
|
|||
content = string.Template(raw).substitute(**kwargs) | |||
|
|||
with open(path.rstrip('.tmpl'), 'wb') as file: | |||
render_path = path[:len('.tmpl')] if path.endswith('.tmpl') else path |
dangra
May 7, 2015
Member
>>> path = 'templ.tmpl'
>>> path[:len('.tmpl')]
'templ'
>>> path = 'footempl.tmpl'
>>> path[:len('.tmpl')]
'foote'
>>> path[:-len('.tmpl')]
'footempl'
>>> path = 'templ.tmpl'
>>> path[:len('.tmpl')]
'templ'
>>> path = 'footempl.tmpl'
>>> path[:len('.tmpl')]
'foote'
>>> path[:-len('.tmpl')]
'footempl'
+1 to keep same behavior. Ignore the rpartition suggestion. |
85b80b6
to
4ffd278
What about striping the extension in the argument to the render_templatefile() call? |
by stripping in the caller you are changing the function public interface and that will lead to more discussions for a bugfix that is very simple to fix within the function. |
ok |
Is there something left to do (in the scope of this PR)? |
what is the point of mocking open() when the test can use a real file? |
I thought this is how people mostly test side-effects. |
for things that creates actual files we let them create temporal files. |
+1 to create a real file; in my experience tests without mocks are easier to read and less likely to break (as they don't rely on implementation details). You can also pause the test and check if correct files exists manually. The downside is execution speed (creating/removing real files is slower), but it shouldn't be a problem in this test case. |
OK, fair enough, I'll use the test_dir. |
+1 to rebase |
Current coverage is
|
cc9ac0b
to
b98ba53
Used tempfile and rebased. |
looks good, squash? |
b98ba53
to
56b3cf0
squahsed |
fix utils.template.render_templatefile() bug +test
71bd79e
into
scrapy:master
thanks! |
I fixed a potential bug (untriggered so far because of the
.py
extension of the template files)but while writing the test, it ended taking up most of the changes done in this PR.
Please comment whether I should just push the bugfix.
The potential bug is that
.rstrip()
inutils.template.render_templatefile()
is used like it is expected to strip a string, not a character class
(think of using
re.sub(r'[\.tmpl]*$, '', path)
instead ofre.sub(r'\.tmpl$', '', path)
.On filepaths that always end with '.py.tmpl', the problem goes unnoticed.
Also, shouldn't there be some expectation or even an assertion
that the template files have the '.tmpl' extension?