Skip to content

Commit 685ec31

Browse files
committed
Reverted default behavior to hardening
1 parent c2e7c56 commit 685ec31

File tree

9 files changed

+36
-32
lines changed

9 files changed

+36
-32
lines changed

integration_tests/test_dependency_manager.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ def test_fail_to_add(self, tmp_repo):
136136
os.chmod(tmp_repo / self.requirements_file, 0o400)
137137

138138
command = [
139-
"codemodder_hardening",
139+
"codemodder",
140140
tmp_repo,
141141
"--output",
142142
self.output_path,

integration_tests/test_multiple_codemods.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def test_two_codemods(self, codemods, tmpdir):
2525
shutil.copy(pathlib.Path(SAMPLES_DIR) / source_file_name, directory)
2626

2727
command = [
28-
"codemodder_hardening",
28+
"codemodder",
2929
directory,
3030
"--output",
3131
str(codetf_path),

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ Repository = "https://github.com/pixee/codemodder-python"
4545

4646
[project.scripts]
4747
codemodder = "codemodder.codemodder:main"
48-
codemodder_hardening = "codemodder.codemodder:harden"
48+
codemodder-remediation = "codemodder.codemodder:remediate"
4949
generate-docs = 'codemodder.scripts.generate_docs:main'
5050
get-hashes = 'codemodder.scripts.get_hashes:main'
5151

src/codemodder/codemodder.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def log_report(context, output, elapsed_ms, files_to_analyze, token_usage):
7373
def apply_codemods(
7474
context: CodemodExecutionContext,
7575
codemods_to_run: Sequence[BaseCodemod],
76-
hardening: bool,
76+
remediation: bool,
7777
) -> TokenUsage:
7878
log_section("scanning")
7979
token_usage = TokenUsage()
@@ -90,7 +90,7 @@ def apply_codemods(
9090
for codemod in codemods_to_run:
9191
# NOTE: this may be used as a progress indicator by upstream tools
9292
logger.info("running codemod %s", codemod.id)
93-
if codemod_token_usage := codemod.apply(context, hardening):
93+
if codemod_token_usage := codemod.apply(context, remediation):
9494
log_token_usage(f"Codemod {codemod.id}", codemod_token_usage)
9595
token_usage += codemod_token_usage
9696

@@ -136,7 +136,7 @@ def run(
136136
sast_only: bool = False,
137137
ai_client: bool = True,
138138
log_matched_files: bool = False,
139-
hardening: bool = False,
139+
remediation: bool = False,
140140
) -> tuple[CodeTF | None, int, TokenUsage]:
141141
start = datetime.datetime.now()
142142

@@ -208,7 +208,7 @@ def run(
208208
context.find_and_fix_paths,
209209
)
210210

211-
token_usage = apply_codemods(context, codemods_to_run, hardening)
211+
token_usage = apply_codemods(context, codemods_to_run, remediation)
212212

213213
elapsed = datetime.datetime.now() - start
214214
elapsed_ms = int(elapsed.total_seconds() * 1000)
@@ -233,7 +233,7 @@ def run(
233233
return codetf, 0, token_usage
234234

235235

236-
def _run_cli(original_args, hardening=False) -> int:
236+
def _run_cli(original_args, remediation=False) -> int:
237237
codemod_registry = registry.load_registered_codemods()
238238
argv = parse_args(original_args, codemod_registry)
239239
if not os.path.exists(argv.directory):
@@ -272,8 +272,8 @@ def _run_cli(original_args, hardening=False) -> int:
272272

273273
_, status, _ = run(
274274
argv.directory,
275-
# Force dry-run if not hardening
276-
argv.dry_run if hardening else True,
275+
# Force dry-run if remediation
276+
True if remediation else argv.dry_run,
277277
argv.output,
278278
argv.output_format,
279279
argv.verbose,
@@ -287,22 +287,22 @@ def _run_cli(original_args, hardening=False) -> int:
287287
codemod_registry=codemod_registry,
288288
sast_only=argv.sonar_issues_json or argv.sarif,
289289
log_matched_files=True,
290-
hardening=hardening,
290+
remediation=remediation,
291291
)
292292
return status
293293

294294

295-
def harden():
295+
def main():
296296
"""
297297
Hardens a project. The application will write all the fixes into the files.
298298
"""
299299
sys_argv = sys.argv[1:]
300-
sys.exit(_run_cli(sys_argv, True))
300+
sys.exit(_run_cli(sys_argv))
301301

302302

303-
def main():
303+
def remediate():
304304
"""
305305
Remediates a project. The application will suggest fix for each separate issue found. No files will be written.
306306
"""
307307
sys_argv = sys.argv[1:]
308-
sys.exit(_run_cli(sys_argv))
308+
sys.exit(_run_cli(sys_argv, True))

src/codemodder/codemods/base_codemod.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ def _apply(
189189
self,
190190
context: CodemodExecutionContext,
191191
rules: list[str],
192-
hardening: bool,
192+
remediation: bool,
193193
) -> None | TokenUsage:
194194
if self.provider and (
195195
not (provider := context.providers.get_provider(self.provider))
@@ -228,7 +228,7 @@ def _apply(
228228
return None
229229

230230
# Do each result independently and outputs the diffs
231-
if not hardening:
231+
if remediation:
232232
# gather positional arguments for the map
233233
resultset_arguments: list[ResultSet | None] = []
234234
path_arguments = []
@@ -283,7 +283,7 @@ def _apply(
283283
return None
284284

285285
def apply(
286-
self, context: CodemodExecutionContext, hardening: bool = False
286+
self, context: CodemodExecutionContext, remediation: bool = False
287287
) -> None | TokenUsage:
288288
"""
289289
Apply the codemod with the given codemod execution context
@@ -300,7 +300,7 @@ def apply(
300300
301301
:param context: The codemod execution context
302302
"""
303-
return self._apply(context, [self._internal_name], hardening)
303+
return self._apply(context, [self._internal_name], remediation)
304304

305305
def _process_file(
306306
self,
@@ -399,9 +399,9 @@ def __init__(
399399
self.requested_rules.extend(requested_rules)
400400

401401
def apply(
402-
self, context: CodemodExecutionContext, hardening: bool = False
402+
self, context: CodemodExecutionContext, remediation: bool = False
403403
) -> None | TokenUsage:
404-
return self._apply(context, self.requested_rules, hardening)
404+
return self._apply(context, self.requested_rules, remediation)
405405

406406
def get_files_to_analyze(
407407
self,

src/codemodder/codemods/test/integration_utils.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ def setup_class(cls):
144144
def _assert_command_line(self, run, output_path):
145145
assert run[
146146
"commandLine"
147-
] == f'codemodder {self.code_dir} --output {output_path} --codemod-include={self.codemod_instance.id} --path-include={self.code_filename} --path-exclude=""' + (
147+
] == f'codemodder-remediation {self.code_dir} --output {output_path} --codemod-include={self.codemod_instance.id} --path-include={self.code_filename} --path-exclude=""' + (
148148
f" --sonar-issues-json={self.sonar_issues_json}"
149149
if self.sonar_issues_json
150150
else ""
@@ -184,7 +184,7 @@ def test_codetf_output(self, codetf_schema):
184184
"""
185185

186186
command = [
187-
"codemodder",
187+
"codemodder-remediation",
188188
self.code_dir,
189189
"--output",
190190
self.output_path,
@@ -350,7 +350,7 @@ def teardown_class(cls):
350350
def _assert_command_line(self, run, output_path):
351351
assert run[
352352
"commandLine"
353-
] == f'codemodder_hardening {self.code_dir} --output {output_path} --codemod-include={self.codemod_instance.id} --path-include={self.code_filename} --path-exclude=""' + (
353+
] == f'codemodder {self.code_dir} --output {output_path} --codemod-include={self.codemod_instance.id} --path-include={self.code_filename} --path-exclude=""' + (
354354
f" --sonar-issues-json={self.sonar_issues_json}"
355355
if self.sonar_issues_json
356356
else ""
@@ -396,7 +396,7 @@ def test_file_rewritten(self, codetf_schema):
396396
output files
397397
"""
398398
command = [
399-
"codemodder_hardening",
399+
"codemodder",
400400
self.code_dir,
401401
"--output",
402402
self.output_path,

src/codemodder/codemods/test/utils.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ def run_and_assert(
7373

7474
path_exclude = [f"{tmp_file_path}:{line}" for line in lines_to_exclude or []]
7575

76-
print(expected_diff_per_change)
7776
self.execution_context = CodemodExecutionContext(
7877
directory=root,
7978
dry_run=True if expected_diff_per_change else False,
@@ -85,7 +84,10 @@ def run_and_assert(
8584
path_exclude=path_exclude,
8685
)
8786

88-
self.codemod.apply(self.execution_context)
87+
self.codemod.apply(
88+
self.execution_context,
89+
remediation=True if expected_diff_per_change else False,
90+
)
8991
changes = self.execution_context.get_changesets(self.codemod.id)
9092

9193
self.changeset = changes
@@ -219,7 +221,6 @@ def run_and_assert(
219221

220222
path_exclude = [f"{tmp_file_path}:{line}" for line in lines_to_exclude or []]
221223

222-
print(expected_diff_per_change)
223224
self.execution_context = CodemodExecutionContext(
224225
directory=root,
225226
dry_run=True if expected_diff_per_change else False,
@@ -232,7 +233,10 @@ def run_and_assert(
232233
path_exclude=path_exclude,
233234
)
234235

235-
self.codemod.apply(self.execution_context)
236+
self.codemod.apply(
237+
self.execution_context,
238+
remediation=True if expected_diff_per_change else False,
239+
)
236240
changes = self.execution_context.get_changesets(self.codemod.id)
237241

238242
if input_code == expected:

src/core_codemods/defectdojo/api.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,11 @@ def from_core_codemod(
7676
def apply(
7777
self,
7878
context: CodemodExecutionContext,
79-
hardening: bool = False,
79+
remediation: bool = False,
8080
) -> None:
8181
self._apply(
8282
context,
8383
# We know this has a tool because we created it with `from_core_codemod`
8484
cast(ToolMetadata, self._metadata.tool).rule_ids,
85-
hardening,
85+
remediation,
8686
)

tests/codemods/test_base_codemod.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ def test_core_codemod_filter_apply_by_extension(mocker, ext, call_count):
8181
Path("file2.py"),
8282
]
8383

84-
codemod.apply(context, hardening=True)
84+
codemod.apply(context)
8585

8686
assert process_file.call_count == call_count
8787

0 commit comments

Comments
 (0)