From a3efe4e03caab1268ea7534ea143677599b9b82b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sun, 14 Apr 2019 21:02:53 +0200 Subject: [PATCH] test/cli: added proj2 test project --- test/cli/proj2/a/a.c | 0 test/cli/proj2/b/b.c | 0 test/cli/proj2/proj2-exclude.cppcheck | 9 +++ test/cli/proj2/proj2.cppcheck | 6 ++ test/cli/test-proj2.py | 85 +++++++++++++++++++++++++++ 5 files changed, 100 insertions(+) create mode 100644 test/cli/proj2/a/a.c create mode 100644 test/cli/proj2/b/b.c create mode 100644 test/cli/proj2/proj2-exclude.cppcheck create mode 100644 test/cli/proj2/proj2.cppcheck create mode 100644 test/cli/test-proj2.py diff --git a/test/cli/proj2/a/a.c b/test/cli/proj2/a/a.c new file mode 100644 index 000000000..e69de29bb diff --git a/test/cli/proj2/b/b.c b/test/cli/proj2/b/b.c new file mode 100644 index 000000000..e69de29bb diff --git a/test/cli/proj2/proj2-exclude.cppcheck b/test/cli/proj2/proj2-exclude.cppcheck new file mode 100644 index 000000000..04b1bcc7d --- /dev/null +++ b/test/cli/proj2/proj2-exclude.cppcheck @@ -0,0 +1,9 @@ + + + + compile_commands.json + + + + + diff --git a/test/cli/proj2/proj2.cppcheck b/test/cli/proj2/proj2.cppcheck new file mode 100644 index 000000000..079023c3e --- /dev/null +++ b/test/cli/proj2/proj2.cppcheck @@ -0,0 +1,6 @@ + + + + compile_commands.json + + diff --git a/test/cli/test-proj2.py b/test/cli/test-proj2.py new file mode 100644 index 000000000..eb6fd89db --- /dev/null +++ b/test/cli/test-proj2.py @@ -0,0 +1,85 @@ + +# python -m pytest test-helloworld.py + +import json +import logging +import os +import subprocess + +COMPILE_COMMANDS_JSON = os.path.join('proj2', 'compile_commands.json') + +def create_compile_commands(path): + j = [{'directory': path+'/a', 'command': 'gcc -c a.c', 'file': 'a.c'}, + {'directory': path+'/b', 'command': 'gcc -c b.c', 'file': 'b.c'}] + f = open(COMPILE_COMMANDS_JSON, 'wt') + f.write(json.dumps(j)) + +# Run Cppcheck with args +def cppcheck(args): + if os.path.isfile('../../bin/debug/cppcheck.exe'): + cmd = '../../bin/debug/cppcheck.exe ' + args + elif os.path.isfile('../../../bin/debug/cppcheck.exe'): + cmd = '../../../bin/debug/cppcheck.exe ' + args + elif os.path.isfile('../../cppcheck'): + cmd = '../../cppcheck ' + args + else: + cmd = '../../../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').replace('\r\n', '\n') + stderr = comm[1].decode(encoding='utf-8', errors='ignore').replace('\r\n', '\n') + return p.returncode, stdout, stderr + +def test_local_path(): + create_compile_commands(os.path.join(os.getcwd(), 'proj2')) + cwd = os.getcwd() + os.chdir('proj2') + ret, stdout, stderr = cppcheck('--project=compile_commands.json') + os.chdir(cwd) + file1 = os.path.join(cwd, 'proj2', 'a', 'a.c') + file2 = os.path.join(cwd, 'proj2', 'b', 'b.c') + assert ret == 0 + assert stdout.find('Checking %s ...' % (file1)) >= 0 + assert stdout.find('Checking %s ...' % (file2)) >= 0 + +def test_relative_path(): + create_compile_commands(os.path.join(os.getcwd(), 'proj2')) + ret, stdout, stderr = cppcheck('--project=' + COMPILE_COMMANDS_JSON) + cwd = os.getcwd() + file1 = os.path.join(cwd, 'proj2', 'a', 'a.c') + file2 = os.path.join(cwd, 'proj2', 'b', 'b.c') + assert ret == 0 + assert stdout.find('Checking %s ...' % (file1)) >= 0 + assert stdout.find('Checking %s ...' % (file2)) >= 0 + +def test_absolute_path(): + create_compile_commands(os.path.join(os.getcwd(), 'proj2')) + cwd = os.getcwd() + ret, stdout, stderr = cppcheck('--project=' + os.path.join(cwd,COMPILE_COMMANDS_JSON)) + file1 = os.path.join(cwd, 'proj2', 'a', 'a.c') + file2 = os.path.join(cwd, 'proj2', 'b', 'b.c') + assert ret == 0 + assert stdout.find('Checking %s ...' % (file1)) >= 0 + assert stdout.find('Checking %s ...' % (file2)) >= 0 + +def test_project_1(): + create_compile_commands(os.path.join(os.getcwd(), 'proj2')) + ret, stdout, stderr = cppcheck('--project=proj2/proj2.cppcheck') + cwd = os.getcwd() + file1 = os.path.join(cwd, 'proj2', 'a', 'a.c') + file2 = os.path.join(cwd, 'proj2', 'b', 'b.c') + assert ret == 0 + assert stdout.find('Checking %s ...' % (file1)) >= 0 + assert stdout.find('Checking %s ...' % (file2)) >= 0 + +def test_project_2(): + create_compile_commands(os.path.join(os.getcwd(), 'proj2')) + ret, stdout, stderr = cppcheck('--project=proj2/proj2-exclude.cppcheck') + cwd = os.getcwd() + file1 = os.path.join(cwd, 'proj2', 'a', 'a.c') + file2 = os.path.join(cwd, 'proj2', 'b', 'b.c') # Excluded by proj2-exclude.cppcheck + assert ret == 0 + assert stdout.find('Checking %s ...' % (file1)) >= 0 + #assert stdout.find('Checking %s ...' % (file2)) < 0 +