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,58 +287,58 @@ 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
for root, dirs, files_names in os.walk(zerver_test_dir, topdown=False): for root, dirs, files_names in os.walk(zerver_test_dir, topdown=False):
for file_name in files_names:
# Check for files starting with alphanumeric characters and ending with '.py'
# Ignore backup files if any
if not file_name[0].isalnum() or not file_name.endswith(".py"):
continue
filepath = os.path.join(root, file_name)
for line in open(filepath):
if search_key not in line:
continue
new_suite = filepath.replace(".py", ".") + suite
args[args.index(suite)] = new_suite
return
for suite in args:
if suite[0].isupper() and "test_" in suite:
classname = suite.rsplit('.', 1)[0]
rewrite_arguments(classname)
elif suite[0].isupper():
rewrite_arguments('class %s(' % (suite,))
for suite in args:
if suite.startswith('test'):
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": # Check for files starting with alphanumeric characters and ending with '.py'
new_suite = os.path.join(root, file_name) # Ignore backup files if any
args[args.index(suite)] = new_suite if not file_name[0].isalnum() or not file_name.endswith(".py"):
break continue
filepath = os.path.join(root, file_name)
for line in open(filepath):
if search_key not in line:
continue
new_suite = filepath.replace(".py", ".") + suite
args[i] = new_suite
return
for suite in args: for suite in args:
args[args.index(suite)] = suite.replace(".py", "") if suite[0].isupper() and "test_" in suite:
classname = suite.rsplit('.', 1)[0]
rewrite_arguments(classname)
elif suite[0].isupper():
rewrite_arguments('class %s(' % (suite,))
# to transform forward slashes '/' introduced by the zerver_test_dir into dots '.' for i, suite in enumerate(args):
# taking care of any forward slashes that might be present if suite.startswith('test'):
for suite in args: for root, dirs, files_names in os.walk(zerver_test_dir):
args[args.index(suite)] = suite.replace("/", ".") for file_name in files_names:
if file_name == suite or file_name == suite + ".py":
new_suite = os.path.join(root, file_name)
args[i] = new_suite
break
for i, suite in enumerate(args):
args[i] = suite.replace(".py", "")
# to transform forward slashes '/' introduced by the zerver_test_dir into dots '.'
# taking care of any forward slashes that might be present
for i, suite in enumerate(args):
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()