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
This commit is contained in:
Jun Wu 2016-07-12 20:39:34 +01:00
parent c04d72741a
commit 3884602d28
17 changed files with 88 additions and 11 deletions

60
tests/check-foreignext.py Executable file
View File

@ -0,0 +1,60 @@
#!/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:]))

View File

@ -48,3 +48,7 @@ New errors are not allowed. Warnings are strongly discouraged.
Skipping cfastmanifest/tree_path.h it has no-che?k-code (glob) Skipping cfastmanifest/tree_path.h it has no-che?k-code (glob)
Skipping cfastmanifest/tree_test.c it has no-che?k-code (glob) Skipping cfastmanifest/tree_test.c it has no-che?k-code (glob)
Skipping hgext3rd/statprof.py it has no-che?k-code (glob) Skipping hgext3rd/statprof.py it has no-che?k-code (glob)
Check foreign extensions are only used after checks
$ hg locate 'test-*.t' | xargs $TESTDIR/check-foreignext.py

View File

@ -1,8 +1,9 @@
$ . $TESTDIR/require-ext.sh remotenames
Setup Setup
$ PYTHONPATH=$TESTDIR/..:$PYTHONPATH $ PYTHONPATH=$TESTDIR/..:$PYTHONPATH
$ export PYTHONPATH $ export PYTHONPATH
$ $PYTHON -c 'import remotenames' || exit 80
$ mkcommit() { $ mkcommit() {
> echo "$1" > "$1" > echo "$1" > "$1"

View File

@ -298,7 +298,7 @@ Test that --addremove/-A works
Test that the extension disables itself when evolution is enabled Test that the extension disables itself when evolution is enabled
$ $PYTHON -c 'import evolve' 2> /dev/null || $PYTHON -c 'import hgext.evolve' 2> /dev/null || exit 80 $ . $TESTDIR/require-ext.sh evolve
$ cat >> .hg/hgrc <<EOF $ cat >> .hg/hgrc <<EOF
> [extensions] > [extensions]
> evolve= > evolve=
@ -476,7 +476,7 @@ Test hg amend works with a logfile
$ rm alogfile $ rm alogfile
Test fbamend with inhibit Test fbamend with inhibit
$ $PYTHON -c 'import inhibit' 2> /dev/null || $PYTHON -c 'import hgext.inhibit' 2> /dev/null || exit 80 $ . $TESTDIR/require-ext.sh inhibit
$ cat >> .hg/hgrc <<EOF $ cat >> .hg/hgrc <<EOF
> [extensions] > [extensions]
> inhibit= > inhibit=
@ -593,6 +593,7 @@ Make sure that unamend does not work without inhibit
[255] [255]
Make sure that unamend works as expected with inhibit Make sure that unamend works as expected with inhibit
$ . $TESTDIR/require-ext.sh directaccess
$ cat >> .hg/hgrc <<EOF $ cat >> .hg/hgrc <<EOF
> [extensions] > [extensions]
> inhibit= > inhibit=

View File

@ -258,6 +258,7 @@ test 'execr' executing in the current directory
$ cd .. $ cd ..
Test that we can recover exec with evolve on Test that we can recover exec with evolve on
$ . $TESTDIR/require-ext.sh evolve
$ cat >> .hg/hgrc <<EOF $ cat >> .hg/hgrc <<EOF
> [extensions] > [extensions]
> evolve= > evolve=

View File

@ -1,3 +1,4 @@
$ . $TESTDIR/require-ext.sh directaccess evolve inhibit
$ . "$TESTDIR/histedit-helpers.sh" $ . "$TESTDIR/histedit-helpers.sh"
Setup Setup

View File

@ -1,3 +1,5 @@
$ . $TESTDIR/require-ext.sh directaccess evolve inhibit
Test of warning for evolve users when inhibit is enabled Test of warning for evolve users when inhibit is enabled
$ cat >> $HGRCPATH <<EOF $ cat >> $HGRCPATH <<EOF
> [ui] > [ui]

View File

@ -1,10 +1,11 @@
$ . $TESTDIR/require-ext.sh directaccess evolve inhibit remotenames
Setup Setup
$ PYTHONPATH=$TESTDIR/..:$PYTHONPATH $ PYTHONPATH=$TESTDIR/..:$PYTHONPATH
$ export PYTHONPATH $ export PYTHONPATH
Test that hg pull creates obsolescence markers for landed diffs Test that hg pull creates obsolescence markers for landed diffs
$ $PYTHON -c 'import remotenames' || exit 80
$ cat >> $HGRCPATH <<EOF $ cat >> $HGRCPATH <<EOF
> [ui] > [ui]
> username = nobody <no.reply@fb.com> > username = nobody <no.reply@fb.com>

View File

@ -1,10 +1,11 @@
$ . $TESTDIR/require-ext.sh directaccess evolve inhibit remotenames
Setup Setup
$ PYTHONPATH=$TESTDIR/..:$PYTHONPATH $ PYTHONPATH=$TESTDIR/..:$PYTHONPATH
$ export PYTHONPATH $ export PYTHONPATH
Test that hg pull creates obsolescence markers for landed diffs Test that hg pull creates obsolescence markers for landed diffs
$ $PYTHON -c 'import remotenames' || exit 80
$ cat >> $HGRCPATH <<EOF $ cat >> $HGRCPATH <<EOF
> [ui] > [ui]
> username = nobody <no.reply@fb.com> > username = nobody <no.reply@fb.com>

View File

@ -1,9 +1,10 @@
$ . $TESTDIR/require-ext.sh remotenames
Setup Setup
$ PYTHONPATH=$TESTDIR/..:$PYTHONPATH $ PYTHONPATH=$TESTDIR/..:$PYTHONPATH
$ export PYTHONPATH $ export PYTHONPATH
$ $PYTHON -c 'import remotenames' || exit 80
$ cat >> $HGRCPATH << EOF $ cat >> $HGRCPATH << EOF
> [ui] > [ui]
> ssh = python "$RUNTESTDIR/dummyssh" > ssh = python "$RUNTESTDIR/dummyssh"

View File

@ -1,9 +1,10 @@
$ . $TESTDIR/require-ext.sh remotenames
Setup Setup
$ PYTHONPATH=$TESTDIR/..:$PYTHONPATH $ PYTHONPATH=$TESTDIR/..:$PYTHONPATH
$ export PYTHONPATH $ export PYTHONPATH
$ $PYTHON -c 'import remotenames' || exit 80
$ extpath=`dirname $TESTDIR` $ extpath=`dirname $TESTDIR`
$ cat > $TESTTMP/pretxnchangegroup.sh << EOF $ cat > $TESTTMP/pretxnchangegroup.sh << EOF
> #!/bin/bash > #!/bin/bash

View File

@ -1,4 +1,4 @@
$ $PYTHON -c 'import remotenames' || exit 80 $ . $TESTDIR/require-ext.sh remotenames
$ extpath=`dirname $TESTDIR` $ extpath=`dirname $TESTDIR`
$ cp $extpath/hgext3rd/reset.py $TESTTMP # use $TESTTMP substitution in message $ cp $extpath/hgext3rd/reset.py $TESTTMP # use $TESTTMP substitution in message
$ cat >> $HGRCPATH << EOF $ cat >> $HGRCPATH << EOF

View File

@ -150,6 +150,7 @@ Verify file status after reset
Reset + Evolve tests Reset + Evolve tests
$ . $TESTDIR/require-ext.sh evolve
$ cat >> .hg/hgrc <<EOF $ cat >> .hg/hgrc <<EOF
> [extensions] > [extensions]
> evolve= > evolve=
@ -193,6 +194,7 @@ a new hash for them
Reset + Inhibit tests, with inhibit reset revives the same commit Reset + Inhibit tests, with inhibit reset revives the same commit
$ . $TESTDIR/require-ext.sh directaccess inhibit
$ cat >> .hg/hgrc <<EOF $ cat >> .hg/hgrc <<EOF
> [extensions] > [extensions]
> evolve = > evolve =

View File

@ -1,4 +1,4 @@
$ $PYTHON -c 'import remotenames' || exit 80 $ . $TESTDIR/require-ext.sh remotenames
$ extpath=`dirname $TESTDIR` $ extpath=`dirname $TESTDIR`
$ cp $extpath/hgext3rd/smartlog.py $TESTTMP # use $TESTTMP substitution in message $ cp $extpath/hgext3rd/smartlog.py $TESTTMP # use $TESTTMP substitution in message
$ cat >> $HGRCPATH << EOF $ cat >> $HGRCPATH << EOF

View File

@ -1,5 +1,5 @@
Skip test if remotenames not present Skip test if remotenames not present
$ $PYTHON -c 'import remotenames' || exit 80 $ . $TESTDIR/require-ext.sh remotenames
Set up extensions (order is important here, we must test tweakdefaults loading last) Set up extensions (order is important here, we must test tweakdefaults loading last)
$ extpath=`dirname $TESTDIR` $ extpath=`dirname $TESTDIR`

View File

@ -1,5 +1,5 @@
Check for remotenames and skip if not present Check for remotenames and skip if not present
$ $PYTHON -c 'import remotenames' || exit 80 $ . $TESTDIR/require-ext.sh remotenames
Set up Set up
$ extpath=`dirname $TESTDIR` $ extpath=`dirname $TESTDIR`

View File

@ -464,6 +464,7 @@ Test non-remotenames use of pull --rebase and --update requires --dest
Prepare a repo for testing divergence warnings with respect to inhibit extension Prepare a repo for testing divergence warnings with respect to inhibit extension
and allowance of prune rebases and allowance of prune rebases
$ . $TESTDIR/require-ext.sh directaccess evolve
$ hg init repodiv && cd repodiv $ hg init repodiv && cd repodiv
$ cat >> .hg/hgrc << EOF $ cat >> .hg/hgrc << EOF
> [experimental] > [experimental]