Skip to content

Commit

Permalink
Only use colored text when printing to a terminal (#870)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhassan-aws authored Feb 28, 2022
1 parent 74f92d6 commit 32331ff
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
26 changes: 18 additions & 8 deletions scripts/cbmc_json_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,11 +375,11 @@ def construct_terse_property_message(properties):

# The Verification is successful and the program is verified
if number_tests_failed == 0:
verification_status = Fore.GREEN + "SUCCESSFUL" + Style.RESET_ALL
verification_status = colored_text(Fore.GREEN, "SUCCESSFUL")
else:
# Go through traces to extract relevant information to be displayed in the summary
# only in the case of failure
verification_status = Fore.RED + "FAILED" + Style.RESET_ALL
verification_status = colored_text(Fore.RED, "FAILED")
for failed_test in failed_tests:
try:
failure_message = failed_test["description"]
Expand Down Expand Up @@ -439,17 +439,17 @@ def construct_property_message(properties):
print("Key not present in json property", e)

if status == "SUCCESS":
message = Fore.GREEN + f"{status}" + Style.RESET_ALL
message = colored_text(Fore.GREEN, f"{status}")
elif status == "UNDETERMINED":
message = Fore.YELLOW + f"{status}" + Style.RESET_ALL
message = colored_text(Fore.YELLOW, f"{status}")
number_tests_undetermined += 1
elif status == "UNREACHABLE":
message = Fore.YELLOW + f"{status}" + Style.RESET_ALL
message = colored_text(Fore.YELLOW, f"{status}")
number_tests_unreachable += 1
else:
number_tests_failed += 1
failed_tests.append(property_instance)
message = Fore.RED + f"{status}" + Style.RESET_ALL
message = colored_text(Fore.RED, f"{status}")

""" Ex - Property 54: calloc.assertion.1
- Status: SUCCESS
Expand All @@ -471,9 +471,9 @@ def construct_property_message(properties):

# The Verification is successful and the program is verified
if number_tests_failed == 0:
verification_status = Fore.GREEN + "SUCCESSFUL" + Style.RESET_ALL
verification_status = colored_text(Fore.GREEN, "SUCCESSFUL")
else:
verification_status = Fore.RED + "FAILED" + Style.RESET_ALL
verification_status = colored_text(Fore.RED, "FAILED")
for failed_test in failed_tests:
# Go through traces to extract relevant information to be displayed in the summary
# only in the case of failure
Expand All @@ -494,6 +494,16 @@ def construct_property_message(properties):

return output_message, number_tests_failed

def colored_text(color, text):
"""
Only use colored text if running in a terminal to avoid dumping escape
characters
"""
if sys.stdout.isatty():
return color + text + Style.RESET_ALL
else:
return text


if __name__ == "__main__":
main()
3 changes: 3 additions & 0 deletions tests/expected/report/uncolor/expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
- Status: SUCCESS
- Status: FAILURE
VERIFICATION:- FAILED
4 changes: 4 additions & 0 deletions tests/expected/report/uncolor/test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
fn main() {
assert!(1 + 1 == 2);
assert!(2 + 2 == 3);
}

0 comments on commit 32331ff

Please sign in to comment.