test-backend: Refactor test-backend to be more pythonic.

This commit is contained in:
Raymond Akornor 2019-03-14 16:01:01 +00:00 committed by Tim Abbott
parent 76891f6a02
commit 193de819b8
1 changed files with 49 additions and 47 deletions

View File

@ -185,7 +185,7 @@ def block_internet():
"https://zulip.readthedocs.io/en/latest/testing/testing.html#internet-access-inside-test-suites") "https://zulip.readthedocs.io/en/latest/testing/testing.html#internet-access-inside-test-suites")
httplib2.Http.request = internet_guard_httplib2 httplib2.Http.request = internet_guard_httplib2
if __name__ == "__main__": def main() -> None:
block_internet() block_internet()
TOOLS_DIR = os.path.dirname(os.path.abspath(__file__)) TOOLS_DIR = os.path.dirname(os.path.abspath(__file__))
os.chdir(os.path.dirname(TOOLS_DIR)) os.chdir(os.path.dirname(TOOLS_DIR))
@ -272,8 +272,10 @@ if __name__ == "__main__":
options = parser.parse_args() options = parser.parse_args()
args = options.args args = options.args
parallel = options.processes
include_webhooks = options.coverage or options.include_webhooks
if options.processes < 1: if parallel < 1:
raise argparse.ArgumentError( raise argparse.ArgumentError(
"option processes: Only positive integers are allowed.") "option processes: Only positive integers are allowed.")
@ -285,18 +287,18 @@ if __name__ == "__main__":
# --nonfatal-errors, so that we don't end up removing tests from # --nonfatal-errors, so that we don't end up removing tests from
# the list that weren't run. # the list that weren't run.
if options.rerun: if options.rerun:
options.processes = 1 parallel = 1
options.fatal_errors = False options.fatal_errors = False
failed_tests = get_failed_tests() failed_tests = get_failed_tests()
if failed_tests: if failed_tests:
args = failed_tests args = failed_tests
if len(args) > 0: if len(args) > 0:
# If we passed a specific set of tests, run in serial mode. # If we passed a specific set of tests, run in serial mode.
options.processes = 1 parallel = 1
# to transform forward slashes '/' present in the argument into dots '.' # to transform forward slashes '/' present in the argument into dots '.'
for suite in args: for i, suite in enumerate(args):
args[args.index(suite)] = suite.rstrip('/').replace("/", ".") args[i] = suite.rstrip('/').replace("/", ".")
def rewrite_arguments(search_key): def rewrite_arguments(search_key):
# type: (str) -> None # type: (str) -> None
@ -311,7 +313,7 @@ if __name__ == "__main__":
if search_key not in line: if search_key not in line:
continue continue
new_suite = filepath.replace(".py", ".") + suite new_suite = filepath.replace(".py", ".") + suite
args[args.index(suite)] = new_suite args[i] = new_suite
return return
for suite in args: for suite in args:
@ -321,22 +323,22 @@ if __name__ == "__main__":
elif suite[0].isupper(): elif suite[0].isupper():
rewrite_arguments('class %s(' % (suite,)) rewrite_arguments('class %s(' % (suite,))
for suite in args: for i, suite in enumerate(args):
if suite.startswith('test'): if suite.startswith('test'):
for root, dirs, files_names in os.walk(zerver_test_dir): for root, dirs, files_names in os.walk(zerver_test_dir):
for file_name in files_names: for file_name in files_names:
if file_name == suite or file_name == suite + ".py": if file_name == suite or file_name == suite + ".py":
new_suite = os.path.join(root, file_name) new_suite = os.path.join(root, file_name)
args[args.index(suite)] = new_suite args[i] = new_suite
break break
for suite in args: for i, suite in enumerate(args):
args[args.index(suite)] = suite.replace(".py", "") args[i] = suite.replace(".py", "")
# to transform forward slashes '/' introduced by the zerver_test_dir into dots '.' # to transform forward slashes '/' introduced by the zerver_test_dir into dots '.'
# taking care of any forward slashes that might be present # taking care of any forward slashes that might be present
for suite in args: for i, suite in enumerate(args):
args[args.index(suite)] = suite.replace("/", ".") args[i] = suite.replace("/", ".")
full_suite = len(args) == 0 full_suite = len(args) == 0
@ -349,8 +351,6 @@ if __name__ == "__main__":
else: else:
suites = args suites = args
include_webhooks = options.coverage or options.include_webhooks
if full_suite and include_webhooks: if full_suite and include_webhooks:
suites.append("zerver.webhooks") suites.append("zerver.webhooks")
@ -384,7 +384,6 @@ if __name__ == "__main__":
subprocess.check_call(['tools/webpack', '--test']) subprocess.check_call(['tools/webpack', '--test'])
TestRunner = get_runner(settings) TestRunner = get_runner(settings)
parallel = options.processes
if parallel > 1: if parallel > 1:
print("-- Running tests in parallel mode with {} " print("-- Running tests in parallel mode with {} "
@ -396,7 +395,7 @@ if __name__ == "__main__":
parallel=parallel, reverse=options.reverse, parallel=parallel, reverse=options.reverse,
keepdb=True) keepdb=True)
failures, failed_tests = test_runner.run_tests(suites, full_suite=full_suite, failures, failed_tests = test_runner.run_tests(suites, full_suite=full_suite,
include_webhooks=options.include_webhooks) include_webhooks=include_webhooks)
write_failed_tests(failed_tests) write_failed_tests(failed_tests)
templates_not_rendered = test_runner.get_shallow_tested_templates() templates_not_rendered = test_runner.get_shallow_tested_templates()
@ -467,3 +466,6 @@ if __name__ == "__main__":
# We'll have printed whether tests passed or failed above # We'll have printed whether tests passed or failed above
sys.exit(bool(failures)) sys.exit(bool(failures))
if __name__ == "__main__":
main()