@@ -69,6 +69,14 @@ def setup_class(cls):
6969 manage_py_path = parent_dir / "manage.py"
7070 manage_py_path .touch ()
7171
72+ if cls .original_code is not NotImplementedError :
73+ # Some tests are easier to understand with the expected new code provided
74+ # instead of calculated
75+ cls .original_code = dedent (cls .original_code ).strip ("\n " )
76+ else :
77+ with open (cls .code_path , "r" , encoding = "utf-8" ) as f : # type: ignore
78+ cls .original_code = f .read ()
79+
7280 def _assert_run_fields (self , run , output_path ):
7381 assert run ["vendor" ] == "pixee"
7482 assert run ["tool" ] == "codemodder-python"
@@ -152,14 +160,15 @@ def _assert_codetf_output(self, codetf_schema):
152160 # CodeTf2 spec requires relative paths
153161 self ._assert_results_fields (results , self .code_filename )
154162
163+ def write_original_code (self ):
164+ with open (self .code_path , "w" , encoding = "utf-8" ) as f :
165+ f .write (self .original_code )
166+
155167 def test_codetf_output (self , codetf_schema ):
156168 """
157169 Tests correct codetf output.
158170 """
159171
160- with open (self .code_path , "r" , encoding = "utf-8" ) as f : # type: ignore
161- original_code = f .read ()
162-
163172 command = [
164173 "codemodder" ,
165174 self .code_dir ,
@@ -175,6 +184,8 @@ def test_codetf_output(self, codetf_schema):
175184 if self .sonar_hotspots_json :
176185 command .append (f"--sonar-hotspots-json={ self .sonar_hotspots_json } " )
177186
187+ self .write_original_code ()
188+
178189 completed_process = subprocess .run (
179190 command ,
180191 check = False ,
@@ -189,7 +200,7 @@ def test_codetf_output(self, codetf_schema):
189200 # check that the original file is not rewritten
190201 with open (self .code_path , "r" , encoding = "utf-8" ) as f :
191202 original_file_code = f .read ()
192- assert original_file_code == original_code
203+ assert original_file_code == self . original_code
193204
194205 def apply_hunk_to_lines (self , lines , hunk ):
195206 # The hunk target line numbers are 1-indexed.
@@ -266,7 +277,7 @@ def apply_diff(self, diff_str, original_str):
266277 return "" .join (patched_lines )
267278
268279 def _get_patched_code_for_each_change (self ) -> list [str ]:
269- with open (self .output_path , "r" , encoding = "utf-8" ) as f :
280+ with open (self .output_path , "r" , encoding = "utf-8" ) as f : # type: ignore
270281 codetf = json .load (f )
271282 changes = codetf ["results" ][0 ]["changeset" ]
272283 patched_codes = []
@@ -349,7 +360,7 @@ def _assert_run_fields(self, run, output_path):
349360 assert run ["elapsed" ] != ""
350361 assert run [
351362 "commandLine"
352- ] == f'codemodder { self .code_dir } --output { output_path } --codemod-include={ self .codemod_instance .id } --path-include={ self .code_filename } --path-exclude=""' + (
363+ ] == f'codemodder_hardening { self .code_dir } --output { output_path } --codemod-include={ self .codemod_instance .id } --path-include={ self .code_filename } --path-exclude=""' + (
353364 f" --sonar-issues-json={ self .sonar_issues_json } "
354365 if self .sonar_issues_json
355366 else ""
@@ -438,7 +449,7 @@ def test_file_rewritten(self, codetf_schema):
438449 output files
439450 """
440451 command = [
441- "codemodder " ,
452+ "codemodder_hardening " ,
442453 self .code_dir ,
443454 "--output" ,
444455 self .output_path ,
@@ -510,6 +521,15 @@ def setup_class(cls):
510521 cls .code_dir = SAMPLES_DIR
511522 cls .code_filename = os .path .relpath (cls .code_path , SAMPLES_DIR )
512523
524+ if cls .original_code is not NotImplementedError :
525+ # Some tests are easier to understand with the expected new code provided
526+ # instead of calculated
527+ cls .original_code = dedent (cls .original_code ).strip ("\n " )
528+ else :
529+ with open (cls .code_path , "r" , encoding = "utf-8" ) as f : # type: ignore
530+ cls .original_code = f .read ()
531+ print (cls .original_code )
532+
513533 # TODO: support sonar integration tests that add a dependency to
514534 # `requirements_file_name`. These tests would not be able to run
515535 # in parallel at this time since they would all override the same
0 commit comments