1
0
mirror of https://github.com/kevin1024/vcrpy.git synced 2025-12-09 01:03:24 +00:00

feat(failure-message): reformat the message with the best requests matches (#459)

The message is less verbose and the display is improved for a better readability.
This commit is contained in:
Arthur Hamon
2019-08-06 01:57:13 +02:00
committed by Josh Peak
parent e3b7116564
commit caa3a67bde
4 changed files with 94 additions and 32 deletions

View File

@@ -5,21 +5,27 @@ class CannotOverwriteExistingCassetteException(Exception):
message = self._get_message(kwargs["cassette"], kwargs["failed_request"])
super(CannotOverwriteExistingCassetteException, self).__init__(message)
def _get_message(self, cassette, failed_request):
@staticmethod
def _get_message(cassette, failed_request):
"""Get the final message related to the exception"""
# Get the similar requests in the cassette that
# have match the most with the request.
best_matches = cassette.find_requests_with_most_matches(failed_request)
# Build a comprehensible message to put in the exception.
best_matches_msg = ""
for best_match in best_matches:
request, _, failed_matchers_assertion_msgs = best_match
best_matches_msg += "Similar request found : (%r).\n" % request
for failed_matcher, assertion_msg in failed_matchers_assertion_msgs:
best_matches_msg += "Matcher failed : %s\n" "%s\n" % (
failed_matcher,
assertion_msg,
)
if best_matches:
# Build a comprehensible message to put in the exception.
best_matches_msg = "Found {} similar requests with {} different matcher(s) :\n".format(
len(best_matches), len(best_matches[0][2]))
for idx, best_match in enumerate(best_matches, start=1):
request, succeeded_matchers, failed_matchers_assertion_msgs = best_match
best_matches_msg += "\n%s - (%r).\n" \
"Matchers succeeded : %s\n" \
"Matchers failed :\n" % (idx, request, succeeded_matchers)
for failed_matcher, assertion_msg in failed_matchers_assertion_msgs:
best_matches_msg += "%s - assertion failure :\n" \
"%s\n" % (failed_matcher, assertion_msg)
else:
best_matches_msg = "No similar requests, that have not been played, found."
return (
"Can't overwrite existing cassette (%r) in "
"your current record mode (%r).\n"

View File

@@ -135,21 +135,8 @@ def get_matchers_results(r1, r2, matchers):
return matches_success, matches_fails
def get_assertion_message(assertion_details, **format_options):
def get_assertion_message(assertion_details):
"""
Get a detailed message about the failing matcher.
"""
msg = ""
if assertion_details:
separator = format_options.get("separator", "-")
title = format_options.get("title", " DETAILS ")
nb_separator = format_options.get("nb_separator", 40)
first_title_line = (
separator * ((nb_separator - len(title)) // 2)
+ title
+ separator * ((nb_separator - len(title)) // 2)
)
msg += "{}\n{}\n{}\n".format(
first_title_line, str(assertion_details), separator * nb_separator
)
return msg
return assertion_details