From 7ec3538a13efac48ec3952ee2f9ac29022fb0b40 Mon Sep 17 00:00:00 2001 From: mmatera Date: Sat, 3 Aug 2024 21:16:53 -0300 Subject: [PATCH] using more print_and_log --- mathics/docpipeline.py | 56 +++++++++++++++++++++++++----------------- 1 file changed, 33 insertions(+), 23 deletions(-) diff --git a/mathics/docpipeline.py b/mathics/docpipeline.py index 58c9b776b..eaad0c96f 100644 --- a/mathics/docpipeline.py +++ b/mathics/docpipeline.py @@ -100,10 +100,10 @@ def reset_user_definitions(self): def print_and_log(self, message): """Print and log a message in the logfile""" - if not self.parameters.quiet: - print(message) if self.logfile: print_and_log(self.logfile, message.encode("utf-8")) + elif not self.parameters.quiet: + print(message) def validate_group_setup( self, @@ -121,7 +121,7 @@ def validate_group_setup( if entity_name is not None: include_names = ", ".join(include_set) - print(f"Testing {entity_name}(s): {include_names}") + self.print_and_log(f"Testing {entity_name}(s): {include_names}") else: include_names = None @@ -152,7 +152,7 @@ class TestStatus: Status parameters of the tests """ - def __init__(self, data_path: Optional[str] = None, quiet=False): + def __init__(self, data_path: Optional[str] = None, quiet: Optional[bool] = False): self.texdatafolder = osp.dirname(data_path) if data_path is not None else None self.total = 0 self.failed = 0 @@ -225,7 +225,9 @@ def test_case( comparison_result = test.compare_result(result) if test_parameters.check_partial_elapsed_time: - print(" comparison took ", datetime.now() - time_start) + test_pipeline.print_and_log( + f" comparison took {datetime.now() - time_start} seconds" + ) if not comparison_result: print("result != wanted") fail_msg = f"Result: {result}\nWanted: {test.result}" @@ -237,7 +239,9 @@ def test_case( time_start = datetime.now() output_ok = test.compare_out(out) if test_parameters.check_partial_elapsed_time: - print(" comparing messages took ", datetime.now() - time_start) + test_pipeline.print_and_log( + f" comparing messages took {datetime.now() - time_start} seconds" + ) if not output_ok: return fail( "Output:\n%s\nWanted:\n%s" @@ -332,14 +336,16 @@ def show_test_summary( failed = test_status.failed print() if test_status.total == 0: - test_parameters.print_and_log( + test_pipeline.print_and_log( f"No {entity_name} found with a name in: {entities_searched}.", ) if "MATHICS_DEBUG_TEST_CREATE" not in os.environ: - print(f"Set environment MATHICS_DEBUG_TEST_CREATE to see {entity_name}.") + test_pipeline.print_and_log( + f"Set environment MATHICS_DEBUG_TEST_CREATE to see {entity_name}." + ) elif failed > 0: - print(SEP) - if test_parameters.data_path is None: + test_pipeline.print_and_log(SEP) + if test_pipeline.parameters.data_path is None: test_pipeline.print_and_log( f"""{failed} test{'s' if failed != 1 else ''} failed.""", ) @@ -684,7 +690,7 @@ def test_all( test_parameters = test_pipeline.parameters test_status = test_pipeline.status if not test_parameters.quiet: - print(f"Testing {version_string}") + test_pipeline.print_and_log(f"Testing {version_string}") try: test_tests( @@ -692,11 +698,11 @@ def test_all( excludes=excludes, ) except KeyboardInterrupt: - print("\nAborted.\n") + test_pipeline.print_and_log("\nAborted.\n") return if test_status.failed > 0: - print(SEP) + test_pipeline.print_and_log(SEP) show_report(test_pipeline) @@ -718,16 +724,18 @@ def save_doctest_data(doctest_pipeline: DocTestPipeline): output_data: Dict[tuple, dict] = doctest_pipeline.output_data if len(output_data) == 0: - print("output data is empty") + doctest_pipeline.print_and_log("output data is empty") return - print("saving", len(output_data), "entries") - print(output_data.keys()) + doctest_pipeline.print_and_log("saving", len(output_data), "entries") + doctest_pipeline.print_and_log(output_data.keys()) doctest_latex_data_path = doctest_pipeline.parameters.data_path - print(f"Writing internal document data to {doctest_latex_data_path}") + doctest_pipeline.print_and_log( + f"Writing internal document data to {doctest_latex_data_path}" + ) i = 0 for key in output_data: i = i + 1 - print(key, output_data[key]) + doctest_pipeline.print_and_log(key, output_data[key]) if i > 9: break with open(doctest_latex_data_path, "wb") as output_file: @@ -741,7 +749,7 @@ def write_doctest_data(doctest_pipeline: DocTestPipeline): """ test_parameters = doctest_pipeline.parameters if not test_parameters.quiet: - print(f"Extracting internal doc data for {version_string}") + doctest_pipeline.print(f"Extracting internal doc data for {version_string}") print("This may take a while...") try: @@ -756,7 +764,7 @@ def write_doctest_data(doctest_pipeline: DocTestPipeline): tests, ) except KeyboardInterrupt: - print("\nAborted.\n") + doctest_pipeline.print_and_log("\nAborted.\n") return print("done.\n") @@ -924,12 +932,14 @@ def main(): test_all(test_pipeline, excludes=excludes) if test_status.total > 0 and start_time is not None: - print("Test evaluation took ", datetime.now() - start_time) + test_pipeline.print_and_log( + f"Test evaluation took {datetime.now() - start_time} seconds" + ) - if test_pipeline.logfile: - test_pipeline.logfile.close() if args.show_statistics: show_lru_cache_statistics() + if test_pipeline.logfile: + test_pipeline.logfile.close() if test_status.failed == 0: print("\nOK")