move remove_file() inside common helpers

This commit is contained in:
otavepto 2024-06-27 02:05:33 +03:00
parent ef2f814765
commit efc062f6be
7 changed files with 34 additions and 47 deletions

View File

@ -1,36 +0,0 @@
#ifndef _TEST_CRASH_PRINTER_HELPER_H
#define _TEST_CRASH_PRINTER_HELPER_H
#include <filesystem>
#include <string>
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

View File

@ -1,7 +1,6 @@
#include "crash_printer/linux.hpp" #include "crash_printer/linux.hpp"
#include "./test_helper.hpp" #include "common_helpers/common_helpers.hpp"
#include <iostream> #include <iostream>
#include <fstream> #include <fstream>
@ -42,7 +41,7 @@ int main()
sigemptyset(&sa_SIGSEGV_prev.sa_mask); // all signals unblocked sigemptyset(&sa_SIGSEGV_prev.sa_mask); // all signals unblocked
sigaction(SIGSEGV, &sa_SIGSEGV_prev, nullptr); 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; std::cerr << "failed to remove log" << std::endl;
return 1; return 1;
} }

View File

@ -1,7 +1,6 @@
#include "crash_printer/linux.hpp" #include "crash_printer/linux.hpp"
#include "./test_helper.hpp" #include "common_helpers/common_helpers.hpp"
#include <iostream> #include <iostream>
#include <fstream> #include <fstream>
@ -42,7 +41,7 @@ int main()
sigemptyset(&sa_SIGSEGV_prev.sa_mask); // all signals unblocked sigemptyset(&sa_SIGSEGV_prev.sa_mask); // all signals unblocked
sigaction(SIGSEGV, &sa_SIGSEGV_prev, nullptr); 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; std::cerr << "failed to remove log" << std::endl;
return 1; return 1;
} }

View File

@ -1,6 +1,6 @@
#include "crash_printer/win.hpp" #include "crash_printer/win.hpp"
#include "./test_helper.hpp" #include "common_helpers/common_helpers.hpp"
#include <iostream> #include <iostream>
#include <fstream> #include <fstream>
@ -39,7 +39,7 @@ int main()
// simulate the existence of previous handler // simulate the existence of previous handler
SetUnhandledExceptionFilter(exception_handler); SetUnhandledExceptionFilter(exception_handler);
if (!remove_file(logs_filepath)) { if (!common_helpers::remove_file(logs_filepath)) {
std::cerr << "failed to remove log" << std::endl; std::cerr << "failed to remove log" << std::endl;
return 1; return 1;
} }

View File

@ -403,6 +403,30 @@ bool common_helpers::dir_exist(const std::wstring &dirpath)
return dir_exist(std::filesystem::path(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) size_t common_helpers::rand_number(size_t max)
{ {
// https://en.cppreference.com/w/cpp/numeric/random/uniform_int_distribution // https://en.cppreference.com/w/cpp/numeric/random/uniform_int_distribution

View File

@ -98,6 +98,10 @@ bool dir_exist(const std::filesystem::path &dirpath);
bool dir_exist(const std::string &dirpath); bool dir_exist(const std::string &dirpath);
bool dir_exist(const std::wstring &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 // between 0 and max, 0 and max are included
size_t rand_number(size_t max); size_t rand_number(size_t max);

View File

@ -1466,7 +1466,6 @@ project "test_crash_printer"
filter {} -- reset the filter and remove all active keywords filter {} -- reset the filter and remove all active keywords
files { -- added to all filters, later defines will be appended files { -- added to all filters, later defines will be appended
'crash_printer/' .. os_iden .. '.cpp', 'crash_printer/crash_printer/' .. os_iden .. '.hpp', 'crash_printer/' .. os_iden .. '.cpp', 'crash_printer/crash_printer/' .. os_iden .. '.hpp',
'crash_printer/tests/test_helper.hpp',
-- helpers -- helpers
'helpers/common_helpers.cpp', 'helpers/common_helpers/**', 'helpers/common_helpers.cpp', 'helpers/common_helpers/**',
-- test files -- test files
@ -1597,7 +1596,6 @@ project "test_crash_printer_sa_handler"
filter {} -- reset the filter and remove all active keywords filter {} -- reset the filter and remove all active keywords
files { -- added to all filters, later defines will be appended files { -- added to all filters, later defines will be appended
'crash_printer/' .. os_iden .. '.cpp', 'crash_printer/crash_printer/' .. os_iden .. '.hpp', 'crash_printer/' .. os_iden .. '.cpp', 'crash_printer/crash_printer/' .. os_iden .. '.hpp',
'crash_printer/tests/test_helper.hpp',
-- helpers -- helpers
'helpers/common_helpers.cpp', 'helpers/common_helpers/**', 'helpers/common_helpers.cpp', 'helpers/common_helpers/**',
-- test files -- test files
@ -1643,7 +1641,6 @@ project "test_crash_printer_sa_sigaction"
filter {} -- reset the filter and remove all active keywords filter {} -- reset the filter and remove all active keywords
files { -- added to all filters, later defines will be appended files { -- added to all filters, later defines will be appended
'crash_printer/' .. os_iden .. '.cpp', 'crash_printer/crash_printer/' .. os_iden .. '.hpp', 'crash_printer/' .. os_iden .. '.cpp', 'crash_printer/crash_printer/' .. os_iden .. '.hpp',
'crash_printer/tests/test_helper.hpp',
-- helpers -- helpers
'helpers/common_helpers.cpp', 'helpers/common_helpers/**', 'helpers/common_helpers.cpp', 'helpers/common_helpers/**',
-- test files -- test files