updated build system

This commit is contained in:
2024-08-08 18:59:14 -05:00
parent 6e4ae2896b
commit 0b5efef569
30 changed files with 1162 additions and 474 deletions

View File

@ -38,7 +38,8 @@ using namespace ::testing;
namespace repertory::test {
#if defined(PROJECT_ENABLE_LIBSODIUM)
[[nodiscard]] auto create_random_file(std::size_t size) -> utils::file::file;
[[nodiscard]] auto
create_random_file(std::size_t size) -> utils::file::i_file &;
#endif // defined(PROJECT_ENABLE_LIBSODIUM)
[[nodiscard]] auto

View File

@ -24,24 +24,24 @@
namespace {
static std::recursive_mutex file_mtx{};
static std::vector<std::string> generated_files;
static std::vector<std::unique_ptr<repertory::utils::file::i_file>>
generated_files{};
static void delete_generated_files() {
repertory::recur_mutex_lock lock{file_mtx};
std::optional<std::string> parent_path;
for (auto &&path : generated_files) {
if (parent_path->empty()) {
parent_path = std::filesystem::path(path).parent_path().string();
parent_path =
repertory::utils::path::get_parent_directory(path->get_path());
}
std::error_code ec{};
std::filesystem::remove(path, ec);
[[maybe_unused]] auto removed = path->remove();
}
generated_files.clear();
if (parent_path.has_value()) {
std::error_code ec{};
std::filesystem::remove_all(*parent_path, ec);
EXPECT_TRUE(repertory::utils::file::remove_directory(*parent_path, true));
}
}
@ -54,26 +54,25 @@ static auto deleter{std::make_unique<file_deleter>()};
namespace repertory::test {
#if defined(PROJECT_ENABLE_LIBSODIUM)
auto create_random_file(std::size_t size) -> utils::file::file {
auto create_random_file(std::size_t size) -> utils::file::i_file & {
recur_mutex_lock lock{file_mtx};
auto path = generate_test_file_name("random");
auto file = utils::file::file::open_or_create_file(path);
EXPECT_TRUE(file);
if (file) {
EXPECT_TRUE(*file);
if (*file) {
data_buffer buf(size);
randombytes_buf(buf.data(), buf.size());
std::size_t bytes_written{};
EXPECT_TRUE(file.write(buf, 0U, &bytes_written));
EXPECT_TRUE(file->write(buf, 0U, &bytes_written));
EXPECT_EQ(size, bytes_written);
EXPECT_EQ(size, file.size());
generated_files.emplace_back(path);
EXPECT_EQ(size, file->size());
}
return file;
generated_files.emplace_back(std::move(file));
return *generated_files.back();
}
#endif // defined(PROJECT_ENABLE_LIBSODIUM)
@ -86,8 +85,9 @@ auto generate_test_file_name(std::string_view file_name_no_extension)
std::string{file_name_no_extension} +
std::to_string(generated_files.size()),
});
generated_files.emplace_back(path);
return path;
generated_files.emplace_back(
std::unique_ptr<utils::file::i_file>(new utils::file::file{path}));
return generated_files.back()->get_path();
}
auto get_test_input_dir() -> std::string {
@ -111,7 +111,7 @@ auto get_test_output_dir() -> std::string {
#endif // defined(_WIN32)
if (not utils::file::is_directory(path)) {
std::filesystem::create_directories(path);
EXPECT_TRUE(utils::file::create_directories(path));
}
return path;

View File

@ -273,7 +273,7 @@ TEST(utils_common, get_environment_variable) {
std::string path;
#if defined(_WIN32)
path.resize(MAX_PATH + 1U);
path.resize(repertory::max_path_length + 1U);
auto size = ::GetEnvironmentVariableA(path_env.c_str(), path.data(), 0U);
path.resize(size);

View File

@ -25,13 +25,13 @@
namespace repertory {
TEST(utils_encrypting_reader, read_file_data) {
const auto token = std::string("moose");
auto source_file = test::create_random_file(
auto &source_file = test::create_random_file(
8U * utils::encryption::encrypting_reader::get_data_chunk_size());
EXPECT_TRUE(source_file);
if (source_file) {
stop_type stop_requested{false};
utils::encryption::encrypting_reader reader(
"test.dat", source_file.get_path().string(), stop_requested, token);
"test.dat", source_file.get_path(), stop_requested, token);
for (std::uint8_t i = 0U; i < 8U; i++) {
data_buffer buffer(
@ -65,13 +65,13 @@ TEST(utils_encrypting_reader, read_file_data) {
TEST(utils_encrypting_reader, read_file_data_in_multiple_chunks) {
const auto token = std::string("moose");
auto source_file = test::create_random_file(
auto &source_file = test::create_random_file(
8U * utils::encryption::encrypting_reader::get_data_chunk_size());
EXPECT_TRUE(source_file);
if (source_file) {
stop_type stop_requested{false};
utils::encryption::encrypting_reader reader(
"test.dat", source_file.get_path().string(), stop_requested, token);
"test.dat", source_file.get_path(), stop_requested, token);
for (std::uint8_t i = 0U; i < 8U; i += 2U) {
data_buffer buffer(
@ -113,13 +113,13 @@ TEST(utils_encrypting_reader, read_file_data_in_multiple_chunks) {
TEST(utils_encrypting_reader, read_file_data_as_stream) {
const auto token = std::string("moose");
auto source_file = test::create_random_file(
auto &source_file = test::create_random_file(
8U * utils::encryption::encrypting_reader::get_data_chunk_size());
EXPECT_TRUE(source_file);
if (source_file) {
stop_type stop_requested{false};
utils::encryption::encrypting_reader reader(
"test.dat", source_file.get_path().string(), stop_requested, token);
"test.dat", source_file.get_path(), stop_requested, token);
auto io_stream = reader.create_iostream();
EXPECT_FALSE(io_stream->seekg(0, std::ios_base::end).fail());
EXPECT_TRUE(io_stream->good());
@ -166,13 +166,13 @@ TEST(utils_encrypting_reader, read_file_data_as_stream) {
TEST(utils_encrypting_reader, read_file_data_in_multiple_chunks_as_stream) {
const auto token = std::string("moose");
auto source_file = test::create_random_file(
auto &source_file = test::create_random_file(
8u * utils::encryption::encrypting_reader::get_data_chunk_size());
EXPECT_TRUE(source_file);
if (source_file) {
stop_type stop_requested{false};
utils::encryption::encrypting_reader reader(
"test.dat", source_file.get_path().string(), stop_requested, token);
"test.dat", source_file.get_path(), stop_requested, token);
auto io_stream = reader.create_iostream();
EXPECT_FALSE(io_stream->seekg(0, std::ios_base::end).fail());
EXPECT_TRUE(io_stream->good());

View File

@ -21,57 +21,81 @@
*/
#include "test.hpp"
namespace {
static constexpr const auto file_type_count{1U};
}
namespace repertory {
TEST(utils_file, can_create_file) {
auto path = test::generate_test_file_name("utils_file");
EXPECT_FALSE(utils::file::is_file(path) || utils::file::is_directory(path));
for (auto idx = 0U; idx < file_type_count; ++idx) {
auto path = test::generate_test_file_name("utils_file");
EXPECT_FALSE(utils::file::is_file(path) || utils::file::is_directory(path));
auto file = utils::file::file::open_or_create_file(path);
EXPECT_TRUE(file);
auto file = idx == 0U ? utils::file::file::open_or_create_file(path)
: utils::file::thread_file::open_or_create_file(path);
EXPECT_TRUE(*file);
EXPECT_TRUE(utils::file::is_file(path));
EXPECT_TRUE(utils::file::is_file(path));
}
}
TEST(utils_file, can_open_file) {
auto path = test::generate_test_file_name("utils_file");
for (auto idx = 0U; idx < file_type_count; ++idx) {
auto path = test::generate_test_file_name("utils_file");
{
auto file = utils::file::file::open_or_create_file(path);
EXPECT_TRUE(file);
}
{
auto file = idx == 0U
? utils::file::file::open_or_create_file(path)
: utils::file::thread_file::open_or_create_file(path);
EXPECT_TRUE(*file);
}
{
auto file = utils::file::file::open_file(path);
EXPECT_TRUE(file);
{
auto file = idx == 0U ? utils::file::file::open_file(path)
: utils::file::thread_file::open_file(path);
EXPECT_TRUE(*file);
}
}
}
TEST(utils_file, open_file_fails_if_not_found) {
auto path = test::generate_test_file_name("utils_file");
for (auto idx = 0U; idx < file_type_count; ++idx) {
auto path = test::generate_test_file_name("utils_file");
auto file = utils::file::file::open_file(path);
EXPECT_FALSE(file);
auto file = idx == 0U ? utils::file::file::open_file(path)
: utils::file::thread_file::open_file(path);
EXPECT_FALSE(*file);
}
}
TEST(utils_file, write_fails_for_read_only_file) {
auto path = test::generate_test_file_name("utils_file");
for (auto idx = 0U; idx < file_type_count; ++idx) {
auto path = test::generate_test_file_name("utils_file");
auto file = utils::file::file::open_or_create_file(path, true);
EXPECT_TRUE(utils::file::is_file(path));
EXPECT_TRUE(file);
std::size_t bytes_written{};
EXPECT_FALSE(file.write(reinterpret_cast<const unsigned char *>("0"), 1U, 0U,
&bytes_written));
EXPECT_EQ(0U, bytes_written);
auto file = idx == 0U
? utils::file::file::open_or_create_file(path, true)
: utils::file::thread_file::open_or_create_file(path, true);
EXPECT_TRUE(utils::file::is_file(path));
EXPECT_TRUE(*file);
std::size_t bytes_written{};
EXPECT_FALSE(file->write(reinterpret_cast<const unsigned char *>("0"), 1U,
0U, &bytes_written));
EXPECT_EQ(0U, bytes_written);
}
}
TEST(utils_file, can_attach_file) {
auto path = test::generate_test_file_name("utils_file");
auto file = utils::file::file::open_or_create_file(path);
auto file2 = utils::file::file::attach_file(file.get_handle());
EXPECT_TRUE(file);
EXPECT_TRUE(file2);
EXPECT_EQ(file.get_path(), file2.get_path());
for (auto idx = 0U; idx < file_type_count; ++idx) {
auto path = test::generate_test_file_name("utils_file");
auto file = idx == 0U ? utils::file::file::open_or_create_file(path)
: utils::file::thread_file::open_or_create_file(path);
auto file2 =
idx == 0U ? utils::file::file::attach_file(file->get_handle())
: utils::file::thread_file::attach_file(file->get_handle());
EXPECT_TRUE(*file);
EXPECT_TRUE(*file2);
EXPECT_EQ(file->get_path(), file2->get_path());
}
}
#if defined(PROJECT_ENABLE_JSON)
@ -100,7 +124,7 @@ TEST(utils_file, read_and_write_json_file_encrypted) {
{
auto file = utils::file::file::open_file(path);
data_buffer encrypted_data{};
EXPECT_TRUE(file.read_all(encrypted_data, 0U));
EXPECT_TRUE(file->read_all(encrypted_data, 0U));
data_buffer decrypted_data{};
EXPECT_TRUE(utils::encryption::decrypt_data("moose", encrypted_data,

View File

@ -21,12 +21,38 @@
*/
#include "test.hpp"
#if defined(_WIN32)
namespace {
static const auto test_path = [](std::string str) -> std::string {
#if defined(PROJECT_ENABLE_WIN32_LONG_PATH_NAMES)
if (repertory::utils::string::begins_with(str, "\\")) {
str = repertory::utils::string::to_lower(
std::filesystem::current_path().string().substr(0U, 2U)) +
str;
}
str = std::string{repertory::utils::path::long_notation} + str;
#else // !defined(PROJECT_ENABLE_WIN32_LONG_PATH_NAMES)
if (repertory::utils::string::begins_with(str, "\\")) {
str = repertory::utils::string::to_lower(
std::filesystem::current_path().string().substr(0U, 2U)) +
str;
}
#endif // defined(PROJECT_ENABLE_WIN32_LONG_PATH_NAMES)
return repertory::utils::string::right_trim(str, '\\');
};
} // namespace
#endif // defined(_WIN32)
namespace repertory {
TEST(utils_path, constants) {
EXPECT_EQ(std::string_view{"\\"}, utils::path::backslash);
EXPECT_EQ(std::wstring_view{L"\\"}, utils::path::backslash_w);
EXPECT_EQ(std::string_view{"."}, utils::path::dot);
EXPECT_EQ(std::wstring_view{L"."}, utils::path::dot_w);
EXPECT_EQ(std::string_view{".\\"}, utils::path::dot_backslash);
EXPECT_EQ(std::wstring_view{L".\\"}, utils::path::dot_backslash_w);
EXPECT_EQ(std::string_view{"./"}, utils::path::dot_slash);
EXPECT_EQ(std::wstring_view{L"./"}, utils::path::dot_slash_w);
EXPECT_EQ(std::string_view{"/"}, utils::path::slash);
@ -82,6 +108,12 @@ TEST(utils_path, get_dot) {
EXPECT_EQ(utils::path::dot_w, utils::path::get_dot<wchar_t>());
}
TEST(utils_path, get_dot_backslash) {
EXPECT_EQ(utils::path::dot_backslash, utils::path::get_dot_backslash<char>());
EXPECT_EQ(utils::path::dot_backslash_w,
utils::path::get_dot_backslash<wchar_t>());
}
TEST(utils_path, get_dot_slash) {
EXPECT_EQ(utils::path::dot_slash, utils::path::get_dot_slash<char>());
EXPECT_EQ(utils::path::dot_slash_w, utils::path::get_dot_slash<wchar_t>());
@ -92,47 +124,53 @@ TEST(utils_path, get_slash) {
EXPECT_EQ(utils::path::slash_w, utils::path::get_slash<wchar_t>());
}
TEST(utils_path, get_long_notation) {
EXPECT_EQ(utils::path::long_notation, utils::path::get_long_notation<char>());
EXPECT_EQ(utils::path::long_notation_w,
utils::path::get_long_notation<wchar_t>());
}
TEST(utils_path, combine) {
auto s = utils::path::combine(R"(\test\path)", {});
#if defined(_WIN32)
EXPECT_STREQ(R"(\test\path)", s.c_str());
EXPECT_STREQ(test_path(R"(\test\path)").c_str(), s.c_str());
#else
EXPECT_STREQ("/test/path", s.c_str());
#endif
s = utils::path::combine(R"(\test)", {R"(\path)"});
#if defined(_WIN32)
EXPECT_STREQ(R"(\test\path)", s.c_str());
EXPECT_STREQ(test_path(R"(\test\path)").c_str(), s.c_str());
#else
EXPECT_STREQ("/test/path", s.c_str());
#endif
s = utils::path::combine(R"(\test)", {R"(\path)", R"(\again\)"});
#if defined(_WIN32)
EXPECT_STREQ(R"(\test\path\again)", s.c_str());
EXPECT_STREQ(test_path(R"(\test\path\again)").c_str(), s.c_str());
#else
EXPECT_STREQ("/test/path/again", s.c_str());
#endif
s = utils::path::combine("/home/test/.dest", {".state"});
#if defined(_WIN32)
EXPECT_STREQ("\\home\\test\\.dest\\.state", s.c_str());
EXPECT_STREQ(test_path(R"(\home\test\.dest\.state)").c_str(), s.c_str());
#else
EXPECT_STREQ("/home/test/.dest/.state", s.c_str());
#endif
#if defined(_WIN32)
s = utils::path::combine(R"(R:\test)", {R"(\path)", R"(\again\)"});
EXPECT_STREQ(R"(r:\test\path\again)", s.c_str());
EXPECT_STREQ(test_path(R"(r:\test\path\again)").c_str(), s.c_str());
s = utils::path::combine("R:", {R"(\path)", R"(\again\)"});
EXPECT_STREQ(R"(r:\path\again)", s.c_str());
EXPECT_STREQ(test_path(R"(r:\path\again)").c_str(), s.c_str());
s = utils::path::combine("R:", {});
EXPECT_STREQ("r:", s.c_str());
EXPECT_STREQ(test_path("r:").c_str(), s.c_str());
s = utils::path::combine("R:", {"\\"});
EXPECT_STREQ("r:", s.c_str());
EXPECT_STREQ(test_path("r:").c_str(), s.c_str());
#endif
}
@ -224,104 +262,116 @@ TEST(utils_path, finalize) {
s = utils::path::finalize(R"(\)");
#if defined(_WIN32)
EXPECT_STREQ(R"(\)", s.c_str());
EXPECT_STREQ(test_path(R"(\)").c_str(), s.c_str());
#else
EXPECT_STREQ("/", s.c_str());
#endif
s = utils::path::finalize("/");
#if defined(_WIN32)
EXPECT_STREQ(R"(\)", s.c_str());
EXPECT_STREQ(test_path(R"(\)").c_str(), s.c_str());
#else
EXPECT_STREQ("/", s.c_str());
#endif
s = utils::path::finalize(R"(\\)");
#if defined(_WIN32)
EXPECT_STREQ(R"(\)", s.c_str());
EXPECT_STREQ(test_path(R"(\)").c_str(), s.c_str());
#else
EXPECT_STREQ("/", s.c_str());
#endif
s = utils::path::finalize("//");
#if defined(_WIN32)
EXPECT_STREQ(R"(\)", s.c_str());
EXPECT_STREQ(test_path(R"(\)").c_str(), s.c_str());
#else
EXPECT_STREQ("/", s.c_str());
#endif
s = utils::path::finalize("/cow///moose/////dog/chicken");
#if defined(_WIN32)
EXPECT_STREQ(R"(\cow\moose\dog\chicken)", s.c_str());
EXPECT_STREQ(test_path(R"(\cow\moose\dog\chicken)").c_str(), s.c_str());
#else
EXPECT_STREQ("/cow/moose/dog/chicken", s.c_str());
#endif
s = utils::path::finalize("\\cow\\\\\\moose\\\\\\\\dog\\chicken/");
#if defined(_WIN32)
EXPECT_STREQ(R"(\cow\moose\dog\chicken)", s.c_str());
EXPECT_STREQ(test_path(R"(\cow\moose\dog\chicken)").c_str(), s.c_str());
#else
EXPECT_STREQ("/cow/moose/dog/chicken", s.c_str());
#endif
s = utils::path::finalize("/cow\\\\/moose\\\\/\\dog\\chicken\\");
#if defined(_WIN32)
EXPECT_STREQ(R"(\cow\moose\dog\chicken)", s.c_str());
EXPECT_STREQ(test_path(R"(\cow\moose\dog\chicken)").c_str(), s.c_str());
#else
EXPECT_STREQ("/cow/moose/dog/chicken", s.c_str());
#endif
#if defined(_WIN32)
s = utils::path::finalize("D:");
EXPECT_STREQ("d:", s.c_str());
EXPECT_STREQ(test_path("d:").c_str(), s.c_str());
s = utils::path::finalize("D:\\");
EXPECT_STREQ("d:", s.c_str());
EXPECT_STREQ(test_path("d:").c_str(), s.c_str());
s = utils::path::finalize("D:\\moose");
EXPECT_STREQ("d:\\moose", s.c_str());
EXPECT_STREQ(test_path("d:\\moose").c_str(), s.c_str());
s = utils::path::finalize("D:\\moose\\");
EXPECT_STREQ("d:\\moose", s.c_str());
EXPECT_STREQ(test_path("d:\\moose").c_str(), s.c_str());
s = utils::path::finalize("D:/");
EXPECT_STREQ("d:", s.c_str());
EXPECT_STREQ(test_path("d:").c_str(), s.c_str());
s = utils::path::finalize("D:/moose");
EXPECT_STREQ("d:\\moose", s.c_str());
EXPECT_STREQ(test_path("d:\\moose").c_str(), s.c_str());
s = utils::path::finalize("D:/moose/");
EXPECT_STREQ("d:\\moose", s.c_str());
EXPECT_STREQ(test_path("d:\\moose").c_str(), s.c_str());
#endif // defined(_WIN32)
}
TEST(utils_path, absolute) {
auto dir = utils::path::absolute(std::filesystem::current_path().string());
auto dir = utils::path::get_current_path<std::string>();
auto path = utils::path::absolute(".");
EXPECT_STREQ(dir.c_str(), path.c_str());
path = utils::path::absolute("./");
EXPECT_STREQ(dir.c_str(), path.c_str());
path = utils::path::absolute(R"(.\)");
EXPECT_STREQ(dir.c_str(), path.c_str());
#if defined(_WIN32)
path = utils::path::absolute(R"(.\moose)");
EXPECT_STREQ((dir + R"(\moose)").c_str(), path.c_str());
path = utils::path::absolute(R"(./moose)");
EXPECT_STREQ((dir + R"(\moose)").c_str(), path.c_str());
auto home_env = utils::get_environment_variable("USERPROFILE");
#else // !defined(_WIN32)
path = utils::path::absolute(R"(.\moose)");
EXPECT_STREQ((dir + R"(/moose)").c_str(), path.c_str());
path = utils::path::absolute(R"(./moose)");
EXPECT_STREQ((dir + R"(/moose)").c_str(), path.c_str());
auto home_env = utils::get_environment_variable("HOME");
#endif // defined(_WIN32)
auto home = utils::path::absolute(home_env);
path = utils::path::absolute("~");
EXPECT_STREQ(home.c_str(), path.c_str());
// path = utils::path::absolute("~/.local");
}
TEST(utils_path, absolute_can_resolve_path_variables) {
std::string home{};
#if defined(_WIN32)
home.resize(MAX_PATH + 1U);
home.resize(repertory::max_path_length + 1U);
auto size = ::GetEnvironmentVariableA("USERPROFILE", home.data(), 0U);
home.resize(size);