test/cli/test-other.py: improved addon tests (#5469)

This commit is contained in:
Oliver Stöneberg 2023-09-25 13:39:17 +02:00 committed by GitHub
parent 3979ade9ea
commit 1959c85e34
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 74 additions and 14 deletions

View File

@ -4,7 +4,6 @@
#
import cppcheck
import sys
@cppcheck.checker
def cast(cfg, data):

View File

@ -16,7 +16,6 @@ from __future__ import print_function
import cppcheckdata
import sys
import os
import re

View File

@ -255,10 +255,14 @@ def test_addon_y2038(tmpdir):
# TODO: trigger warning
with open(test_file, 'wt') as f:
f.write("""
typedef int MISRA_5_6_VIOLATION;
extern void f()
{
time_t t = std::time(nullptr);
(void)t;
}
""")
args = ['--addon=y2038', '--enable=all', test_file]
args = ['--addon=y2038', '--enable=all', '--template={file}:{line}:{column}: {severity}:{inconclusive:inconclusive:} {message} [{id}]', test_file]
exitcode, stdout, stderr = cppcheck(args)
assert exitcode == 0
@ -266,18 +270,20 @@ typedef int MISRA_5_6_VIOLATION;
assert lines == [
'Checking {} ...'.format(test_file)
]
assert stderr == ''
assert stderr == '{}:4:21: warning: time is Y2038-unsafe [y2038-unsafe-call]\n'.format(test_file)
def test_addon_threadsafety(tmpdir):
test_file = os.path.join(tmpdir, 'test.cpp')
# TODO: trigger warning
with open(test_file, 'wt') as f:
f.write("""
typedef int MISRA_5_6_VIOLATION;
extern const char* f()
{
return strerror(1);
}
""")
args = ['--addon=y2038', '--enable=all', test_file]
args = ['--addon=threadsafety', '--enable=all', '--template={file}:{line}:{column}: {severity}:{inconclusive:inconclusive:} {message} [{id}]', test_file]
exitcode, stdout, stderr = cppcheck(args)
assert exitcode == 0
@ -285,18 +291,29 @@ typedef int MISRA_5_6_VIOLATION;
assert lines == [
'Checking {} ...'.format(test_file)
]
assert stderr == ''
assert stderr == '{}:4:12: warning: strerror is MT-unsafe [threadsafety-unsafe-call]\n'.format(test_file)
def test_addon_naming(tmpdir):
# the addon does nothing without a config
addon_file = os.path.join(tmpdir, 'naming1.json')
with open(addon_file, 'wt') as f:
f.write("""
{
"script": "addons/naming.py",
"args": [
"--var=[_a-z].*"
]
}
""")
test_file = os.path.join(tmpdir, 'test.cpp')
# TODO: trigger warning
with open(test_file, 'wt') as f:
f.write("""
typedef int MISRA_5_6_VIOLATION;
int Var;
""")
args = ['--addon=y2038', '--enable=all', test_file]
args = ['--addon={}'.format(addon_file), '--enable=all', '--template={file}:{line}:{column}: {severity}:{inconclusive:inconclusive:} {message} [{id}]', test_file]
exitcode, stdout, stderr = cppcheck(args)
assert exitcode == 0
@ -304,9 +321,11 @@ typedef int MISRA_5_6_VIOLATION;
assert lines == [
'Checking {} ...'.format(test_file)
]
assert stderr == ''
assert stderr == '{}:2:1: style: Variable Var violates naming convention [naming-varname]\n'.format(test_file)
# the namingng addon only works standalone and not in CLI mode - see #12005
@pytest.mark.skip
def test_addon_namingng(tmpdir):
test_file = os.path.join(tmpdir, 'test.cpp')
# TODO: trigger warning
@ -315,7 +334,7 @@ def test_addon_namingng(tmpdir):
typedef int MISRA_5_6_VIOLATION;
""")
args = ['--addon=y2038', '--enable=all', test_file]
args = ['--addon=namingng', '--enable=all', test_file]
exitcode, stdout, stderr = cppcheck(args)
assert exitcode == 0
@ -326,6 +345,49 @@ typedef int MISRA_5_6_VIOLATION;
assert stderr == ''
def test_addon_findcasts(tmpdir):
test_file = os.path.join(tmpdir, 'test.cpp')
with open(test_file, 'wt') as f:
f.write("""
extern void f(char c)
{
int i = (int)c;
(void)i;
}
""")
args = ['--addon=findcasts', '--enable=all', '--template={file}:{line}:{column}: {severity}:{inconclusive:inconclusive:} {message} [{id}]', test_file]
exitcode, stdout, stderr = cppcheck(args)
assert exitcode == 0
lines = stdout.splitlines()
assert lines == [
'Checking {} ...'.format(test_file)
]
assert stderr == '{}:4:21: information: found a cast [findcasts-cast]\n'.format(test_file)
def test_addon_misc(tmpdir):
test_file = os.path.join(tmpdir, 'test.cpp')
with open(test_file, 'wt') as f:
f.write("""
extern void f()
{
char char* [] = {"a" "b"}
}
""")
args = ['--addon=misc', '--enable=all', '--template={file}:{line}:{column}: {severity}:{inconclusive:inconclusive:} {message} [{id}]', test_file]
exitcode, stdout, stderr = cppcheck(args)
assert exitcode == 0
lines = stdout.splitlines()
assert lines == [
'Checking {} ...'.format(test_file)
]
assert stderr == '{}:4:26: style: String concatenation in array initialization, missing comma? [misc-stringConcatInArrayInit]\n'.format(test_file)
def test_invalid_addon_json(tmpdir):
addon_file = os.path.join(tmpdir, 'addon1.json')
with open(addon_file, 'wt') as f: