Skip to content

Commit

Permalink
Render path templates in copy_wihtout_render (fix #456)
Browse files Browse the repository at this point in the history
  • Loading branch information
noirbizarre authored and insspb committed May 28, 2020
1 parent d8672b1 commit 840844d
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 2 deletions.
1 change: 1 addition & 0 deletions cookiecutter/generate.py
Expand Up @@ -323,6 +323,7 @@ def generate_files(
for copy_dir in copy_dirs:
indir = os.path.normpath(os.path.join(root, copy_dir))
outdir = os.path.normpath(os.path.join(project_dir, indir))
outdir = env.from_string(outdir).render(**context)
logger.debug('Copying dir %s to %s without rendering', indir, outdir)
shutil.copytree(indir, outdir)

Expand Down
11 changes: 11 additions & 0 deletions docs/advanced/copy_without_render.rst
Expand Up @@ -15,3 +15,14 @@ To avoid rendering directories and files of a cookiecutter, the `_copy_without_r
"rendered_dir/not_rendered_file.ini"
]
}

**Note**: Only the content of the files will be copied without being rendered. The paths are subject to rendering. This allows you to write::

{
"project_slug": "sample",
"_copy_without_render": [
"{{cookiecutter.repo_name}}/templates/*.html",
]
}

In this example, `{{cookiecutter.repo_name}}` will be rendered as expected but the html file content will be copied without rendering.
@@ -0,0 +1,3 @@
# Fake Project

{{cookiecutter.render_test}}
12 changes: 10 additions & 2 deletions tests/test_generate_copy_without_render.py
Expand Up @@ -31,6 +31,7 @@ def test_generate_copy_without_render_extensions():
'*not-rendered',
'rendered/not_rendered.yml',
'*.txt',
'{{cookiecutter.repo_name}}-rendered/README.md',
],
}
},
Expand All @@ -39,7 +40,7 @@ def test_generate_copy_without_render_extensions():

dir_contents = os.listdir('test_copy_without_render')

assert '{{cookiecutter.repo_name}}-not-rendered' in dir_contents
assert 'test_copy_without_render-not-rendered' in dir_contents
assert 'test_copy_without_render-rendered' in dir_contents

with open('test_copy_without_render/README.txt') as f:
Expand All @@ -59,9 +60,16 @@ def test_generate_copy_without_render_extensions():
assert 'I have been rendered' in f.read()

with open(
'test_copy_without_render/{{cookiecutter.repo_name}}-not-rendered/README.rst'
'test_copy_without_render/'
'test_copy_without_render-not-rendered/'
'README.rst'
) as f:
assert '{{cookiecutter.render_test}}' in f.read()

with open('test_copy_without_render/rendered/not_rendered.yml') as f:
assert '{{cookiecutter.render_test}}' in f.read()

with open(
'test_copy_without_render/' 'test_copy_without_render-rendered/' 'README.md'
) as f:
assert '{{cookiecutter.render_test}}' in f.read()

0 comments on commit 840844d

Please sign in to comment.