-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Fix Jinja whitespace control (#69821) #69822
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Files generated by a Jinja template no longer get a newline too many at the end when the template ends with a Jinja tag. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -77,3 +77,13 @@ def test_statement_prefix(minion_opts, local_salt): | |
| template, dict(opts=minion_opts, saltenv="test", salt=local_salt) | ||
| ) | ||
| assert rendered == "onetwothree" | ||
|
|
||
| def test_no_extra_trailing_newline(minion_opts, local_salt): | ||
| template = """{% for item in ['one', 'two', 'three'] -%} | ||
| {{ item }} | ||
| {% endfor -%} | ||
| """ | ||
| rendered = render_jinja_tmpl( | ||
| template, dict(opts=minion_opts, saltenv="test", salt=local_salt) | ||
| ) | ||
| assert rendered == "one\ntwo\nthree\n" | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please test a few more scenarios here: Test Single-Line Template Without Trailing Newline def test_no_trailing_newline_input(minion_opts, local_salt):
# String explicitly does NOT end with a newline
template = "{% for item in ['one', 'two', 'three'] -%}\n{{ item }}\n{% endfor -%}"
rendered = render_jinja_tmpl(
template, dict(opts=minion_opts, saltenv="test", salt=local_salt)
)
# Expect output without a trailing newline
assert rendered == "one\ntwo\nthree"Test Explicit User Override (keep_trailing_newline = False) def test_explicit_keep_trailing_newline_false(minion_opts, local_salt):
opts = minion_opts.copy()
opts["renderer_options"] = {"jinja_env": {"keep_trailing_newline": False}}
template = "hello\n"
rendered = render_jinja_tmpl(
template, dict(opts=opts, saltenv="test", salt=local_salt)
)
# With keep_trailing_newline=False explicitly set, Jinja strips the newline
assert rendered == "hello"
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The result of So:
I will add a few more tests. |
||
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.
You might need to use
os.linesepinstead of\n. We'll see if the tests pass on Windows.