Skip to content

Commit

Permalink
Do not configure logger in codemodder.run (#905)
Browse files Browse the repository at this point in the history
* Only set logger logging level, not global logging level

* Do not configure logger in `codemodder.run`

* Try using caplog context manager

* Configure the logger directly in the test
  • Loading branch information
drdavella authored Nov 5, 2024
1 parent 199d696 commit f0d95cb
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 44 deletions.
15 changes: 2 additions & 13 deletions src/codemodder/codemodder.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,7 @@
from codemodder.context import CodemodExecutionContext
from codemodder.dependency import Dependency
from codemodder.llm import MisconfiguredAIClient
from codemodder.logging import (
OutputFormat,
configure_logger,
log_list,
log_section,
logger,
)
from codemodder.logging import configure_logger, log_list, log_section, logger
from codemodder.project_analysis.file_parsers.package_store import PackageStore
from codemodder.project_analysis.python_repo_manager import PythonRepoManager
from codemodder.result import ResultSet
Expand Down Expand Up @@ -124,8 +118,6 @@ def run(
output: Path | str | None = None,
output_format: str = "codetf",
verbose: bool = False,
log_format: OutputFormat = OutputFormat.JSON,
project_name: str | None = None,
tool_result_files_map: DefaultDict[str, list[Path]] = defaultdict(list),
path_include: list[str] | None = None,
path_exclude: list[str] | None = None,
Expand All @@ -148,8 +140,6 @@ def run(

provider_registry = providers.load_providers()

configure_logger(verbose, log_format, project_name)

log_section("startup")
logger.info("codemodder: python/%s", __version__)

Expand Down Expand Up @@ -254,15 +244,14 @@ def _run_cli(original_args) -> int:
tool_result_files_map["defectdojo"].extend(argv.defectdojo_findings_json or [])

logger.info("command: %s %s", Path(sys.argv[0]).name, " ".join(original_args))
configure_logger(argv.verbose, argv.log_format, argv.project_name)

_, status = run(
argv.directory,
argv.dry_run,
argv.output,
argv.output_format,
argv.verbose,
argv.log_format,
argv.project_name,
tool_result_files_map,
argv.path_include,
argv.path_exclude,
Expand Down
2 changes: 1 addition & 1 deletion src/codemodder/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,6 @@ def configure_logger(

logging.basicConfig(
format="%(message)s",
level=log_level,
handlers=handlers,
)
logger.setLevel(log_level)
62 changes: 32 additions & 30 deletions tests/test_regex_transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,40 +6,42 @@
)
from codemodder.context import CodemodExecutionContext
from codemodder.file_context import FileContext
from codemodder.logging import OutputFormat, configure_logger
from codemodder.semgrep import SemgrepResult


def test_transformer_no_change(mocker, caplog, tmp_path_factory):
caplog.set_level(logging.DEBUG)
base_dir = tmp_path_factory.mktemp("foo")
code = base_dir / "code.py"
code.write_text("# Something that won't match")

file_context = FileContext(
base_dir,
code,
)
execution_context = CodemodExecutionContext(
directory=base_dir,
dry_run=True,
verbose=False,
registry=mocker.MagicMock(),
providers=mocker.MagicMock(),
repo_manager=mocker.MagicMock(),
path_include=[],
path_exclude=[],
)
pipeline = RegexTransformerPipeline(
pattern=r"hello", replacement="bye", change_description="testing"
)

changeset = pipeline.apply(
context=execution_context,
file_context=file_context,
results=None,
)
assert changeset is None
assert "No changes produced for" in caplog.text
configure_logger(True, OutputFormat.HUMAN)
with caplog.at_level(logging.DEBUG):
base_dir = tmp_path_factory.mktemp("foo")
code = base_dir / "code.py"
code.write_text("# Something that won't match")

file_context = FileContext(
base_dir,
code,
)
execution_context = CodemodExecutionContext(
directory=base_dir,
dry_run=True,
verbose=False,
registry=mocker.MagicMock(),
providers=mocker.MagicMock(),
repo_manager=mocker.MagicMock(),
path_include=[],
path_exclude=[],
)
pipeline = RegexTransformerPipeline(
pattern=r"hello", replacement="bye", change_description="testing"
)

changeset = pipeline.apply(
context=execution_context,
file_context=file_context,
results=None,
)
assert changeset is None
assert "No changes produced for" in caplog.text


def test_transformer(mocker, tmp_path_factory):
Expand Down

0 comments on commit f0d95cb

Please sign in to comment.