-
Notifications
You must be signed in to change notification settings - Fork 2.1k
fix: modifying start and end variable strings #1997
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: modifying start and end variable strings #1997
Conversation
96f9bbe
to
774e4f1
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your work on this, @sacha-c!
This looks really good to me. I had a couple questions, and I've left a request for one additional test.
tests/test_find.py
Outdated
), | ||
], | ||
) | ||
def test_find_template(repo_name, env, expected): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would you add a negative find()
test? I'd like to see demonstrated behavior when the environment variables and the directory name don't align, like "Jinja vars are {%{
and }%}
but the directory is {{ repo }}
".
I'm not advocating that {{ / }}
is a fallback, just wanting a negative test to demonstrate the expected failure case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've added a parametrized test here: template with custom jinja strings but folder with default jinja strings
where the jinja configuration is overriden with {%{
, but the template folder is uses {{
In this case cookiecutter should not be able to find the template folder because it is not accurately named, so it should fail with NonTemplatedInputDirException
@pytest.mark.parametrize('invalid_dirname', ['', '{foo}', '{{foo', 'bar}}']) | ||
def test_ensure_dir_is_templated_raises(invalid_dirname): | ||
"""Verify `ensure_dir_is_templated` raises on wrong directories names input.""" | ||
with pytest.raises(exceptions.NonTemplatedInputDirException): | ||
generate.ensure_dir_is_templated(invalid_dirname) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the expected behavior if the directory is not templated?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
True, I removed this function because I felt it was redundant with the way the folder was found in the first place; both were using the same logic & failing the same way
I had deleted this test but did not add a new test case to exemplify this scenario, this is now fixed 🙂
I've added an additional test case in test_find_template::template missing folder
The error thrown will be NonTemplatedInputDirException
Cookiecutter supports using _jinja2_env_vars to modify the jinja environment. However when modifying variable_start_string or variable_end_string it will not work because we are hard-coding the start and end strings for the main directory creation. This change leverages the _jinja2_env_vars to search for the main directory, to allow this customization of variable_start_string and variable_end_string. I also remove the check because the same logic is used to find the dir, which is therefore redundant.
prompt_and_delete was a function in utils which also made use of read_user_yes_no which was in prompt. This means that prompt could not import utils because it would create a circular import. This change moves prompt_and_delete to prompt, so that prompt may make use of util functions
774e4f1
to
e12ac18
Compare
for more information, see https://pre-commit.ci
What's the latest update on this? Would be extremely useful to have this ability |
|
@kurtmckee @ericof @jensens |
@sacha-c I don't have rights to merge in this repo. A maintainer will need to merge. |
When can we expect a release to include this enhancement? |
I plan to look first at some other PR's, but expect it within next 2-3 weeks. |
On older versions of cookiecutter (pre cookiecutter/cookiecutter#1997) the find.find_template function took one parameter but after that PR it now takes two (in an apparent breaking change).
Description
Cookiecutter supports using _jinja2_env_vars to modify the jinja environment. However when modifying
variable_start_string
orvariable_end_string
it will not work because we are hard-coding the start and end strings for the main directory creation (see here). In addition, the jinja context is not always populated with the _jinja2_env_vars, for example when getting the placeholder prompts hereTechnical details
This change does 3 things (which are in separate commits for readability):
create_env_with_context
so the jinja environment is created similarly and with context each time.prompt_and_delete
fromutils
toprompt
. It was causing circular import issues when havingprompt
importutils
, becauseutils
was importingprompt
. And in any case I would say it makes more sense for it to live inprompt
because it is a prompt as well after all.I also removed the
ensure_dir_is_templated
check because the same logic is used to find the dir, and is therefore redundant.Use-case
In react-native projects, it's extremely common to use
{{
and}}
to pass objects to components. For example:Since
{{
is the default jinja variable string prefix, jinja goes crazy with react-native templates 🤪 .A solution to this is to set
_jinja2_env_vars.variable_start_string == '{%{'
(or something else). But cookiecutter currently has issues when overriding this setting.Testing
Tested locally on a react-native project with
_jinja2_env_vars.variable_start_string == '{%{'
and_jinja2_env_vars.variable_end_string == '}%}'