Skip to content

Commit

Permalink
Merge b98ba53 into 026a1ca
Browse files Browse the repository at this point in the history
  • Loading branch information
Digenis committed Aug 26, 2015
2 parents 026a1ca + b98ba53 commit d151499
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
3 changes: 2 additions & 1 deletion scrapy/utils/template.py
Expand Up @@ -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, 'wb') as file:
file.write(content)
if path.endswith('.tmpl'):
os.remove(path)
Expand Down
41 changes: 41 additions & 0 deletions tests/test_utils_template.py
@@ -1 +1,42 @@
import os
from shutil import rmtree
from tempfile import mkdtemp
import unittest
from scrapy.utils.template import render_templatefile


__doctests__ = ['scrapy.utils.template']


class UtilsRenderTemplateFileTestCase(unittest.TestCase):

def setUp(self):
self.tmp_path = mkdtemp()

def tearDown(self):
rmtree(self.tmp_path)

def test_simple_render(self):

context = dict(project_name='proj', name='spi', classname='TheSpider')
template = u'from ${project_name}.spiders.${name} import ${classname}'
rendered = u'from proj.spiders.spi import TheSpider'

template_path = os.path.join(self.tmp_path, 'templ.py.tmpl')
render_path = os.path.join(self.tmp_path, 'templ.py')

with open(template_path, 'wb') as tmpl_file:
tmpl_file.write(template.encode('utf8'))
assert os.path.isfile(template_path) # Failure of test itself

render_templatefile(template_path, **context)

self.assertFalse(os.path.exists(template_path))
with open(render_path, 'rb') as result:
self.assertEqual(result.read().decode('utf8'), rendered)

os.remove(render_path)
assert not os.path.exists(render_path) # Failure of test iself

if '__main__' == __name__:
unittest.main()

0 comments on commit d151499

Please sign in to comment.