1#include <xrpl/basics/FileUtilities.h>
3#include <xrpl/basics/ByteUtilities.h>
5#include <gtest/gtest.h>
21 explicit TempFile(std::string
const& file, std::string
const& contents)
28 std::ofstream output(file_);
30 throw std::runtime_error(
"Unable to create temporary test file");
39 auto const dir = file_.parent_path();
43 std::cerr <<
"Unable to remove temporary directory '" << dir.string()
44 <<
"': " << ec.
message() <<
'\n';
48 [[nodiscard]] std::filesystem::path
const&
55 std::filesystem::path file_;
60TEST(FileUtilitiesTest, get_file_contents)
62 constexpr char const* kExpectedContents =
"This file is very short. That's all we need.";
64 TempFile
const file(
"test_file",
"This is temporary text that should get overwritten");
67 auto const path = file.file();
76 EXPECT_EQ(good, kExpectedContents);
83 EXPECT_EQ(good, kExpectedContents);
89 EXPECT_TRUE(ec && ec.
value() ==
static_cast<int>(std::errc::file_too_large));
90 EXPECT_TRUE(bad.empty());
T create_directory(T... args)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
TEST(FileUtilitiesTest, get_file_contents)
std::filesystem::path uniqueRandomPath(std::filesystem::path const &base, std::string const &prefix="", std::size_t maxAttempts=100)
Generate a unique, non-existing path under base whose filename starts with prefix and ends with a ran...
std::string getFileContents(std::error_code &ec, std::filesystem::path const &sourcePath, std::optional< std::size_t > maxSize=std::nullopt)
constexpr auto kilobytes(T value) noexcept
void writeFileContents(std::error_code &ec, std::filesystem::path const &destPath, std::string const &contents)
T temp_directory_path(T... args)