Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refs #12274 / fixed #13287 - test/cli/proj2_test.py: made it location-independent / some cleanups #6636

Merged
merged 4 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/importproject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1269,7 +1269,7 @@ bool ImportProject::importCppcheckGuiProject(std::istream &istr, Settings *setti
if (strcmp(name, CppcheckXml::RootPathName) == 0) {
const char* attr = node->Attribute(CppcheckXml::RootPathNameAttrib);
if (attr) {
temp.basePaths.push_back(joinRelativePath(path, attr));
temp.basePaths.push_back(Path::fromNativeSeparators(joinRelativePath(path, attr)));
temp.relativePaths = true;
}
} else if (strcmp(name, CppcheckXml::BuildDirElementName) == 0)
Expand Down Expand Up @@ -1401,7 +1401,7 @@ bool ImportProject::importCppcheckGuiProject(std::istream &istr, Settings *setti
settings->premiumArgs = temp.premiumArgs.substr(1);

for (const std::string &p : paths)
guiProject.pathNames.push_back(p);
guiProject.pathNames.push_back(Path::fromNativeSeparators(p));
settings->supprs.nomsg.addSuppressions(std::move(suppressions));
settings->checkHeaders = temp.checkHeaders;
settings->checkUnusedTemplates = temp.checkUnusedTemplates;
Expand Down
114 changes: 53 additions & 61 deletions test/cli/proj2_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,97 +5,90 @@
import os
from testutils import create_gui_project_file, cppcheck

COMPILE_COMMANDS_JSON = 'compile_commands.json'

ERR_A = ('%s:1:7: error: Division by zero. [zerodiv]\n' +
'x = 3 / 0;\n' +
' ^\n') % os.path.join('a', 'a.c')
ERR_B = ('%s:1:7: error: Division by zero. [zerodiv]\n' +
'x = 3 / 0;\n' +
' ^\n') % os.path.join('b', 'b.c')

def realpath(s):
return os.path.realpath(s).replace('\\', '/')

def create_compile_commands():
j = [{'directory': realpath('proj2/a'), 'command': 'gcc -c a.c', 'file': 'a.c'},
{'directory': realpath('proj2'), 'command': 'gcc -c b/b.c', 'file': 'b/b.c'}]
with open('proj2/' + COMPILE_COMMANDS_JSON, 'wt') as f:
__script_dir = os.path.dirname(os.path.abspath(__file__))
__proj_dir = os.path.join(__script_dir, 'proj2')

__COMPILE_COMMANDS_JSON = 'compile_commands.json'

__ERR_A = ('%s:1:7: error: Division by zero. [zerodiv]\n' +
'x = 3 / 0;\n' +
' ^\n') % os.path.join('a', 'a.c')
__ERR_B = ('%s:1:7: error: Division by zero. [zerodiv]\n' +
'x = 3 / 0;\n' +
' ^\n') % os.path.join('b', 'b.c')

def __create_compile_commands():
j = [{'directory': os.path.join(__proj_dir, 'a'), 'command': 'gcc -c a.c', 'file': 'a.c'},
{'directory': __proj_dir, 'command': 'gcc -c b/b.c', 'file': 'b/b.c'}]
with open(os.path.join(__proj_dir, __COMPILE_COMMANDS_JSON), 'wt') as f:
f.write(json.dumps(j))

# Run Cppcheck from project path
def cppcheck_local(args):
cwd = os.getcwd()
os.chdir('proj2')
ret, stdout, stderr = cppcheck(args)
os.chdir(cwd)
return ret, stdout, stderr

def test_file_filter():
ret, stdout, _ = cppcheck(['proj2/','--file-filter=proj2/a/*'])
ret, stdout, _ = cppcheck(['proj2/','--file-filter=proj2/a/*'], cwd=__script_dir)
file1 = os.path.join('proj2', 'a', 'a.c')
file2 = os.path.join('proj2', 'b', 'b.c')
assert ret == 0
assert ret == 0, stdout
assert stdout.find('Checking %s ...' % file1) >= 0
ret, stdout, _ = cppcheck(['proj2/','--file-filter=proj2/b*'])
ret, stdout, _ = cppcheck(['proj2/','--file-filter=proj2/b*'], cwd=__script_dir)
assert ret == 0, stdout
assert stdout.find('Checking %s ...' % file2) >= 0

def test_local_path():
create_compile_commands()
ret, stdout, _ = cppcheck_local(['--project=compile_commands.json'])
__create_compile_commands()
ret, stdout, _ = cppcheck(['--project=compile_commands.json'], cwd=__proj_dir)
file1 = os.path.join('a', 'a.c')
file2 = os.path.join('b', 'b.c')
assert ret == 0, stdout
assert stdout.find('Checking %s ...' % file1) >= 0
assert stdout.find('Checking %s ...' % file2) >= 0

def test_local_path_force():
create_compile_commands()
ret, stdout, _ = cppcheck_local(['--project=compile_commands.json', '--force'])
__create_compile_commands()
ret, stdout, _ = cppcheck(['--project=compile_commands.json', '--force'], cwd=__proj_dir)
assert ret == 0, stdout
assert stdout.find('AAA') >= 0

def test_local_path_maxconfigs():
create_compile_commands()
ret, stdout, _ = cppcheck_local(['--project=compile_commands.json', '--max-configs=2'])
__create_compile_commands()
ret, stdout, _ = cppcheck(['--project=compile_commands.json', '--max-configs=2'], cwd=__proj_dir)
assert ret == 0, stdout
assert stdout.find('AAA') >= 0

def test_relative_path():
create_compile_commands()
ret, stdout, _ = cppcheck(['--project=proj2/' + COMPILE_COMMANDS_JSON])
__create_compile_commands()
ret, stdout, _ = cppcheck(['--project=proj2/' + __COMPILE_COMMANDS_JSON], cwd=__script_dir)
file1 = os.path.join('proj2', 'a', 'a.c')
file2 = os.path.join('proj2', 'b', 'b.c')
assert ret == 0, stdout
assert stdout.find('Checking %s ...' % file1) >= 0
assert stdout.find('Checking %s ...' % file2) >= 0

def test_absolute_path():
create_compile_commands()
ret, stdout, _ = cppcheck(['--project=' + os.path.realpath('proj2/' + COMPILE_COMMANDS_JSON)])
file1 = os.path.realpath('proj2/a/a.c')
file2 = os.path.realpath('proj2/b/b.c')
__create_compile_commands()
ret, stdout, _ = cppcheck(['--project=' + os.path.join(__proj_dir, __COMPILE_COMMANDS_JSON)], cwd=__script_dir)
file1 = os.path.join(__proj_dir, 'a', 'a.c')
file2 = os.path.join(__proj_dir, 'b', 'b.c')
assert ret == 0, stdout
assert stdout.find('Checking %s ...' % file1) >= 0
assert stdout.find('Checking %s ...' % file2) >= 0

def test_gui_project_loads_compile_commands_1():
create_compile_commands()
ret, stdout, _ = cppcheck(['--project=proj2/proj2.cppcheck'])
__create_compile_commands()
ret, stdout, _ = cppcheck(['--project=proj2/proj2.cppcheck'], cwd=__script_dir)
file1 = os.path.join('proj2', 'a', 'a.c')
file2 = os.path.join('proj2', 'b', 'b.c')
assert ret == 0, stdout
assert stdout.find('Checking %s ...' % file1) >= 0
assert stdout.find('Checking %s ...' % file2) >= 0

def test_gui_project_loads_compile_commands_2():
create_compile_commands()
__create_compile_commands()
exclude_path_1 = 'proj2/b'
create_gui_project_file('proj2/test.cppcheck',
create_gui_project_file(os.path.join(__proj_dir, 'test.cppcheck'),
import_project='compile_commands.json',
exclude_paths=[exclude_path_1])
ret, stdout, _ = cppcheck(['--project=proj2/test.cppcheck'])
ret, stdout, _ = cppcheck(['--project=' + os.path.join('proj2','test.cppcheck')], cwd=__script_dir)
file1 = os.path.join('proj2', 'a', 'a.c')
file2 = os.path.join('proj2', 'b', 'b.c') # Excluded by test.cppcheck
assert ret == 0, stdout
Expand All @@ -104,8 +97,8 @@ def test_gui_project_loads_compile_commands_2():


def test_gui_project_loads_relative_vs_solution():
create_gui_project_file('test.cppcheck', import_project='proj2/proj2.sln')
ret, stdout, _ = cppcheck(['--project=test.cppcheck'])
create_gui_project_file(os.path.join(__script_dir, 'test.cppcheck'), import_project='proj2/proj2.sln')
ret, stdout, _ = cppcheck(['--project=test.cppcheck'], cwd=__script_dir)
file1 = os.path.join('proj2', 'a', 'a.c')
file2 = os.path.join('proj2', 'b', 'b.c')
assert ret == 0, stdout
Expand All @@ -119,11 +112,10 @@ def test_gui_project_loads_relative_vs_solution():
assert stdout.find('Checking %s Release|x64...' % file2) >= 0

def test_gui_project_loads_absolute_vs_solution():
create_gui_project_file('test.cppcheck', import_project=realpath('proj2/proj2.sln'))
ret, stdout, _ = cppcheck(['--project=test.cppcheck'])
file1 = os.path.realpath('proj2/a/a.c')
file2 = os.path.realpath('proj2/b/b.c')
print(stdout)
create_gui_project_file(os.path.join(__script_dir, 'test.cppcheck'), import_project=os.path.join(__proj_dir, 'proj2.sln'))
ret, stdout, _ = cppcheck(['--project=test.cppcheck'], cwd=__script_dir)
file1 = os.path.join(__proj_dir, 'a', 'a.c')
file2 = os.path.join(__proj_dir, 'b', 'b.c')
assert ret == 0, stdout
assert stdout.find('Checking %s Debug|Win32...' % file1) >= 0
assert stdout.find('Checking %s Debug|x64...' % file1) >= 0
Expand All @@ -135,21 +127,21 @@ def test_gui_project_loads_absolute_vs_solution():
assert stdout.find('Checking %s Release|x64...' % file2) >= 0

def test_gui_project_loads_relative_vs_solution_2():
create_gui_project_file('test.cppcheck', root_path='proj2', import_project='proj2/proj2.sln')
ret, stdout, stderr = cppcheck(['--project=test.cppcheck'])
create_gui_project_file(os.path.join(__script_dir, 'test.cppcheck'), root_path='proj2', import_project='proj2/proj2.sln')
ret, stdout, stderr = cppcheck(['--project=test.cppcheck'], cwd=__script_dir)
assert ret == 0, stdout
assert stderr == ERR_A + ERR_B
assert stderr == __ERR_A + __ERR_B

def test_gui_project_loads_relative_vs_solution_with_exclude():
create_gui_project_file('test.cppcheck', root_path='proj2', import_project='proj2/proj2.sln', exclude_paths=['b'])
ret, stdout, stderr = cppcheck(['--project=test.cppcheck'])
create_gui_project_file(os.path.join(__script_dir, 'test.cppcheck'), root_path='proj2', import_project='proj2/proj2.sln', exclude_paths=['b'])
ret, stdout, stderr = cppcheck(['--project=test.cppcheck'], cwd=__script_dir)
assert ret == 0, stdout
assert stderr == ERR_A
assert stderr == __ERR_A

def test_gui_project_loads_absolute_vs_solution_2():
create_gui_project_file('test.cppcheck',
root_path=realpath('proj2'),
import_project=realpath('proj2/proj2.sln'))
ret, stdout, stderr = cppcheck(['--project=test.cppcheck'])
create_gui_project_file(os.path.join(__script_dir, 'test.cppcheck'),
root_path=__proj_dir,
import_project=os.path.join(__proj_dir, 'proj2.sln'))
ret, stdout, stderr = cppcheck(['--project=test.cppcheck'], cwd=__script_dir)
assert ret == 0, stdout
assert stderr == ERR_A + ERR_B
assert stderr == __ERR_A + __ERR_B
Loading