@@ -26,7 +26,14 @@ def measure_performance(title: str) -> Iterator[None]:
2626 print (f"{ padded_name } { padded_time } s" , flush = True ) # noqa: T201
2727
2828
29- def mutate_and_print (path_to_input_file : str , path_to_root : str ) -> bool :
29+ def mutate_and_print (
30+ * ,
31+ path_to_input_file : str ,
32+ path_to_root : str ,
33+ path_to_failed_mutants_dir : str ,
34+ strict_mode : bool = False ,
35+ strict_mode_2 : bool = False ,
36+ ) -> bool :
3037 assert os .path .isfile (path_to_input_file ), path_to_input_file
3138 assert os .path .isdir (path_to_root ), path_to_root
3239 if not os .path .abspath (path_to_root ):
@@ -72,25 +79,31 @@ def mutate_and_print(path_to_input_file: str, path_to_root: str) -> bool:
7279 "-m" ,
7380 "html2pdf4doc.main" ,
7481 "print" ,
75- "--strict" ,
7682 ]
83+ if strict_mode :
84+ cmd .append ("--strict" )
85+ if strict_mode_2 :
86+ cmd .append ("--strict2" )
7787
7888 for path_to_print_ in paths_to_print :
7989 cmd .append (path_to_print_ [0 ])
8090 cmd .append (path_to_print_ [1 ])
8191
8292 relative_path_to_mut_html = Path (path_to_mut_html ).relative_to (path_to_root )
83- path_to_mut_output = f"output/{ relative_path_to_mut_html } "
93+ path_to_mut_output = os .path .join (
94+ path_to_failed_mutants_dir , relative_path_to_mut_html
95+ )
8496
8597 def copy_files_if_needed () -> None :
8698 if os .path .isdir (path_to_mut_output ):
8799 return
88100
89- shutil .rmtree ("output" , ignore_errors = True )
90- Path ("output" ).mkdir (parents = True , exist_ok = True )
101+ Path (path_to_failed_mutants_dir ).mkdir (parents = True , exist_ok = True )
91102
92103 shutil .copytree (
93- "html2pdf4doc" , "output/html2pdf4doc" , dirs_exist_ok = True
104+ "html2pdf4doc" ,
105+ os .path .join (path_to_failed_mutants_dir , "html2pdf4doc" ),
106+ dirs_exist_ok = True ,
94107 )
95108
96109 shutil .rmtree (path_to_mut_output , ignore_errors = True )
@@ -110,6 +123,12 @@ def copy_mutated_file() -> None:
110123 )
111124 shutil .copy (path_to_mut_html , path_to_mut_html_out )
112125
126+ if not os .path .isfile (path_to_mut_pdf ):
127+ print ( # noqa: T201
128+ f"html2pdf4doc_fuzzer: warning: Mutated PDF is missing: { path_to_mut_pdf } "
129+ )
130+ return
131+
113132 path_to_mut_pdf_out = os .path .join (
114133 path_to_mut_output ,
115134 f"{ relative_path_to_mut_html } .{ timestamp } .pdf" ,
@@ -143,19 +162,28 @@ def copy_mutated_file() -> None:
143162
144163
145164def fuzz_test (
146- * , path_to_input_file : str , path_to_root : str , total_mutations : int = 20
165+ * ,
166+ path_to_input_file : str ,
167+ path_to_root : str ,
168+ path_to_failed_mutants_dir : str ,
169+ total_mutations : int = 20 ,
170+ strict_mode : bool = False ,
171+ strict_mode_2 : bool = False ,
147172) -> None :
148- shutil .rmtree ("output" , ignore_errors = True )
149- Path ("output" ).mkdir (parents = True , exist_ok = True )
150-
151173 success_count , failure_count = 0 , 0
152174 for i in range (1 , total_mutations + 1 ):
153175 print ( # noqa: T201
154176 f"html2pdf4doc_fuzzer print cycle #{ i } /{ total_mutations } — "
155177 f"So far: 🟢{ success_count } / 🔴{ failure_count } " ,
156178 flush = True ,
157179 )
158- success = mutate_and_print (path_to_input_file , path_to_root )
180+ success = mutate_and_print (
181+ path_to_input_file = path_to_input_file ,
182+ path_to_root = path_to_root ,
183+ path_to_failed_mutants_dir = path_to_failed_mutants_dir ,
184+ strict_mode = strict_mode ,
185+ strict_mode_2 = strict_mode_2 ,
186+ )
159187 if success :
160188 success_count += 1
161189 else :
@@ -182,24 +210,40 @@ def main() -> None:
182210
183211 parser .add_argument ("input_file" , type = str , help = "TODO" )
184212 parser .add_argument ("root_path" , type = str , help = "TODO" )
213+ parser .add_argument ("path_to_failed_mutants_dir" , type = str , help = "TODO" )
185214 parser .add_argument (
186215 "--total-mutations" ,
187216 type = int ,
188217 choices = range (1 , 1001 ),
189218 required = True ,
190219 help = "An integer between 1 and 1000" ,
191220 )
192-
221+ parser .add_argument (
222+ "--strict" ,
223+ action = "store_true" ,
224+ help = "Enables Strict mode (level 1)." ,
225+ )
226+ parser .add_argument (
227+ "--strict2" ,
228+ action = "store_true" ,
229+ help = "Enables Strict mode (level 2)." ,
230+ )
193231 args = parser .parse_args ()
194232
195233 path_to_input_file = args .input_file
196234 path_to_root = args .root_path
235+ path_to_failed_mutants_dir = args .failed_mutants_path
197236 total_mutations = args .total_mutations
237+ strict_mode = args .strict
238+ strict_mode_2 = args .strict2
198239
199240 fuzz_test (
200241 path_to_input_file = path_to_input_file ,
201242 path_to_root = path_to_root ,
243+ path_to_failed_mutants_dir = path_to_failed_mutants_dir ,
202244 total_mutations = total_mutations ,
245+ strict_mode = strict_mode ,
246+ strict_mode_2 = strict_mode_2 ,
203247 )
204248
205249
0 commit comments