forked from microsoft/winget-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestSource.h
More file actions
204 lines (164 loc) · 11.1 KB
/
Copy pathTestSource.h
File metadata and controls
204 lines (164 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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
#pragma once
#include <ISource.h>
#include <winget/Manifest.h>
#include <SourceFactory.h>
#include <functional>
#include <utility>
namespace TestCommon
{
// IPackageVersion for TestSource
struct TestPackageVersion : public AppInstaller::Repository::IPackageVersion
{
using Manifest = AppInstaller::Manifest::Manifest;
using ISource = AppInstaller::Repository::ISource;
using LocIndString = AppInstaller::Utility::LocIndString;
using MetadataMap = AppInstaller::Repository::IPackageVersion::Metadata;
TestPackageVersion(const Manifest& manifest, std::weak_ptr<const ISource> source = {}, bool hideSystemReferenceStrings = false);
TestPackageVersion(const Manifest& manifest, MetadataMap installationMetadata, std::weak_ptr<const ISource> source = {});
template <typename... Args>
static std::shared_ptr<TestPackageVersion> Make(Args&&... args)
{
return std::make_shared<TestPackageVersion>(std::forward<Args>(args)...);
}
LocIndString GetProperty(AppInstaller::Repository::PackageVersionProperty property) const override;
std::vector<LocIndString> GetMultiProperty(AppInstaller::Repository::PackageVersionMultiProperty property) const override;
Manifest GetManifest() override;
AppInstaller::Repository::Source GetSource() const override;
MetadataMap GetMetadata() const override;
Manifest VersionManifest;
MetadataMap Metadata;
std::weak_ptr<const ISource> Source;
bool HideSystemReferenceStrings = false;
protected:
static void AddIfHasValueAndNotPresent(const AppInstaller::Utility::NormalizedString& value, std::vector<LocIndString>& target, bool folded = false);
};
// IPackage for TestSource
struct TestPackage : public AppInstaller::Repository::IPackage
{
static constexpr AppInstaller::Repository::IPackageType PackageType = AppInstaller::Repository::IPackageType::TestPackage;
using Manifest = AppInstaller::Manifest::Manifest;
using ISource = AppInstaller::Repository::ISource;
using LocIndString = AppInstaller::Utility::LocIndString;
using MetadataMap = TestPackageVersion::MetadataMap;
// Create a package with only available versions using these manifests.
TestPackage(const std::vector<Manifest>& available, std::weak_ptr<const ISource> source = {}, bool hideSystemReferenceStringsOnVersion = false);
// Create a package with an installed version, metadata, and optionally available versions.
TestPackage(const Manifest& installed, MetadataMap installationMetadata, std::weak_ptr<const ISource> source = {});
template <typename... Args>
static std::shared_ptr<TestPackage> Make(Args&&... args)
{
return std::make_shared<TestPackage>(std::forward<Args>(args)...);
}
AppInstaller::Utility::LocIndString GetProperty(AppInstaller::Repository::PackageProperty property) const override;
std::vector<AppInstaller::Repository::PackageVersionKey> GetVersionKeys() const override;
std::shared_ptr<AppInstaller::Repository::IPackageVersion> GetLatestVersion() const override;
std::shared_ptr<AppInstaller::Repository::IPackageVersion> GetVersion(const AppInstaller::Repository::PackageVersionKey& versionKey) const override;
AppInstaller::Repository::Source GetSource() const override;
bool IsSame(const IPackage* other) const override;
const void* CastTo(AppInstaller::Repository::IPackageType type) const override;
std::vector<std::shared_ptr<AppInstaller::Repository::IPackageVersion>> Versions;
std::weak_ptr<const ISource> Source;
size_t DefaultIsSameIdentity = 0;
std::function<bool(const IPackage*, const IPackage*)> IsSameOverride;
};
// ICompositePackage for TestSource
struct TestCompositePackage : public AppInstaller::Repository::ICompositePackage
{
using Manifest = AppInstaller::Manifest::Manifest;
using ISource = AppInstaller::Repository::ISource;
using LocIndString = AppInstaller::Utility::LocIndString;
using MetadataMap = TestPackageVersion::MetadataMap;
// Create a package with only available versions using these manifests.
TestCompositePackage(const std::vector<Manifest>& available, std::weak_ptr<const ISource> source = {}, bool hideSystemReferenceStringsOnVersion = false);
// Create a package with an installed version, metadata, and optionally available versions.
TestCompositePackage(const Manifest& installed, MetadataMap installationMetadata, const std::vector<Manifest>& available = {}, std::weak_ptr<const ISource> source = {});
template <typename... Args>
static std::shared_ptr<TestCompositePackage> Make(Args&&... args)
{
return std::make_shared<TestCompositePackage>(std::forward<Args>(args)...);
}
AppInstaller::Utility::LocIndString GetProperty(AppInstaller::Repository::PackageProperty property) const override;
std::shared_ptr<AppInstaller::Repository::IPackage> GetInstalled() override;
std::vector<std::shared_ptr<AppInstaller::Repository::IPackage>> GetAvailable() override;
std::shared_ptr<TestPackage> Installed;
std::vector<std::shared_ptr<TestPackage>> Available;
};
// An ISource implementation for use across the test code.
struct TestSource : public AppInstaller::Repository::ISource, public std::enable_shared_from_this<TestSource>
{
static constexpr AppInstaller::Repository::ISourceType SourceType = AppInstaller::Repository::ISourceType::TestSource;
const AppInstaller::Repository::SourceDetails& GetDetails() const override;
const std::string& GetIdentifier() const override;
AppInstaller::Repository::SourceInformation GetInformation() const override;
bool QueryFeatureFlag(AppInstaller::Repository::SourceFeatureFlag flag) const override;
std::function<bool(AppInstaller::Repository::SourceFeatureFlag)> QueryFeatureFlagFunction;
AppInstaller::Repository::SearchResult Search(const AppInstaller::Repository::SearchRequest& request) const override;
void* CastTo(AppInstaller::Repository::ISourceType type) override;
AppInstaller::Repository::SourceDetails Details = { "TestSource", "Microsoft.TestSource", "//arg", "", "*TestSource" };
AppInstaller::Repository::SourceInformation Information;
std::function<AppInstaller::Repository::SearchResult(const AppInstaller::Repository::SearchRequest& request)> SearchFunction;
TestSource() = default;
TestSource(const AppInstaller::Repository::SourceDetails& details) : Details(details) {}
};
struct TestSourceReference : public AppInstaller::Repository::ISourceReference
{
using OpenFunctor = std::function<std::shared_ptr<AppInstaller::Repository::ISource>(const AppInstaller::Repository::SourceDetails&)>;
using OpenFunctorWithCustomHeader = std::function<std::shared_ptr<AppInstaller::Repository::ISource>(const AppInstaller::Repository::SourceDetails&, std::optional<std::string>)>;
TestSourceReference(const AppInstaller::Repository::SourceDetails& details, OpenFunctor open) : m_details(details), m_onOpen(open) {}
TestSourceReference(const AppInstaller::Repository::SourceDetails& details, OpenFunctorWithCustomHeader open) : m_details(details), m_onOpenWithCustomHeader(open) {}
std::string GetIdentifier() override { return m_details.Identifier; }
AppInstaller::Repository::SourceDetails& GetDetails() override { return m_details; };
bool SetCustomHeader(std::optional<std::string> header) override { m_header = header; return true; }
bool ShouldUpdateBeforeOpenResult = false;
bool ShouldUpdateBeforeOpen(const std::optional<AppInstaller::Repository::TimeSpan>&) override { return ShouldUpdateBeforeOpenResult; }
std::shared_ptr<AppInstaller::Repository::ISource> Open(AppInstaller::IProgressCallback&) override
{
if (m_onOpenWithCustomHeader)
{
return m_onOpenWithCustomHeader(m_details, m_header);
}
else
{
return m_onOpen(m_details);
}
}
private:
AppInstaller::Repository::SourceDetails m_details;
OpenFunctor m_onOpen;
OpenFunctorWithCustomHeader m_onOpenWithCustomHeader;
std::optional<std::string> m_header;
};
// An ISourceFactory implementation for use across the test code.
struct TestSourceFactory : public AppInstaller::Repository::ISourceFactory
{
using OpenFunctor = std::function<std::shared_ptr<AppInstaller::Repository::ISource>(const AppInstaller::Repository::SourceDetails&)>;
using OpenFunctorWithCustomHeader = std::function<std::shared_ptr<AppInstaller::Repository::ISource>(const AppInstaller::Repository::SourceDetails&, std::optional<std::string>)>;
using AddFunctor = std::function<void(AppInstaller::Repository::SourceDetails&)>;
using UpdateFunctor = std::function<void(const AppInstaller::Repository::SourceDetails&)>;
using RemoveFunctor = std::function<void(const AppInstaller::Repository::SourceDetails&)>;
TestSourceFactory(OpenFunctor open) : OnOpen(std::move(open)) {}
TestSourceFactory(OpenFunctorWithCustomHeader open) : OnOpenWithCustomHeader(std::move(open)) {}
// ISourceFactory
std::string_view TypeName() const override;
std::shared_ptr<AppInstaller::Repository::ISourceReference> Create(const AppInstaller::Repository::SourceDetails& details) override;
bool Add(AppInstaller::Repository::SourceDetails& details, AppInstaller::IProgressCallback&) override;
bool Update(const AppInstaller::Repository::SourceDetails& details, AppInstaller::IProgressCallback&) override;
bool Remove(const AppInstaller::Repository::SourceDetails& details, AppInstaller::IProgressCallback&) override;
// Make copies of self when requested.
operator std::function<std::unique_ptr<AppInstaller::Repository::ISourceFactory>()>();
bool ShouldUpdateBeforeOpenResult = false;
OpenFunctor OnOpen;
OpenFunctorWithCustomHeader OnOpenWithCustomHeader;
AddFunctor OnAdd;
UpdateFunctor OnUpdate;
RemoveFunctor OnRemove;
};
bool AddSource(const AppInstaller::Repository::SourceDetails& details, AppInstaller::IProgressCallback& progress);
bool UpdateSource(std::string_view name, AppInstaller::IProgressCallback& progress);
bool RemoveSource(std::string_view name, AppInstaller::IProgressCallback& progress);
AppInstaller::Repository::Source OpenSource(std::string_view name, AppInstaller::IProgressCallback& progress);
void DropSource(std::string_view name);
std::vector<AppInstaller::Repository::SourceDetails> GetSources();
}