py3: Switch almost all shebang lines to use `python3`.
This causes `upgrade-zulip-from-git`, as well as a no-option run of
`tools/build-release-tarball`, to produce a Zulip install running
Python 3, rather than Python 2. In particular this means that the
virtualenv we create, in which all application code runs, is Python 3.
One shebang line, on `zulip-ec2-configure-interfaces`, explicitly
keeps Python 2, and at least one external ops script, `wal-e`, also
still runs on Python 2. See discussion on the respective previous
commits that made those explicit. There may also be some other
third-party scripts we use, outside of this source tree and running
outside our virtualenv, that still run on Python 2.
2017-08-02 23:15:16 +02:00
|
|
|
#!/usr/bin/env python3
|
2017-11-09 08:41:47 +01:00
|
|
|
import argparse
|
2020-02-09 14:47:22 +01:00
|
|
|
import glob
|
2017-04-05 17:47:13 +02:00
|
|
|
import os
|
2020-04-11 12:24:10 +02:00
|
|
|
import pwd
|
2017-04-05 17:47:13 +02:00
|
|
|
import subprocess
|
|
|
|
import sys
|
2020-09-02 21:58:08 +02:00
|
|
|
from typing import Any, Dict, List
|
2017-04-05 17:47:13 +02:00
|
|
|
|
|
|
|
TOOLS_DIR = os.path.dirname(os.path.abspath(__file__))
|
|
|
|
sys.path.insert(0, os.path.dirname(TOOLS_DIR))
|
|
|
|
ROOT_DIR = os.path.dirname(TOOLS_DIR)
|
|
|
|
|
|
|
|
# check for the venv
|
|
|
|
from tools.lib import sanity_check
|
2020-06-11 00:54:34 +02:00
|
|
|
|
2017-04-05 17:47:13 +02:00
|
|
|
sanity_check.check_venv(__file__)
|
|
|
|
|
2017-10-18 07:20:41 +02:00
|
|
|
# Import this after we do the sanity_check so it doesn't crash.
|
2020-08-07 01:09:47 +02:00
|
|
|
import orjson
|
2020-06-11 00:54:34 +02:00
|
|
|
from zulint.printer import BOLDRED, CYAN, ENDC, GREEN
|
2020-02-09 15:09:37 +01:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
INDEX_JS = os.path.join(ROOT_DIR, "frontend_tests/zjsunit/index.js")
|
|
|
|
NODE_COVERAGE_PATH = os.path.join(ROOT_DIR, "var/node-coverage/coverage-final.json")
|
2020-12-15 21:16:10 +01:00
|
|
|
|
|
|
|
# Ideally, we wouldn't need this line, but it seems to be required to
|
|
|
|
# avoid problems finding node_modules when running `cd tools; ./test-js-with-node`.
|
|
|
|
os.chdir(ROOT_DIR)
|
2020-01-26 15:49:01 +01:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
USAGE = """
|
2017-04-05 17:47:13 +02:00
|
|
|
tools/test-js-with-node - to run all tests
|
|
|
|
tools/test-js-with-node util.js activity.js - to run just a couple tests
|
2019-03-09 22:01:13 +01:00
|
|
|
tools/test-js-with-node --coverage - to generate coverage report
|
2021-02-12 08:20:45 +01:00
|
|
|
"""
|
2017-04-05 17:47:13 +02:00
|
|
|
|
2020-02-09 14:47:22 +01:00
|
|
|
# We do not yet require 100% line coverage for these files:
|
|
|
|
EXEMPT_FILES = {
|
2021-02-12 08:20:45 +01:00
|
|
|
"static/js/admin.js",
|
|
|
|
"static/js/archive.js",
|
|
|
|
"static/js/attachments_ui.js",
|
|
|
|
"static/js/avatar.js",
|
|
|
|
"static/js/billing/helpers.js",
|
|
|
|
"static/js/blueslip.js",
|
|
|
|
"static/js/blueslip_stacktrace.ts",
|
|
|
|
"static/js/click_handlers.js",
|
|
|
|
"static/js/compose_actions.js",
|
|
|
|
"static/js/composebox_typeahead.js",
|
|
|
|
"static/js/compose_fade.js",
|
|
|
|
"static/js/compose.js",
|
|
|
|
"static/js/condense.js",
|
|
|
|
"static/js/confirm_dialog.js",
|
|
|
|
"static/js/copy_and_paste.js",
|
|
|
|
"static/js/csrf.js",
|
|
|
|
"static/js/css_variables.js",
|
|
|
|
"static/js/debug.js",
|
|
|
|
"static/js/desktop_integration.js",
|
|
|
|
"static/js/drafts.js",
|
|
|
|
"static/js/echo.js",
|
|
|
|
"static/js/emoji_picker.js",
|
|
|
|
"static/js/emojisets.js",
|
|
|
|
"static/js/favicon.js",
|
|
|
|
"static/js/feedback_widget.js",
|
|
|
|
"static/js/floating_recipient_bar.js",
|
|
|
|
"static/js/gear_menu.js",
|
|
|
|
"static/js/global.d.ts",
|
|
|
|
"static/js/hashchange.js",
|
|
|
|
"static/js/hbs.d.ts",
|
|
|
|
"static/js/hotkey.js",
|
|
|
|
"static/js/hotspots.js",
|
|
|
|
"static/js/info_overlay.js",
|
|
|
|
"static/js/invite.js",
|
|
|
|
"static/js/lightbox_canvas.js",
|
|
|
|
"static/js/lightbox.js",
|
|
|
|
"static/js/list_util.js",
|
|
|
|
"static/js/loading.js",
|
|
|
|
"static/js/local_message.js",
|
|
|
|
"static/js/localstorage.js",
|
|
|
|
"static/js/message_edit.js",
|
|
|
|
"static/js/message_edit_history.js",
|
|
|
|
"static/js/message_events.js",
|
|
|
|
"static/js/message_fetch.js",
|
|
|
|
"static/js/message_flags.js",
|
|
|
|
"static/js/message_list_data.js",
|
|
|
|
"static/js/message_list.js",
|
|
|
|
"static/js/message_list_view.js",
|
|
|
|
"static/js/message_live_update.js",
|
|
|
|
"static/js/message_scroll.js",
|
|
|
|
"static/js/message_util.js",
|
|
|
|
"static/js/message_viewport.js",
|
|
|
|
"static/js/muting_ui.js",
|
|
|
|
"static/js/narrow.js",
|
|
|
|
"static/js/navigate.js",
|
|
|
|
"static/js/night_mode.js",
|
|
|
|
"static/js/notifications.js",
|
|
|
|
"static/js/overlays.js",
|
|
|
|
"static/js/padded_widget.js",
|
|
|
|
"static/js/page_params.js",
|
|
|
|
"static/js/panels.js",
|
|
|
|
"static/js/pm_list_dom.js",
|
|
|
|
"static/js/poll_widget.js",
|
|
|
|
"static/js/popovers.js",
|
|
|
|
"static/js/ready.js",
|
|
|
|
"static/js/realm_icon.js",
|
|
|
|
"static/js/realm_logo.js",
|
|
|
|
"static/js/recent_topics.js",
|
|
|
|
"static/js/reload.js",
|
|
|
|
"static/js/reminder.js",
|
|
|
|
"static/js/rendered_markdown.js",
|
|
|
|
"static/js/resize.js",
|
|
|
|
"static/js/rows.js",
|
|
|
|
"static/js/scroll_bar.js",
|
|
|
|
"static/js/search_pill_widget.js",
|
|
|
|
"static/js/sent_messages.js",
|
|
|
|
"static/js/server_events.js",
|
|
|
|
"static/js/dropdown_list_widget.js",
|
|
|
|
"static/js/settings_account.js",
|
|
|
|
"static/js/settings_bots.js",
|
|
|
|
"static/js/settings_display.js",
|
|
|
|
"static/js/settings_emoji.js",
|
|
|
|
"static/js/settings_exports.js",
|
|
|
|
"static/js/settings_invites.js",
|
|
|
|
"static/js/settings.js",
|
|
|
|
"static/js/settings_linkifiers.js",
|
|
|
|
"static/js/settings_notifications.js",
|
|
|
|
"static/js/settings_org.js",
|
|
|
|
"static/js/settings_panel_menu.js",
|
|
|
|
"static/js/settings_profile_fields.js",
|
|
|
|
"static/js/settings_sections.js",
|
|
|
|
"static/js/settings_streams.js",
|
|
|
|
"static/js/settings_toggle.js",
|
|
|
|
"static/js/settings_ui.js",
|
|
|
|
"static/js/settings_users.js",
|
|
|
|
"static/js/setup.js",
|
|
|
|
"static/js/spoilers.js",
|
|
|
|
"static/js/stream_color.js",
|
|
|
|
"static/js/stream_create.js",
|
|
|
|
"static/js/stream_edit.js",
|
|
|
|
"static/js/stream_list.js",
|
|
|
|
"static/js/stream_muting.js",
|
|
|
|
"static/js/stream_popover.js",
|
|
|
|
"static/js/stream_ui_updates.js",
|
|
|
|
"static/js/submessage.js",
|
|
|
|
"static/js/subs.js",
|
|
|
|
"static/js/message_view_header.js",
|
|
|
|
"static/js/templates.js",
|
|
|
|
"static/js/timerender.js",
|
|
|
|
"static/js/todo_widget.js",
|
|
|
|
"static/js/topic_list.js",
|
|
|
|
"static/js/topic_zoom.js",
|
|
|
|
"static/js/tutorial.js",
|
|
|
|
"static/js/typing_events.js",
|
|
|
|
"static/js/typing.js",
|
|
|
|
"static/js/ui_init.js",
|
|
|
|
"static/js/ui.js",
|
|
|
|
"static/js/ui_report.js",
|
|
|
|
"static/js/ui_util.js",
|
|
|
|
"static/js/unread_ops.js",
|
|
|
|
"static/js/unread_ui.js",
|
|
|
|
"static/js/upload_widget.js",
|
|
|
|
"static/js/user_status_ui.js",
|
|
|
|
"static/js/zcommand.js",
|
|
|
|
"static/js/zform.js",
|
|
|
|
"static/js/zulip.js",
|
2021-03-01 23:33:52 +01:00
|
|
|
"static/js/zulip_test.js",
|
2017-06-07 04:24:39 +02:00
|
|
|
}
|
|
|
|
|
2021-03-03 05:00:15 +01:00
|
|
|
from tools.lib.test_script import add_provision_check_override_param, assert_provisioning_status_ok
|
|
|
|
|
2017-11-09 08:41:47 +01:00
|
|
|
parser = argparse.ArgumentParser(USAGE)
|
2021-02-12 08:20:45 +01:00
|
|
|
parser.add_argument("--coverage", action="store_true", help="Get coverage report")
|
2021-03-03 05:00:15 +01:00
|
|
|
add_provision_check_override_param(parser)
|
2021-02-12 08:20:45 +01:00
|
|
|
parser.add_argument("args", nargs=argparse.REMAINDER)
|
2017-11-09 08:41:47 +01:00
|
|
|
options = parser.parse_args()
|
2020-01-26 15:57:47 +01:00
|
|
|
individual_files = options.args
|
2017-04-05 17:47:13 +02:00
|
|
|
|
2021-03-02 20:59:19 +01:00
|
|
|
assert_provisioning_status_ok(options.skip_provision_check)
|
2017-04-05 17:47:13 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2020-04-11 12:24:10 +02:00
|
|
|
def get_dev_host() -> str:
|
|
|
|
# See similar code in dev_settings.py. We only use
|
|
|
|
# this to report where you can find coverage reports.
|
|
|
|
# We duplicate the code here to avoid depending on
|
|
|
|
# Django.
|
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
host = os.getenv("EXTERNAL_HOST")
|
2020-04-11 12:24:10 +02:00
|
|
|
if host is not None:
|
|
|
|
return host
|
|
|
|
|
|
|
|
user_id = os.getuid()
|
|
|
|
user_name = pwd.getpwuid(user_id).pw_name
|
|
|
|
if user_name == "zulipdev":
|
2020-04-24 11:54:55 +02:00
|
|
|
hostname = os.uname()[1].lower()
|
2021-02-12 08:20:45 +01:00
|
|
|
if ".zulipdev.org" not in hostname:
|
|
|
|
hostname += ".zulipdev.org"
|
|
|
|
return hostname + ":9991"
|
2020-04-11 12:24:10 +02:00
|
|
|
else:
|
|
|
|
# For local development environments, we use localhost by
|
|
|
|
# default, via the "zulipdev.com" hostname.
|
2021-02-12 08:20:45 +01:00
|
|
|
return "zulipdev.com:9991"
|
2020-04-11 12:24:10 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2020-02-09 15:09:37 +01:00
|
|
|
def print_error(msg: str) -> None:
|
2021-02-12 08:20:45 +01:00
|
|
|
print(BOLDRED + "ERROR:" + ENDC + " " + msg)
|
2020-02-09 15:09:37 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2020-09-02 21:58:08 +02:00
|
|
|
def clean_file(orig_fn: str) -> str:
|
|
|
|
fn = orig_fn
|
|
|
|
if not fn.endswith(".js"):
|
|
|
|
fn += ".js"
|
|
|
|
if "frontend_tests/" not in fn:
|
|
|
|
fn = os.path.join(ROOT_DIR, "frontend_tests", "node_tests", fn)
|
|
|
|
fn = os.path.abspath(fn)
|
|
|
|
if not os.path.exists(fn):
|
|
|
|
print(f"Cannot find {orig_fn} ({fn})")
|
|
|
|
sys.exit(1)
|
|
|
|
return fn
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2020-09-02 21:58:08 +02:00
|
|
|
def clean_files(fns: List[str]) -> List[str]:
|
|
|
|
cleaned_files = [clean_file(fn) for fn in fns]
|
|
|
|
return cleaned_files
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2020-01-26 15:57:47 +01:00
|
|
|
def run_tests_via_node_js() -> int:
|
2021-02-12 08:20:45 +01:00
|
|
|
os.environ["TZ"] = "UTC"
|
2020-01-26 15:57:47 +01:00
|
|
|
|
|
|
|
# The index.js test runner is the real "driver" here, and we launch
|
|
|
|
# with either nyc or node, depending on whether we want coverage
|
|
|
|
# reports. Running under nyc is slower and creates funny
|
|
|
|
# tracebacks, so you generally want to get coverage reports only
|
|
|
|
# after making sure tests will pass.
|
2021-02-12 08:20:45 +01:00
|
|
|
node_tests_cmd = ["node", "--stack-trace-limit=100", INDEX_JS]
|
2020-09-02 21:58:08 +02:00
|
|
|
if individual_files:
|
|
|
|
files = individual_files
|
|
|
|
else:
|
2020-12-15 21:16:10 +01:00
|
|
|
files = sorted(glob.glob(os.path.join(ROOT_DIR, "frontend_tests/node_tests/*.js")))
|
2020-09-02 21:58:08 +02:00
|
|
|
|
|
|
|
node_tests_cmd += clean_files(files)
|
2020-01-26 15:57:47 +01:00
|
|
|
if options.coverage:
|
2021-02-12 08:20:45 +01:00
|
|
|
coverage_dir = os.path.join(ROOT_DIR, "var/node-coverage")
|
2020-01-26 15:57:47 +01:00
|
|
|
|
2020-09-02 22:45:59 +02:00
|
|
|
nyc = os.path.join(ROOT_DIR, "node_modules/.bin/nyc")
|
|
|
|
command = [nyc, "--extension", ".hbs", "--extension", ".ts"]
|
|
|
|
command += ["--report-dir", coverage_dir]
|
|
|
|
command += ["--temp-directory", coverage_dir]
|
|
|
|
command += ["-r=lcov", "-r=json", "-r=text-summary"]
|
2020-01-26 15:57:47 +01:00
|
|
|
command += node_tests_cmd
|
|
|
|
else:
|
|
|
|
# Normal testing, no coverage analysis.
|
|
|
|
# Run the index.js test runner, which runs all the other tests.
|
|
|
|
command = node_tests_cmd
|
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
print("Starting node tests...")
|
2020-01-26 15:57:47 +01:00
|
|
|
|
|
|
|
# If we got this far, we can run the tests!
|
|
|
|
try:
|
|
|
|
ret = subprocess.check_call(command)
|
|
|
|
except OSError:
|
2021-02-12 08:20:45 +01:00
|
|
|
print(f"Bad command: {command}")
|
2020-01-26 15:57:47 +01:00
|
|
|
raise
|
|
|
|
except subprocess.CalledProcessError:
|
2021-02-12 08:20:45 +01:00
|
|
|
print("\n** Tests failed, PLEASE FIX! **\n")
|
2020-01-26 15:57:47 +01:00
|
|
|
sys.exit(1)
|
|
|
|
return ret
|
2017-04-05 17:47:13 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
|
|
|
def check_line_coverage(
|
|
|
|
fn: str, line_coverage: Dict[Any, Any], line_mapping: Dict[Any, Any], log: bool = True
|
|
|
|
) -> bool:
|
2017-06-23 11:53:25 +02:00
|
|
|
missing_lines = []
|
|
|
|
for line in line_coverage:
|
|
|
|
if line_coverage[line] == 0:
|
|
|
|
actual_line = line_mapping[line]
|
|
|
|
missing_lines.append(str(actual_line["start"]["line"]))
|
|
|
|
if missing_lines:
|
|
|
|
if log:
|
2020-06-10 06:41:04 +02:00
|
|
|
print_error(f"{fn} no longer has complete node test coverage")
|
|
|
|
print(" Lines missing coverage: {}".format(", ".join(sorted(missing_lines, key=int))))
|
2017-06-23 11:53:25 +02:00
|
|
|
print()
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2018-04-16 15:46:40 +02:00
|
|
|
def read_coverage() -> Any:
|
2017-06-07 04:24:39 +02:00
|
|
|
coverage_json = None
|
|
|
|
try:
|
2020-08-07 01:09:47 +02:00
|
|
|
with open(NODE_COVERAGE_PATH, "rb") as f:
|
|
|
|
coverage_json = orjson.loads(f.read())
|
2020-04-09 21:51:58 +02:00
|
|
|
except OSError:
|
2017-06-07 04:24:39 +02:00
|
|
|
print(NODE_COVERAGE_PATH + " doesn't exist. Cannot enforce fully covered files.")
|
|
|
|
raise
|
2018-04-16 15:46:40 +02:00
|
|
|
return coverage_json
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2018-04-16 15:46:40 +02:00
|
|
|
def enforce_proper_coverage(coverage_json: Any) -> bool:
|
2020-02-09 14:47:22 +01:00
|
|
|
all_js_files = set(
|
2021-02-12 08:20:45 +01:00
|
|
|
glob.glob("static/js/*.js")
|
|
|
|
+ glob.glob("static/js/*.ts")
|
|
|
|
+ glob.glob("static/shared/js/*.js")
|
|
|
|
+ glob.glob("static/shared/js/*.ts")
|
|
|
|
+ glob.glob("static/js/billing/*.js"),
|
2020-02-09 14:47:22 +01:00
|
|
|
)
|
|
|
|
enforce_fully_covered = all_js_files - EXEMPT_FILES
|
|
|
|
|
2018-04-16 15:46:40 +02:00
|
|
|
coverage_lost = False
|
2017-06-07 04:24:39 +02:00
|
|
|
for relative_path in enforce_fully_covered:
|
|
|
|
path = ROOT_DIR + "/" + relative_path
|
|
|
|
if not (path in coverage_json):
|
2020-02-09 15:11:49 +01:00
|
|
|
coverage_lost = True
|
2020-06-10 06:41:04 +02:00
|
|
|
print_error(f"{relative_path} has no node test coverage")
|
2017-06-07 04:24:39 +02:00
|
|
|
continue
|
2021-02-12 08:20:45 +01:00
|
|
|
line_coverage = coverage_json[path]["s"]
|
|
|
|
line_mapping = coverage_json[path]["statementMap"]
|
2018-04-16 15:46:40 +02:00
|
|
|
if not check_line_coverage(relative_path, line_coverage, line_mapping):
|
|
|
|
coverage_lost = True
|
|
|
|
if coverage_lost:
|
2018-03-25 08:53:40 +02:00
|
|
|
print()
|
2017-06-07 04:24:39 +02:00
|
|
|
print("It looks like your changes lost 100% test coverage in one or more files.")
|
2020-02-09 14:47:22 +01:00
|
|
|
print("Ideally, you should add some tests to restore coverage.")
|
|
|
|
print("A worse option is to update EXEMPT_FILES in `tools/test-js-with-node`.")
|
2017-06-07 04:24:39 +02:00
|
|
|
print("To run this check locally, use `test-js-with-node --coverage`.")
|
2020-02-09 14:47:22 +01:00
|
|
|
print()
|
2017-06-07 04:24:39 +02:00
|
|
|
|
2018-04-16 15:46:40 +02:00
|
|
|
coverage_not_enforced = False
|
2017-06-23 12:04:23 +02:00
|
|
|
for path in coverage_json:
|
2020-02-09 14:47:22 +01:00
|
|
|
relative_path = os.path.relpath(path, ROOT_DIR)
|
|
|
|
if relative_path in EXEMPT_FILES:
|
2021-02-12 08:20:45 +01:00
|
|
|
line_coverage = coverage_json[path]["s"]
|
|
|
|
line_mapping = coverage_json[path]["statementMap"]
|
2020-02-09 14:47:22 +01:00
|
|
|
if check_line_coverage(relative_path, line_coverage, line_mapping, log=False):
|
2018-04-16 15:46:40 +02:00
|
|
|
coverage_not_enforced = True
|
2020-06-09 00:25:09 +02:00
|
|
|
print_error(f"{relative_path} unexpectedly has 100% line coverage.")
|
2018-04-16 15:46:40 +02:00
|
|
|
|
|
|
|
if coverage_not_enforced:
|
2018-03-25 08:53:40 +02:00
|
|
|
print()
|
2020-02-09 14:47:22 +01:00
|
|
|
print("One or more fully covered files are miscategorized.")
|
|
|
|
print("Remove the file(s) from EXEMPT_FILES in `tools/test-js-with-node`.")
|
2018-04-16 15:46:40 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
problems_encountered = coverage_lost or coverage_not_enforced
|
2018-04-16 15:46:40 +02:00
|
|
|
return problems_encountered
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2020-01-26 15:57:47 +01:00
|
|
|
ret = run_tests_via_node_js()
|
|
|
|
|
2018-04-16 15:46:40 +02:00
|
|
|
if options.coverage and ret == 0:
|
|
|
|
if not individual_files:
|
|
|
|
coverage_json = read_coverage()
|
|
|
|
problems_encountered = enforce_proper_coverage(coverage_json)
|
|
|
|
if problems_encountered:
|
|
|
|
ret = 1
|
2017-06-23 12:04:23 +02:00
|
|
|
|
2020-09-03 13:20:34 +02:00
|
|
|
reports_location = f"http://{get_dev_host()}/node-coverage/index.html"
|
|
|
|
print()
|
|
|
|
print("View coverage reports at " + CYAN + reports_location + ENDC)
|
|
|
|
|
2018-03-25 08:53:40 +02:00
|
|
|
print()
|
2017-04-05 17:47:13 +02:00
|
|
|
if ret == 0:
|
2020-02-09 15:09:37 +01:00
|
|
|
print(GREEN + "Test(s) passed. SUCCESS!" + ENDC)
|
2017-04-05 17:47:13 +02:00
|
|
|
else:
|
2020-02-09 15:09:37 +01:00
|
|
|
print(BOLDRED + "FAIL - Test(s) failed" + ENDC)
|
2017-04-05 17:47:13 +02:00
|
|
|
|
|
|
|
sys.exit(ret)
|