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

Clear warnings for each file in codemod cli #1184

Merged
merged 3 commits into from
Aug 5, 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
31 changes: 15 additions & 16 deletions libcst/codemod/_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

from libcst import parse_module, PartialParserConfig
from libcst.codemod._codemod import Codemod
from libcst.codemod._context import CodemodContext
from libcst.codemod._dummy_pool import DummyPool
from libcst.codemod._runner import (
SkipFile,
Expand Down Expand Up @@ -246,30 +247,28 @@
),
)

# Somewhat gross hack to provide the filename in the transform's context.
# 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.
transformer.context = replace(
transformer.context,
filename=filename,
scratch=deepcopy(scratch),
)

# determine the module and package name for this file
try:
module_name_and_package = calculate_module_and_package(
config.repo_root or ".", filename
)
transformer.context = replace(
transformer.context,
full_module_name=module_name_and_package.name,
full_package_name=module_name_and_package.package,
)
mod_name = module_name_and_package.name
pkg_name = module_name_and_package.package

Check warning on line 256 in libcst/codemod/_cli.py

View check run for this annotation

Codecov / codecov/patch

libcst/codemod/_cli.py#L255-L256

Added lines #L255 - L256 were not covered by tests
except ValueError as ex:
print(
f"Failed to determine module name for {filename}: {ex}", file=sys.stderr
)
mod_name = None
pkg_name = None

# Apart from metadata_manager, every field of context should be reset per file
transformer.context = CodemodContext(
scratch=deepcopy(scratch),
filename=filename,
full_module_name=mod_name,
full_package_name=pkg_name,
metadata_manager=transformer.context.metadata_manager,
)

# Run the transform, bail if we failed or if we aren't formatting code
try:
Expand Down Expand Up @@ -420,7 +419,7 @@
operations still to do.
"""

if files_finished <= 0:
if files_finished <= 0 or elapsed_seconds == 0:

Check warning on line 422 in libcst/codemod/_cli.py

View check run for this annotation

Codecov / codecov/patch

libcst/codemod/_cli.py#L422

Added line #L422 was not covered by tests
# Technically infinite but calculating sounds better.
return "[calculating]"

Expand Down
30 changes: 30 additions & 0 deletions libcst/codemod/tests/test_codemod_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
import platform
import subprocess
import sys
import tempfile
from pathlib import Path
from unittest import skipIf

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


Expand Down Expand Up @@ -63,3 +65,31 @@ def test_codemod_external(self) -> None:
stderr=subprocess.STDOUT,
)
assert "Finished codemodding 1 files!" in output

def test_warning_messages_several_files(self) -> None:
code = """
def baz() -> str:
return "{}: {}".format(*baz)
"""
with tempfile.TemporaryDirectory() as tmpdir:
p = Path(tmpdir)
(p / "mod1.py").write_text(CodemodTest.make_fixture_data(code))
(p / "mod2.py").write_text(CodemodTest.make_fixture_data(code))
(p / "mod3.py").write_text(CodemodTest.make_fixture_data(code))
output = subprocess.run(
[
sys.executable,
"-m",
"libcst.tool",
"codemod",
"convert_format_to_fstring.ConvertFormatStringCommand",
str(p),
],
encoding="utf-8",
stderr=subprocess.PIPE,
)
# Each module will generate a warning, so we should get 3 warnings in total
self.assertIn(
"- 3 warnings were generated.",
output.stderr,
)
Loading