cert.py: Refactor arguments handling (#2044)
* cert.py: Add '--quiet' option and test Main sentinel also was required. * travis.yml: Install pytest with --user To avoid possible conflicts with system packages later. * Quickfix
This commit is contained in:
parent
cdb0e8322c
commit
fe2885e430
|
@ -19,7 +19,7 @@ before_install:
|
||||||
# install needed deps
|
# install needed deps
|
||||||
- travis_retry sudo apt-get update -qq
|
- travis_retry sudo apt-get update -qq
|
||||||
- travis_retry sudo apt-get install -qq python-pygments qt5-default qt5-qmake qtbase5-dev qtcreator libxml2-utils libpcre3 gdb unzip wx-common xmlstarlet
|
- travis_retry sudo apt-get install -qq python-pygments qt5-default qt5-qmake qtbase5-dev qtcreator libxml2-utils libpcre3 gdb unzip wx-common xmlstarlet
|
||||||
- travis_retry sudo python2 -m pip install --upgrade pytest==4.6.4
|
- travis_retry python2 -m pip install --user pytest==4.6.4
|
||||||
- travis_retry python2 -m pip install --user pylint
|
- travis_retry python2 -m pip install --user pylint
|
||||||
- travis_retry python2 -m pip install --user unittest2
|
- travis_retry python2 -m pip install --user unittest2
|
||||||
- travis_retry python2 -m pip install --user pexpect
|
- travis_retry python2 -m pip install --user pexpect
|
||||||
|
|
|
@ -9,6 +9,7 @@
|
||||||
# cppcheck --dump main.cpp
|
# cppcheck --dump main.cpp
|
||||||
# python cert.py main.cpp.dump
|
# python cert.py main.cpp.dump
|
||||||
|
|
||||||
|
import argparse
|
||||||
import cppcheckdata
|
import cppcheckdata
|
||||||
import sys
|
import sys
|
||||||
import re
|
import re
|
||||||
|
@ -354,46 +355,59 @@ def api01(data):
|
||||||
arrayFound=False
|
arrayFound=False
|
||||||
token = token.next
|
token = token.next
|
||||||
|
|
||||||
for arg in sys.argv[1:]:
|
|
||||||
if arg == '-verify':
|
def get_args():
|
||||||
|
parser = cppcheckdata.ArgumentParser()
|
||||||
|
parser.add_argument("dumpfile", nargs='*', help="Path of dump files from cppcheck")
|
||||||
|
parser.add_argument('-q', '--quiet', action='store_true',
|
||||||
|
help='do not print "Checking ..." lines')
|
||||||
|
parser.add_argument('--cli', help='Addon is executed from Cppcheck', action='store_true')
|
||||||
|
parser.add_argument("-verify", help=argparse.SUPPRESS, action="store_true")
|
||||||
|
return parser.parse_args()
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
args = get_args()
|
||||||
|
|
||||||
|
if args.verify:
|
||||||
VERIFY = True
|
VERIFY = True
|
||||||
continue
|
|
||||||
if arg == '--cli':
|
|
||||||
continue
|
|
||||||
print('Checking ' + arg + '...')
|
|
||||||
data = cppcheckdata.parsedump(arg)
|
|
||||||
|
|
||||||
if VERIFY:
|
for dumpfile in args.dumpfile:
|
||||||
VERIFY_ACTUAL = []
|
if not args.quiet:
|
||||||
VERIFY_EXPECTED = []
|
print('Checking %s...' % dumpfile)
|
||||||
for tok in data.rawTokens:
|
|
||||||
if tok.str.startswith('//') and 'TODO' not in tok.str:
|
|
||||||
for word in tok.str[2:].split(' '):
|
|
||||||
if re.match(r'cert-[A-Z][A-Z][A-Z][0-9][0-9].*',word):
|
|
||||||
VERIFY_EXPECTED.append(str(tok.linenr) + ':' + word)
|
|
||||||
|
|
||||||
for cfg in data.configurations:
|
data = cppcheckdata.parsedump(dumpfile)
|
||||||
if len(data.configurations) > 1:
|
|
||||||
print('Checking ' + arg + ', config "' + cfg.name + '"...')
|
|
||||||
exp05(cfg)
|
|
||||||
exp42(cfg)
|
|
||||||
exp46(cfg)
|
|
||||||
exp15(cfg)
|
|
||||||
int31(cfg, data.platform)
|
|
||||||
str03(cfg)
|
|
||||||
str05(cfg)
|
|
||||||
str07(cfg)
|
|
||||||
str11(cfg)
|
|
||||||
msc24(cfg)
|
|
||||||
msc30(cfg)
|
|
||||||
api01(cfg)
|
|
||||||
|
|
||||||
if VERIFY:
|
if VERIFY:
|
||||||
for expected in VERIFY_EXPECTED:
|
VERIFY_ACTUAL = []
|
||||||
if expected not in VERIFY_ACTUAL:
|
VERIFY_EXPECTED = []
|
||||||
print('Expected but not seen: ' + expected)
|
for tok in data.rawTokens:
|
||||||
sys.exit(1)
|
if tok.str.startswith('//') and 'TODO' not in tok.str:
|
||||||
for actual in VERIFY_ACTUAL:
|
for word in tok.str[2:].split(' '):
|
||||||
if actual not in VERIFY_EXPECTED:
|
if re.match(r'cert-[A-Z][A-Z][A-Z][0-9][0-9].*',word):
|
||||||
print('Not expected: ' + actual)
|
VERIFY_EXPECTED.append(str(tok.linenr) + ':' + word)
|
||||||
sys.exit(1)
|
|
||||||
|
for cfg in data.configurations:
|
||||||
|
if (len(data.configurations) > 1) and (not args.quiet):
|
||||||
|
print('Checking %s, config %s...' % (dumpfile, cfg.name))
|
||||||
|
exp05(cfg)
|
||||||
|
exp42(cfg)
|
||||||
|
exp46(cfg)
|
||||||
|
exp15(cfg)
|
||||||
|
int31(cfg, data.platform)
|
||||||
|
str03(cfg)
|
||||||
|
str05(cfg)
|
||||||
|
str07(cfg)
|
||||||
|
str11(cfg)
|
||||||
|
msc24(cfg)
|
||||||
|
msc30(cfg)
|
||||||
|
api01(cfg)
|
||||||
|
|
||||||
|
if VERIFY:
|
||||||
|
for expected in VERIFY_EXPECTED:
|
||||||
|
if expected not in VERIFY_ACTUAL:
|
||||||
|
print('Expected but not seen: ' + expected)
|
||||||
|
sys.exit(1)
|
||||||
|
for actual in VERIFY_ACTUAL:
|
||||||
|
if actual not in VERIFY_EXPECTED:
|
||||||
|
print('Not expected: ' + actual)
|
||||||
|
sys.exit(1)
|
||||||
|
|
|
@ -0,0 +1,28 @@
|
||||||
|
# python -m pytest addons/test/test-cert.py
|
||||||
|
|
||||||
|
import sys
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
|
||||||
|
def test_arguments_regression():
|
||||||
|
args_ok = ["-q", "--quiet",
|
||||||
|
"-verify",
|
||||||
|
"--cli"]
|
||||||
|
# Arguments with expected SystemExit
|
||||||
|
args_exit = ["--non-exists", "--non-exists-param=42", "-h", "--help"]
|
||||||
|
|
||||||
|
from addons.cert import get_args
|
||||||
|
|
||||||
|
for arg in args_exit:
|
||||||
|
sys.argv.append(arg)
|
||||||
|
with pytest.raises(SystemExit):
|
||||||
|
get_args()
|
||||||
|
sys.argv.remove(arg)
|
||||||
|
|
||||||
|
for arg in args_ok:
|
||||||
|
sys.argv.append(arg)
|
||||||
|
try:
|
||||||
|
get_args()
|
||||||
|
except SystemExit:
|
||||||
|
pytest.fail("Unexpected SystemExit with '%s'" % arg)
|
||||||
|
sys.argv.remove(arg)
|
Loading…
Reference in New Issue