Skip to content

Commit

Permalink
addons/namingng.py: Add unit test for standalone mode.
Browse files Browse the repository at this point in the history
This reuses the existing unit test, running the addon on the dump file created.

The format of the standalone mode is different so the expected output is
slightly transformed before checking.

This implicitly also tests `cppcheck --dump`, i.e. whether checking 1 file
leads to 1 dump file, and whether the dump filename is indeed file+'.dump'.
This is relevant as some code simply assumes this to be the case.
  • Loading branch information
mvds00 committed Jan 5, 2024
1 parent f0809b6 commit 8ae8678
Showing 1 changed file with 33 additions and 4 deletions.
37 changes: 33 additions & 4 deletions test/cli/test-other.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
# python -m pytest test-other.py

import os
from glob import glob
import re
import sys
import pytest

from testutils import cppcheck, assert_cppcheck
from testutils import cppcheck, assert_cppcheck, addon_standalone


def __test_missing_include(tmpdir, use_j):
Expand Down Expand Up @@ -325,17 +327,18 @@ def test_addon_naming(tmpdir):


def test_addon_namingng(tmpdir):
addon_py = 'namingng.py'
addon_file = os.path.join(tmpdir, 'namingng.json')
addon_config_file = os.path.join(tmpdir, 'namingng.config.json')
with open(addon_file, 'wt') as f:
f.write("""
{
"script": "addons/namingng.py",
"script": "addons/%s",
"args": [
"--configfile=%s"
]
}
"""%(addon_config_file).replace('\\','\\\\'))
"""%(addon_py,addon_config_file.replace('\\','\\\\')))

with open(addon_config_file, 'wt') as f:
f.write("""
Expand Down Expand Up @@ -424,7 +427,7 @@ class _clz {
namespace _invalid_namespace { }
"""%(test_include_file_basename))

args = ['--addon='+addon_file, '--verbose', '--enable=all', test_file]
args = ['--addon='+addon_file, '--dump', '--verbose', '--enable=all', test_file]

exitcode, stdout, stderr = cppcheck(args)
assert exitcode == 0
Expand Down Expand Up @@ -505,6 +508,32 @@ class _clz {
expect.sort()
assert lines == expect

# Perform same analysis again, but now in standalone mode.
dump_files = glob(os.path.join(tmpdir,'*.dump'))
assert len(dump_files) == 1

dump_file = dump_files[0]
assert dump_file == test_file+'.dump'

args = ['--configfile='+addon_config_file,dump_file]
exitcode, stdout, stderr = addon_standalone(addon_py,args)
assert exitcode == 0
lines = stdout.splitlines()
assert lines == [
'Checking {}...'.format(dump_file),
'Checking config ...',
]

# Trim column information and adapt to standalone output format.
lines = [line for line in stderr.splitlines() if line != '']
def standaloneize(line):
pat = '([^:]+):([0-9]+):[0-9]+: ([^:]+): (.*)'
repl = '[\\1:\\2] (\\3) \\4'
return re.sub(pat,repl,line,1)
expect_standalone = [standaloneize(line) for line in lines if line.strip() != '^']
lines.sort()
expect_standalone.sort()
assert lines == expect_standalone

def test_addon_namingng_config(tmpdir):
addon_file = os.path.join(tmpdir, 'namingng.json')
Expand Down

0 comments on commit 8ae8678

Please sign in to comment.