Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
curl_examples: Refactor testing code.
Suyash Vardhan Mathur93cbe6f17516
diff --git a/zerver/openapi/test_curl_examples.py b/zerver/openapi/test_curl_examples.pyindex 9a63624bcc1..4fbcbe54db6 100644--- a/zerver/openapi/test_curl_examples.py+++ b/zerver/openapi/test_curl_examples.py@@ -60,35 +60,35 @@ def test_generated_curl_examples_for_success(client: Client) -> None:if line.startswith("{generate_code_example(curl"):curl_commands_to_test.append(line)- for line in curl_commands_to_test:- # To do an end-to-end test on the documentation examples- # that will be actually shown to users, we use the- # Markdown rendering pipeline to compute the user-facing- # example, and then run that to test it.- curl_command_html = md_engine.convert(line.strip())- unescaped_html = html.unescape(curl_command_html)- curl_command_text = unescaped_html[len("<p><code>curl\n") : -len("</code></p>")]- curl_command_text = curl_command_text.replace(- "BOT_EMAIL_ADDRESS:BOT_API_KEY", AUTHENTICATION_LINE[0]+ for line in curl_commands_to_test:+ # To do an end-to-end test on the documentation examples+ # that will be actually shown to users, we use the+ # Markdown rendering pipeline to compute the user-facing+ # example, and then run that to test it.+ curl_command_html = md_engine.convert(line.strip())+ unescaped_html = html.unescape(curl_command_html)+ curl_command_text = unescaped_html[len("<p><code>curl\n") : -len("</code></p>")]+ curl_command_text = curl_command_text.replace(+ "BOT_EMAIL_ADDRESS:BOT_API_KEY", AUTHENTICATION_LINE[0]+ )++ print("Testing {} ...".format(curl_command_text.split("\n")[0]))++ # Turn the text into an arguments list.+ generated_curl_command = [x for x in shlex.split(curl_command_text) if x != "\n"]++ response_json = None+ response = None+ try:+ # We split this across two lines so if curl fails and+ # returns non-JSON output, we'll still print it.+ response_json = subprocess.check_output(+ generated_curl_command, universal_newlines=True)-- print("Testing {} ...".format(curl_command_text.split("\n")[0]))-- # Turn the text into an arguments list.- generated_curl_command = [x for x in shlex.split(curl_command_text) if x != "\n"]-- response_json = None- response = None- try:- # We split this across two lines so if curl fails and- # returns non-JSON output, we'll still print it.- response_json = subprocess.check_output(- generated_curl_command, universal_newlines=True- )- response = json.loads(response_json)- assert response["result"] == "success"- except (AssertionError, Exception):- error_template = """+ response = json.loads(response_json)+ assert response["result"] == "success"+ except (AssertionError, Exception):+ error_template = """Error verifying the success of the API documentation curl example.File: {file_name}@@ -110,16 +110,16 @@ Common reasons for why this could occur:To learn more about the test itself, see zerver/openapi/test_curl_examples.py."""- print(- error_template.format(- file_name=file_name,- line=line,- curl_command=generated_curl_command,- response=response_json- if response is None- else json.dumps(response, indent=4),- )+ print(+ error_template.format(+ file_name=file_name,+ line=line,+ curl_command=generated_curl_command,+ response=response_json+ if response is None+ else json.dumps(response, indent=4),)- raise+ )+ raiseassert_all_helper_functions_called()