FolderFileWatcher.cpp (14466B)
1 // Copyright (c) Microsoft Corporation. 2 // Licensed under the MIT License. 3 #include "pch.h" 4 #include "TestCommon.h" 5 #include <winget/FolderFileWatcher.h> 6 7 using namespace TestCommon; 8 using namespace AppInstaller; 9 10 namespace 11 { 12 void WriteText(const std::filesystem::path& path) 13 { 14 std::ofstream fileStream{ path }; 15 fileStream << "text"; 16 } 17 18 std::filesystem::path RemoveRoot(const std::filesystem::path& prefix, const std::filesystem::path& source) 19 { 20 auto prefixItr = prefix.begin(); 21 auto sourceItr = source.begin(); 22 23 while (prefixItr != prefix.end() && sourceItr != source.end()) 24 { 25 if (*prefixItr != *sourceItr) 26 { 27 break; 28 } 29 30 ++prefixItr; 31 ++sourceItr; 32 } 33 34 std::filesystem::path result{}; 35 if (prefixItr == prefix.end()) 36 { 37 for (; sourceItr != source.end(); ++sourceItr) 38 { 39 if (result.empty()) 40 { 41 result = *sourceItr; 42 } 43 else 44 { 45 result /= *sourceItr; 46 } 47 } 48 } 49 50 return result; 51 } 52 } 53 54 TEST_CASE("FolderFileWatcher_CreateNewFiles", "[FolderFileWatcher]") 55 { 56 TempDirectory dirToWatch("FolderFileWatcher_CreateNewFiles_", true); 57 58 Utility::FolderFileWatcher folderFileWatcher(dirToWatch.GetPath()); 59 folderFileWatcher.Start(); 60 61 TempFile tempFile1(dirToWatch.GetPath(), "file1_", ".txt"); 62 WriteText(tempFile1.GetPath()); 63 64 TempFile tempFile2(dirToWatch.GetPath(), "file2_", ".txt"); 65 WriteText(tempFile2.GetPath()); 66 67 std::filesystem::path newTestDir = dirToWatch.GetPath(); 68 newTestDir /= "testDir"; 69 std::filesystem::create_directories(newTestDir); 70 71 TempFile tempFile3(newTestDir, "file3_", ".txt"); 72 WriteText(tempFile3.GetPath()); 73 74 std::this_thread::sleep_for(100ms); 75 folderFileWatcher.Stop(); 76 77 auto& watchedFiles = folderFileWatcher.Files(); 78 79 auto tempFile1RelativePath = RemoveRoot(dirToWatch, tempFile1.GetPath()); 80 auto foundTempFile1 = watchedFiles.find(tempFile1RelativePath); 81 REQUIRE(foundTempFile1 != watchedFiles.cend()); 82 83 auto tempFile2RelativePath = RemoveRoot(dirToWatch, tempFile2.GetPath()); 84 auto foundTempFile2 = watchedFiles.find(tempFile2RelativePath); 85 REQUIRE(foundTempFile2 != watchedFiles.cend()); 86 87 auto tempFile3RelativePath = RemoveRoot(dirToWatch, tempFile3.GetPath()); 88 auto foundTempFile3 = watchedFiles.find(tempFile3RelativePath); 89 REQUIRE(foundTempFile3 != watchedFiles.cend()); 90 } 91 92 TEST_CASE("FolderFileWatcher_CreateAfterStop", "[FolderFileWatcher]") 93 { 94 TempDirectory dirToWatch("FolderFileWatcher_CreateAfterStop_", true); 95 96 Utility::FolderFileWatcher folderFileWatcher(dirToWatch.GetPath()); 97 folderFileWatcher.Start(); 98 99 TempFile tempFile1(dirToWatch.GetPath(), "file1_", ".txt"); 100 WriteText(tempFile1.GetPath()); 101 102 std::this_thread::sleep_for(100ms); 103 folderFileWatcher.Stop(); 104 105 TempFile tempFile2(dirToWatch.GetPath(), "file2_", ".txt"); 106 WriteText(tempFile2.GetPath()); 107 108 auto& watchedFiles = folderFileWatcher.Files(); 109 110 auto tempFile1RelativePath = RemoveRoot(dirToWatch, tempFile1.GetPath()); 111 auto foundTempFile1 = watchedFiles.find(tempFile1RelativePath); 112 REQUIRE(foundTempFile1 != watchedFiles.cend()); 113 114 auto tempFile2RelativePath = RemoveRoot(dirToWatch, tempFile2.GetPath()); 115 auto foundTempFile2 = watchedFiles.find(tempFile2RelativePath); 116 REQUIRE(foundTempFile2 == watchedFiles.cend()); 117 } 118 119 TEST_CASE("FolderFileWatcher_CreateNewFilesAndRename", "[FolderFileWatcher]") 120 { 121 TempDirectory dirToWatch("FolderFileWatcher_CreateNewFilesAndRename_", true); 122 123 Utility::FolderFileWatcher folderFileWatcher(dirToWatch.GetPath()); 124 folderFileWatcher.Start(); 125 126 std::filesystem::path tempFile1Path = dirToWatch.GetPath() / "file1.txt"; 127 TempFile tempFile1(tempFile1Path); 128 WriteText(tempFile1Path); 129 130 std::filesystem::path newTestDir = dirToWatch.GetPath(); 131 newTestDir /= "testDir"; 132 std::filesystem::create_directories(newTestDir); 133 134 std::filesystem::path tempFile2Path = newTestDir / "file2.txt"; 135 TempFile tempFile2(tempFile2Path); 136 WriteText(tempFile2Path); 137 138 std::filesystem::path tempFile1PathRenamed = dirToWatch.GetPath() / "file1_renamed.txt"; 139 std::filesystem::path tempFile2PathRenamed = newTestDir / "file2_renamed.txt"; 140 141 tempFile1.Rename(tempFile1PathRenamed); 142 tempFile2.Rename(tempFile2PathRenamed); 143 144 std::this_thread::sleep_for(100ms); 145 folderFileWatcher.Stop(); 146 147 auto& watchedFiles = folderFileWatcher.Files(); 148 149 auto tempFile1RelativePath = RemoveRoot(dirToWatch, tempFile1Path); 150 auto foundTempFile1 = watchedFiles.find(tempFile1RelativePath); 151 REQUIRE(foundTempFile1 == watchedFiles.cend()); 152 153 auto tempFile1RenamedRelativePath = RemoveRoot(dirToWatch, tempFile1PathRenamed); 154 auto foundTempFile1Renamed = watchedFiles.find(tempFile1RenamedRelativePath); 155 REQUIRE(foundTempFile1Renamed != watchedFiles.cend()); 156 157 auto tempFile2RelativePath = RemoveRoot(dirToWatch, tempFile2Path); 158 auto foundTempFile2 = watchedFiles.find(tempFile2RelativePath); 159 REQUIRE(foundTempFile2 == watchedFiles.cend()); 160 161 auto tempFile2RenamedRelativePath = RemoveRoot(dirToWatch, tempFile2PathRenamed); 162 auto foundTempFile2Renamed = watchedFiles.find(tempFile2RenamedRelativePath); 163 REQUIRE(foundTempFile2Renamed != watchedFiles.cend()); 164 } 165 166 TEST_CASE("FolderFileWatcher_CreateNewFilesAndDelete", "[FolderFileWatcher]") 167 { 168 TempDirectory dirToWatch("FolderFileWatcher_CreateNewFilesAndDelete_", true); 169 170 Utility::FolderFileWatcher folderFileWatcher(dirToWatch.GetPath()); 171 folderFileWatcher.Start(); 172 173 TempFile tempFile1(dirToWatch.GetPath(), "file1_", ".txt"); 174 WriteText(tempFile1.GetPath()); 175 176 std::filesystem::path newTestDir = dirToWatch.GetPath(); 177 newTestDir /= "testDir"; 178 std::filesystem::create_directories(newTestDir); 179 180 TempFile tempFile2(newTestDir, "file2_", ".txt"); 181 WriteText(tempFile2.GetPath()); 182 183 // Create files and delete them. 184 std::filesystem::path tempFile3Path; 185 std::filesystem::path tempFile4Path; 186 { 187 TempFile tempFile3(dirToWatch.GetPath(), "file3_", ".txt"); 188 tempFile3Path = tempFile3.GetPath(); 189 WriteText(tempFile3Path); 190 std::filesystem::remove(tempFile3Path); 191 192 TempFile tempFile4(newTestDir, "file4_", ".txt"); 193 tempFile4Path = tempFile4.GetPath(); 194 WriteText(tempFile4Path); 195 std::filesystem::remove(tempFile4Path); 196 } 197 198 std::this_thread::sleep_for(100ms); 199 folderFileWatcher.Stop(); 200 201 auto& watchedFiles = folderFileWatcher.Files(); 202 203 auto tempFile1RelativePath = RemoveRoot(dirToWatch, tempFile1.GetPath()); 204 auto foundTempFile1 = watchedFiles.find(tempFile1RelativePath); 205 REQUIRE(foundTempFile1 != watchedFiles.cend()); 206 207 auto tempFile2RelativePath = RemoveRoot(dirToWatch, tempFile2.GetPath()); 208 auto foundTempFile2 = watchedFiles.find(tempFile2RelativePath); 209 REQUIRE(foundTempFile2 != watchedFiles.cend()); 210 211 auto tempFile3RelativePath = RemoveRoot(dirToWatch, tempFile3Path); 212 auto foundTempFile3 = watchedFiles.find(tempFile3RelativePath); 213 REQUIRE(foundTempFile3 == watchedFiles.cend()); 214 215 auto tempFile4RelativePath = RemoveRoot(dirToWatch, tempFile4Path); 216 auto foundTempFile4 = watchedFiles.find(tempFile4RelativePath); 217 REQUIRE(foundTempFile4 == watchedFiles.cend()); 218 } 219 220 TEST_CASE("FolderFileWatcher_Extension_CreateNewFiles", "[FolderFileWatcher]") 221 { 222 TempDirectory dirToWatch("FolderFileWatcher_Extension_CreateNewFiles", true); 223 224 Utility::FolderFileWatcher folderFileExtensionWatcher(dirToWatch.GetPath(), ".yaml"); 225 folderFileExtensionWatcher.Start(); 226 227 TempFile tempFile1(dirToWatch.GetPath(), "file1_", ".txt"); 228 WriteText(tempFile1.GetPath()); 229 230 TempFile tempFile2(dirToWatch.GetPath(), "file2_", ".yaml"); 231 WriteText(tempFile2.GetPath()); 232 233 std::filesystem::path newTestDir = dirToWatch.GetPath(); 234 newTestDir /= "testDir"; 235 std::filesystem::create_directories(newTestDir); 236 237 TempFile tempFile3(newTestDir, "file3_", ".txt"); 238 WriteText(tempFile3.GetPath()); 239 240 TempFile tempFile4(newTestDir, "file4_", ".yaml"); 241 WriteText(tempFile4.GetPath()); 242 243 std::this_thread::sleep_for(100ms); 244 folderFileExtensionWatcher.Stop(); 245 246 auto& watchedFiles = folderFileExtensionWatcher.Files(); 247 248 auto tempFile1RelativePath = RemoveRoot(dirToWatch, tempFile1.GetPath()); 249 auto foundTempFile1 = watchedFiles.find(tempFile1RelativePath); 250 REQUIRE(foundTempFile1 == watchedFiles.cend()); 251 252 auto tempFile2RelativePath = RemoveRoot(dirToWatch, tempFile2.GetPath()); 253 auto foundTempFile2 = watchedFiles.find(tempFile2RelativePath); 254 REQUIRE(foundTempFile2 != watchedFiles.cend()); 255 256 auto tempFile3RelativePath = RemoveRoot(dirToWatch, tempFile3.GetPath()); 257 auto foundTempFile3 = watchedFiles.find(tempFile3RelativePath); 258 REQUIRE(foundTempFile3 == watchedFiles.cend()); 259 260 auto tempFile4RelativePath = RemoveRoot(dirToWatch, tempFile4.GetPath()); 261 auto foundTempFile4 = watchedFiles.find(tempFile4RelativePath); 262 REQUIRE(foundTempFile4 != watchedFiles.cend()); 263 } 264 265 TEST_CASE("FolderFileWatcher_Extension_CreateAfterStop", "[FolderFileWatcher]") 266 { 267 TempDirectory dirToWatch("FolderFileWatcher_Extension_CreateAfterStop", true); 268 269 Utility::FolderFileWatcher folderFileExtensionWatcher(dirToWatch.GetPath(), ".txt"); 270 folderFileExtensionWatcher.Start(); 271 272 TempFile tempFile1(dirToWatch.GetPath(), "file1_", ".txt"); 273 WriteText(tempFile1.GetPath()); 274 275 std::this_thread::sleep_for(100ms); 276 folderFileExtensionWatcher.Stop(); 277 278 TempFile tempFile2(dirToWatch.GetPath(), "file2_", ".txt"); 279 WriteText(tempFile2.GetPath()); 280 281 auto& watchedFiles = folderFileExtensionWatcher.Files(); 282 283 auto tempFile1RelativePath = RemoveRoot(dirToWatch, tempFile1.GetPath()); 284 auto foundTempFile1 = watchedFiles.find(tempFile1RelativePath); 285 REQUIRE(foundTempFile1 != watchedFiles.cend()); 286 287 auto tempFile2RelativePath = RemoveRoot(dirToWatch, tempFile2.GetPath()); 288 auto foundTempFile2 = watchedFiles.find(tempFile2RelativePath); 289 REQUIRE(foundTempFile2 == watchedFiles.cend()); 290 } 291 292 TEST_CASE("FolderFileWatcher_Extension_CreateNewFilesAndRename", "[FolderFileWatcher]") 293 { 294 TempDirectory dirToWatch("FolderFileWatcher_Extension_CreateNewFilesAndRename", true); 295 296 Utility::FolderFileWatcher folderFileExtensionWatcher(dirToWatch.GetPath(), ".txt"); 297 folderFileExtensionWatcher.Start(); 298 299 std::filesystem::path tempFile1Path = dirToWatch.GetPath() / "file1.txt"; 300 TempFile tempFile1(tempFile1Path); 301 WriteText(tempFile1Path); 302 303 std::filesystem::path newTestDir = dirToWatch.GetPath(); 304 newTestDir /= "testDir"; 305 std::filesystem::create_directories(newTestDir); 306 307 std::filesystem::path tempFile2Path = newTestDir / "file2.txt"; 308 TempFile tempFile2(tempFile2Path); 309 WriteText(tempFile2Path); 310 311 std::filesystem::path tempFile1PathRenamed = dirToWatch.GetPath() / "file1_renamed.txt"; 312 std::filesystem::path tempFile2PathRenamed = newTestDir / "file2_renamed.txt"; 313 314 tempFile1.Rename(tempFile1PathRenamed); 315 tempFile2.Rename(tempFile2PathRenamed); 316 317 std::this_thread::sleep_for(100ms); 318 folderFileExtensionWatcher.Stop(); 319 320 auto& watchedFiles = folderFileExtensionWatcher.Files(); 321 322 auto tempFile1RelativePath = RemoveRoot(dirToWatch, tempFile1Path); 323 auto foundTempFile1 = watchedFiles.find(tempFile1RelativePath); 324 REQUIRE(foundTempFile1 == watchedFiles.cend()); 325 326 auto tempFile1RenamedRelativePath = RemoveRoot(dirToWatch, tempFile1PathRenamed); 327 auto foundTempFile1Renamed = watchedFiles.find(tempFile1RenamedRelativePath); 328 REQUIRE(foundTempFile1Renamed != watchedFiles.cend()); 329 330 auto tempFile2RelativePath = RemoveRoot(dirToWatch, tempFile2Path); 331 auto foundTempFile2 = watchedFiles.find(tempFile2RelativePath); 332 REQUIRE(foundTempFile2 == watchedFiles.cend()); 333 334 auto tempFile2RenamedRelativePath = RemoveRoot(dirToWatch, tempFile2PathRenamed); 335 auto foundTempFile2Renamed = watchedFiles.find(tempFile2RenamedRelativePath); 336 REQUIRE(foundTempFile2Renamed != watchedFiles.cend()); 337 } 338 339 TEST_CASE("FolderFileWatcher_Extension_CreateNewFilesAndDelete", "[FolderFileWatcher]") 340 { 341 TempDirectory dirToWatch("FolderFileWatcher_Extension_CreateNewFilesAndDelete", true); 342 343 Utility::FolderFileWatcher folderFileExtensionWatcher(dirToWatch.GetPath(), ".txt"); 344 folderFileExtensionWatcher.Start(); 345 346 TempFile tempFile1(dirToWatch.GetPath(), "file1_", ".txt"); 347 WriteText(tempFile1.GetPath()); 348 349 std::filesystem::path newTestDir = dirToWatch.GetPath(); 350 newTestDir /= "testDir"; 351 std::filesystem::create_directories(newTestDir); 352 353 TempFile tempFile2(newTestDir, "file2_", ".txt"); 354 WriteText(tempFile2.GetPath()); 355 356 // Create files and delete them. 357 std::filesystem::path tempFile3Path; 358 std::filesystem::path tempFile4Path; 359 { 360 TempFile tempFile3(dirToWatch.GetPath(), "file3_", ".txt"); 361 tempFile3Path = tempFile3.GetPath(); 362 WriteText(tempFile3Path); 363 std::filesystem::remove(tempFile3Path); 364 365 TempFile tempFile4(newTestDir, "file4_", ".txt"); 366 tempFile4Path = tempFile4.GetPath(); 367 WriteText(tempFile4Path); 368 std::filesystem::remove(tempFile4Path); 369 } 370 371 std::this_thread::sleep_for(100ms); 372 folderFileExtensionWatcher.Stop(); 373 374 auto& watchedFiles = folderFileExtensionWatcher.Files(); 375 376 auto tempFile1RelativePath = RemoveRoot(dirToWatch, tempFile1.GetPath()); 377 auto foundTempFile1 = watchedFiles.find(tempFile1RelativePath); 378 REQUIRE(foundTempFile1 != watchedFiles.cend()); 379 380 auto tempFile2RelativePath = RemoveRoot(dirToWatch, tempFile2.GetPath()); 381 auto foundTempFile2 = watchedFiles.find(tempFile2RelativePath); 382 REQUIRE(foundTempFile2 != watchedFiles.cend()); 383 384 auto tempFile3RelativePath = RemoveRoot(dirToWatch, tempFile3Path); 385 auto foundTempFile3 = watchedFiles.find(tempFile3RelativePath); 386 REQUIRE(foundTempFile3 == watchedFiles.cend()); 387 388 auto tempFile4RelativePath = RemoveRoot(dirToWatch, tempFile4Path); 389 auto foundTempFile4 = watchedFiles.find(tempFile4RelativePath); 390 REQUIRE(foundTempFile4 == watchedFiles.cend()); 391 }