diff --git a/src/codemodder/codemodder.py b/src/codemodder/codemodder.py index f2b50f31b..d864d1941 100644 --- a/src/codemodder/codemodder.py +++ b/src/codemodder/codemodder.py @@ -134,13 +134,7 @@ def run(original_args) -> int: log_section("startup") logger.info("codemodder: python/%s", __VERSION__) - - context = CodemodExecutionContext( - Path(argv.directory), - argv.dry_run, - argv.verbose, - codemod_registry, - ) + context = CodemodExecutionContext(argv, codemod_registry) # TODO: this should be a method of CodemodExecutionContext codemods_to_run = codemod_registry.match_codemods( diff --git a/src/codemodder/context.py b/src/codemodder/context.py index bd8d982b2..59cdf6876 100644 --- a/src/codemodder/context.py +++ b/src/codemodder/context.py @@ -1,3 +1,4 @@ +import argparse from pathlib import Path from dataclasses import dataclass import itertools @@ -30,14 +31,14 @@ class CodemodExecutionContext: # pylint: disable=too-many-instance-attributes def __init__( self, - directory: Path, - dry_run: bool, - verbose: bool, + cli_args: argparse.Namespace, registry: CodemodRegistry, ): - self.directory = directory - self.dry_run = dry_run - self.verbose = verbose + self.directory = Path(cli_args.directory) + self.dry_run = cli_args.dry_run + self.verbose = cli_args.verbose + self.path_include = cli_args.path_include + self.path_exclude = cli_args.path_exclude self.dependencies = set() self._results_by_codemod = {} self._failures_by_codemod = {} diff --git a/tests/codemods/base_codemod_test.py b/tests/codemods/base_codemod_test.py index 0fbd4edd8..4dc0b1c10 100644 --- a/tests/codemods/base_codemod_test.py +++ b/tests/codemods/base_codemod_test.py @@ -1,4 +1,5 @@ # pylint: disable=no-member,not-callable,attribute-defined-outside-init +import argparse import libcst as cst from libcst.codemod import CodemodContext from pathlib import Path @@ -19,18 +20,23 @@ class BaseCodemodTest: def setup_method(self): self.file_context = None + def _make_context(self, root): + cli_args = argparse.Namespace( + directory=root, + dry_run=True, + verbose=False, + path_include="", + path_exclude="", + ) + return CodemodExecutionContext(cli_args, registry=mock.MagicMock()) + def run_and_assert(self, tmpdir, input_code, expected): tmp_file_path = tmpdir / "code.py" self.run_and_assert_filepath(tmpdir, tmp_file_path, input_code, expected) def run_and_assert_filepath(self, root, file_path, input_code, expected): input_tree = cst.parse_module(input_code) - self.execution_context = CodemodExecutionContext( - directory=root, - dry_run=True, - verbose=False, - registry=mock.MagicMock(), - ) + self.execution_context = self._make_context(root) self.file_context = FileContext( file_path, [], @@ -69,12 +75,7 @@ def results_by_id_filepath(self, input_code, file_path): return semgrep_run(self.execution_context, results[0].yaml_files) def run_and_assert_filepath(self, root, file_path, input_code, expected): - self.execution_context = CodemodExecutionContext( - directory=root, - dry_run=True, - verbose=False, - registry=mock.MagicMock(), - ) + self.execution_context = self._make_context(root) input_tree = cst.parse_module(input_code) all_results = self.results_by_id_filepath(input_code, file_path) results = all_results[str(file_path)]