Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement FolderFileWatcher #2336

Merged
merged 8 commits into from
Jul 21, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/AppInstallerCLITests/AppInstallerCLITests.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@
<ClCompile Include="Dependencies.cpp" />
<ClCompile Include="Downloader.cpp" />
<ClCompile Include="ExperimentalFeature.cpp" />
<ClCompile Include="FolderFileWatcher.cpp" />
<ClCompile Include="GroupPolicy.cpp" />
<ClCompile Include="HashCommand.cpp" />
<ClCompile Include="HttpClientHelper.cpp" />
Expand Down Expand Up @@ -414,13 +415,13 @@
</CopyFileToFolders>
<CopyFileToFolders Include="TestData\Manifest-Bad-InstallerTypeZip-MultipleNestedInstallers.yaml">
<DeploymentContent>true</DeploymentContent>
</CopyFileToFolders>
</CopyFileToFolders>
<CopyFileToFolders Include="TestData\Manifest-Bad-InstallerTypeZip-NoNestedInstallerFile.yaml">
<DeploymentContent>true</DeploymentContent>
</CopyFileToFolders>
</CopyFileToFolders>
<CopyFileToFolders Include="TestData\Manifest-Bad-InstallerTypeZip-NoNestedInstallerType.yaml">
<DeploymentContent>true</DeploymentContent>
</CopyFileToFolders>
</CopyFileToFolders>
<CopyFileToFolders Include="TestData\Manifest-Bad-InstallerUniqueness-DefaultScope.yaml">
<DeploymentContent>true</DeploymentContent>
</CopyFileToFolders>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
</Filter>
<Filter Include="Source Files\Repository">
<UniqueIdentifier>{13d4d227-0f04-4e57-a663-c3c535438ab3}</UniqueIdentifier>
</Filter>
</Filter>
</ItemGroup>
<ItemGroup>
<ClInclude Include="pch.h">
Expand Down Expand Up @@ -209,6 +209,9 @@
<ClCompile Include="YamlManifest.cpp">
<Filter>Source Files\Common</Filter>
</ClCompile>
<ClCompile Include="FolderFileWatcher.cpp">
<Filter>Source Files\Common</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<None Include="PropertySheet.props" />
Expand Down Expand Up @@ -590,7 +593,7 @@
</CopyFileToFolders>
<CopyFileToFolders Include="TestData\ManifestV1_3-Singleton.yaml">
<Filter>TestData</Filter>
</CopyFileToFolders>
</CopyFileToFolders>
<CopyFileToFolders Include="TestData\MultiFileManifestV1\ManifestV1-MultiFile-DefaultLocale.yaml">
<Filter>TestData\MultiFileManifestV1</Filter>
</CopyFileToFolders>
Expand Down Expand Up @@ -638,7 +641,7 @@
</CopyFileToFolders>
<CopyFileToFolders Include="TestData\MultiFileManifestV1_3\ManifestV1_3-MultiFile-Version.yaml">
<Filter>TestData\MultiFileManifestV1_3</Filter>
</CopyFileToFolders>
</CopyFileToFolders>
<CopyFileToFolders Include="TestData\Installer_Exe_Dependencies.yaml">
<Filter>TestData</Filter>
</CopyFileToFolders>
Expand Down
325 changes: 325 additions & 0 deletions src/AppInstallerCLITests/FolderFileWatcher.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,325 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
#include "pch.h"
#include "TestCommon.h"
#include <winget/FolderFileWatcher.h>

using namespace TestCommon;
using namespace AppInstaller;

namespace
{
void WriteText(const std::filesystem::path& path)
{
std::ofstream fileStream{ path };
fileStream << "text";
}
}

TEST_CASE("FolderFileWatcher_CreateNewFiles", "[FolderFileWatcher]")
{
TempDirectory dirToWatch("FolderFileWatcher_CreateNewFiles_", true);

Utility::FolderFileWatcher folderFileWatcher(dirToWatch.GetPath());
folderFileWatcher.start();

TempFile tmpFile1(dirToWatch.GetPath(), "file1_", ".txt");
WriteText(tmpFile1.GetPath());

TempFile tmpFile2(dirToWatch.GetPath(), "file2_", ".txt");
WriteText(tmpFile2.GetPath());

std::filesystem::path newTestDir = dirToWatch.GetPath();
newTestDir /= "testDir";
std::filesystem::create_directories(newTestDir);

TempFile tmpFile3(newTestDir, "file3_", ".txt");
WriteText(tmpFile3.GetPath());

std::this_thread::sleep_for(100ms);
folderFileWatcher.stop();

auto& watchedFiles = folderFileWatcher.files();

auto foundTmpFile1 = watchedFiles.find(tmpFile1.GetPath().string());
REQUIRE(foundTmpFile1 != watchedFiles.cend());

auto foundTmpFile2 = watchedFiles.find(tmpFile2.GetPath().string());
REQUIRE(foundTmpFile2 != watchedFiles.cend());

auto foundTmpFile3 = watchedFiles.find(tmpFile3.GetPath().string());
REQUIRE(foundTmpFile3 != watchedFiles.cend());
}

TEST_CASE("FolderFileWatcher_CreateAfterStop", "[FolderFileWatcher]")
{
TempDirectory dirToWatch("FolderFileWatcher_CreateAfterStop_", true);

Utility::FolderFileWatcher folderFileWatcher(dirToWatch.GetPath());
folderFileWatcher.start();

TempFile tmpFile1(dirToWatch.GetPath(), "file1_", ".txt");
WriteText(tmpFile1.GetPath());

std::this_thread::sleep_for(100ms);
folderFileWatcher.stop();

TempFile tmpFile2(dirToWatch.GetPath(), "file2_", ".txt");
WriteText(tmpFile2.GetPath());

auto& watchedFiles = folderFileWatcher.files();

auto foundTmpFile1 = watchedFiles.find(tmpFile1.GetPath().string());
REQUIRE(foundTmpFile1 != watchedFiles.cend());

auto foundTmpFile2 = watchedFiles.find(tmpFile2.GetPath().string());
REQUIRE(foundTmpFile2 == watchedFiles.cend());
}

TEST_CASE("FolderFileWatcher_CreateNewFilesAndRename", "[FolderFileWatcher]")
{
TempDirectory dirToWatch("FolderFileWatcher_CreateNewFilesAndRename_", true);

Utility::FolderFileWatcher folderFileWatcher(dirToWatch.GetPath());
folderFileWatcher.start();

std::filesystem::path tmpFile1Path = dirToWatch.GetPath() / "file1.txt";
TempFile tmpFile1(tmpFile1Path);
WriteText(tmpFile1Path);

std::filesystem::path newTestDir = dirToWatch.GetPath();
newTestDir /= "testDir";
std::filesystem::create_directories(newTestDir);

std::filesystem::path tmpFile2Path = newTestDir / "file2.txt";
TempFile tmpFile2(tmpFile2Path);
WriteText(tmpFile2Path);

std::filesystem::path tmpFile1PathRenamed = dirToWatch.GetPath() / "file1_renamed.txt";
std::filesystem::path tmpFile2PathRenamed = newTestDir / "file2_renamed.txt";

tmpFile1.Rename(tmpFile1PathRenamed);
tmpFile2.Rename(tmpFile2PathRenamed);

std::this_thread::sleep_for(100ms);
folderFileWatcher.stop();

auto& watchedFiles = folderFileWatcher.files();

auto foundTmpFile1 = watchedFiles.find(tmpFile1Path.string());
REQUIRE(foundTmpFile1 == watchedFiles.cend());

auto foundTmpFile1Renamed = watchedFiles.find(tmpFile1PathRenamed.string());
REQUIRE(foundTmpFile1Renamed != watchedFiles.cend());

auto foundTmpFile2 = watchedFiles.find(tmpFile2Path.string());
REQUIRE(foundTmpFile2 == watchedFiles.cend());

auto foundTmpFile2Renamed = watchedFiles.find(tmpFile2PathRenamed.string());
REQUIRE(foundTmpFile2Renamed != watchedFiles.cend());
}

TEST_CASE("FolderFileWatcher_CreateNewFilesAndDelete", "[FolderFileWatcher]")
{
TempDirectory dirToWatch("FolderFileWatcher_CreateNewFilesAndDelete_", true);

Utility::FolderFileWatcher folderFileWatcher(dirToWatch.GetPath());
folderFileWatcher.start();

TempFile tmpFile1(dirToWatch.GetPath(), "file1_", ".txt");
WriteText(tmpFile1.GetPath());

std::filesystem::path newTestDir = dirToWatch.GetPath();
newTestDir /= "testDir";
std::filesystem::create_directories(newTestDir);

TempFile tmpFile2(newTestDir, "file2_", ".txt");
WriteText(tmpFile2.GetPath());

// Create files and delete them.
std::filesystem::path tmpFile3Path;
std::filesystem::path tmpFile4Path;
{
TempFile tmpFile3(dirToWatch.GetPath(), "file3_", ".txt");
tmpFile3Path = tmpFile3.GetPath();
WriteText(tmpFile3Path);

TempFile tmpFile4(newTestDir, "file4_", ".txt");
tmpFile4Path = tmpFile4.GetPath();
WriteText(tmpFile4Path);
}

std::this_thread::sleep_for(100ms);
folderFileWatcher.stop();

auto& watchedFiles = folderFileWatcher.files();

auto foundTmpFile1 = watchedFiles.find(tmpFile1.GetPath().string());
REQUIRE(foundTmpFile1 != watchedFiles.cend());

auto foundTmpFile2 = watchedFiles.find(tmpFile2.GetPath().string());
REQUIRE(foundTmpFile2 != watchedFiles.cend());

auto foundTmpFile3 = watchedFiles.find(tmpFile3Path.string());
REQUIRE(foundTmpFile3 == watchedFiles.cend());

auto foundTmpFile4 = watchedFiles.find(tmpFile4Path.string());
REQUIRE(foundTmpFile4 == watchedFiles.cend());
}

TEST_CASE("FolderFileExtensionWatcher_CreateNewFiles", "[FolderFileWatcher]")
{
TempDirectory dirToWatch("FolderFileExtensionWatcher_CreateNewFiles_", true);

Utility::FolderFileExtensionWatcher folderFileExtensionWatcher(dirToWatch.GetPath(), ".yaml");
folderFileExtensionWatcher.start();

TempFile tmpFile1(dirToWatch.GetPath(), "file1_", ".txt");
WriteText(tmpFile1.GetPath());

TempFile tmpFile2(dirToWatch.GetPath(), "file2_", ".yaml");
WriteText(tmpFile2.GetPath());

std::filesystem::path newTestDir = dirToWatch.GetPath();
newTestDir /= "testDir";
std::filesystem::create_directories(newTestDir);

TempFile tmpFile3(newTestDir, "file3_", ".txt");
WriteText(tmpFile3.GetPath());

TempFile tmpFile4(newTestDir, "file4_", ".yaml");
WriteText(tmpFile4.GetPath());

std::this_thread::sleep_for(100ms);
folderFileExtensionWatcher.stop();

auto& watchedFiles = folderFileExtensionWatcher.files();

auto foundTmpFile1 = watchedFiles.find(tmpFile1.GetPath().string());
REQUIRE(foundTmpFile1 == watchedFiles.cend());

auto foundTmpFile2 = watchedFiles.find(tmpFile2.GetPath().string());
REQUIRE(foundTmpFile2 != watchedFiles.cend());

auto foundTmpFile3 = watchedFiles.find(tmpFile3.GetPath().string());
REQUIRE(foundTmpFile3 == watchedFiles.cend());

auto foundTmpFile4 = watchedFiles.find(tmpFile4.GetPath().string());
REQUIRE(foundTmpFile4 != watchedFiles.cend());
}

TEST_CASE("FolderFileExtensionWatcher_CreateAfterStop", "[FolderFileWatcher]")
{
TempDirectory dirToWatch("FolderFileExtensionWatcher_CreateAfterStop_", true);

Utility::FolderFileExtensionWatcher folderFileExtensionWatcher(dirToWatch.GetPath(), ".txt");
folderFileExtensionWatcher.start();

TempFile tmpFile1(dirToWatch.GetPath(), "file1_", ".txt");
WriteText(tmpFile1.GetPath());

std::this_thread::sleep_for(100ms);
folderFileExtensionWatcher.stop();

TempFile tmpFile2(dirToWatch.GetPath(), "file2_", ".txt");
WriteText(tmpFile2.GetPath());

auto& watchedFiles = folderFileExtensionWatcher.files();

auto foundTmpFile1 = watchedFiles.find(tmpFile1.GetPath().string());
REQUIRE(foundTmpFile1 != watchedFiles.cend());

auto foundTmpFile2 = watchedFiles.find(tmpFile2.GetPath().string());
REQUIRE(foundTmpFile2 == watchedFiles.cend());
}

TEST_CASE("FolderFileExtensionWatcher_CreateNewFilesAndRename", "[FolderFileWatcher]")
{
TempDirectory dirToWatch("FolderFileExtensionWatcher_CreateNewFilesAndRename_", true);

Utility::FolderFileExtensionWatcher folderFileExtensionWatcher(dirToWatch.GetPath(), ".txt");
folderFileExtensionWatcher.start();

std::filesystem::path tmpFile1Path = dirToWatch.GetPath() / "file1.txt";
TempFile tmpFile1(tmpFile1Path);
WriteText(tmpFile1Path);

std::filesystem::path newTestDir = dirToWatch.GetPath();
newTestDir /= "testDir";
std::filesystem::create_directories(newTestDir);

std::filesystem::path tmpFile2Path = newTestDir / "file2.txt";
TempFile tmpFile2(tmpFile2Path);
WriteText(tmpFile2Path);

std::filesystem::path tmpFile1PathRenamed = dirToWatch.GetPath() / "file1_renamed.txt";
std::filesystem::path tmpFile2PathRenamed = newTestDir / "file2_renamed.txt";

tmpFile1.Rename(tmpFile1PathRenamed);
tmpFile2.Rename(tmpFile2PathRenamed);

std::this_thread::sleep_for(100ms);
folderFileExtensionWatcher.stop();

auto& watchedFiles = folderFileExtensionWatcher.files();

auto foundTmpFile1 = watchedFiles.find(tmpFile1Path.string());
REQUIRE(foundTmpFile1 == watchedFiles.cend());

auto foundTmpFile1Renamed = watchedFiles.find(tmpFile1PathRenamed.string());
REQUIRE(foundTmpFile1Renamed != watchedFiles.cend());

auto foundTmpFile2 = watchedFiles.find(tmpFile2Path.string());
REQUIRE(foundTmpFile2 == watchedFiles.cend());

auto foundTmpFile2Renamed = watchedFiles.find(tmpFile2PathRenamed.string());
REQUIRE(foundTmpFile2Renamed != watchedFiles.cend());
}

TEST_CASE("FolderFileExtensionWatcher_CreateNewFilesAndDelete", "[FolderFileWatcher]")
{
TempDirectory dirToWatch("FolderFileExtensionWatcher_CreateNewFilesAndDelete_", true);

Utility::FolderFileExtensionWatcher folderFileExtensionWatcher(dirToWatch.GetPath(), ".txt");
folderFileExtensionWatcher.start();

TempFile tmpFile1(dirToWatch.GetPath(), "file1_", ".txt");
WriteText(tmpFile1.GetPath());

std::filesystem::path newTestDir = dirToWatch.GetPath();
newTestDir /= "testDir";
std::filesystem::create_directories(newTestDir);

TempFile tmpFile2(newTestDir, "file2_", ".txt");
WriteText(tmpFile2.GetPath());

// Create files and delete them.
std::filesystem::path tmpFile3Path;
std::filesystem::path tmpFile4Path;
{
TempFile tmpFile3(dirToWatch.GetPath(), "file3_", ".txt");
tmpFile3Path = tmpFile3.GetPath();
WriteText(tmpFile3Path);

TempFile tmpFile4(newTestDir, "file4_", ".txt");
tmpFile4Path = tmpFile4.GetPath();
WriteText(tmpFile4Path);
}

std::this_thread::sleep_for(100ms);
folderFileExtensionWatcher.stop();

auto& watchedFiles = folderFileExtensionWatcher.files();

auto foundTmpFile1 = watchedFiles.find(tmpFile1.GetPath().string());
REQUIRE(foundTmpFile1 != watchedFiles.cend());

auto foundTmpFile2 = watchedFiles.find(tmpFile2.GetPath().string());
REQUIRE(foundTmpFile2 != watchedFiles.cend());

auto foundTmpFile3 = watchedFiles.find(tmpFile3Path.string());
REQUIRE(foundTmpFile3 == watchedFiles.cend());

auto foundTmpFile4 = watchedFiles.find(tmpFile4Path.string());
REQUIRE(foundTmpFile4 == watchedFiles.cend());
}
Loading