1313from faker import Faker
1414from lxml import etree , html
1515
16- from html2pdf4doc import PATH_TO_HTML2PDF4DOC_PY
17-
1816
1917@contextlib .contextmanager
2018def measure_performance (title : str ) -> Iterator [None ]:
@@ -71,7 +69,8 @@ def mutate_and_print(path_to_input_file: str, path_to_root: str) -> bool:
7169
7270 cmd : List [str ] = [
7371 sys .executable ,
74- PATH_TO_HTML2PDF4DOC_PY ,
72+ "-m" ,
73+ "html2pdf4doc.main" ,
7574 "print" ,
7675 "--strict" ,
7776 ]
@@ -80,7 +79,7 @@ def mutate_and_print(path_to_input_file: str, path_to_root: str) -> bool:
8079 cmd .append (path_to_print_ [0 ])
8180 cmd .append (path_to_print_ [1 ])
8281
83- relative_path_to_mut_html = Path (path_to_root ).relative_to ("." )
82+ relative_path_to_mut_html = Path (path_to_mut_html ).relative_to (path_to_root )
8483 path_to_mut_output = f"output/{ relative_path_to_mut_html } "
8584
8685 def copy_files_if_needed () -> None :
@@ -143,30 +142,16 @@ def copy_mutated_file() -> None:
143142 return True
144143
145144
146- def main () -> None :
147- parser = argparse .ArgumentParser ()
148-
149- parser .add_argument ("input_file" , type = str , help = "TODO" )
150- parser .add_argument ("root_path" , type = str , help = "TODO" )
151- parser .add_argument (
152- "--long" ,
153- action = "store_true" ,
154- help = "Run the fuzzer in long mode (more iterations)." ,
155- )
156-
157- args = parser .parse_args ()
158-
159- path_to_input_file = args .input_file
160- path_to_root = args .root_path
161-
145+ def fuzz_test (
146+ * , path_to_input_file : str , path_to_root : str , total_mutations : int = 20
147+ ) -> None :
162148 shutil .rmtree ("output" , ignore_errors = True )
163149 Path ("output" ).mkdir (parents = True , exist_ok = True )
164150
165- total_runs = 200 if args .long else 20
166151 success_count , failure_count = 0 , 0
167- for i in range (1 , total_runs + 1 ):
152+ for i in range (1 , total_mutations + 1 ):
168153 print ( # noqa: T201
169- f"html2pdf4doc_fuzzer print cycle #{ i } /{ total_runs } — "
154+ f"html2pdf4doc_fuzzer print cycle #{ i } /{ total_mutations } — "
170155 f"So far: 🟢{ success_count } / 🔴{ failure_count } " ,
171156 flush = True ,
172157 )
@@ -176,18 +161,44 @@ def main() -> None:
176161 else :
177162 failure_count += 1
178163
179- assert total_runs > 0
180- success_rate_percent = (success_count / total_runs ) * 100
164+ assert total_mutations > 0
165+ success_rate_percent = (success_count / total_mutations ) * 100
181166
182167 print ( # noqa: T201
183168 f"html2pdf4doc_fuzzer: finished { '✅' if failure_count == 0 else '❌' } — "
184- f"Success rate: { success_count } /{ total_runs } ({ success_rate_percent } %)" ,
169+ f"Success rate: { success_count } /{ total_mutations } ({ success_rate_percent } %)" ,
185170 flush = True ,
186171 )
187172
188173 if failure_count > 0 :
189174 sys .exit (1 )
190175
191176
177+ def main () -> None :
178+ parser = argparse .ArgumentParser ()
179+
180+ parser .add_argument ("input_file" , type = str , help = "TODO" )
181+ parser .add_argument ("root_path" , type = str , help = "TODO" )
182+ parser .add_argument (
183+ "--total-mutations" ,
184+ type = int ,
185+ choices = range (1 , 1001 ),
186+ required = True ,
187+ help = "An integer between 1 and 1000" ,
188+ )
189+
190+ args = parser .parse_args ()
191+
192+ path_to_input_file = args .input_file
193+ path_to_root = args .root_path
194+ total_mutations = args .total_mutations
195+
196+ fuzz_test (
197+ path_to_input_file = path_to_input_file ,
198+ path_to_root = path_to_root ,
199+ total_mutations = total_mutations ,
200+ )
201+
202+
192203if __name__ == "__main__" :
193204 main ()
0 commit comments