2017-04-05 17:47:13 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
from __future__ import print_function
|
|
|
|
import optparse
|
|
|
|
import os
|
|
|
|
import subprocess
|
|
|
|
import sys
|
2017-06-07 04:24:39 +02:00
|
|
|
import ujson
|
2017-06-23 11:53:25 +02:00
|
|
|
from typing import Dict
|
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__)
|
|
|
|
|
|
|
|
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
|
|
|
|
tools/test-js-with-node --coverage - to generage coverage report
|
|
|
|
'''
|
|
|
|
|
2017-06-07 04:24:39 +02:00
|
|
|
enforce_fully_covered = {
|
2017-06-24 15:15:37 +02:00
|
|
|
'static/js/activity.js',
|
2017-06-07 04:24:39 +02:00
|
|
|
'static/js/alert_words.js',
|
|
|
|
'static/js/bot_data.js',
|
2017-06-28 00:48:48 +02:00
|
|
|
'static/js/channel.js',
|
2017-06-07 04:24:39 +02:00
|
|
|
'static/js/colorspace.js',
|
|
|
|
'static/js/common.js',
|
|
|
|
'static/js/compose_state.js',
|
|
|
|
'static/js/compose_ui.js',
|
2017-06-26 22:39:04 +02:00
|
|
|
'static/js/composebox_typeahead.js',
|
2017-06-07 04:24:39 +02:00
|
|
|
'static/js/dict.js',
|
2017-06-25 22:47:11 +02:00
|
|
|
'static/js/emoji.js',
|
2017-06-07 04:24:39 +02:00
|
|
|
'static/js/filter.js',
|
2017-06-15 23:48:29 +02:00
|
|
|
'static/js/fenced_code.js',
|
2017-06-07 04:24:39 +02:00
|
|
|
'static/js/hash_util.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',
|
|
|
|
'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',
|
|
|
|
'static/js/search_suggestion.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',
|
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',
|
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',
|
2017-06-08 08:02:49 +02:00
|
|
|
'static/js/util.js',
|
2017-06-07 04:24:39 +02:00
|
|
|
}
|
|
|
|
|
2017-04-05 17:47:13 +02:00
|
|
|
parser = optparse.OptionParser(USAGE)
|
|
|
|
parser.add_option('--coverage', dest='coverage',
|
|
|
|
action="store_true",
|
|
|
|
default=False, help='Get coverage report')
|
|
|
|
parser.add_option('--force', dest='force',
|
|
|
|
action="store_true",
|
|
|
|
default=False, help='Run tests despite possible problems.')
|
|
|
|
(options, args) = parser.parse_args()
|
|
|
|
|
|
|
|
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'
|
2017-05-29 17:47:19 +02:00
|
|
|
os.environ['TZ'] = 'UTC'
|
2017-04-05 17:47:13 +02:00
|
|
|
|
|
|
|
INDEX_JS = 'frontend_tests/zjsunit/index.js'
|
|
|
|
|
|
|
|
# The index.js test runner is the real "driver" here, and we launch
|
|
|
|
# with either istanbul or node, depending on whether we want coverage
|
|
|
|
# reports. Running under istanbul is slower and creates funny
|
|
|
|
# tracebacks, so you generally want to get coverage reports only
|
|
|
|
# after making sure tests will pass.
|
|
|
|
if options.coverage:
|
|
|
|
if args:
|
|
|
|
print('BAD ARGS! Coverage reports run against all files.')
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
istanbul = os.path.join(ROOT_DIR, 'node_modules/.bin/istanbul')
|
|
|
|
command = [istanbul, 'cover', INDEX_JS, '--dir', 'var/node-coverage']
|
|
|
|
else:
|
|
|
|
# Normal testing, no coverage analysis.
|
|
|
|
# Run the index.js test runner, which runs all the other tests.
|
|
|
|
command = ['node', '--stack-trace-limit=100', INDEX_JS] + args
|
|
|
|
|
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
|
|
|
|
|
2017-06-23 11:53:25 +02:00
|
|
|
def check_line_coverage(line_coverage, line_mapping, log=True):
|
|
|
|
# type: (Dict, Dict, bool) -> bool
|
|
|
|
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:
|
|
|
|
print("ERROR: %s no longer has complete node test coverage" % (relative_path,))
|
|
|
|
print(" Lines missing coverage: %s" % (", ".join(sorted(missing_lines, key=int)),))
|
|
|
|
print()
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
2017-06-07 04:24:39 +02:00
|
|
|
NODE_COVERAGE_PATH = 'var/node-coverage/coverage.json'
|
|
|
|
|
|
|
|
if options.coverage and ret == 0:
|
|
|
|
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
|
|
|
|
print()
|
|
|
|
print("=============================================================================")
|
|
|
|
print("Checking enforced fully covered files...")
|
|
|
|
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']
|
2017-06-23 11:53:25 +02:00
|
|
|
if not check_line_coverage(line_coverage, line_mapping):
|
2017-06-07 04:24:39 +02:00
|
|
|
ret = 1
|
|
|
|
if ret:
|
|
|
|
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`.")
|
|
|
|
else:
|
2017-08-01 19:43:35 +02:00
|
|
|
print("Success: All enforced fully covered files still have 100% test coverage!")
|
2017-06-07 04:24:39 +02:00
|
|
|
print("=============================================================================")
|
|
|
|
print()
|
|
|
|
|
2017-06-23 12:04:23 +02:00
|
|
|
print("=============================================================================")
|
|
|
|
print("Checking for fully covered files that are not enforced yet...")
|
|
|
|
ok = True
|
|
|
|
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']
|
|
|
|
if check_line_coverage(line_coverage, line_mapping, log=False) \
|
|
|
|
and not (relative_path in enforce_fully_covered):
|
|
|
|
ok = False
|
|
|
|
print("ERROR: %s has complete node test coverage and is not enforced." % (relative_path,))
|
|
|
|
if ok:
|
|
|
|
print("Success: There are no fully covered files that are not enforced yet!")
|
|
|
|
else:
|
|
|
|
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`.")
|
|
|
|
ret = 1
|
|
|
|
print("=============================================================================")
|
|
|
|
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)
|