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:
Georgy Komarov 2019-07-26 16:40:25 +03:00 committed by Daniel Marjamäki
parent cdb0e8322c
commit fe2885e430
3 changed files with 82 additions and 40 deletions

View File

@ -19,7 +19,7 @@ before_install:
# install needed deps
- 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 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 unittest2
- travis_retry python2 -m pip install --user pexpect

View File

@ -9,6 +9,7 @@
# cppcheck --dump main.cpp
# python cert.py main.cpp.dump
import argparse
import cppcheckdata
import sys
import re
@ -354,46 +355,59 @@ def api01(data):
arrayFound=False
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
continue
if arg == '--cli':
continue
print('Checking ' + arg + '...')
data = cppcheckdata.parsedump(arg)
if VERIFY:
VERIFY_ACTUAL = []
VERIFY_EXPECTED = []
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 dumpfile in args.dumpfile:
if not args.quiet:
print('Checking %s...' % dumpfile)
for cfg in data.configurations:
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)
data = cppcheckdata.parsedump(dumpfile)
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)
if VERIFY:
VERIFY_ACTUAL = []
VERIFY_EXPECTED = []
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:
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)

28
addons/test/test-cert.py Normal file
View File

@ -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)