LoggerHelper.Tests.cpp
1 #include "pch.h" 2 #include "TestHelpers.h" 3 #include <logger_helper.h> 4 5 using namespace Microsoft::VisualStudio::CppUnitTestFramework; 6 using namespace LoggerHelpers; 7 8 namespace UnitTestsCommonUtils 9 { 10 TEST_CLASS(LoggerHelperTests) 11 { 12 public: 13 // get_log_folder_path tests 14 TEST_METHOD(GetLogFolderPath_ValidAppPath_ReturnsPath) 15 { 16 auto result = get_log_folder_path(L"TestApp"); 17 18 Assert::IsFalse(result.empty()); 19 // Should contain the app name or be a valid path 20 auto pathStr = result.wstring(); 21 Assert::IsTrue(pathStr.length() > 0); 22 } 23 24 TEST_METHOD(GetLogFolderPath_EmptyAppPath_ReturnsPath) 25 { 26 auto result = get_log_folder_path(L""); 27 28 // Should still return a base path 29 Assert::IsTrue(true); // Just verify no crash 30 } 31 32 TEST_METHOD(GetLogFolderPath_SpecialCharacters_Works) 33 { 34 auto result = get_log_folder_path(L"Test App With Spaces"); 35 36 // Should handle spaces in path 37 Assert::IsTrue(true); 38 } 39 40 TEST_METHOD(GetLogFolderPath_ConsistentResults) 41 { 42 auto result1 = get_log_folder_path(L"TestApp"); 43 auto result2 = get_log_folder_path(L"TestApp"); 44 45 Assert::AreEqual(result1.wstring(), result2.wstring()); 46 } 47 48 // dir_exists tests 49 TEST_METHOD(DirExists_WindowsDirectory_ReturnsTrue) 50 { 51 bool result = dir_exists(std::filesystem::path(L"C:\\Windows")); 52 Assert::IsTrue(result); 53 } 54 55 TEST_METHOD(DirExists_NonExistentDirectory_ReturnsFalse) 56 { 57 bool result = dir_exists(std::filesystem::path(L"C:\\NonExistentDir12345")); 58 Assert::IsFalse(result); 59 } 60 61 TEST_METHOD(DirExists_FileInsteadOfDir_ReturnsTrue) 62 { 63 // notepad.exe is a file, not a directory 64 bool result = dir_exists(std::filesystem::path(L"C:\\Windows\\notepad.exe")); 65 Assert::IsTrue(result); 66 } 67 68 TEST_METHOD(DirExists_EmptyPath_ReturnsFalse) 69 { 70 bool result = dir_exists(std::filesystem::path(L"")); 71 Assert::IsFalse(result); 72 } 73 74 TEST_METHOD(DirExists_TempDirectory_ReturnsTrue) 75 { 76 wchar_t tempPath[MAX_PATH]; 77 GetTempPathW(MAX_PATH, tempPath); 78 79 bool result = dir_exists(std::filesystem::path(tempPath)); 80 Assert::IsTrue(result); 81 } 82 83 // delete_old_log_folder tests 84 TEST_METHOD(DeleteOldLogFolder_NonExistentFolder_DoesNotCrash) 85 { 86 delete_old_log_folder(std::filesystem::path(L"C:\\NonExistentLogFolder12345")); 87 Assert::IsTrue(true); 88 } 89 90 TEST_METHOD(DeleteOldLogFolder_ValidEmptyFolder_Works) 91 { 92 TestHelpers::TempDirectory tempDir; 93 94 // Create a subfolder structure 95 auto logFolder = std::filesystem::path(tempDir.path()) / L"logs"; 96 std::filesystem::create_directories(logFolder); 97 98 Assert::IsTrue(std::filesystem::exists(logFolder)); 99 100 delete_old_log_folder(logFolder); 101 102 // Folder may or may not be deleted depending on implementation 103 Assert::IsTrue(true); 104 } 105 106 // delete_other_versions_log_folders tests 107 TEST_METHOD(DeleteOtherVersionsLogFolders_NonExistentPath_DoesNotCrash) 108 { 109 delete_other_versions_log_folders(L"C:\\NonExistent\\Path", L"1.0.0"); 110 Assert::IsTrue(true); 111 } 112 113 TEST_METHOD(DeleteOtherVersionsLogFolders_EmptyVersion_DoesNotCrash) 114 { 115 wchar_t tempPath[MAX_PATH]; 116 GetTempPathW(MAX_PATH, tempPath); 117 118 delete_other_versions_log_folders(tempPath, L""); 119 Assert::IsTrue(true); 120 } 121 122 // Thread safety tests 123 TEST_METHOD(GetLogFolderPath_ThreadSafe) 124 { 125 std::vector<std::thread> threads; 126 std::atomic<int> successCount{ 0 }; 127 128 for (int i = 0; i < 10; ++i) 129 { 130 threads.emplace_back([&successCount, i]() { 131 auto path = get_log_folder_path(L"TestApp" + std::to_wstring(i)); 132 if (!path.empty()) 133 { 134 successCount++; 135 } 136 }); 137 } 138 139 for (auto& t : threads) 140 { 141 t.join(); 142 } 143 144 Assert::AreEqual(10, successCount.load()); 145 } 146 147 TEST_METHOD(DirExists_ThreadSafe) 148 { 149 std::vector<std::thread> threads; 150 std::atomic<int> successCount{ 0 }; 151 152 for (int i = 0; i < 10; ++i) 153 { 154 threads.emplace_back([&successCount]() { 155 for (int j = 0; j < 10; ++j) 156 { 157 dir_exists(std::filesystem::path(L"C:\\Windows")); 158 successCount++; 159 } 160 }); 161 } 162 163 for (auto& t : threads) 164 { 165 t.join(); 166 } 167 168 Assert::AreEqual(100, successCount.load()); 169 } 170 171 // Path construction tests 172 TEST_METHOD(GetLogFolderPath_ReturnsValidFilesystemPath) 173 { 174 auto result = get_log_folder_path(L"TestApp"); 175 176 // Should be a valid path that we can use with filesystem operations 177 Assert::IsTrue(result.is_absolute() || result.has_root_name() || !result.empty()); 178 } 179 }; 180 }