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
|
|
|
|
|
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
|
|
|
'''
|
|
|
|
|
2017-06-07 04:24:39 +02:00
|
|
|
enforce_fully_covered = {
|
2020-01-26 15:28:53 +01:00
|
|
|
'static/shared/js/typeahead.js',
|
2019-10-04 23:08:11 +02:00
|
|
|
'static/shared/js/typing_status.js',
|
|
|
|
|
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',
|
2019-06-15 06:31:57 +02:00
|
|
|
'static/js/feature_flags.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',
|
2019-12-26 15:34:17 +01:00
|
|
|
'static/js/fold_dict.ts',
|
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',
|
2019-12-29 14:40:02 +01:00
|
|
|
'static/js/int_dict.ts',
|
2020-01-14 23:29:54 +01:00
|
|
|
'static/js/lazy_set.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',
|
2020-01-06 16:49:53 +01: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-06-14 19:08:36 +02:00
|
|
|
'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',
|
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',
|
2020-01-18 14:03:08 +01:00
|
|
|
'static/js/topic_list_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/unread.js',
|
2019-05-29 06:21:54 +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',
|
pm_list: Simplify redraws for Private Messages.
We now use vdom-ish techniques to track the
list items for the pm list. When we go to update
the list, we only re-render nodes whose data
has changed, with two exceptions:
- Obviously, the first time we do a full render.
- If the keys for the items have changed (i.e.
a new node has come in or the order has changed),
we just re-render the whole list.
If the keys are the same since the last re-render, we
only re-render individual items if their data has
changed.
Most of the new code is in these two modules:
- pm_list_dom.js
- vdom.js
We remove all of the code in pm_list.js that is
related to updating DOM with unread counts.
For presence updates, we are now *never*
re-rendering the whole list, since presence
updates only change individual line items and
don't affect the keys. Instead, we just update
any changed elements in place.
The main thing that makes this all work is the
`update` method in `vdom`, which is totally generic
and essentially does a few simple jobs:
- detect if keys are different
- just render the whole ul as needed
- for items that change, do the appropriate
jQuery to update the item in place
Note that this code seems to play nice with simplebar.
Also, this code continues to use templates to render
the individual list items.
FWIW this code isn't radically different than list_render,
but it's got some key differences:
- There are fewer bells and whistles in this code.
Some of the stuff that list_render does is overkill
for the PM list.
- This code detects data changes.
Note that the vdom scheme is agnostic about templates;
it simply requires the child nodes to provide a render
method. (This is similar to list_render, which is also
technically agnostic about rendering, but which also
does use templates in most cases.)
These fixes are somewhat related to #13605, but we
haven't gotten a solid repro on that issue, and
the scrolling issues there may be orthogonal to the
redraws. But having fewer moving parts here should
help, and we won't get the rug pulled out from under
us on every presence update.
There are two possible extensions to this that are
somewhat overlapping in nature, but can be done
one a time.
* We can do a deeper vdom approach here that
gets us away from templates, and just have
nodes write to an AST. I have this on another
branch, but it might be overkill.
* We can avoid some redraws by detecting where
keys are moving up and down. I'm not completely
sure we need it for the PM list.
If this gets merged, we may want to try similar
things for the stream list, which also does a fairly
complicated mixture of big-hammer re-renders and
surgical updates-in-place (with custom code).
BTW we have 100% line coverage for vdom.js.
2020-01-04 17:17:44 +01:00
|
|
|
'static/js/vdom.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()
|
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-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')
|
|
|
|
command = [nyc, '--extension', '.ts']
|
|
|
|
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:
|
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-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:
|
2020-01-26 15:28:53 +01:00
|
|
|
if '/static/js/' in path or '/static/shared/js' in path:
|
2017-06-23 12:04:23 +02:00
|
|
|
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
|
|
|
|
|
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")
|
2017-04-05 17:47:13 +02:00
|
|
|
print("Test(s) passed. SUCCESS!")
|
|
|
|
else:
|
|
|
|
print("FAIL - Test(s) failed")
|
|
|
|
|
|
|
|
sys.exit(ret)
|