Skip to content

Commit

Permalink
using more print_and_log
Browse files Browse the repository at this point in the history
  • Loading branch information
mmatera committed Aug 4, 2024
1 parent b5eeb1c commit 7ec3538
Showing 1 changed file with 33 additions and 23 deletions.
56 changes: 33 additions & 23 deletions mathics/docpipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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}"
Expand All @@ -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"
Expand Down Expand Up @@ -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.""",
)
Expand Down Expand Up @@ -684,19 +690,19 @@ 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(
test_pipeline,
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)

Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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")
Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit 7ec3538

Please sign in to comment.