From 20f3099fcd5684ecaaff00547bb5828664531249 Mon Sep 17 00:00:00 2001 From: Oleksandr Labetskyi Date: Mon, 11 Dec 2023 18:10:33 +0200 Subject: [PATCH 1/4] #12254: Hot fix --- cli/cmdlineparser.cpp | 8 ++++---- cli/cmdlineparser.h | 2 +- test/cli/test-other.py | 22 ++++++++++++++++++++++ 3 files changed, 27 insertions(+), 5 deletions(-) diff --git a/cli/cmdlineparser.cpp b/cli/cmdlineparser.cpp index df9bd320428..7c4af1b31d0 100644 --- a/cli/cmdlineparser.cpp +++ b/cli/cmdlineparser.cpp @@ -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) diff --git a/cli/cmdlineparser.h b/cli/cmdlineparser.h index 739d2b21b09..0df49349e26 100644 --- a/cli/cmdlineparser.h +++ b/cli/cmdlineparser.h @@ -111,7 +111,7 @@ class CmdLineParser { void printHelp() const; private: - static bool isCppcheckPremium(); + bool isCppcheckPremium() const; template bool parseNumberArg(const char* const arg, std::size_t offset, T& num, bool mustBePositive = false) diff --git a/test/cli/test-other.py b/test/cli/test-other.py index 0ad6573f694..8f66ca90f6b 100644 --- a/test/cli/test-other.py +++ b/test/cli/test-other.py @@ -825,3 +825,25 @@ def test_file_duplicate_2(tmpdir): '3/3 files checked 0% done' ] assert stderr == '' + + +def test_cgf_with_relative_path(tmpdir): + + test_file = os.path.join(tmpdir, 'test.c') + with open(test_file, 'wt'): + pass + + test_cfg = os.path.join(tmpdir, '../../cppcheck.cfg') + with open(test_cfg, 'wt') as f: + f.write(""" + { + "addons": [], + "productName": "Cppcheck Premium 1.2.3.4", + "about": "Cppcheck Premium 1.2.3.4" + } + """) + + args = [test_file] + + _, _, stderr = cppcheck(args) + assert stderr == '' From e11b2d29789d9a290be0ae382505fc3fb95c1c7e Mon Sep 17 00:00:00 2001 From: Oleksandr Labetskyi Date: Tue, 12 Dec 2023 12:13:51 +0200 Subject: [PATCH 2/4] #12254: remove component test, add unit test, fix format --- cli/cmdlineparser.cpp | 2 +- test/cli/test-other.py | 21 --------------------- test/testcmdlineparser.cpp | 14 ++++++++++++++ 3 files changed, 15 insertions(+), 22 deletions(-) diff --git a/cli/cmdlineparser.cpp b/cli/cmdlineparser.cpp index 7c4af1b31d0..7f5156059a9 100644 --- a/cli/cmdlineparser.cpp +++ b/cli/cmdlineparser.cpp @@ -1629,7 +1629,7 @@ void CmdLineParser::printHelp() const mLogger.printRaw(oss.str()); } -bool CmdLineParser::isCppcheckPremium() const{ +bool CmdLineParser::isCppcheckPremium() const { if (mSettings.cppcheckCfgProductName.empty()) mSettings.loadCppcheckCfg(); return startsWith(mSettings.cppcheckCfgProductName, "Cppcheck Premium"); diff --git a/test/cli/test-other.py b/test/cli/test-other.py index 8f66ca90f6b..101f4cb880d 100644 --- a/test/cli/test-other.py +++ b/test/cli/test-other.py @@ -826,24 +826,3 @@ def test_file_duplicate_2(tmpdir): ] assert stderr == '' - -def test_cgf_with_relative_path(tmpdir): - - test_file = os.path.join(tmpdir, 'test.c') - with open(test_file, 'wt'): - pass - - test_cfg = os.path.join(tmpdir, '../../cppcheck.cfg') - with open(test_cfg, 'wt') as f: - f.write(""" - { - "addons": [], - "productName": "Cppcheck Premium 1.2.3.4", - "about": "Cppcheck Premium 1.2.3.4" - } - """) - - args = [test_file] - - _, _, stderr = cppcheck(args) - assert stderr == '' diff --git a/test/testcmdlineparser.cpp b/test/testcmdlineparser.cpp index c4b6ed38ff2..21f23ba9b35 100644 --- a/test/testcmdlineparser.cpp +++ b/test/testcmdlineparser.cpp @@ -124,6 +124,7 @@ class TestCmdlineParser : public TestFixture { TEST_CASE(helplongExclusive); TEST_CASE(version); TEST_CASE(versionWithCfg); + TEST_CASE(versionSwitchWithCfg); TEST_CASE(versionExclusive); TEST_CASE(versionWithInvalidCfg); TEST_CASE(onefile); @@ -421,6 +422,19 @@ class TestCmdlineParser : public TestFixture { (void)logger->str(); //ASSERT_EQUALS("The Product\n", logger->str()); // TODO: include version? } + void versionSwitchWithCfg() { + REDIRECT; + ScopedFile file(Path::join(Path::getPathFromFilename(Path::getCurrentExecutablePath("")), "cppcheck.cfg"), + "{\n" + "\"productName\": \"Cppcheck Premium 1.2.3.4\"" + "}\n"); + const char * const argv[] = {"cppcheck", "--premium=misra-c++-2008", "--version", "file.cpp"}; + // --premium=misra-c++-2008 should be recognized from cfg + ASSERT_EQUALS(CmdLineParser::Result::Exit, parser->parseFromArgs(4, argv)); + // TODO: somehow the config is not loaded on some systems + ASSERT_EQUALS("Cppcheck Premium 1.2.3.4\n", logger->str()); + } + // TODO: test --version with extraVersion void versionExclusive() { From cb8f21c86bdbe599f051bb0cc2afae40b2f93aa1 Mon Sep 17 00:00:00 2001 From: Oleksandr Labetskyi Date: Tue, 12 Dec 2023 15:19:23 +0200 Subject: [PATCH 3/4] #12254: remove unit test, add component test --- test/cli/test-other.py | 38 +++++++++++++++++++++++++++++++++++++- test/cli/testutils.py | 4 ++-- test/testcmdlineparser.cpp | 14 -------------- 3 files changed, 39 insertions(+), 17 deletions(-) diff --git a/test/cli/test-other.py b/test/cli/test-other.py index 101f4cb880d..d638336ef65 100644 --- a/test/cli/test-other.py +++ b/test/cli/test-other.py @@ -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): @@ -826,3 +827,38 @@ def test_file_duplicate_2(tmpdir): ] 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", + "about": "Cppcheck Premium 1.2.3.4" + } + """.replace('NAME', product_name)) + + if os.path.isfile('cppcheck') : + os.chdir('test/cli') + + 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 == '' diff --git a/test/cli/testutils.py b/test/cli/testutils.py index 589a823cbd3..42ccd4867b8 100644 --- a/test/cli/testutils.py +++ b/test/cli/testutils.py @@ -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__)) @@ -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)) diff --git a/test/testcmdlineparser.cpp b/test/testcmdlineparser.cpp index 21f23ba9b35..c4b6ed38ff2 100644 --- a/test/testcmdlineparser.cpp +++ b/test/testcmdlineparser.cpp @@ -124,7 +124,6 @@ class TestCmdlineParser : public TestFixture { TEST_CASE(helplongExclusive); TEST_CASE(version); TEST_CASE(versionWithCfg); - TEST_CASE(versionSwitchWithCfg); TEST_CASE(versionExclusive); TEST_CASE(versionWithInvalidCfg); TEST_CASE(onefile); @@ -422,19 +421,6 @@ class TestCmdlineParser : public TestFixture { (void)logger->str(); //ASSERT_EQUALS("The Product\n", logger->str()); // TODO: include version? } - void versionSwitchWithCfg() { - REDIRECT; - ScopedFile file(Path::join(Path::getPathFromFilename(Path::getCurrentExecutablePath("")), "cppcheck.cfg"), - "{\n" - "\"productName\": \"Cppcheck Premium 1.2.3.4\"" - "}\n"); - const char * const argv[] = {"cppcheck", "--premium=misra-c++-2008", "--version", "file.cpp"}; - // --premium=misra-c++-2008 should be recognized from cfg - ASSERT_EQUALS(CmdLineParser::Result::Exit, parser->parseFromArgs(4, argv)); - // TODO: somehow the config is not loaded on some systems - ASSERT_EQUALS("Cppcheck Premium 1.2.3.4\n", logger->str()); - } - // TODO: test --version with extraVersion void versionExclusive() { From 66ec2bde3906f884b4cd527580469c09d57b5179 Mon Sep 17 00:00:00 2001 From: Oleksandr Labetskyi Date: Tue, 12 Dec 2023 16:58:50 +0200 Subject: [PATCH 4/4] #12254: ci fix --- test/cli/test-other.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/test/cli/test-other.py b/test/cli/test-other.py index d638336ef65..f8cf63096c2 100644 --- a/test/cli/test-other.py +++ b/test/cli/test-other.py @@ -828,7 +828,7 @@ def test_file_duplicate_2(tmpdir): assert stderr == '' -def test_premium_with_relative_path(tmpdir): +def test_premium_with_relative_path(tmpdir): #12254 test_file = os.path.join(tmpdir, 'test.c') @@ -838,7 +838,7 @@ def test_premium_with_relative_path(tmpdir): product_name = 'Cppcheck Premium ' + str(time.time()) test_cfg = lookup_cppcheck_exe() - if(test_cfg.endswith('.exe')) : + if(test_cfg.endswith('.exe')): test_cfg = test_cfg[:-4] test_cfg += '.cfg' with open(test_cfg, 'wt') as f: @@ -850,7 +850,7 @@ def test_premium_with_relative_path(tmpdir): } """.replace('NAME', product_name)) - if os.path.isfile('cppcheck') : + if os.path.isfile('cppcheck'): os.chdir('test/cli') args = ['--premium=misra-c++-2008', test_file] @@ -862,3 +862,5 @@ def test_premium_with_relative_path(tmpdir): _, stdout, stderr = cppcheck(['--version']) assert stdout == product_name + '\n' assert stderr == '' + + os.remove(test_cfg)