forked from microsoft/winget-cli
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPortableInstaller.cpp
More file actions
215 lines (168 loc) · 11.1 KB
/
PortableInstaller.cpp
File metadata and controls
215 lines (168 loc) · 11.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
#include "pch.h"
#include "TestCommon.h"
#include <PortableInstaller.h>
#include <Public/AppInstallerArchitecture.h>
#include <winget/Filesystem.h>
#include <winget/Manifest.h>
#include <winget/PathVariable.h>
#include <winget/PortableARPEntry.h>
#include <winget/SQLiteStorageBase.h>
#include <Microsoft/Schema/IPortableIndex.h>
#include <winget/PortableIndex.h>
using namespace std::string_literals;
using namespace AppInstaller::CLI::Portable;
using namespace AppInstaller::Filesystem;
using namespace AppInstaller::Manifest;
using namespace AppInstaller::Registry::Environment;
using namespace AppInstaller::Repository::Microsoft;
using namespace AppInstaller::SQLite;
using namespace AppInstaller::Repository::Microsoft::Schema;
using namespace AppInstaller::Utility;
using namespace AppInstaller::CLI::Workflow;
using namespace TestCommon;
TEST_CASE("PortableInstaller_InstallToRegistry", "[PortableInstaller]")
{
TempDirectory tempDirectory = TestCommon::TempDirectory("TempDirectory", false);
std::vector<PortableFileEntry> desiredTestState;
TestCommon::TempFile testPortable("testPortable.txt");
std::ofstream file(testPortable, std::ofstream::out);
file.close();
std::filesystem::path targetPath = tempDirectory.GetPath() / "testPortable.txt";
std::filesystem::path symlinkPath = tempDirectory.GetPath() / "testSymlink.exe";
desiredTestState.emplace_back(std::move(PortableFileEntry::CreateFileEntry(testPortable.GetPath(), targetPath, {})));
desiredTestState.emplace_back(std::move(PortableFileEntry::CreateSymlinkEntry(symlinkPath, targetPath)));
PortableInstaller portableInstaller = PortableInstaller(ScopeEnum::User, Architecture::X64, "testProductCode");
portableInstaller.TargetInstallLocation = tempDirectory.GetPath();
portableInstaller.SetDesiredState(desiredTestState);
REQUIRE(portableInstaller.VerifyExpectedState());
portableInstaller.Install(AppInstaller::CLI::Workflow::OperationType::Install);
auto entry = portableInstaller.GetAppsAndFeaturesEntry();
REQUIRE(entry.ProductCode == portableInstaller.GetProductCode());
PortableInstaller portableInstaller2 = PortableInstaller(ScopeEnum::User, Architecture::X64, "testProductCode");
REQUIRE(portableInstaller2.ARPEntryExists());
REQUIRE(std::filesystem::exists(portableInstaller2.PortableTargetFullPath));
REQUIRE(AppInstaller::Filesystem::SymlinkExists(portableInstaller2.PortableSymlinkFullPath));
portableInstaller2.Uninstall();
REQUIRE_FALSE(std::filesystem::exists(portableInstaller2.PortableTargetFullPath));
REQUIRE_FALSE(AppInstaller::Filesystem::SymlinkExists(portableInstaller2.PortableSymlinkFullPath));
REQUIRE_FALSE(std::filesystem::exists(portableInstaller2.InstallLocation));
}
TEST_CASE("PortableInstaller_InstallToIndex_CreateInstallRoot", "[PortableInstaller]")
{
TempDirectory installRootDirectory = TestCommon::TempDirectory("PortableInstallRoot", false);
std::vector<PortableFileEntry> desiredTestState;
TestCommon::TempFile testPortable("testPortable.txt");
std::ofstream file1(testPortable, std::ofstream::out);
file1.close();
TestCommon::TempFile testPortable2("testPortable2.txt");
std::ofstream file2(testPortable2, std::ofstream::out);
file2.close();
TestCommon::TempDirectory testDirectoryFolder("testDirectory", true);
std::filesystem::path installRootPath = installRootDirectory.GetPath();
std::filesystem::path targetPath = installRootPath / "testPortable.txt";
std::filesystem::path targetPath2 = installRootPath / "testPortable2.txt";
std::filesystem::path symlinkPath = installRootPath / "testSymlink.exe";
std::filesystem::path symlinkPath2 = installRootPath / "testSymlink2.exe";
std::filesystem::path directoryPath = installRootPath / "testDirectory";
desiredTestState.emplace_back(std::move(PortableFileEntry::CreateFileEntry(testPortable.GetPath(), targetPath, {})));
desiredTestState.emplace_back(std::move(PortableFileEntry::CreateFileEntry(testPortable2.GetPath(), targetPath2, {})));
desiredTestState.emplace_back(std::move(PortableFileEntry::CreateSymlinkEntry(symlinkPath, targetPath)));
desiredTestState.emplace_back(std::move(PortableFileEntry::CreateSymlinkEntry(symlinkPath2, targetPath2)));
desiredTestState.emplace_back(std::move(PortableFileEntry::CreateDirectoryEntry(testDirectoryFolder.GetPath(), directoryPath)));
PortableInstaller portableInstaller = PortableInstaller(ScopeEnum::User, Architecture::X64, "testProductCode");
portableInstaller.TargetInstallLocation = installRootDirectory.GetPath();
portableInstaller.RecordToIndex = true;
portableInstaller.SetDesiredState(desiredTestState);
REQUIRE(portableInstaller.VerifyExpectedState());
portableInstaller.Install(AppInstaller::CLI::Workflow::OperationType::Install);
REQUIRE(std::filesystem::exists(installRootPath / portableInstaller.GetPortableIndexFileName()));
REQUIRE(std::filesystem::exists(targetPath));
REQUIRE(std::filesystem::exists(targetPath2));
REQUIRE(AppInstaller::Filesystem::SymlinkExists(symlinkPath));
REQUIRE(AppInstaller::Filesystem::SymlinkExists(symlinkPath2));
REQUIRE(std::filesystem::exists(directoryPath));
PortableInstaller portableInstaller2 = PortableInstaller(ScopeEnum::User, Architecture::X64, "testProductCode");
REQUIRE(portableInstaller2.ARPEntryExists());
portableInstaller2.Uninstall();
// Install root directory should be removed since it was created.
REQUIRE_FALSE(std::filesystem::exists(installRootPath));
REQUIRE_FALSE(std::filesystem::exists(targetPath));
REQUIRE_FALSE(std::filesystem::exists(targetPath2));
REQUIRE_FALSE(AppInstaller::Filesystem::SymlinkExists(symlinkPath));
REQUIRE_FALSE(AppInstaller::Filesystem::SymlinkExists(symlinkPath2));
REQUIRE_FALSE(std::filesystem::exists(directoryPath));
}
TEST_CASE("PortableInstaller_InstallToIndex_ExistingInstallRoot", "[PortableInstaller]")
{
TempDirectory installRootDirectory = TestCommon::TempDirectory("PortableInstallRoot", true);
std::vector<PortableFileEntry> desiredTestState;
TestCommon::TempFile testPortable("testPortable.txt");
std::ofstream file1(testPortable, std::ofstream::out);
file1.close();
TestCommon::TempFile testPortable2("testPortable2.txt");
std::ofstream file2(testPortable2, std::ofstream::out);
file2.close();
TestCommon::TempDirectory testDirectoryFolder("testDirectory", true);
std::filesystem::path installRootPath = installRootDirectory.GetPath();
std::filesystem::path targetPath = installRootPath / "testPortable.txt";
std::filesystem::path targetPath2 = installRootPath / "testPortable2.txt";
std::filesystem::path symlinkPath = installRootPath / "testSymlink.exe";
std::filesystem::path symlinkPath2 = installRootPath / "testSymlink2.exe";
std::filesystem::path directoryPath = installRootPath / "testDirectory";
desiredTestState.emplace_back(std::move(PortableFileEntry::CreateFileEntry(testPortable.GetPath(), targetPath, {})));
desiredTestState.emplace_back(std::move(PortableFileEntry::CreateFileEntry(testPortable2.GetPath(), targetPath2, {})));
desiredTestState.emplace_back(std::move(PortableFileEntry::CreateSymlinkEntry(symlinkPath, targetPath)));
desiredTestState.emplace_back(std::move(PortableFileEntry::CreateSymlinkEntry(symlinkPath2, targetPath2)));
desiredTestState.emplace_back(std::move(PortableFileEntry::CreateDirectoryEntry(testDirectoryFolder.GetPath(), directoryPath)));
PortableInstaller portableInstaller = PortableInstaller(ScopeEnum::User, Architecture::X64, "testProductCode");
portableInstaller.TargetInstallLocation = installRootDirectory.GetPath();
portableInstaller.RecordToIndex = true;
portableInstaller.SetDesiredState(desiredTestState);
REQUIRE(portableInstaller.VerifyExpectedState());
portableInstaller.Install(AppInstaller::CLI::Workflow::OperationType::Install);
REQUIRE(std::filesystem::exists(installRootPath / portableInstaller.GetPortableIndexFileName()));
REQUIRE(std::filesystem::exists(targetPath));
REQUIRE(std::filesystem::exists(targetPath2));
REQUIRE(AppInstaller::Filesystem::SymlinkExists(symlinkPath));
REQUIRE(AppInstaller::Filesystem::SymlinkExists(symlinkPath2));
REQUIRE(std::filesystem::exists(directoryPath));
PortableInstaller portableInstaller2 = PortableInstaller(ScopeEnum::User, Architecture::X64, "testProductCode");
REQUIRE(portableInstaller2.ARPEntryExists());
portableInstaller2.Uninstall();
// Install root directory should still exist since it was created previously.
REQUIRE(std::filesystem::exists(installRootPath));
REQUIRE_FALSE(std::filesystem::exists(targetPath));
REQUIRE_FALSE(std::filesystem::exists(targetPath2));
REQUIRE_FALSE(AppInstaller::Filesystem::SymlinkExists(symlinkPath));
REQUIRE_FALSE(AppInstaller::Filesystem::SymlinkExists(symlinkPath2));
REQUIRE_FALSE(std::filesystem::exists(directoryPath));
}
TEST_CASE("PortableInstaller_UnicodeSymlinkPath", "[PortableInstaller]")
{
TempDirectory tempDirectory = TestCommon::TempDirectory("TempDirectory", false);
// Modify install location path to include unicode characters.
std::filesystem::path testInstallLocation = tempDirectory.GetPath() / std::filesystem::path{ ConvertToUTF16("романтический") };
std::vector<PortableFileEntry> desiredTestState;
TestCommon::TempFile testPortable("testPortable.txt");
std::ofstream file(testPortable, std::ofstream::out);
file.close();
std::filesystem::path targetPath = testInstallLocation / "testPortable.txt";
std::filesystem::path symlinkPath = tempDirectory.GetPath() / "testSymlink.exe";
desiredTestState.emplace_back(std::move(PortableFileEntry::CreateFileEntry(testPortable.GetPath(), targetPath, {})));
desiredTestState.emplace_back(std::move(PortableFileEntry::CreateSymlinkEntry(symlinkPath, targetPath)));
PortableInstaller portableInstaller = PortableInstaller(ScopeEnum::User, Architecture::X64, "testProductCode");
portableInstaller.TargetInstallLocation = testInstallLocation;
portableInstaller.SetDesiredState(desiredTestState);
REQUIRE(portableInstaller.VerifyExpectedState());
portableInstaller.Install(AppInstaller::CLI::Workflow::OperationType::Install);
PortableInstaller portableInstaller2 = PortableInstaller(ScopeEnum::User, Architecture::X64, "testProductCode");
REQUIRE(portableInstaller2.ARPEntryExists());
REQUIRE(std::filesystem::exists(portableInstaller2.PortableTargetFullPath));
REQUIRE(AppInstaller::Filesystem::SymlinkExists(portableInstaller2.PortableSymlinkFullPath));
portableInstaller2.Uninstall();
REQUIRE_FALSE(std::filesystem::exists(portableInstaller2.PortableTargetFullPath));
REQUIRE_FALSE(AppInstaller::Filesystem::SymlinkExists(portableInstaller2.PortableSymlinkFullPath));
REQUIRE_FALSE(std::filesystem::exists(portableInstaller2.InstallLocation));
}