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
|
|
|
|
import subprocess
|
|
|
|
import sys
|
2017-11-02 05:28:13 +01:00
|
|
|
from typing import Dict, Any
|
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
|
|
|
|
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.
|
|
|
|
import ujson
|
|
|
|
|
2020-02-09 15:09:37 +01:00
|
|
|
from zulint.printer import GREEN, BOLDRED, ENDC
|
|
|
|
|
2020-01-26 15:49:01 +01:00
|
|
|
INDEX_JS = 'frontend_tests/zjsunit/index.js'
|
|
|
|
NODE_COVERAGE_PATH = 'var/node-coverage/coverage-final.json'
|
|
|
|
|
2017-04-05 17:47:13 +02:00
|
|
|
USAGE = '''
|
|
|
|
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
|
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 = {
|
|
|
|
'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',
|
2020-02-13 21:34:54 +01:00
|
|
|
# Removed temporarily.
|
|
|
|
'static/js/channel.js',
|
2020-02-09 14:47:22 +01:00
|
|
|
'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/debug.js',
|
|
|
|
'static/js/drafts.js',
|
|
|
|
'static/js/echo.js',
|
|
|
|
'static/js/emoji_picker.js',
|
2020-02-21 00:20:39 +01:00
|
|
|
'static/js/emojisets.js',
|
2020-02-09 14:47:22 +01:00
|
|
|
'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/integration_bot_widget.js',
|
|
|
|
'static/js/invite.js',
|
|
|
|
'static/js/lightbox_canvas.js',
|
|
|
|
'static/js/lightbox.js',
|
|
|
|
'static/js/list_render.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_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/pointer.js',
|
|
|
|
'static/js/poll_widget.js',
|
|
|
|
'static/js/popovers.js',
|
2020-02-13 20:56:37 +01:00
|
|
|
# Temporarily missing full coverage
|
|
|
|
'static/js/presence.js',
|
2020-02-09 14:47:22 +01:00
|
|
|
'static/js/ready.js',
|
|
|
|
'static/js/realm_icon.js',
|
|
|
|
'static/js/realm_logo.js',
|
|
|
|
'static/js/reload.js',
|
|
|
|
'static/js/reload_state.js',
|
|
|
|
'static/js/reminder.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/settings_account.js',
|
|
|
|
'static/js/settings_bots.js',
|
2020-02-21 14:26:11 +01:00
|
|
|
'static/js/settings_config.js',
|
2020-02-09 14:47:22 +01:00
|
|
|
'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/starred_messages.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/tab_bar.js',
|
|
|
|
'static/js/templates.js',
|
|
|
|
'static/js/tictactoe_widget.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',
|
2017-06-07 04:24:39 +02:00
|
|
|
}
|
|
|
|
|
2017-11-09 08:41:47 +01:00
|
|
|
parser = argparse.ArgumentParser(USAGE)
|
|
|
|
parser.add_argument('--coverage', dest='coverage',
|
|
|
|
action="store_true",
|
|
|
|
default=False, help='Get coverage report')
|
|
|
|
parser.add_argument('--force', dest='force',
|
|
|
|
action="store_true",
|
|
|
|
default=False, help='Run tests despite possible problems.')
|
|
|
|
parser.add_argument('args', nargs=argparse.REMAINDER)
|
|
|
|
options = parser.parse_args()
|
2020-01-26 15:57:47 +01:00
|
|
|
individual_files = options.args
|
2017-04-05 17:47:13 +02:00
|
|
|
|
2019-06-20 18:27:09 +02:00
|
|
|
from tools.lib.test_script import assert_provisioning_status_ok
|
2017-04-05 17:47:13 +02:00
|
|
|
|
2019-06-20 18:27:09 +02:00
|
|
|
assert_provisioning_status_ok(options.force)
|
2017-04-05 17:47:13 +02:00
|
|
|
|
2020-02-09 15:09:37 +01:00
|
|
|
def print_error(msg: str) -> None:
|
|
|
|
print(BOLDRED + 'ERROR:' + ENDC + ' ' + msg)
|
|
|
|
|
2020-01-26 15:57:47 +01:00
|
|
|
def run_tests_via_node_js() -> int:
|
|
|
|
os.environ['TZ'] = 'UTC'
|
|
|
|
|
|
|
|
# Add ".js" to the end of all the file arguments, so index.js
|
|
|
|
# can actually verify if the file exists or not.
|
|
|
|
for index, arg in enumerate(options.args):
|
|
|
|
if not arg.endswith('.js') and not arg.endswith('.ts'):
|
|
|
|
# If it doesn't end with ".js" or ".ts", assume it is a JS file,
|
|
|
|
# since most files are currently JS files.
|
|
|
|
options.args[index] = arg + '.js'
|
|
|
|
|
|
|
|
# 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.
|
|
|
|
node_tests_cmd = ['node', '--stack-trace-limit=100', INDEX_JS]
|
|
|
|
node_tests_cmd += individual_files
|
|
|
|
if options.coverage:
|
|
|
|
coverage_dir = os.path.join(ROOT_DIR, 'var/node-coverage')
|
|
|
|
coverage_lcov_file = os.path.join(coverage_dir, 'lcov.info')
|
|
|
|
|
|
|
|
nyc = os.path.join(ROOT_DIR, 'node_modules/.bin/nyc')
|
2020-02-10 12:32:56 +01:00
|
|
|
command = [nyc, '--extension', '.hbs', '--extension', '.ts']
|
2020-01-26 15:57:47 +01:00
|
|
|
command += ['--report-dir', coverage_dir]
|
|
|
|
command += ['--temp-directory', coverage_dir, '-r=text-summary']
|
|
|
|
command += node_tests_cmd
|
|
|
|
command += ['&&', 'nyc', 'report', '-r=lcov', '-r=json']
|
|
|
|
command += ['>', coverage_lcov_file]
|
|
|
|
else:
|
|
|
|
# Normal testing, no coverage analysis.
|
|
|
|
# Run the index.js test runner, which runs all the other tests.
|
|
|
|
command = node_tests_cmd
|
|
|
|
|
|
|
|
print('Starting node tests...')
|
|
|
|
|
|
|
|
# If we got this far, we can run the tests!
|
|
|
|
try:
|
|
|
|
ret = subprocess.check_call(command)
|
|
|
|
except OSError:
|
|
|
|
print('Bad command: %s' % (command,))
|
|
|
|
raise
|
|
|
|
except subprocess.CalledProcessError:
|
|
|
|
print('\n** Tests failed, PLEASE FIX! **\n')
|
|
|
|
sys.exit(1)
|
|
|
|
return ret
|
2017-04-05 17:47:13 +02:00
|
|
|
|
2018-04-16 15:46:40 +02:00
|
|
|
def check_line_coverage(fn, line_coverage, line_mapping, log=True):
|
|
|
|
# type: (str, Dict[Any, Any], Dict[Any, Any], bool) -> 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-02-09 15:09:37 +01:00
|
|
|
print_error("%s no longer has complete node test coverage" % (fn,))
|
2017-06-23 11:53:25 +02:00
|
|
|
print(" Lines missing coverage: %s" % (", ".join(sorted(missing_lines, key=int)),))
|
|
|
|
print()
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
2018-04-16 15:46:40 +02:00
|
|
|
def read_coverage() -> Any:
|
2017-06-07 04:24:39 +02:00
|
|
|
coverage_json = None
|
|
|
|
try:
|
|
|
|
with open(NODE_COVERAGE_PATH, 'r') as f:
|
|
|
|
coverage_json = ujson.load(f)
|
|
|
|
except IOError:
|
|
|
|
print(NODE_COVERAGE_PATH + " doesn't exist. Cannot enforce fully covered files.")
|
|
|
|
raise
|
2018-04-16 15:46:40 +02:00
|
|
|
return coverage_json
|
|
|
|
|
|
|
|
def enforce_proper_coverage(coverage_json: Any) -> bool:
|
2020-02-09 14:47:22 +01:00
|
|
|
all_js_files = set(
|
|
|
|
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')
|
|
|
|
)
|
|
|
|
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-02-09 15:09:37 +01:00
|
|
|
print_error("%s has no node test coverage" % (relative_path,))
|
2017-06-07 04:24:39 +02:00
|
|
|
continue
|
|
|
|
line_coverage = coverage_json[path]['s']
|
2017-06-18 03:33:34 +02:00
|
|
|
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:
|
2017-06-23 12:04:23 +02: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-02-09 15:09:37 +01:00
|
|
|
print_error("{} unexpectedly has 100% line coverage.".format(relative_path))
|
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
|
|
|
|
|
|
|
problems_encountered = (coverage_lost or coverage_not_enforced)
|
|
|
|
return problems_encountered
|
|
|
|
|
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
|
|
|
|
2018-03-25 08:53:40 +02:00
|
|
|
print()
|
2017-04-05 17:47:13 +02:00
|
|
|
if ret == 0:
|
2017-06-09 07:17:00 +02:00
|
|
|
if options.coverage:
|
|
|
|
print("View coverage reports at http://127.0.0.1:9991/node-coverage/index.html")
|
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)
|