forked from microsoft/winget-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestCommon.cpp
More file actions
217 lines (183 loc) · 6.4 KB
/
Copy pathTestCommon.cpp
File metadata and controls
217 lines (183 loc) · 6.4 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
216
217
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
#include "pch.h"
#include "TestCommon.h"
#include "TestHooks.h"
#include "winget/GroupPolicy.h"
#include "winget/UserSettings.h"
namespace TestCommon
{
namespace
{
int initRand()
{
srand(static_cast<unsigned int>(time(NULL)));
return rand();
};
inline int getRand()
{
static int randStart = initRand();
return randStart++;
}
inline std::filesystem::path GetTempFilePath(const std::string& baseName, const std::string& baseExt)
{
std::filesystem::path tempFilePath = std::filesystem::temp_directory_path();
tempFilePath /= baseName + std::to_string(getRand()) + baseExt;
return tempFilePath;
}
static TempFileDestructionBehavior s_TempFileDestructorBehavior = TempFileDestructionBehavior::Delete;
static std::vector<std::filesystem::path> s_TempFilesOnFile;
static std::filesystem::path s_TestDataFileBasePath{};
bool CleanVolatileTestRoot(HKEY root)
{
THROW_IF_WIN32_ERROR(RegDeleteTreeW(root, nullptr));
return true;
}
}
TempFile::TempFile(const std::string& baseName, const std::string& baseExt, bool deleteFileOnConstruction)
{
_filepath = GetTempFilePath(baseName, baseExt);
if (deleteFileOnConstruction)
{
std::filesystem::remove(_filepath);
}
}
TempFile::TempFile(const std::filesystem::path& filePath, bool deleteFileOnConstruction)
{
if (filePath.is_relative())
{
_filepath = std::filesystem::temp_directory_path();
_filepath /= filePath;
}
else
{
_filepath = filePath;
}
if (deleteFileOnConstruction)
{
std::filesystem::remove(_filepath);
}
}
TempFile::~TempFile()
{
switch (s_TempFileDestructorBehavior)
{
case TempFileDestructionBehavior::Delete:
std::filesystem::remove_all(_filepath);
break;
case TempFileDestructionBehavior::Keep:
break;
case TempFileDestructionBehavior::ShellExecuteOnFailure:
s_TempFilesOnFile.emplace_back(std::move(_filepath));
break;
}
}
void TempFile::SetDestructorBehavior(TempFileDestructionBehavior behavior)
{
s_TempFileDestructorBehavior = behavior;
}
void TempFile::SetTestFailed(bool failed)
{
if (failed)
{
for (const auto& path : s_TempFilesOnFile)
{
SHELLEXECUTEINFOW seinfo{};
seinfo.cbSize = sizeof(seinfo);
seinfo.lpVerb = L"open";
seinfo.lpFile = path.c_str();
ShellExecuteExW(&seinfo);
}
}
else
{
s_TempFilesOnFile.clear();
}
}
TempDirectory::TempDirectory(const std::string& baseName, bool create)
{
_filepath = GetTempFilePath(baseName, "");
if (create)
{
if (std::filesystem::exists(_filepath))
{
std::filesystem::remove_all(_filepath);
}
std::filesystem::create_directories(_filepath);
}
}
std::filesystem::path TestDataFile::GetPath() const
{
std::filesystem::path result = s_TestDataFileBasePath;
result /= m_path;
return result;
}
void TestDataFile::SetTestDataBasePath(const std::filesystem::path& path)
{
s_TestDataFileBasePath = path;
}
void TestProgress::OnProgress(uint64_t current, uint64_t maximum, AppInstaller::ProgressType type)
{
if (m_OnProgress)
{
m_OnProgress(current, maximum, type);
}
}
void TestProgress::BeginProgress()
{
}
void TestProgress::EndProgress(bool)
{
}
bool TestProgress::IsCancelled()
{
return false;
}
AppInstaller::IProgressCallback::CancelFunctionRemoval TestProgress::SetCancellationFunction(std::function<void()>&&)
{
return {};
}
wil::unique_hkey RegCreateVolatileTestRoot()
{
// First create/open the real test root
wil::unique_hkey root;
THROW_IF_WIN32_ERROR(RegCreateKeyExW(HKEY_CURRENT_USER, L"Software\\Microsoft\\WinGet\\TestRoot", 0, nullptr, REG_OPTION_VOLATILE, KEY_ALL_ACCESS, nullptr, &root, nullptr));
static bool s_ignored = CleanVolatileTestRoot(root.get());
// Create a random name
GUID name{};
(void)CoCreateGuid(&name);
wchar_t nameBuffer[256];
(void)StringFromGUID2(name, nameBuffer, ARRAYSIZE(nameBuffer));
return RegCreateVolatileSubKey(root.get(), nameBuffer);
}
wil::unique_hkey RegCreateVolatileSubKey(HKEY parent, const std::wstring& name)
{
wil::unique_hkey result;
THROW_IF_WIN32_ERROR(RegCreateKeyExW(parent, name.c_str(), 0, nullptr, REG_OPTION_VOLATILE, KEY_ALL_ACCESS, nullptr, &result, nullptr));
return result;
}
void SetRegistryValue(HKEY key, const std::wstring& name, const std::wstring& value, DWORD type)
{
THROW_IF_WIN32_ERROR(RegSetValueExW(key, name.c_str(), 0, type, reinterpret_cast<const BYTE*>(value.c_str()), static_cast<DWORD>(sizeof(wchar_t) * (value.size() + 1))));
}
void SetRegistryValue(HKEY key, const std::wstring& name, const std::vector<BYTE>& value, DWORD type)
{
THROW_IF_WIN32_ERROR(RegSetValueExW(key, name.c_str(), 0, type, reinterpret_cast<const BYTE*>(value.data()), static_cast<DWORD>(value.size())));
}
void SetRegistryValue(HKEY key, const std::wstring& name, DWORD value)
{
THROW_IF_WIN32_ERROR(RegSetValueExW(key, name.c_str(), 0, REG_DWORD, reinterpret_cast<const BYTE*>(&value), sizeof(DWORD)));
}
TestUserSettings::TestUserSettings(bool keepFileSettings)
{
if (!keepFileSettings)
{
m_settings.clear();
}
AppInstaller::Settings::SetUserSettingsOverride(this);
}
TestUserSettings::~TestUserSettings()
{
AppInstaller::Settings::SetUserSettingsOverride(nullptr);
}
}