use f-strings

pull/29353/head
Jan Tattermusch 3 years ago
parent 5302131a4e
commit f970d6a40b
  1. 54
      tools/run_tests/python_utils/bazel_report_helper.py

@ -54,13 +54,11 @@ def _append_to_kokoro_bazel_invocations(invocation_id: str) -> None:
'a') as f: 'a') as f:
f.write(invocation_id + '\n') f.write(invocation_id + '\n')
print( print(
'Added invocation ID %s to kokoro "bazel_invocation_ids" artifact' % f'Added invocation ID {invocation_id} to kokoro "bazel_invocation_ids" artifact',
invocation_id,
file=sys.stderr) file=sys.stderr)
else: else:
print( print(
'Skipped adding invocation ID %s to kokoro "bazel_invocation_ids" artifact' f'Skipped adding invocation ID {invocation_id} to kokoro "bazel_invocation_ids" artifact',
% invocation_id,
file=sys.stderr) file=sys.stderr)
pass pass
@ -68,7 +66,7 @@ def _append_to_kokoro_bazel_invocations(invocation_id: str) -> None:
def _generate_junit_report_string(report_suite_name: str, invocation_id: str, def _generate_junit_report_string(report_suite_name: str, invocation_id: str,
success: bool) -> None: success: bool) -> None:
"""Generate sponge_log.xml formatted report, that will make the bazel invocation reachable as a target in resultstore UI / sponge.""" """Generate sponge_log.xml formatted report, that will make the bazel invocation reachable as a target in resultstore UI / sponge."""
bazel_invocation_url = 'https://source.cloud.google.com/results/invocations/%s' % invocation_id bazel_invocation_url = f'https://source.cloud.google.com/results/invocations/{invocation_id}'
package_name = report_suite_name package_name = report_suite_name
# set testcase name to invocation URL. That way, the link will be displayed in some form # set testcase name to invocation URL. That way, the link will be displayed in some form
# resultstore UI and sponge even in case the bazel invocation succeeds. # resultstore UI and sponge even in case the bazel invocation succeeds.
@ -76,16 +74,15 @@ def _generate_junit_report_string(report_suite_name: str, invocation_id: str,
if success: if success:
# unfortunately, neither resultstore UI nor sponge display the "system-err" output (or any other tags) # unfortunately, neither resultstore UI nor sponge display the "system-err" output (or any other tags)
# on a passing test case. But at least we tried. # on a passing test case. But at least we tried.
test_output_tag = '<system-err>PASSED. See invocation results here: %s</system-err>' % bazel_invocation_url test_output_tag = f'<system-err>PASSED. See invocation results here: {bazel_invocation_url}</system-err>'
else: else:
# The failure output will be displayes in both resultstore UI and sponge when clicking on the failing testcase. # The failure output will be displayes in both resultstore UI and sponge when clicking on the failing testcase.
test_output_tag = '<failure message="Failure">FAILED. See bazel invocation results here: %s</failure>' % bazel_invocation_url test_output_tag = f'<failure message="Failure">FAILED. See bazel invocation results here: {bazel_invocation_url}</failure>'
lines = [ lines = [
'<testsuites>', '<testsuites>',
'<testsuite id="1" name="%s" package="%s">' % f'<testsuite id="1" name="{report_suite_name}" package="{package_name}">',
(report_suite_name, package_name), f'<testcase name="{testcase_name}">',
'<testcase name="%s">' % testcase_name,
test_output_tag, test_output_tag,
'</testcase>' '</testcase>'
'</testsuite>', '</testsuite>',
@ -124,9 +121,9 @@ def _create_bazel_wrapper(report_path: str, report_suite_name: str,
# Using an RC file solves problems with flag ordering in the wrapper. # Using an RC file solves problems with flag ordering in the wrapper.
# (e.g. some flags need to come after the build/test command) # (e.g. some flags need to come after the build/test command)
with open(bazel_rc_filename, 'w') as f: with open(bazel_rc_filename, 'w') as f:
f.write('build --invocation_id="%s"\n' % invocation_id) f.write(f'build --invocation_id="{invocation_id}"\n')
f.write('build --workspace_status_command="%s"\n' % f.write(
workspace_status_command) f'build --workspace_status_command="{workspace_status_command}"\n')
# generate "failing" and "success" report # generate "failing" and "success" report
# the "failing" is named as "sponge_log.xml", which is the name picked up by sponge/resultstore # the "failing" is named as "sponge_log.xml", which is the name picked up by sponge/resultstore
@ -150,16 +147,14 @@ def _create_bazel_wrapper(report_path: str, report_suite_name: str,
'#!/bin/bash', '#!/bin/bash',
'set -ex', 'set -ex',
'', '',
'tools/bazel --bazelrc="%s" "$@" || FAILED=true' % f'tools/bazel --bazelrc="{bazel_rc_filename}" "$@" || FAILED=true',
bazel_rc_filename,
'', '',
] ]
if upload_results: if upload_results:
upload_results_lines = [ upload_results_lines = [
'sleep %s' % _UPLOAD_RBE_RESULTS_DELAY_SECONDS, f'sleep {_UPLOAD_RBE_RESULTS_DELAY_SECONDS}',
'PYTHONHTTPSVERIFY=0 python3 ./tools/run_tests/python_utils/upload_rbe_results.py --invocation_id="%s"' f'PYTHONHTTPSVERIFY=0 python3 ./tools/run_tests/python_utils/upload_rbe_results.py --invocation_id="{invocation_id}"',
% invocation_id,
'', '',
] ]
else: else:
@ -171,8 +166,7 @@ def _create_bazel_wrapper(report_path: str, report_suite_name: str,
' exit 1', ' exit 1',
'else', 'else',
' # success: plant the pre-generated xml report that says "success"', ' # success: plant the pre-generated xml report that says "success"',
' mv -f %s %s' % f' mv -f {success_report_filename} {failing_report_filename}',
(success_report_filename, failing_report_filename),
'fi', 'fi',
] ]
@ -188,16 +182,15 @@ def _create_bazel_wrapper(report_path: str, report_suite_name: str,
intro_lines = [ intro_lines = [
'@echo on', '@echo on',
'', '',
'bazel --bazelrc="%s" %%*' % bazel_rc_filename, f'bazel --bazelrc="{bazel_rc_filename}" %*',
'set BAZEL_EXITCODE=%errorlevel%', 'set BAZEL_EXITCODE=%errorlevel%',
'', '',
] ]
if upload_results: if upload_results:
upload_results_lines = [ upload_results_lines = [
'sleep %s' % _UPLOAD_RBE_RESULTS_DELAY_SECONDS, f'sleep {_UPLOAD_RBE_RESULTS_DELAY_SECONDS}',
'python3 tools/run_tests/python_utils/upload_rbe_results.py --invocation_id="%s" || exit /b 1' f'python3 tools/run_tests/python_utils/upload_rbe_results.py --invocation_id="{invocation_id}" || exit /b 1'
% invocation_id,
'', '',
] ]
else: else:
@ -206,8 +199,7 @@ def _create_bazel_wrapper(report_path: str, report_suite_name: str,
outro_lines = [ outro_lines = [
'if %BAZEL_EXITCODE% == 0 (', 'if %BAZEL_EXITCODE% == 0 (',
' @rem success: plant the pre-generated xml report that says "success"', ' @rem success: plant the pre-generated xml report that says "success"',
' mv -f %s %s' % f' mv -f {success_report_filename} {failing_report_filename}',
(success_report_filename, failing_report_filename),
')', ')',
'exit /b %BAZEL_EXITCODE%', 'exit /b %BAZEL_EXITCODE%',
] ]
@ -218,13 +210,11 @@ def _create_bazel_wrapper(report_path: str, report_suite_name: str,
] ]
f.writelines(lines) f.writelines(lines)
print('Bazel invocation ID: %s' % invocation_id, file=sys.stderr) print(f'Bazel invocation ID: {invocation_id}', file=sys.stderr)
print('Upload test results to BigQuery after bazel runs: %s' % print(f'Upload test results to BigQuery after bazel runs: {upload_results}',
upload_results,
file=sys.stderr)
print('Generated bazel wrapper: %s' % bazel_wrapper_filename,
file=sys.stderr) file=sys.stderr)
print('Generated bazel wrapper: %s' % bazel_wrapper_bat_filename, print(f'Generated bazel wrapper: {bazel_wrapper_filename}', file=sys.stderr)
print(f'Generated bazel wrapper: {bazel_wrapper_bat_filename}',
file=sys.stderr) file=sys.stderr)

Loading…
Cancel
Save