2010-09-06 21:31:06 +02:00
/*
* Cppcheck - A tool for static C / C + + code analysis
2023-01-28 10:16:34 +01:00
* Copyright ( C ) 2007 - 2023 Cppcheck team .
2010-09-06 21:31:06 +02:00
*
* This program is free software : you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation , either version 3 of the License , or
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
* along with this program . If not , see < http : //www.gnu.org/licenses/>.
*/
2023-10-16 12:57:49 +02:00
# include "cmdlinelogger.h"
2010-09-06 21:31:06 +02:00
# include "cmdlineparser.h"
2023-04-30 07:33:19 +02:00
# include "config.h"
2022-08-20 20:54:31 +02:00
# include "cppcheckexecutor.h"
2022-01-27 19:03:20 +01:00
# include "errortypes.h"
2023-08-14 11:37:59 +02:00
# include "helpers.h"
2023-12-01 16:34:14 +01:00
# include "path.h"
2017-05-27 04:33:47 +02:00
# include "platform.h"
2010-09-26 05:19:30 +02:00
# include "redirect.h"
2017-05-27 04:33:47 +02:00
# include "settings.h"
# include "standards.h"
# include "suppressions.h"
2023-01-27 08:18:32 +01:00
# include "fixture.h"
2013-08-15 11:09:40 +02:00
# include "timer.h"
2023-09-08 19:30:25 +02:00
# include "utils.h"
2010-09-06 21:31:06 +02:00
2023-03-02 21:50:14 +01:00
# include <cstdint>
2022-09-16 07:15:49 +02:00
# include <cstdio>
2017-05-27 04:33:47 +02:00
# include <list>
2023-10-16 12:57:49 +02:00
# include <memory>
2017-05-27 04:33:47 +02:00
# include <set>
2023-10-16 12:57:49 +02:00
# include <stdexcept>
2017-05-27 04:33:47 +02:00
# include <string>
2023-10-16 12:57:49 +02:00
# include <unordered_set>
2017-05-27 04:33:47 +02:00
# include <vector>
2015-03-11 22:26:33 +01:00
2011-10-13 20:53:06 +02:00
class TestCmdlineParser : public TestFixture {
2010-09-06 21:31:06 +02:00
public :
2023-05-02 11:48:24 +02:00
TestCmdlineParser ( ) : TestFixture ( " TestCmdlineParser " )
2023-09-11 11:08:23 +02:00
{ }
2010-09-06 21:31:06 +02:00
private :
2023-09-25 13:43:18 +02:00
class CmdLineLoggerTest : public CmdLineLogger
{
public :
CmdLineLoggerTest ( ) = default ;
void printMessage ( const std : : string & message ) override
{
2023-09-26 21:38:15 +02:00
printInternal ( " cppcheck: " + message + ' \n ' ) ;
2023-09-25 13:43:18 +02:00
}
void printError ( const std : : string & message ) override
{
printMessage ( " error: " + message ) ;
}
2023-09-26 21:38:15 +02:00
2023-10-09 22:22:39 +02:00
void printRaw ( const std : : string & message ) override
{
printInternal ( message + ' \n ' ) ;
}
2023-09-26 21:38:15 +02:00
std : : string str ( )
{
std : : string s ;
std : : swap ( buf , s ) ;
return s ;
}
void destroy ( )
{
if ( ! buf . empty ( ) )
2023-11-25 21:12:24 +01:00
throw std : : runtime_error ( " unconsumed messages: " + buf ) ;
2023-09-26 21:38:15 +02:00
}
private :
void printInternal ( const std : : string & message )
{
buf + = message ;
}
std : : string buf ;
2023-09-25 13:43:18 +02:00
} ;
2023-09-26 21:38:15 +02:00
std : : unique_ptr < CmdLineLoggerTest > logger ;
2023-08-12 21:43:12 +02:00
std : : unique_ptr < Settings > settings ;
std : : unique_ptr < CmdLineParser > parser ;
void prepareTestInternal ( ) override {
2023-09-26 21:38:15 +02:00
logger . reset ( new CmdLineLoggerTest ( ) ) ;
2023-08-12 21:43:12 +02:00
settings . reset ( new Settings ( ) ) ;
2023-09-26 21:38:15 +02:00
parser . reset ( new CmdLineParser ( * logger , * settings , settings - > nomsg , settings - > nofail ) ) ;
}
void teardownTestInternal ( ) override {
logger - > destroy ( ) ;
2023-08-12 21:43:12 +02:00
}
2010-09-06 21:31:06 +02:00
2023-11-27 12:06:03 +01:00
// add overload so the enums can be compared without a cast
template < typename T , typename U >
bool assertEquals ( const char * const filename , const unsigned int linenr , const T & expected , const U & actual ) const
{
return TestFixture : : assertEquals ( filename , linenr , expected , actual ) ;
}
bool assertEquals ( const char * const filename , const unsigned int linenr , const CmdLineParser : : Result & expected , const CmdLineParser : : Result & actual ) const
{
return TestFixture : : assertEquals ( filename , linenr , static_cast < int > ( expected ) , static_cast < int > ( actual ) ) ;
}
2022-02-10 23:02:24 +01:00
void run ( ) override {
2010-09-06 21:31:06 +02:00
TEST_CASE ( nooptions ) ;
TEST_CASE ( helpshort ) ;
2023-12-01 14:19:47 +01:00
TEST_CASE ( helpshortExclusive ) ;
2010-09-06 21:31:06 +02:00
TEST_CASE ( helplong ) ;
2023-12-01 14:19:47 +01:00
TEST_CASE ( helplongExclusive ) ;
2023-11-25 21:12:24 +01:00
TEST_CASE ( version ) ;
TEST_CASE ( versionWithCfg ) ;
2023-12-01 14:19:47 +01:00
TEST_CASE ( versionExclusive ) ;
2023-12-01 16:34:14 +01:00
TEST_CASE ( versionWithInvalidCfg ) ;
2010-09-06 22:16:52 +02:00
TEST_CASE ( onefile ) ;
TEST_CASE ( onepath ) ;
TEST_CASE ( optionwithoutfile ) ;
TEST_CASE ( verboseshort ) ;
TEST_CASE ( verboselong ) ;
2020-05-30 11:23:22 +02:00
TEST_CASE ( debugSimplified ) ;
2010-09-06 22:16:52 +02:00
TEST_CASE ( debugwarnings ) ;
TEST_CASE ( forceshort ) ;
TEST_CASE ( forcelong ) ;
2023-08-12 21:43:12 +02:00
TEST_CASE ( relativePaths1 ) ;
TEST_CASE ( relativePaths2 ) ;
TEST_CASE ( relativePaths3 ) ;
TEST_CASE ( relativePaths4 ) ;
2010-09-06 22:16:52 +02:00
TEST_CASE ( quietshort ) ;
TEST_CASE ( quietlong ) ;
2011-01-17 20:03:22 +01:00
TEST_CASE ( defines_noarg ) ;
2011-01-17 20:19:27 +01:00
TEST_CASE ( defines_noarg2 ) ;
TEST_CASE ( defines_noarg3 ) ;
2010-09-08 17:02:22 +02:00
TEST_CASE ( defines ) ;
TEST_CASE ( defines2 ) ;
2010-09-08 17:42:28 +02:00
TEST_CASE ( defines3 ) ;
2012-09-14 06:58:14 +02:00
TEST_CASE ( defines4 ) ;
2023-08-12 21:43:12 +02:00
TEST_CASE ( enforceLanguage1 ) ;
TEST_CASE ( enforceLanguage2 ) ;
TEST_CASE ( enforceLanguage3 ) ;
TEST_CASE ( enforceLanguage4 ) ;
TEST_CASE ( enforceLanguage5 ) ;
TEST_CASE ( enforceLanguage6 ) ;
TEST_CASE ( enforceLanguage7 ) ;
2010-09-08 17:02:22 +02:00
TEST_CASE ( includesnopath ) ;
TEST_CASE ( includes ) ;
2011-01-11 20:03:18 +01:00
TEST_CASE ( includesslash ) ;
TEST_CASE ( includesbackslash ) ;
TEST_CASE ( includesnospace ) ;
2010-09-08 17:02:22 +02:00
TEST_CASE ( includes2 ) ;
2011-10-22 21:24:23 +02:00
TEST_CASE ( includesFile ) ;
2022-09-17 18:50:25 +02:00
TEST_CASE ( includesFileNoFile ) ;
2018-09-04 16:37:43 +02:00
TEST_CASE ( configExcludesFile ) ;
2022-09-17 18:50:25 +02:00
TEST_CASE ( configExcludesFileNoFile ) ;
2010-12-04 13:13:44 +01:00
TEST_CASE ( enabledAll ) ;
TEST_CASE ( enabledStyle ) ;
2011-09-04 20:42:35 +02:00
TEST_CASE ( enabledPerformance ) ;
TEST_CASE ( enabledPortability ) ;
2023-03-09 20:04:20 +01:00
TEST_CASE ( enabledInformation ) ;
2010-12-04 13:13:44 +01:00
TEST_CASE ( enabledUnusedFunction ) ;
TEST_CASE ( enabledMissingInclude ) ;
2014-05-18 19:48:43 +02:00
# ifdef CHECK_INTERNAL
2011-10-28 22:39:46 +02:00
TEST_CASE ( enabledInternal ) ;
2011-10-29 12:47:12 +02:00
# endif
2013-04-01 13:15:05 +02:00
TEST_CASE ( enabledMultiple ) ;
2023-01-21 10:39:44 +01:00
TEST_CASE ( enabledInvalid ) ;
TEST_CASE ( enabledError ) ;
TEST_CASE ( enabledEmpty ) ;
TEST_CASE ( disableAll ) ;
TEST_CASE ( disableMultiple ) ;
TEST_CASE ( disableStylePartial ) ;
2023-03-09 20:04:20 +01:00
TEST_CASE ( disableInformationPartial ) ;
TEST_CASE ( disableInformationPartial2 ) ;
2023-01-21 10:39:44 +01:00
TEST_CASE ( disableInvalid ) ;
TEST_CASE ( disableError ) ;
TEST_CASE ( disableEmpty ) ;
2013-08-15 11:09:40 +02:00
TEST_CASE ( inconclusive ) ;
2010-12-04 13:13:44 +01:00
TEST_CASE ( errorExitcode ) ;
TEST_CASE ( errorExitcodeMissing ) ;
TEST_CASE ( errorExitcodeStr ) ;
2022-09-17 18:50:25 +02:00
TEST_CASE ( exitcodeSuppressionsOld ) ;
2011-01-29 18:04:52 +01:00
TEST_CASE ( exitcodeSuppressions ) ;
TEST_CASE ( exitcodeSuppressionsNoFile ) ;
2022-12-19 22:28:36 +01:00
TEST_CASE ( fileList ) ;
TEST_CASE ( fileListNoFile ) ;
2011-09-30 07:35:23 +02:00
// TEST_CASE(fileListStdin); // Disabled since hangs the test run
2022-09-17 18:50:25 +02:00
TEST_CASE ( fileListInvalid ) ;
2010-12-04 13:13:44 +01:00
TEST_CASE ( inlineSuppr ) ;
TEST_CASE ( jobs ) ;
2023-04-08 22:29:09 +02:00
TEST_CASE ( jobs2 ) ;
2010-12-04 13:13:44 +01:00
TEST_CASE ( jobsMissingCount ) ;
TEST_CASE ( jobsInvalid ) ;
2023-08-14 11:55:32 +02:00
TEST_CASE ( jobsNoJobs ) ;
2023-04-08 22:29:09 +02:00
TEST_CASE ( jobsTooBig ) ;
2011-10-05 07:37:43 +02:00
TEST_CASE ( maxConfigs ) ;
TEST_CASE ( maxConfigsMissingCount ) ;
TEST_CASE ( maxConfigsInvalid ) ;
TEST_CASE ( maxConfigsTooSmall ) ;
2023-08-23 11:20:20 +02:00
TEST_CASE ( reportProgress1 ) ;
TEST_CASE ( reportProgress2 ) ;
TEST_CASE ( reportProgress3 ) ;
TEST_CASE ( reportProgress4 ) ;
TEST_CASE ( reportProgress5 ) ;
2011-10-06 08:10:51 +02:00
TEST_CASE ( stdc99 ) ;
2011-10-16 12:54:58 +02:00
TEST_CASE ( stdcpp11 ) ;
2023-08-12 21:43:12 +02:00
TEST_CASE ( stdunknown1 ) ;
TEST_CASE ( stdunknown2 ) ;
2022-09-17 18:50:25 +02:00
TEST_CASE ( platformWin64 ) ;
TEST_CASE ( platformWin32A ) ;
TEST_CASE ( platformWin32W ) ;
TEST_CASE ( platformUnix32 ) ;
2023-01-26 22:05:40 +01:00
TEST_CASE ( platformUnix32Unsigned ) ;
2022-09-17 18:50:25 +02:00
TEST_CASE ( platformUnix64 ) ;
2023-01-26 22:05:40 +01:00
TEST_CASE ( platformUnix64Unsigned ) ;
2022-09-17 18:50:25 +02:00
TEST_CASE ( platformNative ) ;
TEST_CASE ( platformUnspecified ) ;
2023-01-26 22:05:40 +01:00
TEST_CASE ( platformPlatformFile ) ;
2022-09-17 18:50:25 +02:00
TEST_CASE ( platformUnknown ) ;
2020-11-13 15:52:24 +01:00
TEST_CASE ( plistEmpty ) ;
TEST_CASE ( plistDoesNotExist ) ;
2022-09-17 18:50:25 +02:00
TEST_CASE ( suppressionsOld ) ;
2011-07-29 21:00:24 +02:00
TEST_CASE ( suppressions ) ;
2023-08-12 21:43:12 +02:00
TEST_CASE ( suppressionsNoFile1 ) ;
TEST_CASE ( suppressionsNoFile2 ) ;
TEST_CASE ( suppressionsNoFile3 ) ;
2011-07-29 21:00:24 +02:00
TEST_CASE ( suppressionSingle ) ;
TEST_CASE ( suppressionSingleFile ) ;
2011-09-13 16:55:00 +02:00
TEST_CASE ( suppressionTwo ) ;
TEST_CASE ( suppressionTwoSeparate ) ;
2010-12-04 13:13:44 +01:00
TEST_CASE ( templates ) ;
TEST_CASE ( templatesGcc ) ;
TEST_CASE ( templatesVs ) ;
2011-10-29 23:26:35 +02:00
TEST_CASE ( templatesEdit ) ;
2023-04-08 22:19:52 +02:00
TEST_CASE ( templatesCppcheck1 ) ;
TEST_CASE ( templatesDaca2 ) ;
TEST_CASE ( templatesSelfcheck ) ;
TEST_CASE ( templatesNoPlaceholder ) ;
TEST_CASE ( templateFormatInvalid ) ;
TEST_CASE ( templateFormatEmpty ) ;
TEST_CASE ( templateLocationInvalid ) ;
TEST_CASE ( templateLocationEmpty ) ;
2010-12-04 13:13:44 +01:00
TEST_CASE ( xml ) ;
2011-01-27 13:25:10 +01:00
TEST_CASE ( xmlver2 ) ;
TEST_CASE ( xmlver2both ) ;
TEST_CASE ( xmlver2both2 ) ;
2011-02-02 12:33:57 +01:00
TEST_CASE ( xmlverunknown ) ;
TEST_CASE ( xmlverinvalid ) ;
2013-08-15 11:09:40 +02:00
TEST_CASE ( doc ) ;
2023-12-01 14:19:47 +01:00
TEST_CASE ( docExclusive ) ;
2023-10-05 19:04:06 +02:00
TEST_CASE ( showtimeFile ) ;
TEST_CASE ( showtimeFileTotal ) ;
TEST_CASE ( showtimeTop5 ) ;
TEST_CASE ( showtimeTop5File ) ;
TEST_CASE ( showtimeTop5Summary ) ;
TEST_CASE ( showtimeNone ) ;
TEST_CASE ( showtimeEmpty ) ;
TEST_CASE ( showtimeInvalid ) ;
2023-11-25 21:12:24 +01:00
TEST_CASE ( errorlist ) ;
2023-12-01 16:34:14 +01:00
TEST_CASE ( errorlistWithCfg ) ;
2023-12-01 14:19:47 +01:00
TEST_CASE ( errorlistExclusive ) ;
2023-12-01 16:34:14 +01:00
TEST_CASE ( errorlistWithInvalidCfg ) ;
2011-07-29 21:00:24 +02:00
TEST_CASE ( ignorepathsnopath ) ;
2023-10-21 09:12:59 +02:00
# if defined(USE_WINDOWS_SEH) || defined(USE_UNIX_SIGNAL_HANDLING)
2022-08-20 20:54:31 +02:00
TEST_CASE ( exceptionhandling ) ;
TEST_CASE ( exceptionhandling2 ) ;
TEST_CASE ( exceptionhandling3 ) ;
TEST_CASE ( exceptionhandlingInvalid ) ;
TEST_CASE ( exceptionhandlingInvalid2 ) ;
2023-10-21 09:12:59 +02:00
# else
TEST_CASE ( exceptionhandlingNotSupported ) ;
TEST_CASE ( exceptionhandlingNotSupported2 ) ;
# endif
2022-08-20 20:54:31 +02:00
TEST_CASE ( clang ) ;
TEST_CASE ( clang2 ) ;
TEST_CASE ( clangInvalid ) ;
2022-12-20 20:51:08 +01:00
TEST_CASE ( valueFlowMaxIterations ) ;
TEST_CASE ( valueFlowMaxIterations2 ) ;
TEST_CASE ( valueFlowMaxIterationsInvalid ) ;
TEST_CASE ( valueFlowMaxIterationsInvalid2 ) ;
TEST_CASE ( valueFlowMaxIterationsInvalid3 ) ;
2023-04-08 22:29:09 +02:00
TEST_CASE ( checksMaxTime ) ;
TEST_CASE ( checksMaxTime2 ) ;
TEST_CASE ( checksMaxTimeInvalid ) ;
# ifdef THREADING_MODEL_FORK
TEST_CASE ( loadAverage ) ;
TEST_CASE ( loadAverage2 ) ;
TEST_CASE ( loadAverageInvalid ) ;
2023-10-21 09:12:59 +02:00
# else
TEST_CASE ( loadAverageNotSupported ) ;
2023-04-08 22:29:09 +02:00
# endif
TEST_CASE ( maxCtuDepth ) ;
TEST_CASE ( maxCtuDepthInvalid ) ;
TEST_CASE ( performanceValueflowMaxTime ) ;
TEST_CASE ( performanceValueflowMaxTimeInvalid ) ;
TEST_CASE ( performanceValueFlowMaxIfCount ) ;
TEST_CASE ( performanceValueFlowMaxIfCountInvalid ) ;
TEST_CASE ( templateMaxTime ) ;
TEST_CASE ( templateMaxTimeInvalid ) ;
TEST_CASE ( templateMaxTimeInvalid2 ) ;
TEST_CASE ( typedefMaxTime ) ;
TEST_CASE ( typedefMaxTimeInvalid ) ;
TEST_CASE ( typedefMaxTimeInvalid2 ) ;
TEST_CASE ( templateMaxTime ) ;
TEST_CASE ( templateMaxTime ) ;
2023-10-05 21:36:44 +02:00
TEST_CASE ( project ) ;
TEST_CASE ( projectMultiple ) ;
2023-10-08 09:07:15 +02:00
TEST_CASE ( projectAndSource ) ;
2023-10-05 21:36:44 +02:00
TEST_CASE ( projectEmpty ) ;
TEST_CASE ( projectMissing ) ;
2023-10-08 09:07:15 +02:00
TEST_CASE ( projectNoPaths ) ;
2023-10-08 21:28:57 +02:00
TEST_CASE ( addon ) ;
2023-11-25 21:12:24 +01:00
TEST_CASE ( addonMissing ) ;
2023-10-21 09:12:59 +02:00
# ifdef HAVE_RULES
TEST_CASE ( rule ) ;
# else
TEST_CASE ( ruleNotSupported ) ;
# endif
# ifdef HAVE_RULES
TEST_CASE ( ruleFile ) ;
TEST_CASE ( ruleFileEmpty ) ;
TEST_CASE ( ruleFileMissing ) ;
TEST_CASE ( ruleFileInvalid ) ;
# else
TEST_CASE ( ruleFileNotSupported ) ;
# endif
2023-11-08 09:28:33 +01:00
TEST_CASE ( signedChar ) ;
TEST_CASE ( signedChar2 ) ;
TEST_CASE ( unsignedChar ) ;
TEST_CASE ( unsignedChar2 ) ;
TEST_CASE ( signedCharUnsignedChar ) ;
2023-11-25 21:12:24 +01:00
TEST_CASE ( library ) ;
TEST_CASE ( libraryMissing ) ;
2011-08-06 18:18:20 +02:00
2023-01-01 15:32:37 +01:00
TEST_CASE ( ignorepaths1 ) ;
TEST_CASE ( ignorepaths2 ) ;
TEST_CASE ( ignorepaths3 ) ;
TEST_CASE ( ignorepaths4 ) ;
TEST_CASE ( ignorefilepaths1 ) ;
TEST_CASE ( ignorefilepaths2 ) ;
2011-08-06 18:18:20 +02:00
2011-07-29 21:00:24 +02:00
TEST_CASE ( checkconfig ) ;
2010-12-04 13:13:44 +01:00
TEST_CASE ( unknownParam ) ;
2011-11-30 20:24:01 +01:00
TEST_CASE ( undefs_noarg ) ;
TEST_CASE ( undefs_noarg2 ) ;
TEST_CASE ( undefs_noarg3 ) ;
TEST_CASE ( undefs ) ;
TEST_CASE ( undefs2 ) ;
2023-08-04 17:38:43 +02:00
TEST_CASE ( cppcheckBuildDirExistent ) ;
TEST_CASE ( cppcheckBuildDirNonExistent ) ;
TEST_CASE ( cppcheckBuildDirEmpty ) ;
2023-12-01 16:34:14 +01:00
TEST_CASE ( invalidCppcheckCfg ) ;
2010-09-06 21:31:06 +02:00
}
2014-11-20 14:20:09 +01:00
void nooptions ( ) {
2010-09-06 21:31:06 +02:00
REDIRECT ;
2018-09-24 15:08:16 +02:00
const char * const argv [ ] = { " cppcheck " } ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Exit , parser - > parseFromArgs ( 1 , argv ) ) ;
2023-10-09 22:22:39 +02:00
ASSERT ( startsWith ( logger - > str ( ) , " Cppcheck - A tool for static C/C++ code analysis " ) ) ;
2010-09-06 21:31:06 +02:00
}
2014-11-20 14:20:09 +01:00
void helpshort ( ) {
2010-09-06 21:31:06 +02:00
REDIRECT ;
2018-09-24 15:08:16 +02:00
const char * const argv [ ] = { " cppcheck " , " -h " } ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Exit , parser - > parseFromArgs ( 2 , argv ) ) ;
2023-10-09 22:22:39 +02:00
ASSERT ( startsWith ( logger - > str ( ) , " Cppcheck - A tool for static C/C++ code analysis " ) ) ;
2010-09-06 21:31:06 +02:00
}
2023-12-01 14:19:47 +01:00
void helpshortExclusive ( ) {
REDIRECT ;
const char * const argv [ ] = { " cppcheck " , " --library=missing " , " -h " } ;
ASSERT_EQUALS ( CmdLineParser : : Result : : Exit , parser - > parseFromArgs ( 3 , argv ) ) ;
ASSERT ( startsWith ( logger - > str ( ) , " Cppcheck - A tool for static C/C++ code analysis " ) ) ;
}
2014-11-20 14:20:09 +01:00
void helplong ( ) {
2010-09-06 21:31:06 +02:00
REDIRECT ;
2018-09-24 15:08:16 +02:00
const char * const argv [ ] = { " cppcheck " , " --help " } ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Exit , parser - > parseFromArgs ( 2 , argv ) ) ;
2023-10-09 22:22:39 +02:00
ASSERT ( startsWith ( logger - > str ( ) , " Cppcheck - A tool for static C/C++ code analysis " ) ) ;
2010-09-06 21:31:06 +02:00
}
2023-12-01 14:19:47 +01:00
void helplongExclusive ( ) {
REDIRECT ;
const char * const argv [ ] = { " cppcheck " , " --library=missing " , " --help " } ;
ASSERT_EQUALS ( CmdLineParser : : Result : : Exit , parser - > parseFromArgs ( 3 , argv ) ) ;
ASSERT ( startsWith ( logger - > str ( ) , " Cppcheck - A tool for static C/C++ code analysis " ) ) ;
}
2023-11-25 21:12:24 +01:00
void version ( ) {
2010-09-06 21:31:06 +02:00
REDIRECT ;
2018-09-24 15:08:16 +02:00
const char * const argv [ ] = { " cppcheck " , " --version " } ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Exit , parser - > parseFromArgs ( 2 , argv ) ) ;
2023-11-25 21:12:24 +01:00
ASSERT_EQUALS ( " Cppcheck 2.13 dev \n " , logger - > str ( ) ) ;
2010-09-06 21:31:06 +02:00
}
2010-09-06 22:16:52 +02:00
2023-11-25 21:12:24 +01:00
void versionWithCfg ( ) {
REDIRECT ;
2023-12-01 16:34:14 +01:00
ScopedFile file ( Path : : join ( Path : : getPathFromFilename ( Path : : getCurrentExecutablePath ( " " ) ) , " cppcheck.cfg " ) ,
2023-11-25 21:12:24 +01:00
" { \n "
" \" productName \" : \" The Product \" "
" } \n " ) ;
const char * const argv [ ] = { " cppcheck " , " --version " } ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Exit , parser - > parseFromArgs ( 2 , argv ) ) ;
2023-11-25 21:12:24 +01:00
// TODO: somehow the config is not loaded on some systems
( void ) logger - > str ( ) ; //ASSERT_EQUALS("The Product\n", logger->str()); // TODO: include version?
}
2023-12-01 14:19:47 +01:00
// TODO: test --version with extraVersion
void versionExclusive ( ) {
REDIRECT ;
const char * const argv [ ] = { " cppcheck " , " --library=missing " , " --version " } ;
ASSERT_EQUALS ( CmdLineParser : : Result : : Exit , parser - > parseFromArgs ( 3 , argv ) ) ;
ASSERT_EQUALS ( " Cppcheck 2.13 dev \n " , logger - > str ( ) ) ;
}
2023-11-25 21:12:24 +01:00
2023-12-01 16:34:14 +01:00
void versionWithInvalidCfg ( ) {
REDIRECT ;
ScopedFile file ( Path : : join ( Path : : getPathFromFilename ( Path : : getCurrentExecutablePath ( " " ) ) , " cppcheck.cfg " ) ,
" { \n " ) ;
const char * const argv [ ] = { " cppcheck " , " --version " } ;
ASSERT_EQUALS ( CmdLineParser : : Result : : Fail , parser - > parseFromArgs ( 2 , argv ) ) ;
2023-12-05 20:39:26 +01:00
ASSERT_EQUALS ( " cppcheck: error: could not load cppcheck.cfg - not a valid JSON - syntax error at line 2 near: \n " , logger - > str ( ) ) ;
2023-12-01 16:34:14 +01:00
}
2014-11-20 14:20:09 +01:00
void onefile ( ) {
2010-09-06 22:16:52 +02:00
REDIRECT ;
2018-09-24 15:08:16 +02:00
const char * const argv [ ] = { " cppcheck " , " file.cpp " } ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Success , parser - > parseFromArgs ( 2 , argv ) ) ;
2023-08-12 21:43:12 +02:00
ASSERT_EQUALS ( 1 , ( int ) parser - > getPathNames ( ) . size ( ) ) ;
ASSERT_EQUALS ( " file.cpp " , parser - > getPathNames ( ) . at ( 0 ) ) ;
2010-09-06 22:16:52 +02:00
}
2014-11-20 14:20:09 +01:00
void onepath ( ) {
2010-09-06 22:16:52 +02:00
REDIRECT ;
2018-09-24 15:08:16 +02:00
const char * const argv [ ] = { " cppcheck " , " src " } ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Success , parser - > parseFromArgs ( 2 , argv ) ) ;
2023-08-12 21:43:12 +02:00
ASSERT_EQUALS ( 1 , ( int ) parser - > getPathNames ( ) . size ( ) ) ;
ASSERT_EQUALS ( " src " , parser - > getPathNames ( ) . at ( 0 ) ) ;
2010-09-06 22:16:52 +02:00
}
2014-11-20 14:20:09 +01:00
void optionwithoutfile ( ) {
2010-09-06 22:16:52 +02:00
REDIRECT ;
2018-09-24 15:08:16 +02:00
const char * const argv [ ] = { " cppcheck " , " -v " } ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Fail , parser - > parseFromArgs ( 2 , argv ) ) ;
2023-08-12 21:43:12 +02:00
ASSERT_EQUALS ( 0 , ( int ) parser - > getPathNames ( ) . size ( ) ) ;
2023-09-26 21:38:15 +02:00
ASSERT_EQUALS ( " cppcheck: error: no C or C++ source files found. \n " , logger - > str ( ) ) ;
2010-09-06 22:16:52 +02:00
}
2014-11-20 14:20:09 +01:00
void verboseshort ( ) {
2010-09-06 22:16:52 +02:00
REDIRECT ;
2018-09-24 15:08:16 +02:00
const char * const argv [ ] = { " cppcheck " , " -v " , " file.cpp " } ;
2023-08-12 21:43:12 +02:00
settings - > verbose = false ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Success , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-08-12 21:43:12 +02:00
ASSERT_EQUALS ( true , settings - > verbose ) ;
2010-09-06 22:16:52 +02:00
}
2014-11-20 14:20:09 +01:00
void verboselong ( ) {
2010-09-06 22:16:52 +02:00
REDIRECT ;
2018-09-24 15:08:16 +02:00
const char * const argv [ ] = { " cppcheck " , " --verbose " , " file.cpp " } ;
2023-08-12 21:43:12 +02:00
settings - > verbose = false ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Success , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-08-12 21:43:12 +02:00
ASSERT_EQUALS ( true , settings - > verbose ) ;
2010-09-06 22:16:52 +02:00
}
2020-05-30 11:23:22 +02:00
void debugSimplified ( ) {
REDIRECT ;
const char * const argv [ ] = { " cppcheck " , " --debug-simplified " , " file.cpp " } ;
2023-08-12 21:43:12 +02:00
settings - > debugSimplified = false ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Success , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-08-12 21:43:12 +02:00
ASSERT_EQUALS ( true , settings - > debugSimplified ) ;
2020-05-30 11:23:22 +02:00
}
2014-11-20 14:20:09 +01:00
void debugwarnings ( ) {
2010-09-06 22:16:52 +02:00
REDIRECT ;
2018-09-24 15:08:16 +02:00
const char * const argv [ ] = { " cppcheck " , " --debug-warnings " , " file.cpp " } ;
2023-08-12 21:43:12 +02:00
settings - > debugwarnings = false ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Success , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-08-12 21:43:12 +02:00
ASSERT_EQUALS ( true , settings - > debugwarnings ) ;
2010-09-06 22:16:52 +02:00
}
2014-11-20 14:20:09 +01:00
void forceshort ( ) {
2010-09-06 22:16:52 +02:00
REDIRECT ;
2018-09-24 15:08:16 +02:00
const char * const argv [ ] = { " cppcheck " , " -f " , " file.cpp " } ;
2023-08-12 21:43:12 +02:00
settings - > force = false ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Success , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-08-12 21:43:12 +02:00
ASSERT_EQUALS ( true , settings - > force ) ;
2010-09-06 22:16:52 +02:00
}
2014-11-20 14:20:09 +01:00
void forcelong ( ) {
2010-09-06 22:16:52 +02:00
REDIRECT ;
2018-09-24 15:08:16 +02:00
const char * const argv [ ] = { " cppcheck " , " --force " , " file.cpp " } ;
2023-08-12 21:43:12 +02:00
settings - > force = false ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Success , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-08-12 21:43:12 +02:00
ASSERT_EQUALS ( true , settings - > force ) ;
2010-09-06 22:16:52 +02:00
}
2023-08-12 21:43:12 +02:00
void relativePaths1 ( ) {
2012-04-06 10:49:21 +02:00
REDIRECT ;
2023-08-12 21:43:12 +02:00
settings - > relativePaths = false ;
const char * const argv [ ] = { " cppcheck " , " -rp " , " file.cpp " } ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Success , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-08-12 21:43:12 +02:00
ASSERT_EQUALS ( true , settings - > relativePaths ) ;
}
2012-04-06 10:49:21 +02:00
2023-08-12 21:43:12 +02:00
void relativePaths2 ( ) {
REDIRECT ;
settings - > relativePaths = false ;
const char * const argv [ ] = { " cppcheck " , " --relative-paths " , " file.cpp " } ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Success , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-08-12 21:43:12 +02:00
ASSERT_EQUALS ( true , settings - > relativePaths ) ;
}
2012-04-06 10:49:21 +02:00
2023-08-12 21:43:12 +02:00
void relativePaths3 ( ) {
REDIRECT ;
settings - > relativePaths = false ;
settings - > basePaths . clear ( ) ;
const char * const argv [ ] = { " cppcheck " , " -rp=C:/foo;C: \\ bar " , " file.cpp " } ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Success , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-08-12 21:43:12 +02:00
ASSERT_EQUALS ( true , settings - > relativePaths ) ;
ASSERT_EQUALS ( 2 , settings - > basePaths . size ( ) ) ;
ASSERT_EQUALS ( " C:/foo " , settings - > basePaths [ 0 ] ) ;
ASSERT_EQUALS ( " C:/bar " , settings - > basePaths [ 1 ] ) ;
}
2012-04-06 10:49:21 +02:00
2023-08-12 21:43:12 +02:00
void relativePaths4 ( ) {
REDIRECT ;
settings - > relativePaths = false ;
settings - > basePaths . clear ( ) ;
2012-04-06 10:49:21 +02:00
2023-08-12 21:43:12 +02:00
const char * const argv [ ] = { " cppcheck " , " --relative-paths=C:/foo;C: \\ bar " , " file.cpp " } ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Success , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-08-12 21:43:12 +02:00
ASSERT_EQUALS ( true , settings - > relativePaths ) ;
ASSERT_EQUALS ( 2 , settings - > basePaths . size ( ) ) ;
ASSERT_EQUALS ( " C:/foo " , settings - > basePaths [ 0 ] ) ;
ASSERT_EQUALS ( " C:/bar " , settings - > basePaths [ 1 ] ) ;
2012-04-06 10:49:21 +02:00
}
2014-11-20 14:20:09 +01:00
void quietshort ( ) {
2010-09-06 22:16:52 +02:00
REDIRECT ;
2018-09-24 15:08:16 +02:00
const char * const argv [ ] = { " cppcheck " , " -q " , " file.cpp " } ;
2023-08-12 21:43:12 +02:00
settings - > quiet = false ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Success , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-08-12 21:43:12 +02:00
ASSERT_EQUALS ( true , settings - > quiet ) ;
2010-09-06 22:16:52 +02:00
}
2014-11-20 14:20:09 +01:00
void quietlong ( ) {
2010-09-06 22:16:52 +02:00
REDIRECT ;
2018-09-24 15:08:16 +02:00
const char * const argv [ ] = { " cppcheck " , " --quiet " , " file.cpp " } ;
2023-08-12 21:43:12 +02:00
settings - > quiet = false ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Success , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-08-12 21:43:12 +02:00
ASSERT_EQUALS ( true , settings - > quiet ) ;
2010-09-06 22:16:52 +02:00
}
2010-09-08 17:02:22 +02:00
2014-11-20 14:20:09 +01:00
void defines_noarg ( ) {
2011-01-17 20:03:22 +01:00
REDIRECT ;
2018-09-24 15:08:16 +02:00
const char * const argv [ ] = { " cppcheck " , " -D " } ;
2011-05-04 20:00:22 +02:00
// Fails since -D has no param
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Fail , parser - > parseFromArgs ( 2 , argv ) ) ;
2023-09-26 21:38:15 +02:00
ASSERT_EQUALS ( " cppcheck: error: argument to '-D' is missing. \n " , logger - > str ( ) ) ;
2011-01-17 20:03:22 +01:00
}
2014-11-20 14:20:09 +01:00
void defines_noarg2 ( ) {
2011-01-17 20:19:27 +01:00
REDIRECT ;
2018-09-24 15:08:16 +02:00
const char * const argv [ ] = { " cppcheck " , " -D " , " -v " , " file.cpp " } ;
2011-05-04 20:00:22 +02:00
// Fails since -D has no param
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Fail , parser - > parseFromArgs ( 4 , argv ) ) ;
2023-09-26 21:38:15 +02:00
ASSERT_EQUALS ( " cppcheck: error: argument to '-D' is missing. \n " , logger - > str ( ) ) ;
2011-01-17 20:19:27 +01:00
}
2014-11-20 14:20:09 +01:00
void defines_noarg3 ( ) {
2011-01-17 20:19:27 +01:00
REDIRECT ;
2018-09-24 15:08:16 +02:00
const char * const argv [ ] = { " cppcheck " , " -D " , " --quiet " , " file.cpp " } ;
2011-05-04 20:00:22 +02:00
// Fails since -D has no param
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Fail , parser - > parseFromArgs ( 4 , argv ) ) ;
2023-09-26 21:38:15 +02:00
ASSERT_EQUALS ( " cppcheck: error: argument to '-D' is missing. \n " , logger - > str ( ) ) ;
2011-01-17 20:19:27 +01:00
}
2014-11-20 14:20:09 +01:00
void defines ( ) {
2010-09-08 17:02:22 +02:00
REDIRECT ;
2018-09-24 15:08:16 +02:00
const char * const argv [ ] = { " cppcheck " , " -D_WIN32 " , " file.cpp " } ;
2023-08-12 21:43:12 +02:00
settings - > userDefines . clear ( ) ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Success , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-08-12 21:43:12 +02:00
ASSERT_EQUALS ( " _WIN32=1 " , settings - > userDefines ) ;
2010-09-08 17:02:22 +02:00
}
2014-11-20 14:20:09 +01:00
void defines2 ( ) {
2010-09-08 17:02:22 +02:00
REDIRECT ;
2018-09-24 15:08:16 +02:00
const char * const argv [ ] = { " cppcheck " , " -D_WIN32 " , " -DNODEBUG " , " file.cpp " } ;
2023-08-12 21:43:12 +02:00
settings - > userDefines . clear ( ) ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Success , parser - > parseFromArgs ( 4 , argv ) ) ;
2023-08-12 21:43:12 +02:00
ASSERT_EQUALS ( " _WIN32=1;NODEBUG=1 " , settings - > userDefines ) ;
2010-09-08 17:02:22 +02:00
}
2010-09-08 17:42:28 +02:00
2014-11-20 14:20:09 +01:00
void defines3 ( ) {
2010-09-08 17:42:28 +02:00
REDIRECT ;
2018-09-24 15:08:16 +02:00
const char * const argv [ ] = { " cppcheck " , " -D " , " DEBUG " , " file.cpp " } ;
2023-08-12 21:43:12 +02:00
settings - > userDefines . clear ( ) ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Success , parser - > parseFromArgs ( 4 , argv ) ) ;
2023-08-12 21:43:12 +02:00
ASSERT_EQUALS ( " DEBUG=1 " , settings - > userDefines ) ;
2012-07-05 19:35:41 +02:00
}
2014-11-20 14:20:09 +01:00
void defines4 ( ) {
2012-07-05 19:35:41 +02:00
REDIRECT ;
2018-09-24 15:08:16 +02:00
const char * const argv [ ] = { " cppcheck " , " -DDEBUG= " , " file.cpp " } ; // #5137 - defining empty macro
2023-08-12 21:43:12 +02:00
settings - > userDefines . clear ( ) ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Success , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-08-12 21:43:12 +02:00
ASSERT_EQUALS ( " DEBUG= " , settings - > userDefines ) ;
}
void enforceLanguage1 ( ) {
REDIRECT ;
const char * const argv [ ] = { " cppcheck " , " file.cpp " } ;
settings - > enforcedLang = Settings : : Language : : None ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Success , parser - > parseFromArgs ( 2 , argv ) ) ;
2023-08-12 21:43:12 +02:00
ASSERT_EQUALS ( Settings : : Language : : None , settings - > enforcedLang ) ;
}
void enforceLanguage2 ( ) {
REDIRECT ;
const char * const argv [ ] = { " cppcheck " , " -x " , " c++ " , " file.cpp " } ;
settings - > enforcedLang = Settings : : Language : : None ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Success , parser - > parseFromArgs ( 4 , argv ) ) ;
2023-08-12 21:43:12 +02:00
ASSERT_EQUALS ( Settings : : Language : : CPP , settings - > enforcedLang ) ;
}
void enforceLanguage3 ( ) {
REDIRECT ;
const char * const argv [ ] = { " cppcheck " , " -x " } ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Fail , parser - > parseFromArgs ( 2 , argv ) ) ;
2023-09-26 21:38:15 +02:00
ASSERT_EQUALS ( " cppcheck: error: no language given to '-x' option. \n " , logger - > str ( ) ) ;
2023-08-12 21:43:12 +02:00
}
void enforceLanguage4 ( ) {
REDIRECT ;
const char * const argv [ ] = { " cppcheck " , " -x " , " --inconclusive " , " file.cpp " } ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Fail , parser - > parseFromArgs ( 4 , argv ) ) ;
2023-09-26 21:38:15 +02:00
ASSERT_EQUALS ( " cppcheck: error: no language given to '-x' option. \n " , logger - > str ( ) ) ;
2023-08-12 21:43:12 +02:00
}
void enforceLanguage5 ( ) {
REDIRECT ;
const char * const argv [ ] = { " cppcheck " , " --language=c++ " , " file.cpp " } ;
settings - > enforcedLang = Settings : : Language : : None ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Success , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-08-12 21:43:12 +02:00
ASSERT_EQUALS ( Settings : : Language : : CPP , settings - > enforcedLang ) ;
}
void enforceLanguage6 ( ) {
REDIRECT ;
const char * const argv [ ] = { " cppcheck " , " --language=c " , " file.cpp " } ;
settings - > enforcedLang = Settings : : Language : : None ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Success , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-08-12 21:43:12 +02:00
ASSERT_EQUALS ( Settings : : Language : : C , settings - > enforcedLang ) ;
}
void enforceLanguage7 ( ) {
REDIRECT ;
const char * const argv [ ] = { " cppcheck " , " --language=unknownLanguage " , " file.cpp " } ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Fail , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-09-26 21:38:15 +02:00
ASSERT_EQUALS ( " cppcheck: error: unknown language 'unknownLanguage' enforced. \n " , logger - > str ( ) ) ;
2012-09-10 18:51:32 +02:00
}
2014-11-20 14:20:09 +01:00
void includesnopath ( ) {
2010-09-08 17:02:22 +02:00
REDIRECT ;
2018-09-24 15:08:16 +02:00
const char * const argv [ ] = { " cppcheck " , " -I " } ;
2013-08-08 15:50:45 +02:00
// Fails since -I has no param
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Fail , parser - > parseFromArgs ( 2 , argv ) ) ;
2023-09-26 21:38:15 +02:00
ASSERT_EQUALS ( " cppcheck: error: argument to '-I' is missing. \n " , logger - > str ( ) ) ;
2010-09-08 17:02:22 +02:00
}
2014-11-20 14:20:09 +01:00
void includes ( ) {
2010-09-08 17:02:22 +02:00
REDIRECT ;
2018-09-24 15:08:16 +02:00
const char * const argv [ ] = { " cppcheck " , " -I " , " include " , " file.cpp " } ;
2023-08-12 21:43:12 +02:00
settings - > includePaths . clear ( ) ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Success , parser - > parseFromArgs ( 4 , argv ) ) ;
2023-08-12 21:43:12 +02:00
ASSERT_EQUALS ( " include/ " , settings - > includePaths . front ( ) ) ;
2010-09-08 17:02:22 +02:00
}
2014-11-20 14:20:09 +01:00
void includesslash ( ) {
2011-01-11 20:03:18 +01:00
REDIRECT ;
2018-09-24 15:08:16 +02:00
const char * const argv [ ] = { " cppcheck " , " -I " , " include/ " , " file.cpp " } ;
2023-08-12 21:43:12 +02:00
settings - > includePaths . clear ( ) ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Success , parser - > parseFromArgs ( 4 , argv ) ) ;
2023-08-12 21:43:12 +02:00
ASSERT_EQUALS ( " include/ " , settings - > includePaths . front ( ) ) ;
2011-01-11 20:03:18 +01:00
}
2014-11-20 14:20:09 +01:00
void includesbackslash ( ) {
2011-01-11 20:03:18 +01:00
REDIRECT ;
2018-09-24 15:08:16 +02:00
const char * const argv [ ] = { " cppcheck " , " -I " , " include \\ " , " file.cpp " } ;
2023-08-12 21:43:12 +02:00
settings - > includePaths . clear ( ) ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Success , parser - > parseFromArgs ( 4 , argv ) ) ;
2023-08-12 21:43:12 +02:00
ASSERT_EQUALS ( " include/ " , settings - > includePaths . front ( ) ) ;
2011-01-11 20:03:18 +01:00
}
2014-11-20 14:20:09 +01:00
void includesnospace ( ) {
2011-01-11 20:03:18 +01:00
REDIRECT ;
2018-09-24 15:08:16 +02:00
const char * const argv [ ] = { " cppcheck " , " -Iinclude " , " file.cpp " } ;
2023-08-12 21:43:12 +02:00
settings - > includePaths . clear ( ) ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Success , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-08-12 21:43:12 +02:00
ASSERT_EQUALS ( " include/ " , settings - > includePaths . front ( ) ) ;
2011-01-11 20:03:18 +01:00
}
2014-11-20 14:20:09 +01:00
void includes2 ( ) {
2010-09-08 17:02:22 +02:00
REDIRECT ;
2018-09-24 15:08:16 +02:00
const char * const argv [ ] = { " cppcheck " , " -I " , " include/ " , " -I " , " framework/ " , " file.cpp " } ;
2023-08-12 21:43:12 +02:00
settings - > includePaths . clear ( ) ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Success , parser - > parseFromArgs ( 6 , argv ) ) ;
2023-08-12 21:43:12 +02:00
ASSERT_EQUALS ( " include/ " , settings - > includePaths . front ( ) ) ;
settings - > includePaths . pop_front ( ) ;
ASSERT_EQUALS ( " framework/ " , settings - > includePaths . front ( ) ) ;
2010-09-08 17:02:22 +02:00
}
2010-12-04 13:13:44 +01:00
2014-11-20 14:20:09 +01:00
void includesFile ( ) {
2022-09-17 18:50:25 +02:00
REDIRECT ;
2023-08-14 11:37:59 +02:00
ScopedFile file ( " includes.txt " ,
" path/sub \n "
" path2/sub1 \n " ) ;
const char * const argv [ ] = { " cppcheck " , " --includes-file=includes.txt " , " file.cpp " } ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Success , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-08-14 11:37:59 +02:00
ASSERT_EQUALS ( 2 , settings - > includePaths . size ( ) ) ;
auto it = settings - > includePaths . cbegin ( ) ;
ASSERT_EQUALS ( " path/sub/ " , * it + + ) ;
ASSERT_EQUALS ( " path2/sub1/ " , * it ) ;
2022-09-17 18:50:25 +02:00
}
void includesFileNoFile ( ) {
2011-10-22 21:24:23 +02:00
REDIRECT ;
2018-09-04 16:37:43 +02:00
const char * const argv [ ] = { " cppcheck " , " --includes-file=fileThatDoesNotExist.txt " , " file.cpp " } ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Fail , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-09-26 21:38:15 +02:00
ASSERT_EQUALS ( " cppcheck: error: unable to open includes file at 'fileThatDoesNotExist.txt' \n " , logger - > str ( ) ) ;
2018-09-04 16:37:43 +02:00
}
void configExcludesFile ( ) {
REDIRECT ;
2023-08-14 11:37:59 +02:00
ScopedFile file ( " excludes.txt " ,
" path/sub \n "
" path2/sub1 \n " ) ;
const char * const argv [ ] = { " cppcheck " , " --config-excludes-file=excludes.txt " , " file.cpp " } ;
settings - > configExcludePaths . clear ( ) ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Success , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-08-14 11:37:59 +02:00
ASSERT_EQUALS ( 2 , settings - > configExcludePaths . size ( ) ) ;
auto it = settings - > configExcludePaths . cbegin ( ) ;
ASSERT_EQUALS ( " path/sub/ " , * it + + ) ;
ASSERT_EQUALS ( " path2/sub1/ " , * it ) ;
2022-09-17 18:50:25 +02:00
}
void configExcludesFileNoFile ( ) {
REDIRECT ;
const char * const argv [ ] = { " cppcheck " , " --config-excludes-file=fileThatDoesNotExist.txt " , " file.cpp " } ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Fail , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-09-26 21:38:15 +02:00
ASSERT_EQUALS ( " cppcheck: error: unable to open config excludes file at 'fileThatDoesNotExist.txt' \n " , logger - > str ( ) ) ;
2011-10-22 21:24:23 +02:00
}
2014-11-20 14:20:09 +01:00
void enabledAll ( ) {
2010-12-04 13:13:44 +01:00
REDIRECT ;
2018-09-24 15:08:16 +02:00
const char * const argv [ ] = { " cppcheck " , " --enable=all " , " file.cpp " } ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Success , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-08-12 21:43:12 +02:00
ASSERT ( settings - > severity . isEnabled ( Severity : : style ) ) ;
ASSERT ( settings - > severity . isEnabled ( Severity : : warning ) ) ;
ASSERT ( settings - > checks . isEnabled ( Checks : : unusedFunction ) ) ;
ASSERT ( settings - > checks . isEnabled ( Checks : : missingInclude ) ) ;
ASSERT ( ! settings - > checks . isEnabled ( Checks : : internalCheck ) ) ;
2010-12-04 13:13:44 +01:00
}
2014-11-20 14:20:09 +01:00
void enabledStyle ( ) {
2010-12-04 13:13:44 +01:00
REDIRECT ;
2018-09-24 15:08:16 +02:00
const char * const argv [ ] = { " cppcheck " , " --enable=style " , " file.cpp " } ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Success , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-08-12 21:43:12 +02:00
ASSERT ( settings - > severity . isEnabled ( Severity : : style ) ) ;
ASSERT ( settings - > severity . isEnabled ( Severity : : warning ) ) ;
ASSERT ( settings - > severity . isEnabled ( Severity : : performance ) ) ;
ASSERT ( settings - > severity . isEnabled ( Severity : : portability ) ) ;
ASSERT ( ! settings - > checks . isEnabled ( Checks : : unusedFunction ) ) ;
ASSERT ( ! settings - > checks . isEnabled ( Checks : : internalCheck ) ) ;
2011-09-04 20:42:35 +02:00
}
2014-11-20 14:20:09 +01:00
void enabledPerformance ( ) {
2011-09-04 20:42:35 +02:00
REDIRECT ;
2018-09-24 15:08:16 +02:00
const char * const argv [ ] = { " cppcheck " , " --enable=performance " , " file.cpp " } ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Success , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-08-12 21:43:12 +02:00
ASSERT ( ! settings - > severity . isEnabled ( Severity : : style ) ) ;
ASSERT ( ! settings - > severity . isEnabled ( Severity : : warning ) ) ;
ASSERT ( settings - > severity . isEnabled ( Severity : : performance ) ) ;
ASSERT ( ! settings - > severity . isEnabled ( Severity : : portability ) ) ;
ASSERT ( ! settings - > checks . isEnabled ( Checks : : unusedFunction ) ) ;
ASSERT ( ! settings - > checks . isEnabled ( Checks : : missingInclude ) ) ;
2011-09-04 20:42:35 +02:00
}
2014-11-20 14:20:09 +01:00
void enabledPortability ( ) {
2011-09-04 20:42:35 +02:00
REDIRECT ;
2018-09-24 15:08:16 +02:00
const char * const argv [ ] = { " cppcheck " , " --enable=portability " , " file.cpp " } ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Success , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-08-12 21:43:12 +02:00
ASSERT ( ! settings - > severity . isEnabled ( Severity : : style ) ) ;
ASSERT ( ! settings - > severity . isEnabled ( Severity : : warning ) ) ;
ASSERT ( ! settings - > severity . isEnabled ( Severity : : performance ) ) ;
ASSERT ( settings - > severity . isEnabled ( Severity : : portability ) ) ;
ASSERT ( ! settings - > checks . isEnabled ( Checks : : unusedFunction ) ) ;
ASSERT ( ! settings - > checks . isEnabled ( Checks : : missingInclude ) ) ;
2010-12-04 13:13:44 +01:00
}
2023-03-09 20:04:20 +01:00
void enabledInformation ( ) {
REDIRECT ;
const char * const argv [ ] = { " cppcheck " , " --enable=information " , " file.cpp " } ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Success , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-08-12 21:43:12 +02:00
ASSERT ( settings - > severity . isEnabled ( Severity : : information ) ) ;
ASSERT ( settings - > checks . isEnabled ( Checks : : missingInclude ) ) ;
2023-09-26 21:38:15 +02:00
ASSERT_EQUALS ( " cppcheck: '--enable=information' will no longer implicitly enable 'missingInclude' starting with 2.16. Please enable it explicitly if you require it. \n " , logger - > str ( ) ) ;
2023-03-09 20:04:20 +01:00
}
2014-11-20 14:20:09 +01:00
void enabledUnusedFunction ( ) {
2010-12-04 13:13:44 +01:00
REDIRECT ;
2018-09-24 15:08:16 +02:00
const char * const argv [ ] = { " cppcheck " , " --enable=unusedFunction " , " file.cpp " } ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Success , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-08-12 21:43:12 +02:00
ASSERT ( settings - > checks . isEnabled ( Checks : : unusedFunction ) ) ;
2010-12-04 13:13:44 +01:00
}
2014-11-20 14:20:09 +01:00
void enabledMissingInclude ( ) {
2010-12-04 13:13:44 +01:00
REDIRECT ;
2018-09-24 15:08:16 +02:00
const char * const argv [ ] = { " cppcheck " , " --enable=missingInclude " , " file.cpp " } ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Success , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-08-12 21:43:12 +02:00
ASSERT ( settings - > checks . isEnabled ( Checks : : missingInclude ) ) ;
2010-12-04 13:13:44 +01:00
}
2014-05-18 19:48:43 +02:00
# ifdef CHECK_INTERNAL
2014-11-20 14:20:09 +01:00
void enabledInternal ( ) {
2011-10-28 22:39:46 +02:00
REDIRECT ;
2018-09-24 15:08:16 +02:00
const char * const argv [ ] = { " cppcheck " , " --enable=internal " , " file.cpp " } ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Success , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-08-12 21:43:12 +02:00
ASSERT ( settings - > checks . isEnabled ( Checks : : internalCheck ) ) ;
2011-10-28 22:39:46 +02:00
}
2011-10-29 12:47:12 +02:00
# endif
2011-10-28 22:39:46 +02:00
2014-11-20 14:20:09 +01:00
void enabledMultiple ( ) {
2013-04-01 13:15:05 +02:00
REDIRECT ;
2018-09-24 15:08:16 +02:00
const char * const argv [ ] = { " cppcheck " , " --enable=missingInclude,portability,warning " , " file.cpp " } ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Success , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-08-12 21:43:12 +02:00
ASSERT ( ! settings - > severity . isEnabled ( Severity : : style ) ) ;
ASSERT ( settings - > severity . isEnabled ( Severity : : warning ) ) ;
ASSERT ( ! settings - > severity . isEnabled ( Severity : : performance ) ) ;
ASSERT ( settings - > severity . isEnabled ( Severity : : portability ) ) ;
ASSERT ( ! settings - > checks . isEnabled ( Checks : : unusedFunction ) ) ;
ASSERT ( settings - > checks . isEnabled ( Checks : : missingInclude ) ) ;
2013-04-01 13:15:05 +02:00
}
2023-01-21 10:39:44 +01:00
void enabledInvalid ( ) {
REDIRECT ;
const char * const argv [ ] = { " cppcheck " , " --enable=warning,missingIncludeSystem,style " , " file.cpp " } ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Fail , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-09-26 21:38:15 +02:00
ASSERT_EQUALS ( " cppcheck: error: --enable parameter with the unknown name 'missingIncludeSystem' \n " , logger - > str ( ) ) ;
2023-01-21 10:39:44 +01:00
}
void enabledError ( ) {
REDIRECT ;
const char * const argv [ ] = { " cppcheck " , " --enable=error " , " file.cpp " } ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Fail , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-09-26 21:38:15 +02:00
ASSERT_EQUALS ( " cppcheck: error: --enable parameter with the unknown name 'error' \n " , logger - > str ( ) ) ;
2023-01-21 10:39:44 +01:00
}
void enabledEmpty ( ) {
REDIRECT ;
const char * const argv [ ] = { " cppcheck " , " --enable= " , " file.cpp " } ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Fail , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-09-26 21:38:15 +02:00
ASSERT_EQUALS ( " cppcheck: error: --enable parameter is empty \n " , logger - > str ( ) ) ;
2023-01-21 10:39:44 +01:00
}
2023-08-12 21:43:12 +02:00
2023-01-21 10:39:44 +01:00
void disableAll ( ) {
REDIRECT ;
2023-08-12 21:43:12 +02:00
const char * const argv [ ] = { " cppcheck " , " --enable=all " , " --disable=all " , " file.cpp " } ;
settings - > severity . clear ( ) ;
settings - > checks . clear ( ) ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Success , parser - > parseFromArgs ( 4 , argv ) ) ;
2023-08-12 21:43:12 +02:00
ASSERT_EQUALS ( true , settings - > severity . isEnabled ( Severity : : error ) ) ;
ASSERT_EQUALS ( false , settings - > severity . isEnabled ( Severity : : warning ) ) ;
ASSERT_EQUALS ( false , settings - > severity . isEnabled ( Severity : : style ) ) ;
ASSERT_EQUALS ( false , settings - > severity . isEnabled ( Severity : : performance ) ) ;
ASSERT_EQUALS ( false , settings - > severity . isEnabled ( Severity : : portability ) ) ;
ASSERT_EQUALS ( false , settings - > severity . isEnabled ( Severity : : debug ) ) ;
ASSERT_EQUALS ( false , settings - > checks . isEnabled ( Checks : : unusedFunction ) ) ;
ASSERT_EQUALS ( false , settings - > checks . isEnabled ( Checks : : missingInclude ) ) ;
ASSERT_EQUALS ( false , settings - > checks . isEnabled ( Checks : : internalCheck ) ) ;
2023-01-21 10:39:44 +01:00
}
void disableMultiple ( ) {
REDIRECT ;
2023-08-12 21:43:12 +02:00
const char * const argv [ ] = { " cppcheck " , " --enable=all " , " --disable=style " , " --disable=unusedFunction " , " file.cpp " } ;
settings - > severity . clear ( ) ;
settings - > checks . clear ( ) ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Success , parser - > parseFromArgs ( 5 , argv ) ) ;
2023-08-12 21:43:12 +02:00
ASSERT_EQUALS ( true , settings - > severity . isEnabled ( Severity : : error ) ) ;
ASSERT_EQUALS ( true , settings - > severity . isEnabled ( Severity : : warning ) ) ;
ASSERT_EQUALS ( false , settings - > severity . isEnabled ( Severity : : style ) ) ;
ASSERT_EQUALS ( true , settings - > severity . isEnabled ( Severity : : performance ) ) ;
ASSERT_EQUALS ( true , settings - > severity . isEnabled ( Severity : : portability ) ) ;
ASSERT_EQUALS ( true , settings - > severity . isEnabled ( Severity : : debug ) ) ;
ASSERT_EQUALS ( false , settings - > checks . isEnabled ( Checks : : unusedFunction ) ) ;
ASSERT_EQUALS ( true , settings - > checks . isEnabled ( Checks : : missingInclude ) ) ;
ASSERT_EQUALS ( false , settings - > checks . isEnabled ( Checks : : internalCheck ) ) ;
2023-01-21 10:39:44 +01:00
}
// make sure the implied "style" checks are not added when "--enable=style" is specified
void disableStylePartial ( ) {
REDIRECT ;
2023-08-12 21:43:12 +02:00
const char * const argv [ ] = { " cppcheck " , " --enable=style " , " --disable=performance " , " --enable=unusedFunction " , " file.cpp " } ;
settings - > severity . clear ( ) ;
settings - > checks . clear ( ) ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Success , parser - > parseFromArgs ( 5 , argv ) ) ;
2023-08-12 21:43:12 +02:00
ASSERT_EQUALS ( true , settings - > severity . isEnabled ( Severity : : error ) ) ;
ASSERT_EQUALS ( true , settings - > severity . isEnabled ( Severity : : warning ) ) ;
ASSERT_EQUALS ( true , settings - > severity . isEnabled ( Severity : : style ) ) ;
ASSERT_EQUALS ( false , settings - > severity . isEnabled ( Severity : : performance ) ) ;
ASSERT_EQUALS ( true , settings - > severity . isEnabled ( Severity : : portability ) ) ;
ASSERT_EQUALS ( false , settings - > severity . isEnabled ( Severity : : debug ) ) ;
ASSERT_EQUALS ( true , settings - > checks . isEnabled ( Checks : : unusedFunction ) ) ;
ASSERT_EQUALS ( false , settings - > checks . isEnabled ( Checks : : missingInclude ) ) ;
ASSERT_EQUALS ( false , settings - > checks . isEnabled ( Checks : : internalCheck ) ) ;
2023-01-21 10:39:44 +01:00
}
2023-03-09 20:04:20 +01:00
void disableInformationPartial ( ) {
REDIRECT ;
const char * const argv [ ] = { " cppcheck " , " --enable=information " , " --disable=missingInclude " , " file.cpp " } ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Success , parser - > parseFromArgs ( 4 , argv ) ) ;
2023-08-12 21:43:12 +02:00
ASSERT ( settings - > severity . isEnabled ( Severity : : information ) ) ;
ASSERT ( ! settings - > checks . isEnabled ( Checks : : missingInclude ) ) ;
2023-09-26 21:38:15 +02:00
ASSERT_EQUALS ( " cppcheck: '--enable=information' will no longer implicitly enable 'missingInclude' starting with 2.16. Please enable it explicitly if you require it. \n " , logger - > str ( ) ) ;
2023-03-09 20:04:20 +01:00
}
void disableInformationPartial2 ( ) {
REDIRECT ;
const char * const argv [ ] = { " cppcheck " , " --enable=missingInclude " , " --disable=information " , " file.cpp " } ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Success , parser - > parseFromArgs ( 4 , argv ) ) ;
2023-08-12 21:43:12 +02:00
ASSERT ( ! settings - > severity . isEnabled ( Severity : : information ) ) ;
ASSERT ( settings - > checks . isEnabled ( Checks : : missingInclude ) ) ;
2023-03-09 20:04:20 +01:00
}
2023-01-21 10:39:44 +01:00
void disableInvalid ( ) {
REDIRECT ;
const char * const argv [ ] = { " cppcheck " , " --disable=leaks " , " file.cpp " } ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Fail , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-09-26 21:38:15 +02:00
ASSERT_EQUALS ( " cppcheck: error: --disable parameter with the unknown name 'leaks' \n " , logger - > str ( ) ) ;
2023-01-21 10:39:44 +01:00
}
void disableError ( ) {
REDIRECT ;
const char * const argv [ ] = { " cppcheck " , " --disable=error " , " file.cpp " } ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Fail , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-09-26 21:38:15 +02:00
ASSERT_EQUALS ( " cppcheck: error: --disable parameter with the unknown name 'error' \n " , logger - > str ( ) ) ;
2023-01-21 10:39:44 +01:00
}
void disableEmpty ( ) {
REDIRECT ;
const char * const argv [ ] = { " cppcheck " , " --disable= " , " file.cpp " } ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Fail , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-09-26 21:38:15 +02:00
ASSERT_EQUALS ( " cppcheck: error: --disable parameter is empty \n " , logger - > str ( ) ) ;
2023-01-21 10:39:44 +01:00
}
2014-11-20 14:20:09 +01:00
void inconclusive ( ) {
2013-08-15 11:09:40 +02:00
REDIRECT ;
2023-08-12 21:43:12 +02:00
const char * const argv [ ] = { " cppcheck " , " --inconclusive " , " file.cpp " } ;
settings - > certainty . clear ( ) ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Success , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-08-12 21:43:12 +02:00
ASSERT_EQUALS ( true , settings - > certainty . isEnabled ( Certainty : : inconclusive ) ) ;
2013-08-15 11:09:40 +02:00
}
2014-11-20 14:20:09 +01:00
void errorExitcode ( ) {
2010-12-04 13:13:44 +01:00
REDIRECT ;
2018-09-24 15:08:16 +02:00
const char * const argv [ ] = { " cppcheck " , " --error-exitcode=5 " , " file.cpp " } ;
2023-08-12 21:43:12 +02:00
settings - > exitCode = 0 ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Success , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-08-12 21:43:12 +02:00
ASSERT_EQUALS ( 5 , settings - > exitCode ) ;
2010-12-04 13:13:44 +01:00
}
2014-11-20 14:20:09 +01:00
void errorExitcodeMissing ( ) {
2010-12-04 13:13:44 +01:00
REDIRECT ;
2018-09-24 15:08:16 +02:00
const char * const argv [ ] = { " cppcheck " , " --error-exitcode= " , " file.cpp " } ;
2011-05-04 20:00:22 +02:00
// Fails since exit code not given
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Fail , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-09-26 21:38:15 +02:00
ASSERT_EQUALS ( " cppcheck: error: argument to '--error-exitcode=' is not valid - not an integer. \n " , logger - > str ( ) ) ;
2010-12-04 13:13:44 +01:00
}
2014-11-20 14:20:09 +01:00
void errorExitcodeStr ( ) {
2010-12-04 13:13:44 +01:00
REDIRECT ;
2018-09-24 15:08:16 +02:00
const char * const argv [ ] = { " cppcheck " , " --error-exitcode=foo " , " file.cpp " } ;
2011-05-04 20:00:22 +02:00
// Fails since invalid exit code
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Fail , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-09-26 21:38:15 +02:00
ASSERT_EQUALS ( " cppcheck: error: argument to '--error-exitcode=' is not valid - not an integer. \n " , logger - > str ( ) ) ;
2010-12-04 13:13:44 +01:00
}
2014-11-20 14:20:09 +01:00
void exitcodeSuppressionsOld ( ) {
2011-01-29 18:04:52 +01:00
REDIRECT ;
2018-09-24 15:08:16 +02:00
const char * const argv [ ] = { " cppcheck " , " --exitcode-suppressions " , " suppr.txt " , " file.cpp " } ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Fail , parser - > parseFromArgs ( 4 , argv ) ) ;
2023-09-26 21:38:15 +02:00
ASSERT_EQUALS ( " cppcheck: error: unrecognized command line option: \" --exitcode-suppressions \" . \n " , logger - > str ( ) ) ;
2011-01-29 18:04:52 +01:00
}
2014-11-20 14:20:09 +01:00
void exitcodeSuppressions ( ) {
2010-12-04 13:13:44 +01:00
REDIRECT ;
2023-08-14 11:37:59 +02:00
ScopedFile file ( " suppr.txt " ,
" uninitvar \n "
" unusedFunction \n " ) ;
2018-09-24 15:08:16 +02:00
const char * const argv [ ] = { " cppcheck " , " --exitcode-suppressions=suppr.txt " , " file.cpp " } ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Success , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-08-14 11:37:59 +02:00
ASSERT_EQUALS ( 2 , settings - > nofail . getSuppressions ( ) . size ( ) ) ;
auto it = settings - > nofail . getSuppressions ( ) . cbegin ( ) ;
2023-10-16 14:09:03 +02:00
ASSERT_EQUALS ( " uninitvar " , ( it + + ) - > errorId ) ;
ASSERT_EQUALS ( " unusedFunction " , it - > errorId ) ;
2011-01-29 18:04:52 +01:00
}
2014-11-20 14:20:09 +01:00
void exitcodeSuppressionsNoFile ( ) {
2011-01-29 18:04:52 +01:00
REDIRECT ;
2018-09-24 15:08:16 +02:00
const char * const argv [ ] = { " cppcheck " , " --exitcode-suppressions " , " file.cpp " } ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Fail , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-09-26 21:38:15 +02:00
ASSERT_EQUALS ( " cppcheck: error: unrecognized command line option: \" --exitcode-suppressions \" . \n " , logger - > str ( ) ) ;
2010-12-04 13:13:44 +01:00
}
2022-12-19 22:28:36 +01:00
void fileList ( ) {
2010-12-04 13:13:44 +01:00
REDIRECT ;
2023-08-14 11:37:59 +02:00
ScopedFile file ( " files.txt " ,
" file1.c \n "
" file2.cpp \n " ) ;
2022-09-17 18:50:25 +02:00
const char * const argv [ ] = { " cppcheck " , " --file-list=files.txt " , " file.cpp " } ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Success , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-08-14 11:37:59 +02:00
ASSERT_EQUALS ( 3 , parser - > getPathNames ( ) . size ( ) ) ;
auto it = parser - > getPathNames ( ) . cbegin ( ) ;
ASSERT_EQUALS ( " file1.c " , * it + + ) ;
ASSERT_EQUALS ( " file2.cpp " , * it + + ) ;
ASSERT_EQUALS ( " file.cpp " , * it ) ;
2022-12-19 22:28:36 +01:00
}
2022-09-17 18:50:25 +02:00
2022-12-19 22:28:36 +01:00
void fileListNoFile ( ) {
2022-09-17 18:50:25 +02:00
REDIRECT ;
const char * const argv [ ] = { " cppcheck " , " --file-list=files.txt " , " file.cpp " } ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Fail , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-09-26 21:38:15 +02:00
ASSERT_EQUALS ( " cppcheck: error: couldn't open the file: \" files.txt \" . \n " , logger - > str ( ) ) ;
2022-12-19 22:28:36 +01:00
}
2011-09-30 01:23:30 +02:00
2014-01-11 07:54:06 +01:00
/* void fileListStdin() {
// TODO: Give it some stdin to read from, fails because the list of
// files in stdin (_pathnames) is empty
REDIRECT ;
2018-09-24 15:08:16 +02:00
const char * const argv [ ] = { " cppcheck " , " --file-list=- " , " file.cpp " } ;
2023-08-12 21:43:12 +02:00
TODO_ASSERT_EQUALS ( true , false , parser - > parseFromArgs ( 3 , argv ) ) ;
2014-01-11 07:54:06 +01:00
} */
2010-12-04 13:13:44 +01:00
2022-09-17 18:50:25 +02:00
void fileListInvalid ( ) {
REDIRECT ;
const char * const argv [ ] = { " cppcheck " , " --file-list " , " files.txt " , " file.cpp " } ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Fail , parser - > parseFromArgs ( 4 , argv ) ) ;
2023-09-26 21:38:15 +02:00
ASSERT_EQUALS ( " cppcheck: error: unrecognized command line option: \" --file-list \" . \n " , logger - > str ( ) ) ;
2022-09-17 18:50:25 +02:00
}
2014-11-20 14:20:09 +01:00
void inlineSuppr ( ) {
2010-12-04 13:13:44 +01:00
REDIRECT ;
2018-09-24 15:08:16 +02:00
const char * const argv [ ] = { " cppcheck " , " --inline-suppr " , " file.cpp " } ;
2023-08-12 21:43:12 +02:00
settings - > inlineSuppressions = false ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Success , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-08-12 21:43:12 +02:00
ASSERT ( settings - > inlineSuppressions ) ;
2010-12-04 13:13:44 +01:00
}
2014-11-20 14:20:09 +01:00
void jobs ( ) {
2010-12-04 13:13:44 +01:00
REDIRECT ;
2018-09-24 15:08:16 +02:00
const char * const argv [ ] = { " cppcheck " , " -j " , " 3 " , " file.cpp " } ;
2023-08-12 21:43:12 +02:00
settings - > jobs = 0 ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Success , parser - > parseFromArgs ( 4 , argv ) ) ;
2023-08-12 21:43:12 +02:00
ASSERT_EQUALS ( 3 , settings - > jobs ) ;
2010-12-04 13:13:44 +01:00
}
2023-04-08 22:29:09 +02:00
void jobs2 ( ) {
REDIRECT ;
const char * const argv [ ] = { " cppcheck " , " -j3 " , " file.cpp " } ;
2023-08-12 21:43:12 +02:00
settings - > jobs = 0 ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Success , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-08-12 21:43:12 +02:00
ASSERT_EQUALS ( 3 , settings - > jobs ) ;
2023-04-08 22:29:09 +02:00
}
2014-11-20 14:20:09 +01:00
void jobsMissingCount ( ) {
2010-12-04 13:13:44 +01:00
REDIRECT ;
2018-09-24 15:08:16 +02:00
const char * const argv [ ] = { " cppcheck " , " -j " , " file.cpp " } ;
2011-05-04 20:00:22 +02:00
// Fails since -j is missing thread count
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Fail , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-09-26 21:38:15 +02:00
ASSERT_EQUALS ( " cppcheck: error: argument to '-j' is not valid - not an integer. \n " , logger - > str ( ) ) ;
2010-12-04 13:13:44 +01:00
}
2014-11-20 14:20:09 +01:00
void jobsInvalid ( ) {
2010-12-04 13:13:44 +01:00
REDIRECT ;
2018-09-24 15:08:16 +02:00
const char * const argv [ ] = { " cppcheck " , " -j " , " e " , " file.cpp " } ;
2011-05-04 20:00:22 +02:00
// Fails since invalid count given for -j
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Fail , parser - > parseFromArgs ( 4 , argv ) ) ;
2023-09-26 21:38:15 +02:00
ASSERT_EQUALS ( " cppcheck: error: argument to '-j' is not valid - not an integer. \n " , logger - > str ( ) ) ;
2023-04-08 22:29:09 +02:00
}
2023-08-14 11:55:32 +02:00
void jobsNoJobs ( ) {
REDIRECT ;
const char * const argv [ ] = { " cppcheck " , " -j0 " , " file.cpp " } ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Fail , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-09-26 21:38:15 +02:00
ASSERT_EQUALS ( " cppcheck: error: argument for '-j' must be greater than 0. \n " , logger - > str ( ) ) ;
2023-08-14 11:55:32 +02:00
}
2023-04-08 22:29:09 +02:00
void jobsTooBig ( ) {
REDIRECT ;
2023-08-14 11:55:32 +02:00
const char * const argv [ ] = { " cppcheck " , " -j1025 " , " file.cpp " } ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Fail , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-09-26 21:38:15 +02:00
ASSERT_EQUALS ( " cppcheck: error: argument for '-j' is allowed to be 1024 at max. \n " , logger - > str ( ) ) ;
2010-12-04 13:13:44 +01:00
}
2014-11-20 14:20:09 +01:00
void maxConfigs ( ) {
2011-10-05 07:37:43 +02:00
REDIRECT ;
2018-09-24 15:08:16 +02:00
const char * const argv [ ] = { " cppcheck " , " -f " , " --max-configs=12 " , " file.cpp " } ;
2023-08-12 21:43:12 +02:00
settings - > force = false ;
settings - > maxConfigs = 0 ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Success , parser - > parseFromArgs ( 4 , argv ) ) ;
2023-08-12 21:43:12 +02:00
ASSERT_EQUALS ( 12 , settings - > maxConfigs ) ;
ASSERT_EQUALS ( false , settings - > force ) ;
2011-10-05 07:37:43 +02:00
}
2014-11-20 14:20:09 +01:00
void maxConfigsMissingCount ( ) {
2011-10-05 07:37:43 +02:00
REDIRECT ;
2018-09-24 15:08:16 +02:00
const char * const argv [ ] = { " cppcheck " , " --max-configs= " , " file.cpp " } ;
2011-10-05 07:37:43 +02:00
// Fails since --max-configs= is missing limit
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Fail , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-09-26 21:38:15 +02:00
ASSERT_EQUALS ( " cppcheck: error: argument to '--max-configs=' is not valid - not an integer. \n " , logger - > str ( ) ) ;
2011-10-05 07:37:43 +02:00
}
2014-11-20 14:20:09 +01:00
void maxConfigsInvalid ( ) {
2011-10-05 07:37:43 +02:00
REDIRECT ;
2018-09-24 15:08:16 +02:00
const char * const argv [ ] = { " cppcheck " , " --max-configs=e " , " file.cpp " } ;
2011-10-05 07:37:43 +02:00
// Fails since invalid count given for --max-configs=
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Fail , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-09-26 21:38:15 +02:00
ASSERT_EQUALS ( " cppcheck: error: argument to '--max-configs=' is not valid - not an integer. \n " , logger - > str ( ) ) ;
2011-10-05 07:37:43 +02:00
}
2014-11-20 14:20:09 +01:00
void maxConfigsTooSmall ( ) {
2011-10-05 07:37:43 +02:00
REDIRECT ;
2018-09-24 15:08:16 +02:00
const char * const argv [ ] = { " cppcheck " , " --max-configs=0 " , " file.cpp " } ;
2011-10-05 07:37:43 +02:00
// Fails since limit must be greater than 0
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Fail , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-09-26 21:38:15 +02:00
ASSERT_EQUALS ( " cppcheck: error: argument to '--max-configs=' must be greater than 0. \n " , logger - > str ( ) ) ;
2011-10-22 11:11:54 +02:00
}
2011-10-05 07:37:43 +02:00
2023-08-23 11:20:20 +02:00
void reportProgress1 ( ) {
2010-12-04 13:13:44 +01:00
REDIRECT ;
2018-09-24 15:08:16 +02:00
const char * const argv [ ] = { " cppcheck " , " --report-progress " , " file.cpp " } ;
2023-08-23 11:20:20 +02:00
settings - > reportProgress = - 1 ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Success , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-08-23 11:20:20 +02:00
ASSERT_EQUALS ( 10 , settings - > reportProgress ) ;
}
void reportProgress2 ( ) {
REDIRECT ;
const char * const argv [ ] = { " cppcheck " , " --report-progress= " , " file.cpp " } ;
settings - > reportProgress = - 1 ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Fail , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-09-26 21:38:15 +02:00
ASSERT_EQUALS ( " cppcheck: error: argument to '--report-progress=' is not valid - not an integer. \n " , logger - > str ( ) ) ;
2023-08-23 11:20:20 +02:00
}
void reportProgress3 ( ) {
REDIRECT ;
const char * const argv [ ] = { " cppcheck " , " --report-progress=-1 " , " file.cpp " } ;
settings - > reportProgress = - 1 ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Fail , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-09-26 21:38:15 +02:00
ASSERT_EQUALS ( " cppcheck: error: argument to '--report-progress=' needs to be a positive integer. \n " , logger - > str ( ) ) ;
2023-08-23 11:20:20 +02:00
}
void reportProgress4 ( ) {
REDIRECT ;
const char * const argv [ ] = { " cppcheck " , " --report-progress=0 " , " file.cpp " } ;
settings - > reportProgress = - 1 ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Success , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-08-23 11:20:20 +02:00
ASSERT_EQUALS ( 0 , settings - > reportProgress ) ;
}
void reportProgress5 ( ) {
REDIRECT ;
const char * const argv [ ] = { " cppcheck " , " --report-progress=1 " , " file.cpp " } ;
settings - > reportProgress = - 1 ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Success , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-08-23 11:20:20 +02:00
ASSERT_EQUALS ( 1 , settings - > reportProgress ) ;
2010-12-04 13:13:44 +01:00
}
2014-11-20 14:20:09 +01:00
void stdc99 ( ) {
2011-10-06 08:10:51 +02:00
REDIRECT ;
2018-09-24 15:08:16 +02:00
const char * const argv [ ] = { " cppcheck " , " --std=c99 " , " file.cpp " } ;
2023-08-12 21:43:12 +02:00
settings - > standards . c = Standards : : C89 ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Success , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-08-12 21:43:12 +02:00
ASSERT ( settings - > standards . c = = Standards : : C99 ) ;
2011-10-06 08:10:51 +02:00
}
2014-11-20 14:20:09 +01:00
void stdcpp11 ( ) {
2011-10-16 12:54:58 +02:00
REDIRECT ;
2018-09-24 15:08:16 +02:00
const char * const argv [ ] = { " cppcheck " , " --std=c++11 " , " file.cpp " } ;
2023-08-12 21:43:12 +02:00
settings - > standards . cpp = Standards : : CPP03 ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Success , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-08-12 21:43:12 +02:00
ASSERT ( settings - > standards . cpp = = Standards : : CPP11 ) ;
2011-10-16 12:54:58 +02:00
}
2022-04-15 16:17:36 +02:00
2023-08-12 21:43:12 +02:00
void stdunknown1 ( ) {
REDIRECT ;
const char * const argv [ ] = { " cppcheck " , " --std=d++11 " , " file.cpp " } ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Fail , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-09-26 21:38:15 +02:00
ASSERT_EQUALS ( " cppcheck: error: unknown --std value 'd++11' \n " , logger - > str ( ) ) ;
2023-08-12 21:43:12 +02:00
}
void stdunknown2 ( ) {
2022-04-15 16:17:36 +02:00
REDIRECT ;
2023-08-12 21:43:12 +02:00
const char * const argv [ ] = { " cppcheck " , " --std=cplusplus11 " , " file.cpp " } ;
2023-11-27 12:06:03 +01:00
TODO_ASSERT_EQUALS ( static_cast < int > ( CmdLineParser : : Result : : Fail ) , static_cast < int > ( CmdLineParser : : Result : : Success ) , static_cast < int > ( parser - > parseFromArgs ( 3 , argv ) ) ) ;
2023-09-26 21:38:15 +02:00
TODO_ASSERT_EQUALS ( " cppcheck: error: unknown --std value 'cplusplus11' \n " , " " , logger - > str ( ) ) ;
2022-04-15 16:17:36 +02:00
}
2011-10-16 12:54:58 +02:00
2022-09-17 18:50:25 +02:00
void platformWin64 ( ) {
2013-08-15 11:09:40 +02:00
REDIRECT ;
2018-09-24 15:08:16 +02:00
const char * const argv [ ] = { " cppcheck " , " --platform=win64 " , " file.cpp " } ;
2023-10-13 16:02:04 +02:00
ASSERT ( settings - > platform . set ( Platform : : Type : : Unspecified ) ) ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Success , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-10-13 16:02:04 +02:00
ASSERT_EQUALS ( Platform : : Type : : Win64 , settings - > platform . type ) ;
2022-09-17 18:50:25 +02:00
}
void platformWin32A ( ) {
REDIRECT ;
const char * const argv [ ] = { " cppcheck " , " --platform=win32A " , " file.cpp " } ;
2023-10-13 16:02:04 +02:00
ASSERT ( settings - > platform . set ( Platform : : Type : : Unspecified ) ) ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Success , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-10-13 16:02:04 +02:00
ASSERT_EQUALS ( Platform : : Type : : Win32A , settings - > platform . type ) ;
2022-09-17 18:50:25 +02:00
}
void platformWin32W ( ) {
REDIRECT ;
const char * const argv [ ] = { " cppcheck " , " --platform=win32W " , " file.cpp " } ;
2023-10-13 16:02:04 +02:00
ASSERT ( settings - > platform . set ( Platform : : Type : : Unspecified ) ) ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Success , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-10-13 16:02:04 +02:00
ASSERT_EQUALS ( Platform : : Type : : Win32W , settings - > platform . type ) ;
2022-09-17 18:50:25 +02:00
}
void platformUnix32 ( ) {
REDIRECT ;
const char * const argv [ ] = { " cppcheck " , " --platform=unix32 " , " file.cpp " } ;
2023-10-13 16:02:04 +02:00
ASSERT ( settings - > platform . set ( Platform : : Type : : Unspecified ) ) ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Success , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-10-13 16:02:04 +02:00
ASSERT_EQUALS ( Platform : : Type : : Unix32 , settings - > platform . type ) ;
2022-09-17 18:50:25 +02:00
}
2023-01-26 22:05:40 +01:00
void platformUnix32Unsigned ( ) {
REDIRECT ;
const char * const argv [ ] = { " cppcheck " , " --platform=unix32-unsigned " , " file.cpp " } ;
2023-10-13 16:02:04 +02:00
ASSERT ( settings - > platform . set ( Platform : : Type : : Unspecified ) ) ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Success , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-10-13 16:02:04 +02:00
ASSERT_EQUALS ( Platform : : Type : : Unix32 , settings - > platform . type ) ;
2023-01-26 22:05:40 +01:00
}
2022-09-17 18:50:25 +02:00
void platformUnix64 ( ) {
REDIRECT ;
const char * const argv [ ] = { " cppcheck " , " --platform=unix64 " , " file.cpp " } ;
2023-10-13 16:02:04 +02:00
ASSERT ( settings - > platform . set ( Platform : : Type : : Unspecified ) ) ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Success , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-10-13 16:02:04 +02:00
ASSERT_EQUALS ( Platform : : Type : : Unix64 , settings - > platform . type ) ;
2022-09-17 18:50:25 +02:00
}
2023-01-26 22:05:40 +01:00
void platformUnix64Unsigned ( ) {
REDIRECT ;
const char * const argv [ ] = { " cppcheck " , " --platform=unix64-unsigned " , " file.cpp " } ;
2023-10-13 16:02:04 +02:00
ASSERT ( settings - > platform . set ( Platform : : Type : : Unspecified ) ) ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Success , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-10-13 16:02:04 +02:00
ASSERT_EQUALS ( Platform : : Type : : Unix64 , settings - > platform . type ) ;
2023-01-26 22:05:40 +01:00
}
2022-09-17 18:50:25 +02:00
void platformNative ( ) {
REDIRECT ;
const char * const argv [ ] = { " cppcheck " , " --platform=native " , " file.cpp " } ;
2023-10-13 16:02:04 +02:00
ASSERT ( settings - > platform . set ( Platform : : Type : : Unspecified ) ) ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Success , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-10-13 16:02:04 +02:00
ASSERT_EQUALS ( Platform : : Type : : Native , settings - > platform . type ) ;
2022-09-17 18:50:25 +02:00
}
void platformUnspecified ( ) {
REDIRECT ;
const char * const argv [ ] = { " cppcheck " , " --platform=unspecified " , " file.cpp " } ;
2023-10-13 16:02:04 +02:00
ASSERT ( settings - > platform . set ( Platform : : Type : : Native ) ) ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Success , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-10-13 16:02:04 +02:00
ASSERT_EQUALS ( Platform : : Type : : Unspecified , settings - > platform . type ) ;
2022-09-17 18:50:25 +02:00
}
2023-01-26 22:05:40 +01:00
void platformPlatformFile ( ) {
2022-09-17 18:50:25 +02:00
REDIRECT ;
const char * const argv [ ] = { " cppcheck " , " --platform=avr8 " , " file.cpp " } ;
2023-10-13 16:02:04 +02:00
ASSERT ( settings - > platform . set ( Platform : : Type : : Unspecified ) ) ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Success , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-10-13 16:02:04 +02:00
ASSERT_EQUALS ( Platform : : Type : : File , settings - > platform . type ) ;
2023-01-26 22:05:40 +01:00
}
2022-09-17 18:50:25 +02:00
void platformUnknown ( ) {
REDIRECT ;
const char * const argv [ ] = { " cppcheck " , " --platform=win128 " , " file.cpp " } ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Fail , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-09-26 21:38:15 +02:00
ASSERT_EQUALS ( " cppcheck: error: unrecognized platform: 'win128'. \n " , logger - > str ( ) ) ;
2013-08-15 11:09:40 +02:00
}
2020-11-13 15:52:24 +01:00
void plistEmpty ( ) {
REDIRECT ;
const char * const argv [ ] = { " cppcheck " , " --plist-output= " , " file.cpp " } ;
2023-08-12 21:43:12 +02:00
settings - > plistOutput = " " ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Success , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-08-12 21:43:12 +02:00
ASSERT ( settings - > plistOutput = = " ./ " ) ;
2020-11-13 15:52:24 +01:00
}
void plistDoesNotExist ( ) {
REDIRECT ;
const char * const argv [ ] = { " cppcheck " , " --plist-output=./cppcheck_reports " , " file.cpp " } ;
// Fails since folder pointed by --plist-output= does not exist
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Fail , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-11-25 21:12:24 +01:00
ASSERT_EQUALS ( " cppcheck: error: plist folder does not exist: 'cppcheck_reports'. \n " , logger - > str ( ) ) ;
2020-11-13 15:52:24 +01:00
}
2014-11-20 14:20:09 +01:00
void suppressionsOld ( ) {
2010-12-04 13:13:44 +01:00
REDIRECT ;
2018-09-24 15:08:16 +02:00
const char * const argv [ ] = { " cppcheck " , " --suppressions " , " suppr.txt " , " file.cpp " } ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Fail , parser - > parseFromArgs ( 4 , argv ) ) ;
2023-09-26 21:38:15 +02:00
ASSERT_EQUALS ( " cppcheck: error: unrecognized command line option: \" --suppressions \" . \n " , logger - > str ( ) ) ;
2010-12-04 13:13:44 +01:00
}
2014-11-20 14:20:09 +01:00
void suppressions ( ) {
2011-01-28 12:18:07 +01:00
REDIRECT ;
2023-08-14 11:37:59 +02:00
ScopedFile file ( " suppr.txt " ,
" uninitvar \n "
" unusedFunction \n " ) ;
2018-09-24 15:08:16 +02:00
const char * const argv [ ] = { " cppcheck " , " --suppressions-list=suppr.txt " , " file.cpp " } ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Success , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-08-14 11:37:59 +02:00
ASSERT_EQUALS ( 2 , settings - > nomsg . getSuppressions ( ) . size ( ) ) ;
auto it = settings - > nomsg . getSuppressions ( ) . cbegin ( ) ;
2023-10-16 14:09:03 +02:00
ASSERT_EQUALS ( " uninitvar " , ( it + + ) - > errorId ) ;
ASSERT_EQUALS ( " unusedFunction " , it - > errorId ) ;
2011-01-28 12:18:07 +01:00
}
2023-08-12 21:43:12 +02:00
void suppressionsNoFile1 ( ) {
2011-01-28 12:18:07 +01:00
REDIRECT ;
2023-08-12 21:43:12 +02:00
const char * const argv [ ] = { " cppcheck " , " --suppressions-list= " , " file.cpp " } ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Fail , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-09-26 21:38:15 +02:00
ASSERT_EQUALS ( false , logger - > str ( ) . find ( " If you want to pass two files " ) ! = std : : string : : npos ) ;
2023-08-12 21:43:12 +02:00
}
2012-01-12 21:21:51 +01:00
2023-08-12 21:43:12 +02:00
void suppressionsNoFile2 ( ) {
REDIRECT ;
const char * const argv [ ] = { " cppcheck " , " --suppressions-list=a.suppr,b.suppr " , " file.cpp " } ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Fail , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-09-26 21:38:15 +02:00
ASSERT_EQUALS ( true , logger - > str ( ) . find ( " If you want to pass two files " ) ! = std : : string : : npos ) ;
2023-08-12 21:43:12 +02:00
}
2012-01-12 21:21:51 +01:00
2023-08-12 21:43:12 +02:00
void suppressionsNoFile3 ( ) {
REDIRECT ;
const char * const argv [ ] = { " cppcheck " , " --suppressions-list=a.suppr b.suppr " , " file.cpp " } ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Fail , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-09-26 21:38:15 +02:00
ASSERT_EQUALS ( true , logger - > str ( ) . find ( " If you want to pass two files " ) ! = std : : string : : npos ) ;
2011-01-28 12:18:07 +01:00
}
2018-04-09 06:43:48 +02:00
static Suppressions : : ErrorMessage errorMessage ( const std : : string & errorId , const std : : string & fileName , int lineNumber ) {
Suppressions : : ErrorMessage e ;
e . errorId = errorId ;
e . setFileName ( fileName ) ;
e . lineNumber = lineNumber ;
return e ;
}
2014-11-20 14:20:09 +01:00
void suppressionSingle ( ) {
2011-02-16 10:26:40 +01:00
REDIRECT ;
2018-09-24 15:08:16 +02:00
const char * const argv [ ] = { " cppcheck " , " --suppress=uninitvar " , " file.cpp " } ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Success , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-08-12 21:43:12 +02:00
ASSERT_EQUALS ( true , settings - > nomsg . isSuppressed ( errorMessage ( " uninitvar " , " file.cpp " , 1 ) ) ) ;
2011-02-16 10:26:40 +01:00
}
2014-11-20 14:20:09 +01:00
void suppressionSingleFile ( ) {
2011-02-16 10:26:40 +01:00
REDIRECT ;
2018-09-24 15:08:16 +02:00
const char * const argv [ ] = { " cppcheck " , " --suppress=uninitvar:file.cpp " , " file.cpp " } ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Success , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-08-12 21:43:12 +02:00
ASSERT_EQUALS ( true , settings - > nomsg . isSuppressed ( errorMessage ( " uninitvar " , " file.cpp " , 1U ) ) ) ;
2011-02-16 10:26:40 +01:00
}
2014-11-20 14:20:09 +01:00
void suppressionTwo ( ) {
2011-09-13 16:55:00 +02:00
REDIRECT ;
2018-09-24 15:08:16 +02:00
const char * const argv [ ] = { " cppcheck " , " --suppress=uninitvar,noConstructor " , " file.cpp " } ;
2023-11-27 12:06:03 +01:00
TODO_ASSERT_EQUALS ( static_cast < int > ( CmdLineParser : : Result : : Success ) , static_cast < int > ( CmdLineParser : : Result : : Fail ) , static_cast < int > ( parser - > parseFromArgs ( 3 , argv ) ) ) ;
2023-08-12 21:43:12 +02:00
TODO_ASSERT_EQUALS ( true , false , settings - > nomsg . isSuppressed ( errorMessage ( " uninitvar " , " file.cpp " , 1U ) ) ) ;
TODO_ASSERT_EQUALS ( true , false , settings - > nomsg . isSuppressed ( errorMessage ( " noConstructor " , " file.cpp " , 1U ) ) ) ;
2023-09-26 21:38:15 +02:00
TODO_ASSERT_EQUALS ( " " , " cppcheck: error: Failed to add suppression. Invalid id \" uninitvar,noConstructor \" \n " , logger - > str ( ) ) ;
2011-09-13 16:55:00 +02:00
}
2014-11-20 14:20:09 +01:00
void suppressionTwoSeparate ( ) {
2011-09-13 16:55:00 +02:00
REDIRECT ;
2018-09-24 15:08:16 +02:00
const char * const argv [ ] = { " cppcheck " , " --suppress=uninitvar " , " --suppress=noConstructor " , " file.cpp " } ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Success , parser - > parseFromArgs ( 4 , argv ) ) ;
2023-08-12 21:43:12 +02:00
ASSERT_EQUALS ( true , settings - > nomsg . isSuppressed ( errorMessage ( " uninitvar " , " file.cpp " , 1U ) ) ) ;
ASSERT_EQUALS ( true , settings - > nomsg . isSuppressed ( errorMessage ( " noConstructor " , " file.cpp " , 1U ) ) ) ;
2011-09-13 16:55:00 +02:00
}
2014-11-20 14:20:09 +01:00
void templates ( ) {
2010-12-04 13:13:44 +01:00
REDIRECT ;
2023-08-18 11:59:14 +02:00
const char * const argv [ ] = { " cppcheck " , " --template={file}:{line},{severity},{id},{message} " , " --template-location={file}:{line}:{column} {info} " , " file.cpp " } ;
2023-08-12 21:43:12 +02:00
settings - > templateFormat . clear ( ) ;
settings - > templateLocation . clear ( ) ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Success , parser - > parseFromArgs ( 4 , argv ) ) ;
2023-08-12 21:43:12 +02:00
ASSERT_EQUALS ( " {file}:{line},{severity},{id},{message} " , settings - > templateFormat ) ;
ASSERT_EQUALS ( " {file}:{line}:{column} {info} " , settings - > templateLocation ) ;
2010-12-04 13:13:44 +01:00
}
2014-11-20 14:20:09 +01:00
void templatesGcc ( ) {
2010-12-04 13:13:44 +01:00
REDIRECT ;
2023-08-18 11:59:14 +02:00
const char * const argv [ ] = { " cppcheck " , " --template=gcc " , " file.cpp " } ;
2023-08-12 21:43:12 +02:00
settings - > templateFormat . clear ( ) ;
settings - > templateLocation . clear ( ) ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Success , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-08-12 21:43:12 +02:00
ASSERT_EQUALS ( " {file}:{line}:{column}: warning: {message} [{id}] \n {code} " , settings - > templateFormat ) ;
ASSERT_EQUALS ( " {file}:{line}:{column}: note: {info} \n {code} " , settings - > templateLocation ) ;
2010-12-04 13:13:44 +01:00
}
2014-11-20 14:20:09 +01:00
void templatesVs ( ) {
2010-12-04 13:13:44 +01:00
REDIRECT ;
2023-08-18 11:59:14 +02:00
const char * const argv [ ] = { " cppcheck " , " --template=vs " , " file.cpp " } ;
2023-08-12 21:43:12 +02:00
settings - > templateFormat . clear ( ) ;
settings - > templateLocation . clear ( ) ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Success , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-08-12 21:43:12 +02:00
ASSERT_EQUALS ( " {file}({line}): {severity}: {message} " , settings - > templateFormat ) ;
ASSERT_EQUALS ( " " , settings - > templateLocation ) ;
2010-12-04 13:13:44 +01:00
}
2014-11-20 14:20:09 +01:00
void templatesEdit ( ) {
2011-10-29 23:26:35 +02:00
REDIRECT ;
2023-08-18 11:59:14 +02:00
const char * const argv [ ] = { " cppcheck " , " --template=edit " , " file.cpp " } ;
2023-08-12 21:43:12 +02:00
settings - > templateFormat . clear ( ) ;
settings - > templateLocation . clear ( ) ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Success , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-08-12 21:43:12 +02:00
ASSERT_EQUALS ( " {file} +{line}: {severity}: {message} " , settings - > templateFormat ) ;
ASSERT_EQUALS ( " " , settings - > templateLocation ) ;
2023-04-08 22:19:52 +02:00
}
void templatesCppcheck1 ( ) {
REDIRECT ;
const char * const argv [ ] = { " cppcheck " , " --template=cppcheck1 " , " file.cpp " } ;
2023-08-12 21:43:12 +02:00
settings - > templateFormat . clear ( ) ;
settings - > templateLocation . clear ( ) ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Success , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-08-12 21:43:12 +02:00
ASSERT_EQUALS ( " {callstack}: ({severity}{inconclusive:, inconclusive}) {message} " , settings - > templateFormat ) ;
ASSERT_EQUALS ( " " , settings - > templateLocation ) ;
2023-04-08 22:19:52 +02:00
}
void templatesDaca2 ( ) {
REDIRECT ;
const char * const argv [ ] = { " cppcheck " , " --template=daca2 " , " file.cpp " } ;
2023-08-12 21:43:12 +02:00
settings - > templateFormat . clear ( ) ;
settings - > templateLocation . clear ( ) ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Success , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-08-12 21:43:12 +02:00
ASSERT_EQUALS ( " {file}:{line}:{column}: {severity}:{inconclusive:inconclusive:} {message} [{id}] " , settings - > templateFormat ) ;
ASSERT_EQUALS ( " {file}:{line}:{column}: note: {info} " , settings - > templateLocation ) ;
ASSERT_EQUALS ( true , settings - > daca ) ;
2023-04-08 22:19:52 +02:00
}
void templatesSelfcheck ( ) {
REDIRECT ;
const char * const argv [ ] = { " cppcheck " , " --template=selfcheck " , " file.cpp " } ;
2023-08-12 21:43:12 +02:00
settings - > templateFormat . clear ( ) ;
settings - > templateLocation . clear ( ) ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Success , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-08-12 21:43:12 +02:00
ASSERT_EQUALS ( " {file}:{line}:{column}: {severity}:{inconclusive:inconclusive:} {message} [{id}] \n {code} " , settings - > templateFormat ) ;
ASSERT_EQUALS ( " {file}:{line}:{column}: note: {info} \n {code} " , settings - > templateLocation ) ;
2023-04-08 22:19:52 +02:00
}
// TODO: we should bail out on this
void templatesNoPlaceholder ( ) {
REDIRECT ;
const char * const argv [ ] = { " cppcheck " , " --template=selfchek " , " file.cpp " } ;
2023-08-12 21:43:12 +02:00
settings - > templateFormat . clear ( ) ;
settings - > templateLocation . clear ( ) ;
2023-11-27 12:06:03 +01:00
TODO_ASSERT_EQUALS ( static_cast < int > ( CmdLineParser : : Result : : Fail ) , static_cast < int > ( CmdLineParser : : Result : : Success ) , static_cast < int > ( parser - > parseFromArgs ( 3 , argv ) ) ) ;
2023-08-12 21:43:12 +02:00
ASSERT_EQUALS ( " selfchek " , settings - > templateFormat ) ;
ASSERT_EQUALS ( " " , settings - > templateLocation ) ;
2023-04-08 22:19:52 +02:00
}
void templateFormatInvalid ( ) {
REDIRECT ;
2023-08-12 21:43:12 +02:00
settings - > templateFormat . clear ( ) ;
settings - > templateLocation . clear ( ) ;
2023-04-08 22:19:52 +02:00
const char * const argv [ ] = { " cppcheck " , " --template " , " file.cpp " } ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Fail , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-09-26 21:38:15 +02:00
ASSERT_EQUALS ( " cppcheck: error: unrecognized command line option: \" --template \" . \n " , logger - > str ( ) ) ;
2023-04-08 22:19:52 +02:00
}
// will use the default
// TODO: bail out on empty?
void templateFormatEmpty ( ) {
REDIRECT ;
2023-08-12 21:43:12 +02:00
settings - > templateFormat . clear ( ) ;
settings - > templateLocation . clear ( ) ;
2023-04-08 22:19:52 +02:00
const char * const argv [ ] = { " cppcheck " , " --template= " , " file.cpp " } ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Success , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-08-12 21:43:12 +02:00
ASSERT_EQUALS ( " {file}:{line}:{column}: {inconclusive:}{severity}:{inconclusive: inconclusive:} {message} [{id}] \n {code} " , settings - > templateFormat ) ;
ASSERT_EQUALS ( " {file}:{line}:{column}: note: {info} \n {code} " , settings - > templateLocation ) ;
2023-04-08 22:19:52 +02:00
}
void templateLocationInvalid ( ) {
REDIRECT ;
const char * const argv [ ] = { " cppcheck " , " --template-location " , " --template={file} " , " file.cpp " } ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Fail , parser - > parseFromArgs ( 4 , argv ) ) ;
2023-09-26 21:38:15 +02:00
ASSERT_EQUALS ( " cppcheck: error: unrecognized command line option: \" --template-location \" . \n " , logger - > str ( ) ) ;
2023-04-08 22:19:52 +02:00
}
// will use the default
// TODO: bail out on empty?
void templateLocationEmpty ( ) {
REDIRECT ;
2023-08-12 21:43:12 +02:00
settings - > templateFormat . clear ( ) ;
settings - > templateLocation . clear ( ) ;
2023-04-08 22:19:52 +02:00
const char * const argv [ ] = { " cppcheck " , " --template-location= " , " file.cpp " } ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Success , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-08-12 21:43:12 +02:00
ASSERT_EQUALS ( " {file}:{line}:{column}: {inconclusive:}{severity}:{inconclusive: inconclusive:} {message} [{id}] \n {code} " , settings - > templateFormat ) ;
ASSERT_EQUALS ( " {file}:{line}:{column}: note: {info} \n {code} " , settings - > templateLocation ) ;
2011-10-29 23:26:35 +02:00
}
2014-11-20 14:20:09 +01:00
void xml ( ) {
2010-12-04 13:13:44 +01:00
REDIRECT ;
2018-09-24 15:08:16 +02:00
const char * const argv [ ] = { " cppcheck " , " --xml " , " file.cpp " } ;
2023-08-12 21:43:12 +02:00
settings - > xml_version = 1 ;
settings - > xml = false ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Success , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-08-12 21:43:12 +02:00
ASSERT ( settings - > xml ) ;
ASSERT_EQUALS ( 1 , settings - > xml_version ) ;
2011-02-02 13:04:50 +01:00
}
2014-11-20 14:20:09 +01:00
void xmlver2 ( ) {
2011-01-27 13:25:10 +01:00
REDIRECT ;
2018-09-24 15:08:16 +02:00
const char * const argv [ ] = { " cppcheck " , " --xml-version=2 " , " file.cpp " } ;
2023-08-12 21:43:12 +02:00
settings - > xml_version = 1 ;
settings - > xml = false ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Success , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-08-12 21:43:12 +02:00
ASSERT ( settings - > xml ) ;
ASSERT_EQUALS ( 2 , settings - > xml_version ) ;
2011-01-27 13:25:10 +01:00
}
2014-11-20 14:20:09 +01:00
void xmlver2both ( ) {
2011-01-27 13:25:10 +01:00
REDIRECT ;
2018-09-24 15:08:16 +02:00
const char * const argv [ ] = { " cppcheck " , " --xml " , " --xml-version=2 " , " file.cpp " } ;
2023-08-12 21:43:12 +02:00
settings - > xml_version = 1 ;
settings - > xml = false ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Success , parser - > parseFromArgs ( 4 , argv ) ) ;
2023-08-12 21:43:12 +02:00
ASSERT ( settings - > xml ) ;
ASSERT_EQUALS ( 2 , settings - > xml_version ) ;
2011-01-27 13:25:10 +01:00
}
2014-11-20 14:20:09 +01:00
void xmlver2both2 ( ) {
2011-01-27 13:25:10 +01:00
REDIRECT ;
2018-09-24 15:08:16 +02:00
const char * const argv [ ] = { " cppcheck " , " --xml-version=2 " , " --xml " , " file.cpp " } ;
2023-08-12 21:43:12 +02:00
settings - > xml_version = 1 ;
settings - > xml = false ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Success , parser - > parseFromArgs ( 4 , argv ) ) ;
2023-08-12 21:43:12 +02:00
ASSERT ( settings - > xml ) ;
ASSERT_EQUALS ( 2 , settings - > xml_version ) ;
2010-12-04 13:13:44 +01:00
}
2014-11-20 14:20:09 +01:00
void xmlverunknown ( ) {
2011-02-02 12:33:57 +01:00
REDIRECT ;
2018-09-24 15:08:16 +02:00
const char * const argv [ ] = { " cppcheck " , " --xml " , " --xml-version=3 " , " file.cpp " } ;
2011-05-04 20:00:22 +02:00
// FAils since unknown XML format version
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Fail , parser - > parseFromArgs ( 4 , argv ) ) ;
2023-09-26 21:38:15 +02:00
ASSERT_EQUALS ( " cppcheck: error: '--xml-version' can only be 2. \n " , logger - > str ( ) ) ;
2011-02-02 12:33:57 +01:00
}
2014-11-20 14:20:09 +01:00
void xmlverinvalid ( ) {
2011-02-02 12:33:57 +01:00
REDIRECT ;
2018-09-24 15:08:16 +02:00
const char * const argv [ ] = { " cppcheck " , " --xml " , " --xml-version=a " , " file.cpp " } ;
2011-05-04 20:00:22 +02:00
// FAils since unknown XML format version
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Fail , parser - > parseFromArgs ( 4 , argv ) ) ;
2023-09-26 21:38:15 +02:00
ASSERT_EQUALS ( " cppcheck: error: argument to '--xml-version=' is not valid - not an integer. \n " , logger - > str ( ) ) ;
2011-02-02 12:33:57 +01:00
}
2014-11-20 14:20:09 +01:00
void doc ( ) {
2013-08-15 11:09:40 +02:00
REDIRECT ;
2018-09-24 15:08:16 +02:00
const char * const argv [ ] = { " cppcheck " , " --doc " } ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Exit , parser - > parseFromArgs ( 2 , argv ) ) ;
2023-10-09 22:22:39 +02:00
ASSERT ( startsWith ( logger - > str ( ) , " ## " ) ) ;
2013-08-15 11:09:40 +02:00
}
2023-12-01 14:19:47 +01:00
void docExclusive ( ) {
REDIRECT ;
const char * const argv [ ] = { " cppcheck " , " --library=missing " , " --doc " } ;
ASSERT_EQUALS ( CmdLineParser : : Result : : Exit , parser - > parseFromArgs ( 3 , argv ) ) ;
ASSERT ( startsWith ( logger - > str ( ) , " ## " ) ) ;
}
2023-10-05 19:04:06 +02:00
void showtimeSummary ( ) {
2013-08-15 11:09:40 +02:00
REDIRECT ;
2023-08-12 21:43:12 +02:00
const char * const argv [ ] = { " cppcheck " , " --showtime=summary " , " file.cpp " } ;
settings - > showtime = SHOWTIME_MODES : : SHOWTIME_NONE ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Success , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-08-12 21:43:12 +02:00
ASSERT ( settings - > showtime = = SHOWTIME_MODES : : SHOWTIME_SUMMARY ) ;
2013-08-15 11:09:40 +02:00
}
2023-10-05 19:04:06 +02:00
void showtimeFile ( ) {
REDIRECT ;
const char * const argv [ ] = { " cppcheck " , " --showtime=file " , " file.cpp " } ;
settings - > showtime = SHOWTIME_MODES : : SHOWTIME_NONE ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Success , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-10-05 19:04:06 +02:00
ASSERT ( settings - > showtime = = SHOWTIME_MODES : : SHOWTIME_FILE ) ;
}
void showtimeFileTotal ( ) {
REDIRECT ;
const char * const argv [ ] = { " cppcheck " , " --showtime=file-total " , " file.cpp " } ;
settings - > showtime = SHOWTIME_MODES : : SHOWTIME_NONE ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Success , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-10-05 19:04:06 +02:00
ASSERT ( settings - > showtime = = SHOWTIME_MODES : : SHOWTIME_FILE_TOTAL ) ;
}
void showtimeTop5 ( ) {
REDIRECT ;
const char * const argv [ ] = { " cppcheck " , " --showtime=top5 " , " file.cpp " } ;
settings - > showtime = SHOWTIME_MODES : : SHOWTIME_NONE ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Success , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-10-05 19:04:06 +02:00
ASSERT ( settings - > showtime = = SHOWTIME_MODES : : SHOWTIME_TOP5_FILE ) ;
ASSERT_EQUALS ( " cppcheck: --showtime=top5 is deprecated and will be removed in Cppcheck 2.14. Please use --showtime=top5_file or --showtime=top5_summary instead. \n " , logger - > str ( ) ) ;
}
void showtimeTop5File ( ) {
REDIRECT ;
const char * const argv [ ] = { " cppcheck " , " --showtime=top5_file " , " file.cpp " } ;
settings - > showtime = SHOWTIME_MODES : : SHOWTIME_NONE ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Success , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-10-05 19:04:06 +02:00
ASSERT ( settings - > showtime = = SHOWTIME_MODES : : SHOWTIME_TOP5_FILE ) ;
}
void showtimeTop5Summary ( ) {
REDIRECT ;
const char * const argv [ ] = { " cppcheck " , " --showtime=top5_summary " , " file.cpp " } ;
settings - > showtime = SHOWTIME_MODES : : SHOWTIME_NONE ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Success , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-10-05 19:04:06 +02:00
ASSERT ( settings - > showtime = = SHOWTIME_MODES : : SHOWTIME_TOP5_SUMMARY ) ;
}
void showtimeNone ( ) {
REDIRECT ;
const char * const argv [ ] = { " cppcheck " , " --showtime=none " , " file.cpp " } ;
settings - > showtime = SHOWTIME_MODES : : SHOWTIME_FILE ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Success , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-10-05 19:04:06 +02:00
ASSERT ( settings - > showtime = = SHOWTIME_MODES : : SHOWTIME_NONE ) ;
}
void showtimeEmpty ( ) {
REDIRECT ;
const char * const argv [ ] = { " cppcheck " , " --showtime= " , " file.cpp " } ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Fail , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-10-05 19:04:06 +02:00
ASSERT_EQUALS ( " cppcheck: error: no mode provided for --showtime \n " , logger - > str ( ) ) ;
}
void showtimeInvalid ( ) {
REDIRECT ;
const char * const argv [ ] = { " cppcheck " , " --showtime=top10 " , " file.cpp " } ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Fail , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-10-05 19:04:06 +02:00
ASSERT_EQUALS ( " cppcheck: error: unrecognized --showtime mode: 'top10'. Supported modes: file, file-total, summary, top5, top5_file, top5_summary. \n " , logger - > str ( ) ) ;
}
2023-11-25 21:12:24 +01:00
void errorlist ( ) {
2011-01-27 10:14:08 +01:00
REDIRECT ;
2018-09-24 15:08:16 +02:00
const char * const argv [ ] = { " cppcheck " , " --errorlist " } ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Exit , parser - > parseFromArgs ( 2 , argv ) ) ;
2023-11-25 21:12:24 +01:00
ASSERT_EQUALS ( " " , logger - > str ( ) ) ; // empty since it is logged via ErrorLogger
2023-12-01 20:56:30 +01:00
const std : : string errout_s = GET_REDIRECT_OUTPUT ;
ASSERT ( startsWith ( errout_s , ErrorMessage : : getXMLHeader ( " " ) ) ) ;
ASSERT ( endsWith ( errout_s , " </results> \n " ) ) ;
2011-01-27 10:14:08 +01:00
}
2023-12-01 16:34:14 +01:00
void errorlistWithCfg ( ) {
REDIRECT ;
ScopedFile file ( Path : : join ( Path : : getPathFromFilename ( Path : : getCurrentExecutablePath ( " " ) ) , " cppcheck.cfg " ) ,
R " ({ " productName " : " The Product " } \n ) " ) ;
const char * const argv [ ] = { " cppcheck " , " --errorlist " } ;
ASSERT_EQUALS ( CmdLineParser : : Result : : Exit , parser - > parseFromArgs ( 2 , argv ) ) ;
ASSERT_EQUALS ( " " , logger - > str ( ) ) ; // empty since it is logged via ErrorLogger
ASSERT ( startsWith ( GET_REDIRECT_OUTPUT , ErrorMessage : : getXMLHeader ( " The Product " ) ) ) ;
}
2011-01-27 10:14:08 +01:00
2023-12-01 14:19:47 +01:00
void errorlistExclusive ( ) {
REDIRECT ;
const char * const argv [ ] = { " cppcheck " , " --library=missing " , " --errorlist " } ;
ASSERT_EQUALS ( CmdLineParser : : Result : : Exit , parser - > parseFromArgs ( 3 , argv ) ) ;
ASSERT_EQUALS ( " " , logger - > str ( ) ) ; // empty since it is logged via ErrorLogger
2023-12-01 20:56:30 +01:00
const std : : string errout_s = GET_REDIRECT_OUTPUT ;
ASSERT ( startsWith ( errout_s , ErrorMessage : : getXMLHeader ( " " ) ) ) ;
ASSERT ( endsWith ( errout_s , " </results> \n " ) ) ;
2023-12-01 14:19:47 +01:00
}
2023-12-01 16:34:14 +01:00
void errorlistWithInvalidCfg ( ) {
REDIRECT ;
ScopedFile file ( Path : : join ( Path : : getPathFromFilename ( Path : : getCurrentExecutablePath ( " " ) ) , " cppcheck.cfg " ) ,
" { \n " ) ;
const char * const argv [ ] = { " cppcheck " , " --errorlist " } ;
ASSERT_EQUALS ( CmdLineParser : : Result : : Fail , parser - > parseFromArgs ( 2 , argv ) ) ;
2023-12-05 20:39:26 +01:00
ASSERT_EQUALS ( " cppcheck: error: could not load cppcheck.cfg - not a valid JSON - syntax error at line 2 near: \n " , logger - > str ( ) ) ;
2023-12-01 16:34:14 +01:00
}
2014-11-20 14:20:09 +01:00
void ignorepathsnopath ( ) {
2011-01-31 14:25:51 +01:00
REDIRECT ;
2018-09-24 15:08:16 +02:00
const char * const argv [ ] = { " cppcheck " , " -i " } ;
2011-05-04 20:00:22 +02:00
// Fails since no ignored path given
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Fail , parser - > parseFromArgs ( 2 , argv ) ) ;
2023-09-26 21:38:15 +02:00
ASSERT_EQUALS ( " cppcheck: error: argument to '-i' is missing. \n " , logger - > str ( ) ) ;
2011-01-31 14:25:51 +01:00
}
2023-10-21 09:12:59 +02:00
# if defined(USE_WINDOWS_SEH) || defined(USE_UNIX_SIGNAL_HANDLING)
2022-08-20 20:54:31 +02:00
void exceptionhandling ( ) {
REDIRECT ;
2023-08-12 21:43:12 +02:00
const char * const argv [ ] = { " cppcheck " , " --exception-handling " , " file.cpp " } ;
settings - > exceptionHandling = false ;
2022-08-20 20:54:31 +02:00
CppCheckExecutor : : setExceptionOutput ( stderr ) ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Success , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-08-12 21:43:12 +02:00
ASSERT ( settings - > exceptionHandling ) ;
2022-08-20 20:54:31 +02:00
ASSERT_EQUALS ( stderr , CppCheckExecutor : : getExceptionOutput ( ) ) ;
}
void exceptionhandling2 ( ) {
REDIRECT ;
2023-08-12 21:43:12 +02:00
const char * const argv [ ] = { " cppcheck " , " --exception-handling=stderr " , " file.cpp " } ;
settings - > exceptionHandling = false ;
2022-08-20 20:54:31 +02:00
CppCheckExecutor : : setExceptionOutput ( stdout ) ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Success , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-08-12 21:43:12 +02:00
ASSERT ( settings - > exceptionHandling ) ;
2022-08-20 20:54:31 +02:00
ASSERT_EQUALS ( stderr , CppCheckExecutor : : getExceptionOutput ( ) ) ;
}
void exceptionhandling3 ( ) {
REDIRECT ;
2023-08-12 21:43:12 +02:00
const char * const argv [ ] = { " cppcheck " , " --exception-handling=stdout " , " file.cpp " } ;
settings - > exceptionHandling = false ;
2022-08-20 20:54:31 +02:00
CppCheckExecutor : : setExceptionOutput ( stderr ) ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Success , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-08-12 21:43:12 +02:00
ASSERT ( settings - > exceptionHandling ) ;
2022-08-20 20:54:31 +02:00
ASSERT_EQUALS ( stdout , CppCheckExecutor : : getExceptionOutput ( ) ) ;
}
void exceptionhandlingInvalid ( ) {
REDIRECT ;
const char * const argv [ ] = { " cppcheck " , " --exception-handling=exfile " } ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Fail , parser - > parseFromArgs ( 2 , argv ) ) ;
2023-09-26 21:38:15 +02:00
ASSERT_EQUALS ( " cppcheck: error: invalid '--exception-handling' argument \n " , logger - > str ( ) ) ;
2022-08-20 20:54:31 +02:00
}
void exceptionhandlingInvalid2 ( ) {
REDIRECT ;
const char * const argv [ ] = { " cppcheck " , " --exception-handling-foo " } ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Fail , parser - > parseFromArgs ( 2 , argv ) ) ;
2023-09-26 21:38:15 +02:00
ASSERT_EQUALS ( " cppcheck: error: unrecognized command line option: \" --exception-handling-foo \" . \n " , logger - > str ( ) ) ;
2022-08-20 20:54:31 +02:00
}
2023-10-21 09:12:59 +02:00
# else
void exceptionhandlingNotSupported ( ) {
REDIRECT ;
const char * const argv [ ] = { " cppcheck " , " --exception-handling " , " file.cpp " } ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Fail , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-10-21 09:12:59 +02:00
ASSERT_EQUALS ( " cppcheck: error: Option --exception-handling is not supported since Cppcheck has not been built with any exception handling enabled. \n " , logger - > str ( ) ) ;
}
void exceptionhandlingNotSupported2 ( ) {
REDIRECT ;
const char * const argv [ ] = { " cppcheck " , " --exception-handling=stderr " , " file.cpp " } ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Fail , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-10-21 09:12:59 +02:00
ASSERT_EQUALS ( " cppcheck: error: Option --exception-handling is not supported since Cppcheck has not been built with any exception handling enabled. \n " , logger - > str ( ) ) ;
}
# endif
2022-08-20 20:54:31 +02:00
void clang ( ) {
REDIRECT ;
2023-08-12 21:43:12 +02:00
const char * const argv [ ] = { " cppcheck " , " --clang " , " file.cpp " } ;
settings - > clang = false ;
settings - > clangExecutable = " exe " ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Success , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-08-12 21:43:12 +02:00
ASSERT ( settings - > clang ) ;
ASSERT_EQUALS ( " exe " , settings - > clangExecutable ) ;
2022-08-20 20:54:31 +02:00
}
void clang2 ( ) {
REDIRECT ;
2023-08-12 21:43:12 +02:00
const char * const argv [ ] = { " cppcheck " , " --clang=clang-14 " , " file.cpp " } ;
settings - > clang = false ;
settings - > clangExecutable = " " ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Success , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-08-12 21:43:12 +02:00
ASSERT ( settings - > clang ) ;
ASSERT_EQUALS ( " clang-14 " , settings - > clangExecutable ) ;
2022-08-20 20:54:31 +02:00
}
void clangInvalid ( ) {
REDIRECT ;
const char * const argv [ ] = { " cppcheck " , " --clang-foo " } ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Fail , parser - > parseFromArgs ( 2 , argv ) ) ;
2023-09-26 21:38:15 +02:00
ASSERT_EQUALS ( " cppcheck: error: unrecognized command line option: \" --clang-foo \" . \n " , logger - > str ( ) ) ;
2022-12-20 20:51:08 +01:00
}
void valueFlowMaxIterations ( ) {
REDIRECT ;
2023-08-12 21:43:12 +02:00
const char * const argv [ ] = { " cppcheck " , " --valueflow-max-iterations=0 " , " file.cpp " } ;
settings - > valueFlowMaxIterations = SIZE_MAX ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Success , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-08-12 21:43:12 +02:00
ASSERT_EQUALS ( 0 , settings - > valueFlowMaxIterations ) ;
2022-12-20 20:51:08 +01:00
}
void valueFlowMaxIterations2 ( ) {
REDIRECT ;
2023-08-12 21:43:12 +02:00
const char * const argv [ ] = { " cppcheck " , " --valueflow-max-iterations=11 " , " file.cpp " } ;
settings - > valueFlowMaxIterations = SIZE_MAX ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Success , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-08-12 21:43:12 +02:00
ASSERT_EQUALS ( 11 , settings - > valueFlowMaxIterations ) ;
2022-12-20 20:51:08 +01:00
}
void valueFlowMaxIterationsInvalid ( ) {
REDIRECT ;
const char * const argv [ ] = { " cppcheck " , " --valueflow-max-iterations " } ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Fail , parser - > parseFromArgs ( 2 , argv ) ) ;
2023-09-26 21:38:15 +02:00
ASSERT_EQUALS ( " cppcheck: error: unrecognized command line option: \" --valueflow-max-iterations \" . \n " , logger - > str ( ) ) ;
2022-12-20 20:51:08 +01:00
}
void valueFlowMaxIterationsInvalid2 ( ) {
REDIRECT ;
const char * const argv [ ] = { " cppcheck " , " --valueflow-max-iterations=seven " } ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Fail , parser - > parseFromArgs ( 2 , argv ) ) ;
2023-09-26 21:38:15 +02:00
ASSERT_EQUALS ( " cppcheck: error: argument to '--valueflow-max-iterations=' is not valid - not an integer. \n " , logger - > str ( ) ) ;
2022-12-20 20:51:08 +01:00
}
void valueFlowMaxIterationsInvalid3 ( ) {
REDIRECT ;
const char * const argv [ ] = { " cppcheck " , " --valueflow-max-iterations=-1 " } ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Fail , parser - > parseFromArgs ( 2 , argv ) ) ;
2023-09-26 21:38:15 +02:00
ASSERT_EQUALS ( " cppcheck: error: argument to '--valueflow-max-iterations=' is not valid - needs to be positive. \n " , logger - > str ( ) ) ;
2023-04-08 22:29:09 +02:00
}
void checksMaxTime ( ) {
REDIRECT ;
const char * const argv [ ] = { " cppcheck " , " --checks-max-time=12 " , " file.cpp " } ;
2023-08-12 21:43:12 +02:00
settings - > checksMaxTime = 0 ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Success , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-08-12 21:43:12 +02:00
ASSERT_EQUALS ( 12 , settings - > checksMaxTime ) ;
2023-04-08 22:29:09 +02:00
}
void checksMaxTime2 ( ) {
REDIRECT ;
const char * const argv [ ] = { " cppcheck " , " --checks-max-time=-1 " , " file.cpp " } ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Fail , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-09-26 21:38:15 +02:00
ASSERT_EQUALS ( " cppcheck: error: argument to '--checks-max-time=' needs to be a positive integer. \n " , logger - > str ( ) ) ;
2023-04-08 22:29:09 +02:00
}
void checksMaxTimeInvalid ( ) {
REDIRECT ;
const char * const argv [ ] = { " cppcheck " , " --checks-max-time=one " , " file.cpp " } ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Fail , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-09-26 21:38:15 +02:00
ASSERT_EQUALS ( " cppcheck: error: argument to '--checks-max-time=' is not valid - not an integer. \n " , logger - > str ( ) ) ;
2023-04-08 22:29:09 +02:00
}
# ifdef THREADING_MODEL_FORK
void loadAverage ( ) {
REDIRECT ;
const char * const argv [ ] = { " cppcheck " , " -l " , " 12 " , " file.cpp " } ;
2023-08-12 21:43:12 +02:00
settings - > loadAverage = 0 ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Success , parser - > parseFromArgs ( 4 , argv ) ) ;
2023-08-12 21:43:12 +02:00
ASSERT_EQUALS ( 12 , settings - > loadAverage ) ;
2023-04-08 22:29:09 +02:00
}
void loadAverage2 ( ) {
REDIRECT ;
const char * const argv [ ] = { " cppcheck " , " -l12 " , " file.cpp " } ;
2023-08-12 21:43:12 +02:00
settings - > loadAverage = 0 ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Success , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-08-12 21:43:12 +02:00
ASSERT_EQUALS ( 12 , settings - > loadAverage ) ;
2023-04-08 22:29:09 +02:00
}
void loadAverageInvalid ( ) {
REDIRECT ;
const char * const argv [ ] = { " cppcheck " , " -l " , " one " , " file.cpp " } ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Fail , parser - > parseFromArgs ( 4 , argv ) ) ;
2023-09-26 21:38:15 +02:00
ASSERT_EQUALS ( " cppcheck: error: argument to '-l' is not valid - not an integer. \n " , logger - > str ( ) ) ;
2023-04-08 22:29:09 +02:00
}
2023-10-21 09:12:59 +02:00
# else
void loadAverageNotSupported ( ) {
REDIRECT ;
const char * const argv [ ] = { " cppcheck " , " -l " , " 12 " , " file.cpp " } ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Fail , parser - > parseFromArgs ( 4 , argv ) ) ;
2023-10-21 09:12:59 +02:00
ASSERT_EQUALS ( " cppcheck: error: Option -l cannot be used as Cppcheck has not been built with fork threading model. \n " , logger - > str ( ) ) ;
}
2023-04-08 22:29:09 +02:00
# endif
void maxCtuDepth ( ) {
REDIRECT ;
const char * const argv [ ] = { " cppcheck " , " --max-ctu-depth=12 " , " file.cpp " } ;
2023-08-12 21:43:12 +02:00
settings - > maxCtuDepth = 0 ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Success , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-08-12 21:43:12 +02:00
ASSERT_EQUALS ( 12 , settings - > maxCtuDepth ) ;
2023-04-08 22:29:09 +02:00
}
void maxCtuDepthInvalid ( ) {
REDIRECT ;
const char * const argv [ ] = { " cppcheck " , " --max-ctu-depth=one " , " file.cpp " } ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Fail , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-09-26 21:38:15 +02:00
ASSERT_EQUALS ( " cppcheck: error: argument to '--max-ctu-depth=' is not valid - not an integer. \n " , logger - > str ( ) ) ;
2023-04-08 22:29:09 +02:00
}
void performanceValueflowMaxTime ( ) {
REDIRECT ;
const char * const argv [ ] = { " cppcheck " , " --performance-valueflow-max-time=12 " , " file.cpp " } ;
2023-08-12 21:43:12 +02:00
settings - > performanceValueFlowMaxTime = 0 ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Success , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-08-12 21:43:12 +02:00
ASSERT_EQUALS ( 12 , settings - > performanceValueFlowMaxTime ) ;
2023-04-08 22:29:09 +02:00
}
void performanceValueflowMaxTimeInvalid ( ) {
REDIRECT ;
const char * const argv [ ] = { " cppcheck " , " --performance-valueflow-max-time=one " , " file.cpp " } ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Fail , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-09-26 21:38:15 +02:00
ASSERT_EQUALS ( " cppcheck: error: argument to '--performance-valueflow-max-time=' is not valid - not an integer. \n " , logger - > str ( ) ) ;
2023-04-08 22:29:09 +02:00
}
void performanceValueFlowMaxIfCount ( ) {
REDIRECT ;
const char * const argv [ ] = { " cppcheck " , " --performance-valueflow-max-if-count=12 " , " file.cpp " } ;
2023-08-12 21:43:12 +02:00
settings - > performanceValueFlowMaxIfCount = 0 ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Success , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-08-12 21:43:12 +02:00
ASSERT_EQUALS ( 12 , settings - > performanceValueFlowMaxIfCount ) ;
2023-04-08 22:29:09 +02:00
}
void performanceValueFlowMaxIfCountInvalid ( ) {
REDIRECT ;
const char * const argv [ ] = { " cppcheck " , " --performance-valueflow-max-if-count=one " , " file.cpp " } ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Fail , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-09-26 21:38:15 +02:00
ASSERT_EQUALS ( " cppcheck: error: argument to '--performance-valueflow-max-if-count=' is not valid - not an integer. \n " , logger - > str ( ) ) ;
2022-08-20 20:54:31 +02:00
}
2023-04-08 22:29:09 +02:00
void templateMaxTime ( ) {
REDIRECT ;
const char * const argv [ ] = { " cppcheck " , " --template-max-time=12 " , " file.cpp " } ;
2023-08-12 21:43:12 +02:00
settings - > templateMaxTime = 0 ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Success , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-08-12 21:43:12 +02:00
ASSERT_EQUALS ( 12 , settings - > templateMaxTime ) ;
2023-04-08 22:29:09 +02:00
}
void templateMaxTimeInvalid ( ) {
REDIRECT ;
const char * const argv [ ] = { " cppcheck " , " --template-max-time=one " , " file.cpp " } ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Fail , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-09-26 21:38:15 +02:00
ASSERT_EQUALS ( " cppcheck: error: argument to '--template-max-time=' is not valid - not an integer. \n " , logger - > str ( ) ) ;
2023-04-08 22:29:09 +02:00
}
void templateMaxTimeInvalid2 ( ) {
REDIRECT ;
const char * const argv [ ] = { " cppcheck " , " --template-max-time=-1 " , " file.cpp " } ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Fail , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-09-26 21:38:15 +02:00
ASSERT_EQUALS ( " cppcheck: error: argument to '--template-max-time=' is not valid - needs to be positive. \n " , logger - > str ( ) ) ;
2023-04-08 22:29:09 +02:00
}
void typedefMaxTime ( ) {
REDIRECT ;
const char * const argv [ ] = { " cppcheck " , " --typedef-max-time=12 " , " file.cpp " } ;
2023-08-12 21:43:12 +02:00
settings - > typedefMaxTime = 0 ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Success , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-08-12 21:43:12 +02:00
ASSERT_EQUALS ( 12 , settings - > typedefMaxTime ) ;
2023-04-08 22:29:09 +02:00
}
void typedefMaxTimeInvalid ( ) {
REDIRECT ;
const char * const argv [ ] = { " cppcheck " , " --typedef-max-time=one " , " file.cpp " } ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Fail , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-09-26 21:38:15 +02:00
ASSERT_EQUALS ( " cppcheck: error: argument to '--typedef-max-time=' is not valid - not an integer. \n " , logger - > str ( ) ) ;
2023-04-08 22:29:09 +02:00
}
void typedefMaxTimeInvalid2 ( ) {
REDIRECT ;
const char * const argv [ ] = { " cppcheck " , " --typedef-max-time=-1 " , " file.cpp " } ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Fail , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-09-26 21:38:15 +02:00
ASSERT_EQUALS ( " cppcheck: error: argument to '--typedef-max-time=' is not valid - needs to be positive. \n " , logger - > str ( ) ) ;
2023-04-08 22:29:09 +02:00
}
2023-10-05 21:36:44 +02:00
void project ( ) {
REDIRECT ;
2023-10-08 09:07:15 +02:00
ScopedFile file ( " project.cppcheck " ,
" <project> \n "
" <paths> \n "
" <dir name= \" dir \" /> \n "
" </paths> \n "
" </project> " ) ;
const char * const argv [ ] = { " cppcheck " , " --project=project.cppcheck " } ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Success , parser - > parseFromArgs ( 2 , argv ) ) ;
2023-10-08 09:07:15 +02:00
ASSERT_EQUALS ( 1 , parser - > getPathNames ( ) . size ( ) ) ;
auto it = parser - > getPathNames ( ) . cbegin ( ) ;
ASSERT_EQUALS ( " dir " , * it ) ;
2023-10-05 21:36:44 +02:00
}
void projectMultiple ( ) {
REDIRECT ;
ScopedFile file ( " project.cppcheck " , " <project></project> " ) ;
const char * const argv [ ] = { " cppcheck " , " --project=project.cppcheck " , " --project=project.cppcheck " , " file.cpp " } ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Fail , parser - > parseFromArgs ( 4 , argv ) ) ;
2023-10-05 21:36:44 +02:00
ASSERT_EQUALS ( " cppcheck: error: multiple --project options are not supported. \n " , logger - > str ( ) ) ;
}
2023-10-08 09:07:15 +02:00
void projectAndSource ( ) {
REDIRECT ;
ScopedFile file ( " project.cppcheck " , " <project></project> " ) ;
const char * const argv [ ] = { " cppcheck " , " --project=project.cppcheck " , " file.cpp " } ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Fail , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-10-08 09:07:15 +02:00
ASSERT_EQUALS ( " cppcheck: error: --project cannot be used in conjunction with source files. \n " , logger - > str ( ) ) ;
}
2023-10-05 21:36:44 +02:00
void projectEmpty ( ) {
REDIRECT ;
const char * const argv [ ] = { " cppcheck " , " --project= " , " file.cpp " } ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Fail , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-10-05 21:36:44 +02:00
ASSERT_EQUALS ( " cppcheck: error: failed to open project ''. The file does not exist. \n " , logger - > str ( ) ) ;
}
void projectMissing ( ) {
REDIRECT ;
const char * const argv [ ] = { " cppcheck " , " --project=project.cppcheck " , " file.cpp " } ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Fail , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-10-05 21:36:44 +02:00
ASSERT_EQUALS ( " cppcheck: error: failed to open project 'project.cppcheck'. The file does not exist. \n " , logger - > str ( ) ) ;
}
2023-10-08 09:07:15 +02:00
void projectNoPaths ( ) {
ScopedFile file ( " project.cppcheck " , " <project></project> " ) ;
const char * const argv [ ] = { " cppcheck " , " --project=project.cppcheck " } ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Fail , parser - > parseFromArgs ( 2 , argv ) ) ;
2023-10-08 09:07:15 +02:00
ASSERT_EQUALS ( " cppcheck: error: no C or C++ source files found. \n " , logger - > str ( ) ) ;
}
2023-10-08 21:28:57 +02:00
void addon ( ) {
REDIRECT ;
const char * const argv [ ] = { " cppcheck " , " --addon=misra " , " file.cpp " } ;
settings - > addons . clear ( ) ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Success , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-10-08 21:28:57 +02:00
ASSERT_EQUALS ( 1 , settings - > addons . size ( ) ) ;
ASSERT_EQUALS ( " misra " , * settings - > addons . cbegin ( ) ) ;
}
2023-11-25 21:12:24 +01:00
void addonMissing ( ) {
REDIRECT ;
const char * const argv [ ] = { " cppcheck " , " --addon=misra2 " , " file.cpp " } ;
settings - > addons . clear ( ) ;
ASSERT ( ! parser - > fillSettingsFromArgs ( 3 , argv ) ) ;
ASSERT_EQUALS ( 1 , settings - > addons . size ( ) ) ;
ASSERT_EQUALS ( " misra2 " , * settings - > addons . cbegin ( ) ) ;
ASSERT_EQUALS ( " Did not find addon misra2.py \n " , logger - > str ( ) ) ;
}
2023-11-08 09:28:33 +01:00
void signedChar ( ) {
REDIRECT ;
const char * const argv [ ] = { " cppcheck " , " --fsigned-char " , " file.cpp " } ;
settings - > platform . defaultSign = ' \0 ' ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Success , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-11-08 09:28:33 +01:00
ASSERT_EQUALS ( ' s ' , settings - > platform . defaultSign ) ;
}
void signedChar2 ( ) {
REDIRECT ;
const char * const argv [ ] = { " cppcheck " , " --platform=avr8 " , " --fsigned-char " , " file.cpp " } ;
settings - > platform . defaultSign = ' \0 ' ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Success , parser - > parseFromArgs ( 4 , argv ) ) ;
2023-11-08 09:28:33 +01:00
ASSERT_EQUALS ( ' s ' , settings - > platform . defaultSign ) ;
}
void unsignedChar ( ) {
REDIRECT ;
const char * const argv [ ] = { " cppcheck " , " --funsigned-char " , " file.cpp " } ;
settings - > platform . defaultSign = ' \0 ' ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Success , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-11-08 09:28:33 +01:00
ASSERT_EQUALS ( ' u ' , settings - > platform . defaultSign ) ;
}
void unsignedChar2 ( ) {
REDIRECT ;
const char * const argv [ ] = { " cppcheck " , " --platform=mips32 " , " --funsigned-char " , " file.cpp " } ;
settings - > platform . defaultSign = ' \0 ' ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Success , parser - > parseFromArgs ( 4 , argv ) ) ;
2023-11-08 09:28:33 +01:00
ASSERT_EQUALS ( ' u ' , settings - > platform . defaultSign ) ;
}
void signedCharUnsignedChar ( ) {
REDIRECT ;
const char * const argv [ ] = { " cppcheck " , " --fsigned-char " , " --funsigned-char " , " file.cpp " } ;
settings - > platform . defaultSign = ' \0 ' ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Success , parser - > parseFromArgs ( 4 , argv ) ) ;
2023-11-08 09:28:33 +01:00
ASSERT_EQUALS ( ' u ' , settings - > platform . defaultSign ) ;
}
2023-10-21 09:12:59 +02:00
# ifdef HAVE_RULES
void rule ( ) {
REDIRECT ;
const char * const argv [ ] = { " cppcheck " , " --rule=.+ " , " file.cpp " } ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Success , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-10-21 09:12:59 +02:00
ASSERT_EQUALS ( 1 , settings - > rules . size ( ) ) ;
auto it = settings - > rules . cbegin ( ) ;
ASSERT_EQUALS ( " .+ " , it - > pattern ) ;
}
# else
void ruleNotSupported ( ) {
REDIRECT ;
const char * const argv [ ] = { " cppcheck " , " --rule=.+ " , " file.cpp " } ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Fail , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-10-21 09:12:59 +02:00
ASSERT_EQUALS ( " cppcheck: error: Option --rule cannot be used as Cppcheck has not been built with rules support. \n " , logger - > str ( ) ) ;
}
# endif
# ifdef HAVE_RULES
void ruleFile ( ) {
REDIRECT ;
ScopedFile file ( " rule.xml " ,
" <rules> \n "
" <rule> \n "
" <pattern>.+</pattern> \n "
" </rule> \n "
" </rules> " ) ;
const char * const argv [ ] = { " cppcheck " , " --rule-file=rule.xml " , " file.cpp " } ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Success , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-10-21 09:12:59 +02:00
ASSERT_EQUALS ( 1 , settings - > rules . size ( ) ) ;
auto it = settings - > rules . cbegin ( ) ;
ASSERT_EQUALS ( " .+ " , it - > pattern ) ;
}
void ruleFileEmpty ( ) {
REDIRECT ;
const char * const argv [ ] = { " cppcheck " , " --rule-file= " , " file.cpp " } ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Fail , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-10-21 09:12:59 +02:00
ASSERT_EQUALS ( " cppcheck: error: unable to load rule-file '' (XML_ERROR_FILE_NOT_FOUND). \n " , logger - > str ( ) ) ;
}
void ruleFileMissing ( ) {
REDIRECT ;
const char * const argv [ ] = { " cppcheck " , " --rule-file=rule.xml " , " file.cpp " } ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Fail , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-10-21 09:12:59 +02:00
ASSERT_EQUALS ( " cppcheck: error: unable to load rule-file 'rule.xml' (XML_ERROR_FILE_NOT_FOUND). \n " , logger - > str ( ) ) ;
}
void ruleFileInvalid ( ) {
REDIRECT ;
ScopedFile file ( " rule.xml " , " " ) ;
const char * const argv [ ] = { " cppcheck " , " --rule-file=rule.xml " , " file.cpp " } ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Fail , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-10-21 09:12:59 +02:00
ASSERT_EQUALS ( " cppcheck: error: unable to load rule-file 'rule.xml' (XML_ERROR_EMPTY_DOCUMENT). \n " , logger - > str ( ) ) ;
}
# else
void ruleFileNotSupported ( ) {
REDIRECT ;
const char * const argv [ ] = { " cppcheck " , " --rule-file=rule.xml " , " file.cpp " } ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Fail , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-10-21 09:12:59 +02:00
ASSERT_EQUALS ( " cppcheck: error: Option --rule-file cannot be used as Cppcheck has not been built with rules support. \n " , logger - > str ( ) ) ;
}
# endif
2023-11-25 21:12:24 +01:00
void library ( ) {
REDIRECT ;
const char * const argv [ ] = { " cppcheck " , " --library=posix " , " file.cpp " } ;
settings - > libraries . clear ( ) ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Success , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-11-25 21:12:24 +01:00
ASSERT_EQUALS ( 1 , settings - > libraries . size ( ) ) ;
ASSERT_EQUALS ( " posix " , * settings - > libraries . cbegin ( ) ) ;
}
void libraryMissing ( ) {
REDIRECT ;
const char * const argv [ ] = { " cppcheck " , " --library=posix2 " , " file.cpp " } ;
settings - > libraries . clear ( ) ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( false , parser - > fillSettingsFromArgs ( 3 , argv ) ) ;
2023-11-25 21:12:24 +01:00
ASSERT_EQUALS ( 1 , settings - > libraries . size ( ) ) ;
ASSERT_EQUALS ( " posix2 " , * settings - > libraries . cbegin ( ) ) ;
ASSERT_EQUALS ( " cppcheck: Failed to load library configuration file 'posix2'. File not found \n " , logger - > str ( ) ) ;
}
2023-01-01 15:32:37 +01:00
void ignorepaths1 ( ) {
REDIRECT ;
const char * const argv [ ] = { " cppcheck " , " -isrc " , " file.cpp " } ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Success , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-08-12 21:43:12 +02:00
ASSERT_EQUALS ( 1 , parser - > getIgnoredPaths ( ) . size ( ) ) ;
ASSERT_EQUALS ( " src " , parser - > getIgnoredPaths ( ) [ 0 ] ) ;
2023-01-01 15:32:37 +01:00
}
2022-08-20 20:54:31 +02:00
2023-01-01 15:32:37 +01:00
void ignorepaths2 ( ) {
REDIRECT ;
const char * const argv [ ] = { " cppcheck " , " -i " , " src " , " file.cpp " } ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Success , parser - > parseFromArgs ( 4 , argv ) ) ;
2023-08-12 21:43:12 +02:00
ASSERT_EQUALS ( 1 , parser - > getIgnoredPaths ( ) . size ( ) ) ;
ASSERT_EQUALS ( " src " , parser - > getIgnoredPaths ( ) [ 0 ] ) ;
2023-01-01 15:32:37 +01:00
}
2011-01-31 14:25:51 +01:00
2023-01-01 15:32:37 +01:00
void ignorepaths3 ( ) {
REDIRECT ;
const char * const argv [ ] = { " cppcheck " , " -isrc " , " -imodule " , " file.cpp " } ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Success , parser - > parseFromArgs ( 4 , argv ) ) ;
2023-08-12 21:43:12 +02:00
ASSERT_EQUALS ( 2 , parser - > getIgnoredPaths ( ) . size ( ) ) ;
ASSERT_EQUALS ( " src " , parser - > getIgnoredPaths ( ) [ 0 ] ) ;
ASSERT_EQUALS ( " module " , parser - > getIgnoredPaths ( ) [ 1 ] ) ;
2023-01-01 15:32:37 +01:00
}
2022-01-04 15:48:08 +01:00
2023-01-01 15:32:37 +01:00
void ignorepaths4 ( ) {
2014-01-12 12:34:05 +01:00
REDIRECT ;
2018-09-24 15:08:16 +02:00
const char * const argv [ ] = { " cppcheck " , " -i " , " src " , " -i " , " module " , " file.cpp " } ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Success , parser - > parseFromArgs ( 6 , argv ) ) ;
2023-08-12 21:43:12 +02:00
ASSERT_EQUALS ( 2 , parser - > getIgnoredPaths ( ) . size ( ) ) ;
ASSERT_EQUALS ( " src " , parser - > getIgnoredPaths ( ) [ 0 ] ) ;
ASSERT_EQUALS ( " module " , parser - > getIgnoredPaths ( ) [ 1 ] ) ;
2023-01-01 15:32:37 +01:00
}
2022-01-04 15:48:08 +01:00
2023-01-01 15:32:37 +01:00
void ignorefilepaths1 ( ) {
REDIRECT ;
const char * const argv [ ] = { " cppcheck " , " -ifoo.cpp " , " file.cpp " } ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Success , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-08-12 21:43:12 +02:00
ASSERT_EQUALS ( 1 , parser - > getIgnoredPaths ( ) . size ( ) ) ;
ASSERT_EQUALS ( " foo.cpp " , parser - > getIgnoredPaths ( ) [ 0 ] ) ;
2023-01-01 15:32:37 +01:00
}
2022-01-04 15:48:08 +01:00
2023-01-01 15:32:37 +01:00
void ignorefilepaths2 ( ) {
2014-01-12 12:34:05 +01:00
REDIRECT ;
2018-09-24 15:08:16 +02:00
const char * const argv [ ] = { " cppcheck " , " -isrc/foo.cpp " , " file.cpp " } ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Success , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-08-12 21:43:12 +02:00
ASSERT_EQUALS ( 1 , parser - > getIgnoredPaths ( ) . size ( ) ) ;
ASSERT_EQUALS ( " src/foo.cpp " , parser - > getIgnoredPaths ( ) [ 0 ] ) ;
2023-01-01 15:32:37 +01:00
}
2011-02-03 13:43:42 +01:00
2014-11-20 14:20:09 +01:00
void checkconfig ( ) {
2011-05-04 20:07:02 +02:00
REDIRECT ;
2018-09-24 15:08:16 +02:00
const char * const argv [ ] = { " cppcheck " , " --check-config " , " file.cpp " } ;
2023-08-12 21:43:12 +02:00
settings - > checkConfiguration = false ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Success , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-08-12 21:43:12 +02:00
ASSERT_EQUALS ( true , settings - > checkConfiguration ) ;
2011-05-04 20:07:02 +02:00
}
2014-11-20 14:20:09 +01:00
void unknownParam ( ) {
2010-12-04 13:13:44 +01:00
REDIRECT ;
2018-09-24 15:08:16 +02:00
const char * const argv [ ] = { " cppcheck " , " --foo " , " file.cpp " } ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Fail , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-09-26 21:38:15 +02:00
ASSERT_EQUALS ( " cppcheck: error: unrecognized command line option: \" --foo \" . \n " , logger - > str ( ) ) ;
2010-12-04 13:13:44 +01:00
}
2011-11-30 20:24:01 +01:00
2014-11-20 14:20:09 +01:00
void undefs ( ) {
2011-11-30 20:24:01 +01:00
REDIRECT ;
2018-09-24 15:08:16 +02:00
const char * const argv [ ] = { " cppcheck " , " -U_WIN32 " , " file.cpp " } ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Success , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-08-12 21:43:12 +02:00
ASSERT_EQUALS ( 1 , settings - > userUndefs . size ( ) ) ;
ASSERT ( settings - > userUndefs . find ( " _WIN32 " ) ! = settings - > userUndefs . end ( ) ) ;
2011-11-30 20:24:01 +01:00
}
2014-11-20 14:20:09 +01:00
void undefs2 ( ) {
2011-11-30 20:24:01 +01:00
REDIRECT ;
2018-09-24 15:08:16 +02:00
const char * const argv [ ] = { " cppcheck " , " -U_WIN32 " , " -UNODEBUG " , " file.cpp " } ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Success , parser - > parseFromArgs ( 4 , argv ) ) ;
2023-08-12 21:43:12 +02:00
ASSERT_EQUALS ( 2 , settings - > userUndefs . size ( ) ) ;
ASSERT ( settings - > userUndefs . find ( " _WIN32 " ) ! = settings - > userUndefs . end ( ) ) ;
ASSERT ( settings - > userUndefs . find ( " NODEBUG " ) ! = settings - > userUndefs . end ( ) ) ;
2011-11-30 20:24:01 +01:00
}
2014-11-20 14:20:09 +01:00
void undefs_noarg ( ) {
2011-11-30 20:24:01 +01:00
REDIRECT ;
2018-09-24 15:08:16 +02:00
const char * const argv [ ] = { " cppcheck " , " -U " } ;
2011-11-30 20:24:01 +01:00
// Fails since -U has no param
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Fail , parser - > parseFromArgs ( 2 , argv ) ) ;
2023-09-26 21:38:15 +02:00
ASSERT_EQUALS ( " cppcheck: error: argument to '-U' is missing. \n " , logger - > str ( ) ) ;
2011-11-30 20:24:01 +01:00
}
2014-11-20 14:20:09 +01:00
void undefs_noarg2 ( ) {
2011-11-30 20:24:01 +01:00
REDIRECT ;
2018-09-24 15:08:16 +02:00
const char * const argv [ ] = { " cppcheck " , " -U " , " -v " , " file.cpp " } ;
2011-11-30 20:24:01 +01:00
// Fails since -U has no param
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Fail , parser - > parseFromArgs ( 4 , argv ) ) ;
2023-09-26 21:38:15 +02:00
ASSERT_EQUALS ( " cppcheck: error: argument to '-U' is missing. \n " , logger - > str ( ) ) ;
2011-11-30 20:24:01 +01:00
}
2014-11-20 14:20:09 +01:00
void undefs_noarg3 ( ) {
2011-11-30 20:24:01 +01:00
REDIRECT ;
2018-09-24 15:08:16 +02:00
const char * const argv [ ] = { " cppcheck " , " -U " , " --quiet " , " file.cpp " } ;
2011-11-30 20:24:01 +01:00
// Fails since -U has no param
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Fail , parser - > parseFromArgs ( 4 , argv ) ) ;
2023-09-26 21:38:15 +02:00
ASSERT_EQUALS ( " cppcheck: error: argument to '-U' is missing. \n " , logger - > str ( ) ) ;
2011-11-30 20:24:01 +01:00
}
2023-08-04 17:38:43 +02:00
void cppcheckBuildDirExistent ( ) {
REDIRECT ;
2023-08-12 21:43:12 +02:00
const char * const argv [ ] = { " cppcheck " , " --cppcheck-build-dir=. " , " file.cpp " } ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Success , parser - > parseFromArgs ( 3 , argv ) ) ;
2023-08-04 17:38:43 +02:00
}
void cppcheckBuildDirNonExistent ( ) {
REDIRECT ;
const char * const argv [ ] = { " cppcheck " , " --cppcheck-build-dir=non-existent-path " } ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Fail , parser - > parseFromArgs ( 2 , argv ) ) ;
2023-09-26 21:38:15 +02:00
ASSERT_EQUALS ( " cppcheck: error: Directory 'non-existent-path' specified by --cppcheck-build-dir argument has to be existent. \n " , logger - > str ( ) ) ;
2023-08-04 17:38:43 +02:00
}
void cppcheckBuildDirEmpty ( ) {
REDIRECT ;
const char * const argv [ ] = { " cppcheck " , " --cppcheck-build-dir= " } ;
2023-11-27 12:06:03 +01:00
ASSERT_EQUALS ( CmdLineParser : : Result : : Fail , parser - > parseFromArgs ( 2 , argv ) ) ;
2023-09-26 21:38:15 +02:00
ASSERT_EQUALS ( " cppcheck: error: Directory '' specified by --cppcheck-build-dir argument has to be existent. \n " , logger - > str ( ) ) ;
2023-08-04 17:38:43 +02:00
}
2023-12-01 16:34:14 +01:00
void invalidCppcheckCfg ( ) {
REDIRECT ;
ScopedFile file ( Path : : join ( Path : : getPathFromFilename ( Path : : getCurrentExecutablePath ( " " ) ) , " cppcheck.cfg " ) ,
" { \n " ) ;
const char * const argv [ ] = { " cppcheck " , " test.cpp " } ;
ASSERT_EQUALS ( CmdLineParser : : Result : : Fail , parser - > parseFromArgs ( 2 , argv ) ) ;
2023-12-05 20:39:26 +01:00
ASSERT_EQUALS ( " cppcheck: error: could not load cppcheck.cfg - not a valid JSON - syntax error at line 2 near: \n " , logger - > str ( ) ) ;
2023-12-01 16:34:14 +01:00
}
2010-09-06 21:31:06 +02:00
} ;
REGISTER_TEST ( TestCmdlineParser )