sapling/tests/check-foreignext.py
Jun Wu 3884602d28 check-code: enfore checking before using common foreign extensions
Summary:
It's a common mistake that our tests require foreign extensions (namely evolve
and remotenames) without checking them first.

This diff adds checks to catch these mistakes, adds missing checks, and unifies
our checking logic using `require-ext.sh`, which is aware of `hgext3rd` and
prints skip message.

This affects `arc lint` so hopefully our new testing code would be free of this
kind of mistakes.

Test Plan: `arc lint` would catch errors

Reviewers: #mercurial, ttung, rmcelroy

Reviewed By: rmcelroy

Subscribers: rmcelroy, mjpieters

Differential Revision: https://phabricator.intern.facebook.com/D3550977

Signature: t1:3550977:1468455857:e849dfd9e3cbc446cc6e6c662050ee88a3366e6c
2016-07-12 20:39:34 +01:00

61 lines
1.8 KiB
Python
Executable File

#!/usr/bin/env python
from __future__ import absolute_import, print_function
import re
import sys
"""
Check if a test is using foreign extensions without proper checks
"""
# foreign extensions
exts = ['directaccess', 'evolve', 'inhibit', 'remotenames']
extre = re.compile(r'(%s)' % '|'.join(exts))
checkres = [
(re.compile(r'^\s*>\s*%s\s*=\s*$' % extre.pattern),
'use "$ . $TESTDIR/require-ext.sh %(name)s" to skip the test'
' if %(name)s is not available'),
(re.compile(r'^\s*\$.*--config[ =\']*extensions.%s=' % extre.pattern),
'use "$ . $TESTDIR/require-ext.sh %(name)s" to skip the test'
' if %(name)s is not available'),
]
# $ . $TESTDIR/require-ext.sh foreignext
requirere = re.compile(r'require-ext\.sh ((?:%s|\s+)+)' % extre.pattern)
def checkfile(path):
errors = []
with open(path) as f:
required = set()
for i, line in enumerate(f):
msg = None
m = requirere.search(line)
if m:
required.update(m.group(1).split())
for regex, msg in checkres:
m = regex.search(line)
if not m:
continue
name = m.group(1)
if name in required:
continue
# line[:-1] is to remove the last "\n"
errors.append((path, i + 1, line[:-1], msg % {'name': name}))
# only one error per extension per file
required.add(name)
return errors
def checkfiles(paths):
errors = []
for path in sys.argv[1:]:
errors += checkfile(path)
return sorted(set(errors))
def printerrors(errors):
# same format with check-code.py
for fname, lineno, line, msg in errors:
print('%s:%d:\n > %s\n %s' % (fname, lineno, line, msg))
printerrors(checkfiles(sys.argv[1:]))