Skip to content
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 enable-jinja2-autoescape when kwargs can't be known #397

Merged
merged 1 commit into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/core_codemods/enable_jinja2_autoescape.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ class EnableJinja2Autoescape(SimpleCodemod):
- pattern: jinja2.Environment(...)
- pattern-not: jinja2.Environment(..., autoescape=True, ...)
- pattern-not: jinja2.Environment(..., autoescape=jinja2.select_autoescape(...), ...)
# Exclude cases where the arguments can't be precisely determined
- pattern-not: jinja2.Environment(**$KWARGS)
- pattern-inside: |
import jinja2
...
Expand Down
43 changes: 43 additions & 0 deletions tests/codemods/test_enable_jinja2_autoescape.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,42 @@ def test_autoescape_enabled(self, tmpdir):
def test_autoescape_callable(self, tmpdir, code):
self.run_and_assert(tmpdir, code, code)

def test_kwargs_unpacked(self, tmpdir):
input_code = (
expexted_output
) = """
import jinja2
env = jinja2.Environment(**kwargs)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would've thought a more telling test case would be
env = jinja2.Environment(**{"autoescape": True}) but I think it will trigger in all cases.

var = "hello"
"""
self.run_and_assert(tmpdir, input_code, expexted_output)

def test_kwargs_unpacked_with_autoescape(self, tmpdir):
input_code = """
import jinja2
env = jinja2.Environment(**kwargs, autoescape=False)
var = "hello"
"""
expexted_output = """
import jinja2
env = jinja2.Environment(**kwargs, autoescape=True)
var = "hello"
"""
self.run_and_assert(tmpdir, input_code, expexted_output)

def test_kwargs_unpacked_with_autoescape_before(self, tmpdir):
input_code = """
import jinja2
env = jinja2.Environment(autoescape=False, **kwargs)
var = "hello"
"""
expexted_output = """
import jinja2
env = jinja2.Environment(autoescape=True, **kwargs)
var = "hello"
"""
self.run_and_assert(tmpdir, input_code, expexted_output)

def test_aiohttp_import_setup(self, tmpdir):
input_code = """
import aiohttp_jinja2
Expand Down Expand Up @@ -180,3 +216,10 @@ def test_aiohttp_autoescape_callable(self, tmpdir):
aiohttp_jinja2.setup(app, autoescape=jinja2.select_autoescape())
"""
self.run_and_assert(tmpdir, input_code, input_code)

def test_aiohttp_autoescape_kwargs(self, tmpdir):
input_code = """
import aiohttp_jinja2
aiohttp_jinja2.setup(app, **kwargs)
"""
self.run_and_assert(tmpdir, input_code, input_code)
Loading