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
|
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
|
|
|
|
|
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
|
|
|
'''
|
|
|
|
|
2017-06-07 04:24:39 +02:00
|
|
|
enforce_fully_covered = {
|
2017-10-07 20:45:13 +02:00
|
|
|
'static/js/activity.js',
|
2017-06-07 04:24:39 +02:00
|
|
|
'static/js/alert_words.js',
|
2018-07-20 18:52:41 +02:00
|
|
|
'static/js/alert_words_ui.js',
|
2017-06-07 04:24:39 +02:00
|
|
|
'static/js/bot_data.js',
|
2018-04-19 15:46:56 +02:00
|
|
|
'static/js/buddy_data.js',
|
2018-04-19 14:17:22 +02:00
|
|
|
'static/js/buddy_list.js',
|
2017-06-28 00:48:48 +02:00
|
|
|
'static/js/channel.js',
|
2018-11-28 23:12:40 +01:00
|
|
|
'static/js/color_data.js',
|
2017-06-07 04:24:39 +02:00
|
|
|
'static/js/colorspace.js',
|
|
|
|
'static/js/common.js',
|
2018-03-29 15:05:37 +02:00
|
|
|
'static/js/components.js',
|
2018-04-10 23:57:51 +02:00
|
|
|
'static/js/compose_pm_pill.js',
|
2017-06-07 04:24:39 +02:00
|
|
|
'static/js/compose_state.js',
|
|
|
|
'static/js/compose_ui.js',
|
2019-01-06 00:31:11 +01:00
|
|
|
# Temporarily removed until we finish merging emoji commits.
|
|
|
|
# 'static/js/composebox_typeahead.js',
|
2019-03-27 11:11:20 +01:00
|
|
|
'static/js/dict.ts',
|
2017-06-25 22:47:11 +02:00
|
|
|
'static/js/emoji.js',
|
2017-06-15 23:48:29 +02:00
|
|
|
'static/js/fenced_code.js',
|
2018-03-09 22:04:07 +01:00
|
|
|
'static/js/fetch_status.js',
|
|
|
|
'static/js/filter.js',
|
2018-12-04 23:29:43 +01:00
|
|
|
'static/js/hash_util.js',
|
2018-07-23 00:45:46 +02:00
|
|
|
'static/js/keydown_util.js',
|
2018-08-26 21:41:49 +02:00
|
|
|
'static/js/input_pill.js',
|
2018-08-24 14:39:22 +02:00
|
|
|
'static/js/list_cursor.js',
|
2017-06-17 00:31:33 +02:00
|
|
|
'static/js/markdown.js',
|
2017-06-20 03:20:02 +02:00
|
|
|
'static/js/message_store.js',
|
2017-06-07 04:24:39 +02:00
|
|
|
'static/js/muting.js',
|
2017-12-14 19:02:06 +01:00
|
|
|
'static/js/narrow_state.js',
|
2017-06-07 04:24:39 +02:00
|
|
|
'static/js/people.js',
|
|
|
|
'static/js/pm_conversations.js',
|
2017-06-23 15:52:16 +02:00
|
|
|
'static/js/pm_list.js',
|
2017-06-17 00:31:33 +02:00
|
|
|
'static/js/presence.js',
|
2017-06-07 04:24:39 +02:00
|
|
|
'static/js/reactions.js',
|
|
|
|
'static/js/recent_senders.js',
|
|
|
|
'static/js/rtl.js',
|
2018-05-22 21:50:24 +02:00
|
|
|
'static/js/schema.js',
|
2018-04-24 14:28:24 +02:00
|
|
|
'static/js/scroll_util.js',
|
2018-06-22 12:19:40 +02:00
|
|
|
'static/js/search.js',
|
2017-06-07 04:24:39 +02:00
|
|
|
'static/js/search_suggestion.js',
|
2018-07-29 16:11:48 +02:00
|
|
|
'static/js/search_util.js',
|
2019-01-26 19:29:06 +01:00
|
|
|
'static/js/server_events_dispatch.js',
|
2018-03-05 03:13:29 +01:00
|
|
|
# Removed because we're migrating code from uncovered other settings pages to here.
|
|
|
|
# 'static/js/settings_ui.js',
|
2018-12-14 17:52:01 +01:00
|
|
|
'static/js/settings_muting.js',
|
2018-01-17 09:13:19 +01:00
|
|
|
'static/js/settings_user_groups.js',
|
2019-02-13 10:22:16 +01:00
|
|
|
# Removed because of an intermediate state of the subscription
|
|
|
|
# notifications migration.
|
|
|
|
# 'static/js/stream_data.js',
|
2017-07-03 22:18:18 +02:00
|
|
|
'static/js/stream_events.js',
|
2017-06-07 04:24:39 +02:00
|
|
|
'static/js/stream_sort.js',
|
2019-04-02 17:07:37 +02:00
|
|
|
'static/js/stream_ui_updates.js',
|
2018-04-18 08:48:31 +02:00
|
|
|
'static/js/top_left_corner.js',
|
2017-07-24 18:22:37 +02:00
|
|
|
'static/js/topic_data.js',
|
2017-06-07 04:24:39 +02:00
|
|
|
'static/js/topic_generator.js',
|
2018-02-20 13:08:50 +01:00
|
|
|
'static/js/transmit.js',
|
2017-06-20 23:59:40 +02:00
|
|
|
'static/js/typeahead_helper.js',
|
2017-06-07 04:24:39 +02:00
|
|
|
'static/js/typing_data.js',
|
|
|
|
'static/js/typing_status.js',
|
|
|
|
'static/js/unread.js',
|
2017-06-23 12:05:32 +02:00
|
|
|
'static/js/user_events.js',
|
2018-03-15 15:50:30 +01:00
|
|
|
'static/js/user_groups.js',
|
2018-03-07 15:28:11 +01:00
|
|
|
'static/js/user_pill.js',
|
2018-04-19 17:47:41 +02:00
|
|
|
'static/js/user_search.js',
|
2018-12-19 17:11:17 +01:00
|
|
|
'static/js/user_status.js',
|
2017-06-08 08:02:49 +02:00
|
|
|
'static/js/util.js',
|
2018-07-03 08:59:28 +02:00
|
|
|
'static/js/widgetize.js',
|
2018-06-21 15:11:21 +02:00
|
|
|
'static/js/search_pill.js',
|
2019-01-08 18:06:06 +01:00
|
|
|
'static/js/billing/billing.js',
|
2019-01-30 14:08:46 +01:00
|
|
|
'static/js/billing/upgrade.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()
|
2017-04-05 17:47:13 +02:00
|
|
|
|
|
|
|
from tools.lib.test_script import get_provisioning_status
|
|
|
|
|
|
|
|
if not options.force:
|
|
|
|
ok, msg = get_provisioning_status()
|
|
|
|
if not ok:
|
|
|
|
print(msg)
|
|
|
|
print('If you really know what you are doing, use --force to run anyway.')
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
os.environ['NODE_PATH'] = 'static'
|
2018-05-25 17:51:48 +02:00
|
|
|
os.environ['TZ'] = 'UTC'
|
2017-04-05 17:47:13 +02:00
|
|
|
|
|
|
|
INDEX_JS = 'frontend_tests/zjsunit/index.js'
|
|
|
|
|
2018-03-10 20:24:48 +01:00
|
|
|
# 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):
|
2019-03-28 18:13:06 +01:00
|
|
|
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.
|
2018-03-10 20:24:48 +01:00
|
|
|
options.args[index] = arg + '.js'
|
|
|
|
|
2018-04-16 15:46:40 +02:00
|
|
|
individual_files = options.args
|
|
|
|
|
2017-04-05 17:47:13 +02:00
|
|
|
# The index.js test runner is the real "driver" here, and we launch
|
2018-04-21 01:58:30 +02:00
|
|
|
# with either nyc or node, depending on whether we want coverage
|
|
|
|
# reports. Running under nyc is slower and creates funny
|
2017-04-05 17:47:13 +02:00
|
|
|
# tracebacks, so you generally want to get coverage reports only
|
|
|
|
# after making sure tests will pass.
|
2018-04-21 01:58:30 +02:00
|
|
|
node_tests_cmd = ['node', '--stack-trace-limit=100', INDEX_JS]
|
|
|
|
node_tests_cmd += individual_files
|
2017-04-05 17:47:13 +02:00
|
|
|
if options.coverage:
|
2018-04-16 15:46:40 +02:00
|
|
|
coverage_dir = os.path.join(ROOT_DIR, 'var/node-coverage')
|
2018-04-21 01:58:30 +02:00
|
|
|
coverage_lcov_file = os.path.join(coverage_dir, 'lcov.info')
|
|
|
|
|
|
|
|
nyc = os.path.join(ROOT_DIR, 'node_modules/.bin/nyc')
|
2019-03-27 10:54:16 +01:00
|
|
|
command = [nyc, '--extension', '.ts']
|
|
|
|
command += ['--report-dir', coverage_dir]
|
2018-04-21 01:58:30 +02:00
|
|
|
command += ['--temp-directory', coverage_dir, '-r=text-summary']
|
|
|
|
command += node_tests_cmd
|
|
|
|
command += ['&&', 'nyc', 'report', '-r=lcov', '-r=json']
|
|
|
|
command += ['>', coverage_lcov_file]
|
2017-04-05 17:47:13 +02:00
|
|
|
else:
|
|
|
|
# Normal testing, no coverage analysis.
|
|
|
|
# Run the index.js test runner, which runs all the other tests.
|
2018-04-21 01:58:30 +02:00
|
|
|
command = node_tests_cmd
|
2017-04-05 17:47:13 +02:00
|
|
|
|
2017-08-15 20:50:10 +02:00
|
|
|
print('Starting node tests...')
|
|
|
|
|
2017-04-05 17:47:13 +02:00
|
|
|
# If we got this far, we can run the tests!
|
|
|
|
try:
|
|
|
|
ret = subprocess.check_call(command)
|
|
|
|
except OSError:
|
|
|
|
print('Bad command: %s' % (command,))
|
|
|
|
raise
|
2018-05-24 16:41:34 +02:00
|
|
|
except subprocess.CalledProcessError:
|
2018-05-24 17:56:11 +02:00
|
|
|
print('\n** Tests failed, PLEASE FIX! **\n')
|
2018-05-14 22:26:23 +02:00
|
|
|
sys.exit(1)
|
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:
|
2018-04-16 15:46:40 +02: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-21 01:58:30 +02:00
|
|
|
NODE_COVERAGE_PATH = 'var/node-coverage/coverage-final.json'
|
2017-06-07 04:24:39 +02: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:
|
|
|
|
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:
|
|
|
|
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):
|
|
|
|
print("ERROR: %s has no node test coverage" % (relative_path,))
|
|
|
|
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.")
|
|
|
|
print("Usually, the right fix for this is to add some tests.")
|
|
|
|
print("But also check out the include/exclude lists in `tools/test-js-with-node`.")
|
|
|
|
print("To run this check locally, use `test-js-with-node --coverage`.")
|
|
|
|
|
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:
|
|
|
|
if '/static/js/' in path:
|
|
|
|
relative_path = os.path.relpath(path, ROOT_DIR)
|
|
|
|
line_coverage = coverage_json[path]['s']
|
|
|
|
line_mapping = coverage_json[path]['statementMap']
|
2018-04-16 15:46:40 +02:00
|
|
|
if check_line_coverage(relative_path, line_coverage, line_mapping, log=False) \
|
2017-06-23 12:04:23 +02:00
|
|
|
and not (relative_path in enforce_fully_covered):
|
2018-04-16 15:46:40 +02:00
|
|
|
coverage_not_enforced = True
|
2017-06-23 12:04:23 +02:00
|
|
|
print("ERROR: %s has complete node test coverage and is not enforced." % (relative_path,))
|
2018-04-16 15:46:40 +02:00
|
|
|
|
|
|
|
if coverage_not_enforced:
|
2018-03-25 08:53:40 +02:00
|
|
|
print()
|
2017-06-23 12:04:23 +02:00
|
|
|
print("There are one or more fully covered files that are not enforced.")
|
|
|
|
print("Add the file(s) to enforce_fully_covered 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
|
|
|
|
|
|
|
|
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")
|
2017-04-05 17:47:13 +02:00
|
|
|
print("Test(s) passed. SUCCESS!")
|
|
|
|
else:
|
|
|
|
print("FAIL - Test(s) failed")
|
|
|
|
|
|
|
|
sys.exit(ret)
|