From 6a83c5d3f3981c30c4b3203cec03bc262122920c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sun, 14 Apr 2019 10:45:20 +0200 Subject: [PATCH] Add cli testing --- test/cli/1-helloworld/main.c | 8 ++++++ test/cli/readme.txt | 17 ++++++++++++ test/cli/test-helloworld.py | 53 ++++++++++++++++++++++++++++++++++++ 3 files changed, 78 insertions(+) create mode 100644 test/cli/1-helloworld/main.c create mode 100644 test/cli/readme.txt create mode 100644 test/cli/test-helloworld.py diff --git a/test/cli/1-helloworld/main.c b/test/cli/1-helloworld/main.c new file mode 100644 index 000000000..675d69054 --- /dev/null +++ b/test/cli/1-helloworld/main.c @@ -0,0 +1,8 @@ +#include + +int main() { + printf("Hello world!\n"); + x = 3 / 0; // ERROR + return 0; +} + diff --git a/test/cli/readme.txt b/test/cli/readme.txt new file mode 100644 index 000000000..b237cfa2e --- /dev/null +++ b/test/cli/readme.txt @@ -0,0 +1,17 @@ + +Systemtesting of Cppcheck CLI on some projects + +addons +base path +exclude folders +importing projects + * visual studio + * compile database + - different generators bear/cmake/.. + - different platforms +suppressions + +Different paths: + * relative + * absolute + diff --git a/test/cli/test-helloworld.py b/test/cli/test-helloworld.py new file mode 100644 index 000000000..b034dd067 --- /dev/null +++ b/test/cli/test-helloworld.py @@ -0,0 +1,53 @@ + +# python -m pytest test-helloworld.py + +import logging +import os +import subprocess + +# Run Cppcheck with args +def cppcheck(args): + cmd = '%s %s' % (os.path.expanduser('~/cppcheck/cppcheck'), args) + logging.info(cmd) + p = subprocess.Popen(cmd.split(), stdout=subprocess.PIPE, stderr=subprocess.PIPE) + comm = p.communicate() + stdout = comm[0].decode(encoding='utf-8', errors='ignore') + stderr = comm[1].decode(encoding='utf-8', errors='ignore') + return p.returncode, stdout, stderr + +def test_relative_path(): + ret, stdout, stderr = cppcheck('1-helloworld') + assert ret == 0 + assert stdout == 'Checking 1-helloworld/main.c ...\n' + assert stderr == '[1-helloworld/main.c:5]: (error) Division by zero.\n' + +def test_local_path(): + cwd = os.getcwd() + os.chdir('1-helloworld') + ret, stdout, stderr = cppcheck('.') + os.chdir(cwd) + assert ret == 0 + assert stdout == 'Checking main.c ...\n' + assert stderr == '[main.c:5]: (error) Division by zero.\n' + +def test_absolute_path(): + prjpath = '%s/1-helloworld' % (os.getcwd()) + ret, stdout, stderr = cppcheck(prjpath) + assert ret == 0 + assert stdout == 'Checking %s/main.c ...\n' % (prjpath) + assert stderr == '[%s/main.c:5]: (error) Division by zero.\n' % (prjpath) + +def test_basepath_relative_path(): + prjpath = '1-helloworld' + ret, stdout, stderr = cppcheck('%s -rp=%s' % (prjpath, prjpath)) + assert ret == 0 + assert stdout == 'Checking %s/main.c ...\n' % (prjpath) + assert stderr == '[main.c:5]: (error) Division by zero.\n' + +def test_basepath_absolute_path(): + prjpath = '%s/1-helloworld' % (os.getcwd()) + ret, stdout, stderr = cppcheck('%s -rp=%s' % (prjpath, prjpath)) + assert ret == 0 + assert stdout == 'Checking %s/main.c ...\n' % (prjpath) + assert stderr == '[main.c:5]: (error) Division by zero.\n' +