From 008310aad988d9a61f6e2a2d41abd5198d8618f0 Mon Sep 17 00:00:00 2001 From: sfan5 Date: Sat, 20 Jan 2024 17:16:08 +0100 Subject: [PATCH] Add unit tests for fs::CopyFileContents --- src/unittest/test_filepath.cpp | 50 +++++++++++++++++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-) diff --git a/src/unittest/test_filepath.cpp b/src/unittest/test_filepath.cpp index f1a79062d..e5c08d656 100644 --- a/src/unittest/test_filepath.cpp +++ b/src/unittest/test_filepath.cpp @@ -38,6 +38,8 @@ public: void testRemoveLastPathComponent(); void testRemoveLastPathComponentWithTrailingDelimiter(); void testRemoveRelativePathComponent(); + void testSafeWriteToFile(); + void testCopyFileContents(); }; static TestFilePath g_test_instance; @@ -49,6 +51,8 @@ void TestFilePath::runTests(IGameDef *gamedef) TEST(testRemoveLastPathComponent); TEST(testRemoveLastPathComponentWithTrailingDelimiter); TEST(testRemoveRelativePathComponent); + TEST(testSafeWriteToFile); + TEST(testCopyFileContents); } //////////////////////////////////////////////////////////////////////////////// @@ -56,7 +60,7 @@ void TestFilePath::runTests(IGameDef *gamedef) // adjusts a POSIX path to system-specific conventions // -> changes '/' to DIR_DELIM // -> absolute paths start with "C:\\" on windows -std::string p(std::string path) +static std::string p(std::string path) { for (size_t i = 0; i < path.size(); ++i) { if (path[i] == '/') { @@ -262,3 +266,47 @@ void TestFilePath::testRemoveRelativePathComponent() result = fs::RemoveRelativePathComponents(path); UASSERT(result == p("/a/e")); } + + +void TestFilePath::testSafeWriteToFile() +{ + const std::string dest_path = getTestTempFile(); + const std::string test_data("hello\0world", 11); + fs::safeWriteToFile(dest_path, test_data); + UASSERT(fs::PathExists(dest_path)); + std::string contents_actual; + UASSERT(fs::ReadFile(dest_path, contents_actual)); + UASSERTEQ(auto, contents_actual, test_data); +} + +void TestFilePath::testCopyFileContents() +{ + const auto dir_path = getTestTempDirectory(); + const auto file1 = dir_path + DIR_DELIM "src", file2 = dir_path + DIR_DELIM "dst"; + const std::string test_data("hello\0world", 11); + + // error case + UASSERT(!fs::CopyFileContents(file1, "somewhere")); + + { + std::ofstream ofs(file1); + ofs << test_data; + } + + // normal case + UASSERT(fs::CopyFileContents(file1, file2)); + std::string contents_actual; + UASSERT(fs::ReadFile(file2, contents_actual)); + UASSERTEQ(auto, contents_actual, test_data); + + // should overwrite and truncate + { + std::ofstream ofs(file2); + for (int i = 0; i < 10; i++) + ofs << "OH MY GAH"; + } + UASSERT(fs::CopyFileContents(file1, file2)); + contents_actual.clear(); + UASSERT(fs::ReadFile(file2, contents_actual)); + UASSERTEQ(auto, contents_actual, test_data); +}