2020-03-16 13:44:15 +01:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
import argparse
|
|
|
|
import os
|
|
|
|
import shlex
|
2020-06-11 00:54:34 +02:00
|
|
|
import subprocess
|
|
|
|
import sys
|
2020-03-16 13:44:15 +01:00
|
|
|
|
2021-03-24 12:25:30 +01:00
|
|
|
import requests
|
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
sys.path.append(os.path.join(os.path.dirname(__file__), ".."))
|
2020-11-28 19:27:44 +01:00
|
|
|
from scripts.lib.zulip_tools import ENDC, FAIL, OKGREEN
|
|
|
|
|
2020-03-16 13:44:15 +01:00
|
|
|
ZULIP_PATH = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
2021-03-24 12:25:30 +01:00
|
|
|
from zerver.lib.test_fixtures import reset_zulip_test_database
|
2020-03-16 13:44:15 +01:00
|
|
|
|
2020-05-09 22:26:43 +02:00
|
|
|
# Request the special webpack setup for frontend integration tests,
|
|
|
|
# where webpack assets are compiled up front rather than running in
|
2020-09-05 15:37:07 +02:00
|
|
|
# watch mode.
|
|
|
|
os.environ["PUPPETEER_TESTS"] = "1"
|
2020-05-09 22:26:43 +02:00
|
|
|
|
2021-03-01 06:35:46 +01:00
|
|
|
# The locale can have impact how Firefox does locale-aware sorting,
|
|
|
|
# which we do verify in some tests.
|
|
|
|
os.environ["LC_ALL"] = "en_US.UTF-8"
|
|
|
|
|
2020-03-16 13:44:15 +01:00
|
|
|
os.environ["CHROMIUM_EXECUTABLE"] = os.path.join(ZULIP_PATH, "node_modules/.bin/chromium")
|
|
|
|
os.environ.pop("http_proxy", "")
|
|
|
|
os.environ.pop("https_proxy", "")
|
|
|
|
|
|
|
|
usage = """test-js-with-puppeteer [options]
|
|
|
|
test-js-with-puppeteer # Run all test files
|
2021-04-01 14:37:19 +02:00
|
|
|
test-js-with-puppeteer navigation.ts # Run a single test file
|
|
|
|
test-js-with-puppeteer navi # Run a single test file navigation.ts
|
|
|
|
test-js-with-puppeteer login.ts compose.ts # Run a few test files
|
|
|
|
test-js-with-puppeteer login compose # Run a few test files, login.ts and compose.ts here"""
|
2020-03-16 13:44:15 +01:00
|
|
|
|
|
|
|
sys.path.insert(0, ZULIP_PATH)
|
|
|
|
|
|
|
|
# check for the venv
|
|
|
|
from tools.lib import sanity_check
|
2020-06-11 00:54:34 +02:00
|
|
|
|
2020-03-16 13:44:15 +01:00
|
|
|
sanity_check.check_venv(__file__)
|
|
|
|
|
2020-06-17 20:36:00 +02:00
|
|
|
from typing import Iterable, Tuple
|
2020-06-11 00:54:34 +02:00
|
|
|
|
|
|
|
from tools.lib.test_script import (
|
2021-03-03 05:00:15 +01:00
|
|
|
add_provision_check_override_param,
|
2020-06-11 00:54:34 +02:00
|
|
|
assert_provisioning_status_ok,
|
|
|
|
find_js_test_files,
|
|
|
|
prepare_puppeteer_run,
|
|
|
|
)
|
2020-03-16 13:44:15 +01:00
|
|
|
from tools.lib.test_server import test_server_running
|
|
|
|
|
2021-03-03 05:00:15 +01:00
|
|
|
parser = argparse.ArgumentParser(usage)
|
|
|
|
|
|
|
|
parser.add_argument("--interactive", action="store_true", help="Run tests interactively")
|
|
|
|
add_provision_check_override_param(parser)
|
2021-03-12 12:30:43 +01:00
|
|
|
parser.add_argument("--firefox", action="store_true", help="Run tests with firefox.")
|
2021-03-03 05:00:15 +01:00
|
|
|
parser.add_argument(
|
|
|
|
"tests", nargs=argparse.REMAINDER, help="Specific tests to run; by default, runs all tests"
|
|
|
|
)
|
|
|
|
|
|
|
|
options = parser.parse_args()
|
|
|
|
|
2020-03-16 13:44:15 +01:00
|
|
|
|
|
|
|
def run_tests(files: Iterable[str], external_host: str) -> None:
|
2021-02-12 08:20:45 +01:00
|
|
|
test_dir = os.path.join(ZULIP_PATH, "frontend_tests/puppeteer_tests")
|
2020-04-17 08:51:06 +02:00
|
|
|
test_files = find_js_test_files(test_dir, files)
|
2020-03-16 13:44:15 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
def run_tests(test_number: int = 0) -> Tuple[int, int]:
|
2020-03-16 13:44:15 +01:00
|
|
|
ret = 1
|
2020-06-17 20:36:00 +02:00
|
|
|
current_test_num = test_number
|
|
|
|
for test_file in test_files[test_number:]:
|
2020-03-16 13:44:15 +01:00
|
|
|
test_name = os.path.basename(test_file)
|
2021-02-20 05:52:06 +01:00
|
|
|
cmd = [
|
|
|
|
"node",
|
|
|
|
"--unhandled-rejections=strict",
|
|
|
|
os.path.join(ZULIP_PATH, "node_modules/.bin/ts-node"),
|
|
|
|
"--script-mode",
|
|
|
|
"--transpile-only",
|
|
|
|
test_file,
|
|
|
|
]
|
2021-02-12 08:19:30 +01:00
|
|
|
print(
|
|
|
|
"\n\n===================== {}\nRunning {}\n\n".format(
|
|
|
|
test_name, " ".join(map(shlex.quote, cmd))
|
|
|
|
),
|
|
|
|
flush=True,
|
|
|
|
)
|
2020-03-16 13:44:15 +01:00
|
|
|
ret = subprocess.call(cmd)
|
2021-03-24 12:25:30 +01:00
|
|
|
|
|
|
|
# Resetting test environment.
|
|
|
|
reset_zulip_test_database()
|
|
|
|
# We are calling to /flush_caches to remove all the server-side caches.
|
2021-03-25 20:47:54 +01:00
|
|
|
response = requests.post("http://zulip.zulipdev.com:9981/flush_caches")
|
2021-03-24 12:25:30 +01:00
|
|
|
assert response.status_code == 200
|
|
|
|
|
2020-03-16 13:44:15 +01:00
|
|
|
if ret != 0:
|
2020-06-17 20:36:00 +02:00
|
|
|
return ret, current_test_num
|
|
|
|
current_test_num += 1
|
|
|
|
return 0, -1
|
2020-03-16 13:44:15 +01:00
|
|
|
|
2021-03-18 00:11:54 +01:00
|
|
|
with test_server_running(options.skip_provision_check, external_host):
|
2020-03-16 13:44:15 +01:00
|
|
|
# Important: do this next call inside the `with` block, when Django
|
|
|
|
# will be pointing at the test database.
|
2021-02-12 08:20:45 +01:00
|
|
|
subprocess.check_call("tools/setup/generate-test-credentials")
|
2020-03-16 13:44:15 +01:00
|
|
|
if options.interactive:
|
|
|
|
response = input('Press Enter to run tests, "q" to quit: ')
|
|
|
|
ret = 1
|
2020-06-17 20:36:00 +02:00
|
|
|
failed_test_num = 0
|
2021-02-12 08:20:45 +01:00
|
|
|
while response != "q" and failed_test_num != -1:
|
2020-06-17 20:36:00 +02:00
|
|
|
ret, failed_test_num = run_tests(failed_test_num)
|
2020-03-16 13:44:15 +01:00
|
|
|
if ret != 0:
|
|
|
|
response = input('Tests failed. Press Enter to re-run tests, "q" to quit: ')
|
|
|
|
else:
|
|
|
|
ret = 1
|
2020-06-17 20:36:00 +02:00
|
|
|
ret = run_tests()[0]
|
2020-03-16 13:44:15 +01:00
|
|
|
if ret != 0:
|
2021-02-12 08:19:30 +01:00
|
|
|
print(
|
|
|
|
f"""
|
2020-11-28 19:27:44 +01:00
|
|
|
{FAIL}The Puppeteer frontend tests failed!{ENDC}
|
|
|
|
For help debugging, read:
|
2020-09-05 15:25:49 +02:00
|
|
|
https://zulip.readthedocs.io/en/latest/testing/testing-with-puppeteer.html
|
2021-02-12 08:19:30 +01:00
|
|
|
or report and ask for help in chat.zulip.org""",
|
|
|
|
file=sys.stderr,
|
|
|
|
)
|
2021-03-15 18:39:44 +01:00
|
|
|
if os.environ.get("GITHUB_ACTIONS"):
|
2020-03-26 21:00:05 +01:00
|
|
|
print("", file=sys.stderr)
|
2021-02-12 08:19:30 +01:00
|
|
|
print(
|
2021-03-15 18:39:44 +01:00
|
|
|
"""
|
|
|
|
See https://docs.github.com/en/rest/reference/actions#artifacts for information
|
|
|
|
on how to view the generated screenshots, which are uploaded as Artifacts.
|
|
|
|
Screenshots are extremely helpful for understanding puppeteer test failures.
|
|
|
|
""",
|
2021-02-12 08:19:30 +01:00
|
|
|
file=sys.stderr,
|
|
|
|
)
|
2020-03-26 21:00:05 +01:00
|
|
|
print("", file=sys.stderr)
|
2020-09-05 15:25:49 +02:00
|
|
|
else:
|
2021-02-12 08:19:30 +01:00
|
|
|
print(
|
|
|
|
"It's also worthy to see screenshots generated on failure stored under var/puppeteer/*.png"
|
|
|
|
)
|
2020-03-16 13:44:15 +01:00
|
|
|
sys.exit(ret)
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2020-03-16 13:44:15 +01:00
|
|
|
external_host = "zulipdev.com:9981"
|
2021-03-02 20:59:19 +01:00
|
|
|
assert_provisioning_status_ok(options.skip_provision_check)
|
2021-03-12 12:30:43 +01:00
|
|
|
prepare_puppeteer_run(is_firefox=options.firefox)
|
2020-03-16 13:44:15 +01:00
|
|
|
run_tests(options.tests, external_host)
|
2020-11-28 19:27:44 +01:00
|
|
|
print(f"{OKGREEN}All tests passed!{ENDC}")
|
2020-03-16 13:44:15 +01:00
|
|
|
sys.exit(0)
|