From fe2885e430320343c45928e362080a61d4db8b13 Mon Sep 17 00:00:00 2001 From: Georgy Komarov Date: Fri, 26 Jul 2019 16:40:25 +0300 Subject: [PATCH] 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 --- .travis.yml | 2 +- addons/cert.py | 92 +++++++++++++++++++++++----------------- addons/test/test-cert.py | 28 ++++++++++++ 3 files changed, 82 insertions(+), 40 deletions(-) create mode 100644 addons/test/test-cert.py diff --git a/.travis.yml b/.travis.yml index 73af95dc5..bb5c14fa9 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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 diff --git a/addons/cert.py b/addons/cert.py index 6d5184d7b..cb6749468 100755 --- a/addons/cert.py +++ b/addons/cert.py @@ -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) diff --git a/addons/test/test-cert.py b/addons/test/test-cert.py new file mode 100644 index 000000000..b7694fd60 --- /dev/null +++ b/addons/test/test-cert.py @@ -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)