From efc062f6beea2a8a562429a61e81f20f30d68760 Mon Sep 17 00:00:00 2001 From: otavepto <153766569+otavepto@users.noreply.github.com> Date: Thu, 27 Jun 2024 02:05:33 +0300 Subject: [PATCH] move remove_file() inside common helpers --- crash_printer/tests/test_helper.hpp | 36 ------------------- crash_printer/tests/test_linux_sa_handler.cpp | 5 ++- .../tests/test_linux_sa_sigaction.cpp | 5 ++- crash_printer/tests/test_win.cpp | 4 +-- helpers/common_helpers.cpp | 24 +++++++++++++ helpers/common_helpers/common_helpers.hpp | 4 +++ premake5.lua | 3 -- 7 files changed, 34 insertions(+), 47 deletions(-) delete mode 100644 crash_printer/tests/test_helper.hpp diff --git a/crash_printer/tests/test_helper.hpp b/crash_printer/tests/test_helper.hpp deleted file mode 100644 index ad0fb1c1..00000000 --- a/crash_printer/tests/test_helper.hpp +++ /dev/null @@ -1,36 +0,0 @@ -#ifndef _TEST_CRASH_PRINTER_HELPER_H -#define _TEST_CRASH_PRINTER_HELPER_H - - -#include -#include - -static inline bool remove_file(const std::string &file) -{ - const std::filesystem::path p_file(std::filesystem::u8path(file)); - if (!std::filesystem::exists(p_file)) { - return true; - } - - if (std::filesystem::is_directory(p_file)) { - return false; - } - - return std::filesystem::remove(p_file); -} - -static inline bool remove_file(const std::wstring &file) -{ - if (!std::filesystem::exists(file)) { - return true; - } - - if (std::filesystem::is_directory(file)) { - return false; - } - - return std::filesystem::remove(file); -} - - -#endif // _TEST_CRASH_PRINTER_HELPER_H diff --git a/crash_printer/tests/test_linux_sa_handler.cpp b/crash_printer/tests/test_linux_sa_handler.cpp index 8e605098..67e0054a 100644 --- a/crash_printer/tests/test_linux_sa_handler.cpp +++ b/crash_printer/tests/test_linux_sa_handler.cpp @@ -1,7 +1,6 @@ #include "crash_printer/linux.hpp" -#include "./test_helper.hpp" - +#include "common_helpers/common_helpers.hpp" #include #include @@ -42,7 +41,7 @@ int main() sigemptyset(&sa_SIGSEGV_prev.sa_mask); // all signals unblocked sigaction(SIGSEGV, &sa_SIGSEGV_prev, nullptr); - if (!remove_file(logs_filepath)) { + if (!common_helpers::remove_file(logs_filepath)) { std::cerr << "failed to remove log" << std::endl; return 1; } diff --git a/crash_printer/tests/test_linux_sa_sigaction.cpp b/crash_printer/tests/test_linux_sa_sigaction.cpp index b771ec1d..ac56f2ed 100644 --- a/crash_printer/tests/test_linux_sa_sigaction.cpp +++ b/crash_printer/tests/test_linux_sa_sigaction.cpp @@ -1,7 +1,6 @@ #include "crash_printer/linux.hpp" -#include "./test_helper.hpp" - +#include "common_helpers/common_helpers.hpp" #include #include @@ -42,7 +41,7 @@ int main() sigemptyset(&sa_SIGSEGV_prev.sa_mask); // all signals unblocked sigaction(SIGSEGV, &sa_SIGSEGV_prev, nullptr); - if (!remove_file(logs_filepath)) { + if (!common_helpers::remove_file(logs_filepath)) { std::cerr << "failed to remove log" << std::endl; return 1; } diff --git a/crash_printer/tests/test_win.cpp b/crash_printer/tests/test_win.cpp index 0790c001..6eca3b6d 100644 --- a/crash_printer/tests/test_win.cpp +++ b/crash_printer/tests/test_win.cpp @@ -1,6 +1,6 @@ #include "crash_printer/win.hpp" -#include "./test_helper.hpp" +#include "common_helpers/common_helpers.hpp" #include #include @@ -39,7 +39,7 @@ int main() // simulate the existence of previous handler SetUnhandledExceptionFilter(exception_handler); - if (!remove_file(logs_filepath)) { + if (!common_helpers::remove_file(logs_filepath)) { std::cerr << "failed to remove log" << std::endl; return 1; } diff --git a/helpers/common_helpers.cpp b/helpers/common_helpers.cpp index 83d9b902..eb6c8908 100644 --- a/helpers/common_helpers.cpp +++ b/helpers/common_helpers.cpp @@ -403,6 +403,30 @@ bool common_helpers::dir_exist(const std::wstring &dirpath) return dir_exist(std::filesystem::path(dirpath)); } +bool common_helpers::remove_file(const std::filesystem::path &filepath) +{ + if (!std::filesystem::exists(filepath)) { + return true; + } + + if (std::filesystem::is_directory(filepath)) { + return false; + } + + return std::filesystem::remove(filepath); +} + +bool common_helpers::remove_file(const std::string &filepath) +{ + return remove_file(std::filesystem::u8path(filepath)); +} + +bool common_helpers::remove_file(const std::wstring &filepath) +{ + return remove_file(std::filesystem::path(filepath)); +} + + size_t common_helpers::rand_number(size_t max) { // https://en.cppreference.com/w/cpp/numeric/random/uniform_int_distribution diff --git a/helpers/common_helpers/common_helpers.hpp b/helpers/common_helpers/common_helpers.hpp index a1cb3fc5..aef1c7d9 100644 --- a/helpers/common_helpers/common_helpers.hpp +++ b/helpers/common_helpers/common_helpers.hpp @@ -98,6 +98,10 @@ bool dir_exist(const std::filesystem::path &dirpath); bool dir_exist(const std::string &dirpath); bool dir_exist(const std::wstring &dirpath); +bool remove_file(const std::filesystem::path &filepath); +bool remove_file(const std::string &filepath); +bool remove_file(const std::wstring &filepath); + // between 0 and max, 0 and max are included size_t rand_number(size_t max); diff --git a/premake5.lua b/premake5.lua index 402df093..418c5b41 100644 --- a/premake5.lua +++ b/premake5.lua @@ -1466,7 +1466,6 @@ project "test_crash_printer" filter {} -- reset the filter and remove all active keywords files { -- added to all filters, later defines will be appended 'crash_printer/' .. os_iden .. '.cpp', 'crash_printer/crash_printer/' .. os_iden .. '.hpp', - 'crash_printer/tests/test_helper.hpp', -- helpers 'helpers/common_helpers.cpp', 'helpers/common_helpers/**', -- test files @@ -1597,7 +1596,6 @@ project "test_crash_printer_sa_handler" filter {} -- reset the filter and remove all active keywords files { -- added to all filters, later defines will be appended 'crash_printer/' .. os_iden .. '.cpp', 'crash_printer/crash_printer/' .. os_iden .. '.hpp', - 'crash_printer/tests/test_helper.hpp', -- helpers 'helpers/common_helpers.cpp', 'helpers/common_helpers/**', -- test files @@ -1643,7 +1641,6 @@ project "test_crash_printer_sa_sigaction" filter {} -- reset the filter and remove all active keywords files { -- added to all filters, later defines will be appended 'crash_printer/' .. os_iden .. '.cpp', 'crash_printer/crash_printer/' .. os_iden .. '.hpp', - 'crash_printer/tests/test_helper.hpp', -- helpers 'helpers/common_helpers.cpp', 'helpers/common_helpers/**', -- test files