From d6beccc445012a06778fea86c6e2cd327a9a71df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliver=20St=C3=B6neberg?= Date: Tue, 22 Aug 2023 15:25:28 +0200 Subject: [PATCH] fixed #11783 (Remove .plist files generated by unit tests) (#5312) --- Makefile | 2 +- test/helpers.cpp | 27 ++++++++++++++++++++++++--- test/testprocessexecutor.cpp | 4 ++-- test/testsingleexecutor.cpp | 6 +++--- test/testthreadexecutor.cpp | 4 ++-- 5 files changed, 32 insertions(+), 11 deletions(-) diff --git a/Makefile b/Makefile index d1256debd..767a71d13 100644 --- a/Makefile +++ b/Makefile @@ -671,7 +671,7 @@ cli/threadexecutor.o: cli/threadexecutor.cpp cli/cppcheckexecutor.h cli/executor test/fixture.o: test/fixture.cpp externals/tinyxml2/tinyxml2.h lib/check.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/importproject.h lib/library.h lib/mathlib.h lib/platform.h lib/settings.h lib/standards.h lib/suppressions.h lib/utils.h test/fixture.h test/options.h test/redirect.h $(CXX) ${INCLUDE_FOR_TEST} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ test/fixture.cpp -test/helpers.o: test/helpers.cpp externals/simplecpp/simplecpp.h lib/config.h lib/errortypes.h lib/importproject.h lib/library.h lib/mathlib.h lib/path.h lib/platform.h lib/preprocessor.h lib/settings.h lib/standards.h lib/suppressions.h lib/templatesimplifier.h lib/token.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/vfvalue.h test/helpers.h +test/helpers.o: test/helpers.cpp cli/filelister.h externals/simplecpp/simplecpp.h lib/config.h lib/errortypes.h lib/importproject.h lib/library.h lib/mathlib.h lib/path.h lib/pathmatch.h lib/platform.h lib/preprocessor.h lib/settings.h lib/standards.h lib/suppressions.h lib/templatesimplifier.h lib/token.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/vfvalue.h test/helpers.h $(CXX) ${INCLUDE_FOR_TEST} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ test/helpers.cpp test/main.o: test/main.cpp externals/simplecpp/simplecpp.h lib/check.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/importproject.h lib/library.h lib/mathlib.h lib/platform.h lib/preprocessor.h lib/settings.h lib/standards.h lib/suppressions.h lib/utils.h test/fixture.h test/options.h diff --git a/test/helpers.cpp b/test/helpers.cpp index 0d33758cc..dd832e3f2 100644 --- a/test/helpers.cpp +++ b/test/helpers.cpp @@ -18,7 +18,9 @@ #include "helpers.h" +#include "filelister.h" #include "path.h" +#include "pathmatch.h" #include "preprocessor.h" #include @@ -26,6 +28,7 @@ #include #include // IWYU pragma: keep #include +#include #include #include #include @@ -41,6 +44,7 @@ class Suppressions; +// TODO: better path-only usage ScopedFile::ScopedFile(std::string name, const std::string &content, std::string path) : mName(std::move(name)) , mPath(Path::toNativeSeparators(std::move(path))) @@ -65,18 +69,35 @@ ScopedFile::ScopedFile(std::string name, const std::string &content, std::string ScopedFile::~ScopedFile() { const int remove_res = std::remove(mFullPath.c_str()); if (remove_res != 0) { - std::cout << "ScopedFile(" << mFullPath + ") - could not delete file (" << remove_res << ")"; + std::cout << "ScopedFile(" << mFullPath + ") - could not delete file (" << remove_res << ")" << std::endl; } if (!mPath.empty() && mPath != Path::getCurrentPath()) { + // TODO: remove all files + // TODO: simplify the function call + // hack to be able to delete *.plist output files + std::map files; + const std::string res = FileLister::addFiles(files, mPath, {".plist"}, false, PathMatch({})); + if (!res.empty()) { + std::cout << "ScopedFile(" << mPath + ") - generating file list failed (" << res << ")" << std::endl; + } + for (const auto &f : files) + { + const std::string &file = f.first; + const int rm_f_res = std::remove(file.c_str()); + if (rm_f_res != 0) { + std::cout << "ScopedFile(" << mPath + ") - could not delete '" << file << "' (" << rm_f_res << ")" << std::endl; + } + } + #ifdef _WIN32 if (!RemoveDirectoryA(mPath.c_str())) { - std::cout << "ScopedFile(" << mFullPath + ") - could not delete folder (" << GetLastError() << ")"; + std::cout << "ScopedFile(" << mFullPath + ") - could not delete folder (" << GetLastError() << ")" << std::endl; } #else const int rmdir_res = rmdir(mPath.c_str()); if (rmdir_res == -1) { const int err = errno; - std::cout << "ScopedFile(" << mFullPath + ") - could not delete folder (" << err << ")"; + std::cout << "ScopedFile(" << mFullPath + ") - could not delete folder (" << err << ")" << std::endl; } #endif } diff --git a/test/testprocessexecutor.cpp b/test/testprocessexecutor.cpp index 5bd701eab..e3febc758 100644 --- a/test/testprocessexecutor.cpp +++ b/test/testprocessexecutor.cpp @@ -140,8 +140,8 @@ private: } void many_threads_plist() { - const char plistOutput[] = "plist"; - ScopedFile plistFile("dummy", plistOutput); + const char plistOutput[] = "plist_process/"; + ScopedFile plistFile("dummy", "", plistOutput); check(16, 100, 100, "int main()\n" diff --git a/test/testsingleexecutor.cpp b/test/testsingleexecutor.cpp index f80530f82..352b074d2 100644 --- a/test/testsingleexecutor.cpp +++ b/test/testsingleexecutor.cpp @@ -182,15 +182,15 @@ private: } void many_files_plist() { - const char plistOutput[] = "plist"; - ScopedFile plistFile("dummy", plistOutput); + const std::string plistOutput = "plist_" + fprefix() + "/"; + ScopedFile plistFile("dummy", "", plistOutput); check(100, 100, "int main()\n" "{\n" " char *a = malloc(10);\n" " return 0;\n" - "}", dinit(CheckOptions, $.plistOutput = plistOutput)); + "}", dinit(CheckOptions, $.plistOutput = plistOutput.c_str())); } void no_errors_more_files() { diff --git a/test/testthreadexecutor.cpp b/test/testthreadexecutor.cpp index e05eff6d5..7dccdf51e 100644 --- a/test/testthreadexecutor.cpp +++ b/test/testthreadexecutor.cpp @@ -138,8 +138,8 @@ private: } void many_threads_plist() { - const char plistOutput[] = "plist"; - ScopedFile plistFile("dummy", plistOutput); + const char plistOutput[] = "plist_thread/"; + ScopedFile plistFile("dummy", "", plistOutput); check(16, 100, 100, "int main()\n"