You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
spdlog/tests/utils.cpp

104 lines
2.8 KiB
C++

9 years ago
#include "includes.h"
6 years ago
#ifdef _WIN32
2 years ago
#include <windows.h>
#else
2 years ago
#include <sys/types.h>
#include <dirent.h>
6 years ago
#endif
9 years ago
2 years ago
void prepare_logdir() {
9 years ago
spdlog::drop_all();
9 years ago
#ifdef _WIN32
6 years ago
system("rmdir /S /Q test_logs");
9 years ago
#else
auto rv = system("rm -rf test_logs");
2 years ago
if (rv != 0) {
throw std::runtime_error("Failed to rm -rf test_logs");
}
9 years ago
#endif
9 years ago
}
2 years ago
std::string file_contents(const std::string &filename) {
std::ifstream ifs(filename, std::ios_base::binary);
2 years ago
if (!ifs) {
9 years ago
throw std::runtime_error("Failed open file ");
}
8 years ago
return std::string((std::istreambuf_iterator<char>(ifs)), (std::istreambuf_iterator<char>()));
9 years ago
}
2 years ago
std::size_t count_lines(const std::string &filename) {
9 years ago
std::ifstream ifs(filename);
2 years ago
if (!ifs) {
9 years ago
throw std::runtime_error("Failed open file ");
}
9 years ago
std::string line;
size_t counter = 0;
while (std::getline(ifs, line)) counter++;
9 years ago
return counter;
}
2 years ago
void require_message_count(const std::string &filename, const std::size_t messages) {
if (strlen(spdlog::details::os::default_eol) == 0) {
REQUIRE(count_lines(filename) == 1);
2 years ago
} else {
REQUIRE(count_lines(filename) == messages);
}
}
2 years ago
std::size_t get_filesize(const std::string &filename) {
9 years ago
std::ifstream ifs(filename, std::ifstream::ate | std::ifstream::binary);
2 years ago
if (!ifs) {
9 years ago
throw std::runtime_error("Failed open file ");
}
9 years ago
return static_cast<std::size_t>(ifs.tellg());
9 years ago
}
// source: https://stackoverflow.com/a/2072890/192001
2 years ago
bool ends_with(std::string const &value, std::string const &ending) {
if (ending.size() > value.size()) {
8 years ago
return false;
}
8 years ago
return std::equal(ending.rbegin(), ending.rend(), value.rbegin());
}
#ifdef _WIN32
6 years ago
// Based on: https://stackoverflow.com/a/37416569/192001
2 years ago
std::size_t count_files(const std::string &folder) {
6 years ago
size_t counter = 0;
WIN32_FIND_DATAA ffd;
6 years ago
6 years ago
// Start iterating over the files in the folder directory.
6 years ago
HANDLE hFind = ::FindFirstFileA((folder + "\\*").c_str(), &ffd);
2 years ago
if (hFind != INVALID_HANDLE_VALUE) {
do // Managed to locate and create an handle to that folder.
{
if (ffd.cFileName[0] != '.') counter++;
} while (::FindNextFileA(hFind, &ffd) != 0);
::FindClose(hFind);
2 years ago
} else {
throw std::runtime_error("Failed open folder " + folder);
}
return counter;
}
#else
// Based on: https://stackoverflow.com/a/2802255/192001
2 years ago
std::size_t count_files(const std::string &folder) {
size_t counter = 0;
DIR *dp = opendir(folder.c_str());
2 years ago
if (dp == nullptr) {
throw std::runtime_error("Failed open folder " + folder);
}
struct dirent *ep = nullptr;
2 years ago
while ((ep = readdir(dp)) != nullptr) {
if (ep->d_name[0] != '.') counter++;
}
(void)closedir(dp);
return counter;
}
#endif