Skip to content

Commit

Permalink
Fix enable-jinja2-autoescape when kwargs can't be known (#397)
Browse files Browse the repository at this point in the history
Handle case where autoescape setting can't be known
  • Loading branch information
drdavella authored Mar 20, 2024
1 parent ad9c600 commit 86fafcd
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
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)
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)

0 comments on commit 86fafcd

Please sign in to comment.