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

#12254: cppcheck.cfg can't be loaded from relative paths anymore #5753

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 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
8 changes: 4 additions & 4 deletions cli/cmdlineparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1629,10 +1629,10 @@ void CmdLineParser::printHelp() const
mLogger.printRaw(oss.str());
}

bool CmdLineParser::isCppcheckPremium() {
Settings settings;
settings.loadCppcheckCfg(); // TODO: how to handle errors?
return startsWith(settings.cppcheckCfgProductName, "Cppcheck Premium");
bool CmdLineParser::isCppcheckPremium() const {
if (mSettings.cppcheckCfgProductName.empty())
mSettings.loadCppcheckCfg();
return startsWith(mSettings.cppcheckCfgProductName, "Cppcheck Premium");
}

bool CmdLineParser::tryLoadLibrary(Library& destination, const std::string& basepath, const char* filename)
Expand Down
2 changes: 1 addition & 1 deletion cli/cmdlineparser.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ class CmdLineParser {
void printHelp() const;

private:
static bool isCppcheckPremium();
bool isCppcheckPremium() const;

template<typename T>
bool parseNumberArg(const char* const arg, std::size_t offset, T& num, bool mustBePositive = false)
Expand Down
39 changes: 38 additions & 1 deletion test/cli/test-other.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
import os
import sys
import pytest
import time

from testutils import cppcheck, assert_cppcheck
from testutils import cppcheck, assert_cppcheck, lookup_cppcheck_exe


def __test_missing_include(tmpdir, use_j):
Expand Down Expand Up @@ -825,3 +826,39 @@ def test_file_duplicate_2(tmpdir):
'3/3 files checked 0% done'
]
assert stderr == ''


def test_premium_with_relative_path(tmpdir):


test_file = os.path.join(tmpdir, 'test.c')
with open(test_file, 'wt'):
pass

product_name = 'Cppcheck Premium ' + str(time.time())

test_cfg = lookup_cppcheck_exe()
if(test_cfg.endswith('.exe')) :
test_cfg = test_cfg[:-4]
test_cfg += '.cfg'
with open(test_cfg, 'wt') as f:
f.write("""
{
"addons": [],
"productName": "NAME",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use {} placeholder and call format() on the string instead. You can also get rid of the product_name variable if you do not use it in any other way and just fold it in here.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's also used for an assert on line 863.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's also used for an assert on line 863.

I knew I overlooked it somehow.

Still you should use format() instead of replace().

"about": "Cppcheck Premium 1.2.3.4"
}
""".replace('NAME', product_name))

if os.path.isfile('cppcheck') :
os.chdir('test/cli')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just no. If you need to change the directory for running a process please pass the cwd parameter to the call. cppcheck() might already expose that. If it doesn't please pass that through as a an additional positional parameter.


args = ['--premium=misra-c++-2008', test_file]

exitcode, _, stderr = cppcheck(args)
assert exitcode == 0
assert stderr == ''

_, stdout, stderr = cppcheck(['--version'])
assert stdout == product_name + '\n'
assert stderr == ''
danmar marked this conversation as resolved.
Show resolved Hide resolved
4 changes: 2 additions & 2 deletions test/cli/testutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def create_gui_project_file(project_file, root_path=None, import_project=None, p
f.close()


def __lookup_cppcheck_exe():
def lookup_cppcheck_exe():
# path the script is located in
script_path = os.path.dirname(os.path.realpath(__file__))

Expand All @@ -63,7 +63,7 @@ def __lookup_cppcheck_exe():

# Run Cppcheck with args
def cppcheck(args, env=None):
exe = __lookup_cppcheck_exe()
exe = lookup_cppcheck_exe()
assert exe is not None, 'no cppcheck binary found'

logging.info(exe + ' ' + ' '.join(args))
Expand Down
Loading