mirror of
https://github.com/zulip/zulip.git
synced 2025-11-02 21:13:36 +00:00
curl_examples: Refactor testing code.
The list curl_commands_to_test gets filled already and so, the code to test each command in the list can be moved out of the block that opens the file. The only change in this commit is reducing an indentation for the entire block. This has been done to reuse the whole block in case the file does not exist.
This commit is contained in:
committed by
Tim Abbott
parent
2359ddd402
commit
93cbe6f175
@@ -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
|
||||
)
|
||||
raise
|
||||
|
||||
assert_all_helper_functions_called()
|
||||
|
||||
Reference in New Issue
Block a user