コード例 #1
0
ファイル: resources.py プロジェクト: diecutter/piecutter
    def test_render(self):
        """DirResource.render() returns an iterable of rendered templates."""
        with temporary_directory() as template_dir:
            # Setup.
            expected_filename = 'rendered-filename/{args[0]!s}'
            filename_engine = MockEngine(expected_filename)
            expected_content = u'rendered-content/{args[0]!s}'
            content_engine = MockEngine(expected_content)
            context = {'fake': 'fake-context'}
            dir_path = join(template_dir, 'dir')
            mkdir(dir_path)
            dir_path += sep
            file_path = join(dir_path, 'file')
            open(file_path, 'w').write('data')
            resource = resources.DirResource(path=dir_path,
                                             engine=content_engine,
                                             filename_engine=filename_engine)
            # Render.
            rendered = resource.render(context)
            # Check result.
            rendered = [part for part in rendered]
            self.assertEqual(len(rendered), 1)  # One file rendered.
            self.assertEqual(len(rendered[0]), 2)  # filename, content.
            self.assertEqual(rendered[0][0], 'rendered-filename/file')

            self.assertEqual(u''.join(rendered[0][1]),
                             u'rendered-content/data')
コード例 #2
0
ファイル: resources.py プロジェクト: diecutter/piecutter
 def test_render_dynamic_tree(self):
     """DirResource.render_tree() uses directory tree template."""
     content_engine = mock.MagicMock()
     content_engine.render = lambda t, c: t
     filename_engine = mock.MagicMock()
     context = {'fake': 'fake-context'}
     with temporary_directory() as template_dir:
         dir_path = template_dir
         directory_tree = [{
             'template': 'template_one.txt',
             'filename': '1.txt',
             'context': {}
         }, {
             'template': 'template_two.txt',
             'filename': '2.txt',
             'context': {}
         }]
         open(join(dir_path, resources.TREE_TEMPLATE),
              'w').write(json.dumps(directory_tree))
         dir_path += sep
         resource = resources.DirResource(path=dir_path,
                                          engine=content_engine,
                                          filename_engine=filename_engine)
         rendered = list(resource.render_tree(context))
         self.assertEqual(rendered,
                          [(unicode(join(template_dir, 'template_one.txt')),
                            u'1.txt', context),
                           (unicode(join(template_dir, 'template_two.txt')),
                            u'2.txt', context)])
     self.assertFalse(filename_engine.called)
コード例 #3
0
ファイル: resources.py プロジェクト: diecutter/piecutter
    def test_render_dynamic_tree_relative_paths(self):
        """Raises exception if directory tree contains some non relative path.

        .. warning::

           This is a security test!

           Since dynamic tree templates can be defined by user, we have to
           check templates path. We don't want users to be able to render
           arbitrary locations on the filesystem.

        """
        content_engine = mock.MagicMock()
        content_engine.render = lambda t, c: t
        filename_engine = mock.MagicMock()
        context = {'fake': 'fake-context'}
        with temporary_directory() as template_dir:
            dir_path = template_dir
            directory_tree = [{
                'template': '../template_one.txt',
                'filename': '1.txt',
                'context': {}
            }]
            open(join(dir_path, resources.TREE_TEMPLATE),
                 'w').write(json.dumps(directory_tree))
            dir_path += sep
            resource = resources.DirResource(path=dir_path,
                                             engine=content_engine,
                                             filename_engine=filename_engine)
            generate_content = lambda: [(filename, ''.join(content)) for (
                filename, content) in resource.render(context)]
            self.assertRaises(ValueError, generate_content)
        self.assertFalse(filename_engine.called)
コード例 #4
0
ファイル: resources.py プロジェクト: diecutter/piecutter
 def test_render_tree(self):
     """DirResource.render_tree() recurses nested files and directories."""
     expected_output_filename = 'rendered/{args[0]!s}'
     filename_engine = MockEngine(expected_output_filename)
     context = {'fake': 'fake-context'}
     with temporary_directory() as template_dir:
         dir_path = join(template_dir, 'dummy')
         mkdir(dir_path)
         for dir_name in ('+a+', 'b'):
             mkdir(join(dir_path, dir_name))
             for file_name in ('+one+', 'two'):
                 file_path = join(dir_path, dir_name, file_name)
                 open(file_path, 'w')
         dir_path += sep
         resource = resources.DirResource(path=dir_path,
                                          filename_engine=filename_engine)
         rendered = list(resource.render_tree(context))
         self.assertEqual(rendered,
                          [(join(template_dir, 'dummy', '+a+',
                                 '+one+'), 'rendered/+a+/+one+', context),
                           (join(template_dir, 'dummy', '+a+',
                                 'two'), 'rendered/+a+/two', context),
                           (join(template_dir, 'dummy', 'b',
                                 '+one+'), 'rendered/b/+one+', context),
                           (join(template_dir, 'dummy', 'b',
                                 'two'), 'rendered/b/two', context)])
コード例 #5
0
ファイル: resources.py プロジェクト: diecutter/piecutter
 def test_has_tree_template(self):
     """DirResource.has_tree_template() checks if directory tree exists."""
     with temporary_directory() as template_dir:
         dir_path = template_dir
         resource = resources.DirResource(path=dir_path)
         self.assertFalse(resource.has_tree_template())
         open(join(dir_path, resources.TREE_TEMPLATE), 'w')
         self.assertTrue(resource.has_tree_template())
コード例 #6
0
ファイル: resources.py プロジェクト: diecutter/piecutter
 def test_exists_file(self):
     """DirResource.exists is False if path points a file."""
     with temporary_directory() as template_dir:
         path = join(template_dir, 'dummy')
         open(path, 'w')
         self.assertTrue(isfile(path))  # Check initial status.
         resource = resources.DirResource(path=path)
         self.assertTrue(resource.exists is False)
コード例 #7
0
ファイル: resources.py プロジェクト: diecutter/piecutter
 def test_exists_dir(self):
     """DirResource.exists is True if path points a directory."""
     with temporary_directory() as template_dir:
         path = join(template_dir, 'dummy')
         mkdir(path)
         self.assertTrue(isdir(path))  # Check initial status.
         resource = resources.DirResource(path=path)
         self.assertTrue(resource.exists is True)
コード例 #8
0
ファイル: resources.py プロジェクト: diecutter/piecutter
 def test_no_trailing_slash(self):
     """DirResource with no trailing slash uses dirname as prefix."""
     with temporary_directory() as template_dir:
         dir_path = join(template_dir, 'dummy')
         mkdir(dir_path)
         file_path = join(dir_path, 'one')
         open(file_path, 'w')
         resource = resources.DirResource(path=dir_path)
         self.assertEqual(resource.read(), 'dummy/one')
コード例 #9
0
ファイル: resources.py プロジェクト: diecutter/piecutter
 def test_read_one_flat(self):
     """DirResource.read() one file returns one filename."""
     with temporary_directory() as template_dir:
         dir_path = join(template_dir, 'dummy')
         mkdir(dir_path)
         file_path = join(dir_path, 'one')
         open(file_path, 'w')
         dir_path += sep
         resource = resources.DirResource(path=dir_path)
         self.assertEqual(resource.read(), 'one')
コード例 #10
0
ファイル: resources.py プロジェクト: diecutter/piecutter
 def test_read_two_flat(self):
     """DirResource.read() two files returns two filenames."""
     with temporary_directory() as template_dir:
         dir_path = join(template_dir, 'dummy')
         mkdir(dir_path)
         for file_name in ('one', 'two'):
             file_path = join(dir_path, file_name)
             open(file_path, 'w')
         dir_path += sep
         resource = resources.DirResource(path=dir_path)
         self.assertEqual(resource.read(), "one\ntwo")
コード例 #11
0
ファイル: resources.py プロジェクト: diecutter/piecutter
 def test_read_nested(self):
     """DirResource.read() recurses nested files and directories."""
     with temporary_directory() as template_dir:
         dir_path = join(template_dir, 'dummy')
         mkdir(dir_path)
         for dir_name in ('a', 'b'):
             mkdir(join(dir_path, dir_name))
             for file_name in ('one', 'two'):
                 file_path = join(dir_path, dir_name, file_name)
                 open(file_path, 'w')
         dir_path += sep
         resource = resources.DirResource(path=dir_path)
         self.assertEqual(resource.read(), "a/one\na/two\nb/one\nb/two")
コード例 #12
0
ファイル: local.py プロジェクト: adamchainz/diecutter
    def get_resource(self, request):
        """Return the resource matching request.

        Return value is a :py:class:`FileResource` or :py:class`DirResource`.

        """
        path = self.get_resource_path(request)
        engine = self.get_engine(request)
        filename_engine = self.get_filename_engine(request)
        if isdir(path):
            resource = resources.DirResource(path=path,
                                             engine=engine,
                                             filename_engine=filename_engine)
        else:
            resource = resources.FileResource(path=path,
                                              engine=engine,
                                              filename_engine=filename_engine)
        return resource
コード例 #13
0
ファイル: resources.py プロジェクト: diecutter/piecutter
 def test_render_template_error(self):
     with temporary_directory() as template_dir:
         # Setup.
         expected_filename = 'rendered-filename/{args[0]!s}'
         filename_engine = MockEngine(expected_filename)
         content_engine = MockEngine(fail=TemplateError('error!'))
         context = {'fake': 'fake-context'}
         dir_path = join(template_dir, 'dir')
         mkdir(dir_path)
         dir_path += sep
         file_path = join(dir_path, 'file')
         open(file_path, 'w').write('data')
         resource = resources.DirResource(path=dir_path,
                                          engine=content_engine,
                                          filename_engine=filename_engine)
         # Render.
         generate_content = lambda: [(filename, ''.join(content)) for (
             filename, content) in resource.render(context)]
         self.assertRaises(TemplateError, generate_content)
コード例 #14
0
ファイル: resources.py プロジェクト: diecutter/piecutter
 def test_read_empty(self):
     """DirResource.read() empty dir returns empty string."""
     with temporary_directory() as path:
         resource = resources.DirResource(path=path)
         self.assertEqual(resource.read(), u'')
コード例 #15
0
ファイル: resources.py プロジェクト: diecutter/piecutter
 def test_exists_false(self):
     """DirResource.exists is False if dir doesn't exist at path."""
     path = join('i', 'do', 'not', 'exist')
     self.assertFalse(exists(path))  # Just in case.
     resource = resources.DirResource(path=path)
     self.assertTrue(resource.exists is False)
コード例 #16
0
ファイル: resources.py プロジェクト: diecutter/piecutter
 def test_content_type(self):
     """DirResource.content_type is 'application/zip'."""
     resource = resources.DirResource()
     self.assertEqual(resource.content_type, 'text/directory')