|
| 1 | + |
| 2 | +# python -m pytest test-helloworld.py |
| 3 | + |
| 4 | +import json |
| 5 | +import logging |
| 6 | +import os |
| 7 | +import subprocess |
| 8 | + |
| 9 | +COMPILE_COMMANDS_JSON = os.path.join('proj2', 'compile_commands.json') |
| 10 | + |
| 11 | +def create_compile_commands(path): |
| 12 | + j = [{'directory': path+'/a', 'command': 'gcc -c a.c', 'file': 'a.c'}, |
| 13 | + {'directory': path+'/b', 'command': 'gcc -c b.c', 'file': 'b.c'}] |
| 14 | + f = open(COMPILE_COMMANDS_JSON, 'wt') |
| 15 | + f.write(json.dumps(j)) |
| 16 | + |
| 17 | +# Run Cppcheck with args |
| 18 | +def cppcheck(args): |
| 19 | + if os.path.isfile('../../bin/debug/cppcheck.exe'): |
| 20 | + cmd = '../../bin/debug/cppcheck.exe ' + args |
| 21 | + elif os.path.isfile('../../../bin/debug/cppcheck.exe'): |
| 22 | + cmd = '../../../bin/debug/cppcheck.exe ' + args |
| 23 | + elif os.path.isfile('../../cppcheck'): |
| 24 | + cmd = '../../cppcheck ' + args |
| 25 | + else: |
| 26 | + cmd = '../../../cppcheck ' + args |
| 27 | + logging.info(cmd) |
| 28 | + p = subprocess.Popen(cmd.split(), stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 29 | + comm = p.communicate() |
| 30 | + stdout = comm[0].decode(encoding='utf-8', errors='ignore').replace('\r\n', '\n') |
| 31 | + stderr = comm[1].decode(encoding='utf-8', errors='ignore').replace('\r\n', '\n') |
| 32 | + return p.returncode, stdout, stderr |
| 33 | + |
| 34 | +def test_local_path(): |
| 35 | + create_compile_commands(os.path.join(os.getcwd(), 'proj2')) |
| 36 | + cwd = os.getcwd() |
| 37 | + os.chdir('proj2') |
| 38 | + ret, stdout, stderr = cppcheck('--project=compile_commands.json') |
| 39 | + os.chdir(cwd) |
| 40 | + file1 = os.path.join(cwd, 'proj2', 'a', 'a.c') |
| 41 | + file2 = os.path.join(cwd, 'proj2', 'b', 'b.c') |
| 42 | + assert ret == 0 |
| 43 | + assert stdout.find('Checking %s ...' % (file1)) >= 0 |
| 44 | + assert stdout.find('Checking %s ...' % (file2)) >= 0 |
| 45 | + |
| 46 | +def test_relative_path(): |
| 47 | + create_compile_commands(os.path.join(os.getcwd(), 'proj2')) |
| 48 | + ret, stdout, stderr = cppcheck('--project=' + COMPILE_COMMANDS_JSON) |
| 49 | + cwd = os.getcwd() |
| 50 | + file1 = os.path.join(cwd, 'proj2', 'a', 'a.c') |
| 51 | + file2 = os.path.join(cwd, 'proj2', 'b', 'b.c') |
| 52 | + assert ret == 0 |
| 53 | + assert stdout.find('Checking %s ...' % (file1)) >= 0 |
| 54 | + assert stdout.find('Checking %s ...' % (file2)) >= 0 |
| 55 | + |
| 56 | +def test_absolute_path(): |
| 57 | + create_compile_commands(os.path.join(os.getcwd(), 'proj2')) |
| 58 | + cwd = os.getcwd() |
| 59 | + ret, stdout, stderr = cppcheck('--project=' + os.path.join(cwd,COMPILE_COMMANDS_JSON)) |
| 60 | + file1 = os.path.join(cwd, 'proj2', 'a', 'a.c') |
| 61 | + file2 = os.path.join(cwd, 'proj2', 'b', 'b.c') |
| 62 | + assert ret == 0 |
| 63 | + assert stdout.find('Checking %s ...' % (file1)) >= 0 |
| 64 | + assert stdout.find('Checking %s ...' % (file2)) >= 0 |
| 65 | + |
| 66 | +def test_project_1(): |
| 67 | + create_compile_commands(os.path.join(os.getcwd(), 'proj2')) |
| 68 | + ret, stdout, stderr = cppcheck('--project=proj2/proj2.cppcheck') |
| 69 | + cwd = os.getcwd() |
| 70 | + file1 = os.path.join(cwd, 'proj2', 'a', 'a.c') |
| 71 | + file2 = os.path.join(cwd, 'proj2', 'b', 'b.c') |
| 72 | + assert ret == 0 |
| 73 | + assert stdout.find('Checking %s ...' % (file1)) >= 0 |
| 74 | + assert stdout.find('Checking %s ...' % (file2)) >= 0 |
| 75 | + |
| 76 | +def test_project_2(): |
| 77 | + create_compile_commands(os.path.join(os.getcwd(), 'proj2')) |
| 78 | + ret, stdout, stderr = cppcheck('--project=proj2/proj2-exclude.cppcheck') |
| 79 | + cwd = os.getcwd() |
| 80 | + file1 = os.path.join(cwd, 'proj2', 'a', 'a.c') |
| 81 | + file2 = os.path.join(cwd, 'proj2', 'b', 'b.c') # Excluded by proj2-exclude.cppcheck |
| 82 | + assert ret == 0 |
| 83 | + assert stdout.find('Checking %s ...' % (file1)) >= 0 |
| 84 | + #assert stdout.find('Checking %s ...' % (file2)) < 0 |
| 85 | + |
0 commit comments