forked from microsoft/winget-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPinFlow.cpp
More file actions
201 lines (167 loc) · 7.89 KB
/
Copy pathPinFlow.cpp
File metadata and controls
201 lines (167 loc) · 7.89 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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
#include "pch.h"
#include "WorkflowCommon.h"
#include "TestHooks.h"
#include <Commands/PinCommand.h>
#include <Workflows/PinFlow.h>
#include <Microsoft/PinningIndex.h>
#include <AppInstallerRuntime.h>
#include <AppInstallerVersions.h>
#include <winget/PinningData.h>
using namespace TestCommon;
using namespace AppInstaller::CLI;
using namespace AppInstaller::CLI::Workflow;
using namespace AppInstaller::Repository::Microsoft;
using namespace AppInstaller::Utility;
using namespace AppInstaller::Pinning;
using namespace AppInstaller::SQLite;
TEST_CASE("PinFlow_Add", "[PinFlow][workflow]")
{
TempFile indexFile("pinningIndex", ".db");
TestHook::SetPinningIndex_Override pinningIndexOverride(indexFile.GetPath());
std::ostringstream pinAddOutput;
TestContext addContext{ pinAddOutput, std::cin };
OverrideForCompositeInstalledSource(addContext, CreateTestSource({ TSR::TestInstaller_Exe }));
addContext.Args.AddArg(Execution::Args::Type::Query, TSR::TestInstaller_Exe.Query);
addContext.Args.AddArg(Execution::Args::Type::BlockingPin);
PinAddCommand pinAdd({});
pinAdd.Execute(addContext);
INFO(pinAddOutput.str());
SECTION("Pin is saved")
{
auto index = PinningIndex::Open(indexFile.GetPath().u8string(), SQLiteStorageBase::OpenDisposition::Read);
auto pins = index.GetAllPins();
REQUIRE(pins.size() == 1);
REQUIRE(pins[0].GetType() == PinType::Blocking);
REQUIRE(pins[0].GetGatedVersion().ToString() == "");
REQUIRE(pins[0].GetKey().PackageId == "AppInstallerCliTest.TestExeInstaller");
REQUIRE(pins[0].GetKey().SourceId == "*TestSource");
std::ostringstream pinListOutput;
TestContext listContext{ pinListOutput, std::cin };
OverrideForCompositeInstalledSource(listContext, CreateTestSource({ TSR::TestInstaller_Exe }));
listContext.Args.AddArg(Execution::Args::Type::Query, TSR::TestInstaller_Exe.Query);
PinListCommand pinList({});
pinList.Execute(listContext);
INFO(pinListOutput.str());
REQUIRE(pinListOutput.str().find("AppInstallerCliTest.TestExeInstaller"));
REQUIRE(pinListOutput.str().find("Blocking"));
}
SECTION("Remove pin")
{
std::ostringstream pinRemoveOutput;
TestContext removeContext{ pinRemoveOutput, std::cin };
OverrideForCompositeInstalledSource(removeContext, CreateTestSource({ TSR::TestInstaller_Exe }));
removeContext.Args.AddArg(Execution::Args::Type::Query, TSR::TestInstaller_Exe.Query);
PinRemoveCommand pinRemove({});
pinRemove.Execute(removeContext);
INFO(pinRemoveOutput.str());
auto index = PinningIndex::Open(indexFile.GetPath().u8string(), SQLiteStorageBase::OpenDisposition::Read);
auto pins = index.GetAllPins();
REQUIRE(pins.empty());
}
SECTION("Reset pins")
{
std::ostringstream pinResetOutput;
TestContext resetContext{ pinResetOutput, std::cin };
SECTION("Without --force")
{
OverrideForCompositeInstalledSource(resetContext, CreateTestSource({ TSR::TestInstaller_Exe }));
PinResetCommand pinReset({});
pinReset.Execute(resetContext);
INFO(pinResetOutput.str());
auto index = PinningIndex::Open(indexFile.GetPath().u8string(), SQLiteStorageBase::OpenDisposition::Read);
auto pins = index.GetAllPins();
REQUIRE(pins.size() == 1);
}
SECTION("With --force")
{
resetContext.Args.AddArg(Execution::Args::Type::Force);
PinResetCommand pinReset({});
pinReset.Execute(resetContext);
INFO(pinResetOutput.str());
auto index = PinningIndex::Open(indexFile.GetPath().u8string(), SQLiteStorageBase::OpenDisposition::Read);
auto pins = index.GetAllPins();
REQUIRE(pins.empty());
}
}
SECTION("Update pin")
{
std::ostringstream pinUpdateOutput;
TestContext updateContext{ pinUpdateOutput, std::cin };
OverrideForCompositeInstalledSource(updateContext, CreateTestSource({ TSR::TestInstaller_Exe }));
updateContext.Args.AddArg(Execution::Args::Type::Query, TSR::TestInstaller_Exe.Query);
SECTION("Without --force")
{
PinAddCommand pinUpdate({});
pinUpdate.Execute(updateContext);
INFO(pinUpdateOutput.str());
REQUIRE_TERMINATED_WITH(updateContext, APPINSTALLER_CLI_ERROR_PIN_ALREADY_EXISTS);
auto index = PinningIndex::Open(indexFile.GetPath().u8string(), SQLiteStorageBase::OpenDisposition::Read);
auto pins = index.GetAllPins();
REQUIRE(pins.size() == 1);
REQUIRE(pins[0].GetType() == PinType::Blocking);
}
SECTION("With --force")
{
updateContext.Args.AddArg(Execution::Args::Type::Force);
PinAddCommand pinUpdate({});
pinUpdate.Execute(updateContext);
INFO(pinUpdateOutput.str());
auto index = PinningIndex::Open(indexFile.GetPath().u8string(), SQLiteStorageBase::OpenDisposition::Read);
auto pins = index.GetAllPins();
REQUIRE(pins.size() == 1);
REQUIRE(pins[0].GetType() == PinType::Pinning);
}
}
}
TEST_CASE("PinFlow_Add_NotFound", "[PinFlow][workflow]")
{
TempFile indexFile("pinningIndex", ".db");
TestHook::SetPinningIndex_Override pinningIndexOverride(indexFile.GetPath());
std::ostringstream pinAddOutput;
TestContext addContext{ pinAddOutput, std::cin };
OverrideForCompositeInstalledSource(addContext, CreateTestSource({}));
addContext.Args.AddArg(Execution::Args::Type::Query, "This package doesn't exist"sv);
PinAddCommand pinAdd({});
pinAdd.Execute(addContext);
INFO(pinAddOutput.str());
REQUIRE_TERMINATED_WITH(addContext, APPINSTALLER_CLI_ERROR_NO_APPLICATIONS_FOUND);
}
TEST_CASE("PinFlow_ListEmpty", "[PinFlow][workflow]")
{
TempFile indexFile("pinningIndex", ".db");
TestHook::SetPinningIndex_Override pinningIndexOverride(indexFile.GetPath());
std::ostringstream pinListOutput;
TestContext listContext{ pinListOutput, std::cin };
OverrideForCompositeInstalledSource(listContext, CreateTestSource({}));
PinListCommand pinList({});
pinList.Execute(listContext);
INFO(pinListOutput.str());
REQUIRE(pinListOutput.str().find(Resource::LocString(Resource::String::PinNoPinsExist)) != std::string::npos);
}
TEST_CASE("PinFlow_RemoveNonExisting", "[PinFlow][workflow]")
{
TempFile indexFile("pinningIndex", ".db");
TestHook::SetPinningIndex_Override pinningIndexOverride(indexFile.GetPath());
std::ostringstream pinRemoveOutput;
TestContext removeContext{ pinRemoveOutput, std::cin };
OverrideForCompositeInstalledSource(removeContext, CreateTestSource({ TSR::TestInstaller_Exe }));
removeContext.Args.AddArg(Execution::Args::Type::Query, TSR::TestInstaller_Exe.Query);
PinRemoveCommand pinRemove({});
pinRemove.Execute(removeContext);
INFO(pinRemoveOutput.str());
REQUIRE_TERMINATED_WITH(removeContext, APPINSTALLER_CLI_ERROR_PIN_DOES_NOT_EXIST);
}
TEST_CASE("PinFlow_ResetEmpty", "[PinFlow][workflow]")
{
TempFile indexFile("pinningIndex", ".db");
TestHook::SetPinningIndex_Override pinningIndexOverride(indexFile.GetPath());
std::ostringstream pinResetOutput;
TestContext resetContext{ pinResetOutput, std::cin };
resetContext.Args.AddArg(Execution::Args::Type::Force);
PinResetCommand pinReset({});
pinReset.Execute(resetContext);
INFO(pinResetOutput.str());
REQUIRE(pinResetOutput.str().find(Resource::LocString(Resource::String::PinNoPinsExist)) != std::string::npos);
}