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

Clean warnings in context when transforming multiple files #665

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions libcst/codemod/_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,10 +245,13 @@ def _execute_transform( # noqa: C901
# We do this after the fork so that a context that was initialized with
# some defaults before calling parallel_exec_transform_with_prettyprint
# will be updated per-file.
# We should clean the warnings array as well, otherwise warnings will be
# passed from previous file
transformer.context = replace(
transformer.context,
filename=filename,
scratch={},
warnings=[],
)

# attempt to work out the module and package name for this file
Expand Down
34 changes: 33 additions & 1 deletion libcst/codemod/tests/test_codemod_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@

import subprocess
import sys
import tempfile
from pathlib import Path

from libcst._parser.entrypoints import is_native
from libcst.testing.utils import UnitTest

from libcst.codemod import CodemodTest

class TestCodemodCLI(UnitTest):
def test_codemod_formatter_error_input(self) -> None:
Expand All @@ -38,3 +39,34 @@ def test_codemod_formatter_error_input(self) -> None:
"error: cannot format -: Cannot parse: 13:10: async with AsyncExitStack() as stack:",
rlt.stderr.decode("utf-8"),
)

def test_warning_messages_several_files(self) -> None:
mod = """
def baz() -> str:
return "{}: {}".format(*baz)

def foobar() -> str:
return "{x}: {y}".format(**baz)
"""
with tempfile.TemporaryDirectory() as root:
p = Path(root)
(p / "mod1.py").write_text(CodemodTest.make_fixture_data(mod))
(p / "mod2.py").write_text(CodemodTest.make_fixture_data(mod))
(p / "mod3.py").write_text(CodemodTest.make_fixture_data(mod))
rlt = subprocess.run(
[
"python",
"-m",
"libcst.tool",
"codemod",
"convert_format_to_fstring.ConvertFormatStringCommand",
p,
Copy link
Contributor

Choose a reason for hiding this comment

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

Looks like we're getting test failures on Python 3.7 + Windows.

I think the problem may be here - the other test in this module calls str on the Path before passing it to subprocess.run.

],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
# Each module will generate 2 warnings, so we should get 6 warnings in total
self.assertIn(
"- 6 warnings were generated.",
Copy link
Contributor

Choose a reason for hiding this comment

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

I take it 6 because there are 2 per file - would it have been 2 + 4 + 6 = 12 prior to this fix?

rlt.stderr.decode("utf-8"),
)