1
0
mirror of https://github.com/scrapy/scrapy.git synced 2025-03-14 04:21:15 +00:00
scrapy/tests/test_utils_template.py
Matthew Donoughe 40d9ca3bdd
use pathlib
2022-10-17 17:40:10 -04:00

42 lines
1.2 KiB
Python

from pathlib import Path
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 = 'from ${project_name}.spiders.${name} import ${classname}'
rendered = 'from proj.spiders.spi import TheSpider'
template_path = Path(self.tmp_path, 'templ.py.tmpl')
render_path = Path(self.tmp_path, 'templ.py')
template_path.write_text(template, encoding='utf8')
assert template_path.is_file() # Failure of test itself
render_templatefile(str(template_path), **context)
self.assertFalse(template_path.exists())
self.assertEqual(render_path.read_text(encoding='utf8'), rendered)
render_path.unlink()
assert not render_path.exists() # Failure of test itself
if '__main__' == __name__:
unittest.main()