winget-cli

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

TestSource.h (11885B)


      1 // Copyright (c) Microsoft Corporation.
      2 // Licensed under the MIT License.
      3 #pragma once
      4 #include <ISource.h>
      5 #include <winget/Manifest.h>
      6 #include <SourceFactory.h>
      7 
      8 #include <functional>
      9 #include <utility>
     10 
     11 namespace TestCommon
     12 {
     13     struct TestSource;
     14 
     15     // IPackageVersion for TestSource
     16     struct TestPackageVersion : public AppInstaller::Repository::IPackageVersion
     17     {
     18         using Manifest = AppInstaller::Manifest::Manifest;
     19         using ISource = AppInstaller::Repository::ISource;
     20         using LocIndString = AppInstaller::Utility::LocIndString;
     21         using MetadataMap = AppInstaller::Repository::IPackageVersion::Metadata;
     22 
     23         TestPackageVersion(const Manifest& manifest, std::weak_ptr<const ISource> source = {}, bool hideSystemReferenceStrings = false);
     24         TestPackageVersion(const Manifest& manifest, MetadataMap installationMetadata, std::weak_ptr<const ISource> source = {});
     25 
     26         template <typename... Args>
     27         static std::shared_ptr<TestPackageVersion> Make(Args&&... args)
     28         {
     29             return std::make_shared<TestPackageVersion>(std::forward<Args>(args)...);
     30         }
     31 
     32         LocIndString GetProperty(AppInstaller::Repository::PackageVersionProperty property) const override;
     33         std::vector<LocIndString> GetMultiProperty(AppInstaller::Repository::PackageVersionMultiProperty property) const override;
     34         Manifest GetManifest() override;
     35         AppInstaller::Repository::Source GetSource() const override;
     36         MetadataMap GetMetadata() const override;
     37 
     38         Manifest VersionManifest;
     39         MetadataMap Metadata;
     40         std::weak_ptr<const ISource> Source;
     41         bool HideSystemReferenceStrings = false;
     42 
     43     protected:
     44         static void AddIfHasValueAndNotPresent(const AppInstaller::Utility::NormalizedString& value, std::vector<LocIndString>& target, bool folded = false);
     45         TestSource* GetTestSource() const;
     46     };
     47 
     48     // IPackage for TestSource
     49     struct TestPackage : public AppInstaller::Repository::IPackage
     50     {
     51         static constexpr AppInstaller::Repository::IPackageType PackageType = AppInstaller::Repository::IPackageType::TestPackage;
     52 
     53         using Manifest = AppInstaller::Manifest::Manifest;
     54         using ISource = AppInstaller::Repository::ISource;
     55         using LocIndString = AppInstaller::Utility::LocIndString;
     56         using MetadataMap = TestPackageVersion::MetadataMap;
     57 
     58         // Create a package with only available versions using these manifests.
     59         TestPackage(const std::vector<Manifest>& available, std::weak_ptr<const ISource> source = {}, bool hideSystemReferenceStringsOnVersion = false);
     60 
     61         // Create a package with an installed version, metadata, and optionally available versions.
     62         TestPackage(const Manifest& installed, MetadataMap installationMetadata, std::weak_ptr<const ISource> source = {});
     63 
     64         template <typename... Args>
     65         static std::shared_ptr<TestPackage> Make(Args&&... args)
     66         {
     67             return std::make_shared<TestPackage>(std::forward<Args>(args)...);
     68         }
     69 
     70         AppInstaller::Utility::LocIndString GetProperty(AppInstaller::Repository::PackageProperty property) const override;
     71         std::vector<AppInstaller::Utility::LocIndString> GetMultiProperty(AppInstaller::Repository::PackageMultiProperty property) const override;
     72         std::vector<AppInstaller::Repository::PackageVersionKey> GetVersionKeys() const override;
     73         std::shared_ptr<AppInstaller::Repository::IPackageVersion> GetLatestVersion() const override;
     74         std::shared_ptr<AppInstaller::Repository::IPackageVersion> GetVersion(const AppInstaller::Repository::PackageVersionKey& versionKey) const override;
     75         AppInstaller::Repository::Source GetSource() const override;
     76         bool IsSame(const IPackage* other) const override;
     77         const void* CastTo(AppInstaller::Repository::IPackageType type) const override;
     78 
     79         std::vector<std::shared_ptr<AppInstaller::Repository::IPackageVersion>> Versions;
     80         std::weak_ptr<const ISource> Source;
     81         size_t DefaultIsSameIdentity = 0;
     82         std::function<bool(const IPackage*, const IPackage*)> IsSameOverride;
     83 
     84     protected:
     85         TestSource* GetTestSource() const;
     86     };
     87 
     88     // ICompositePackage for TestSource
     89     struct TestCompositePackage : public AppInstaller::Repository::ICompositePackage
     90     {
     91         using Manifest = AppInstaller::Manifest::Manifest;
     92         using ISource = AppInstaller::Repository::ISource;
     93         using LocIndString = AppInstaller::Utility::LocIndString;
     94         using MetadataMap = TestPackageVersion::MetadataMap;
     95 
     96         // Create a package with only available versions using these manifests.
     97         TestCompositePackage(const std::vector<Manifest>& available, std::weak_ptr<const ISource> source = {}, bool hideSystemReferenceStringsOnVersion = false);
     98 
     99         // Create a package with an installed version, metadata, and optionally available versions.
    100         TestCompositePackage(const Manifest& installed, MetadataMap installationMetadata, const std::vector<Manifest>& available = {}, std::weak_ptr<const ISource> source = {});
    101 
    102         template <typename... Args>
    103         static std::shared_ptr<TestCompositePackage> Make(Args&&... args)
    104         {
    105             return std::make_shared<TestCompositePackage>(std::forward<Args>(args)...);
    106         }
    107 
    108         AppInstaller::Utility::LocIndString GetProperty(AppInstaller::Repository::PackageProperty property) const override;
    109         std::shared_ptr<AppInstaller::Repository::IPackage> GetInstalled() override;
    110         std::vector<std::shared_ptr<AppInstaller::Repository::IPackage>> GetAvailable() override;
    111 
    112         std::shared_ptr<TestPackage> Installed;
    113         std::vector<std::shared_ptr<TestPackage>> Available;
    114     };
    115 
    116     // An ISource implementation for use across the test code.
    117     struct TestSource : public AppInstaller::Repository::ISource, public std::enable_shared_from_this<TestSource>
    118     {
    119         static constexpr AppInstaller::Repository::ISourceType SourceType = AppInstaller::Repository::ISourceType::TestSource;
    120 
    121         const AppInstaller::Repository::SourceDetails& GetDetails() const override;
    122         const std::string& GetIdentifier() const override;
    123         AppInstaller::Repository::SourceInformation GetInformation() const override;
    124 
    125         bool QueryFeatureFlag(AppInstaller::Repository::SourceFeatureFlag flag) const override;
    126         std::function<bool(AppInstaller::Repository::SourceFeatureFlag)> QueryFeatureFlagFunction;
    127 
    128         AppInstaller::Repository::SearchResult Search(const AppInstaller::Repository::SearchRequest& request) const override;
    129         void* CastTo(AppInstaller::Repository::ISourceType type) override;
    130 
    131         AppInstaller::Repository::SourceDetails Details = { "TestSource", "Microsoft.TestSource", "//arg", "", "*TestSource" };
    132         AppInstaller::Repository::SourceInformation Information;
    133         std::function<AppInstaller::Repository::SearchResult(const AppInstaller::Repository::SearchRequest& request)> SearchFunction;
    134 
    135         TestSource() = default;
    136         TestSource(const AppInstaller::Repository::SourceDetails& details) : Details(details) {}
    137 
    138         // Tracking for potential network impacts
    139         void IncrementCountOfCallsRequiringVersionData();
    140         size_t CountOfCallsRequiringVersionData = 0;
    141 
    142         void IncrementCountOfCallsRequiringManifestData();
    143         size_t CountOfCallsRequiringManifestData = 0;
    144     };
    145 
    146     struct TestSourceReference : public AppInstaller::Repository::ISourceReference
    147     {
    148         using OpenFunctor = std::function<std::shared_ptr<AppInstaller::Repository::ISource>(const AppInstaller::Repository::SourceDetails&)>;
    149         using OpenFunctorWithCustomHeader = std::function<std::shared_ptr<AppInstaller::Repository::ISource>(const AppInstaller::Repository::SourceDetails&, std::optional<std::string>)>;
    150 
    151         TestSourceReference(const AppInstaller::Repository::SourceDetails& details, OpenFunctor open) : m_details(details), m_onOpen(open) {}
    152         TestSourceReference(const AppInstaller::Repository::SourceDetails& details, OpenFunctorWithCustomHeader open) : m_details(details), m_onOpenWithCustomHeader(open) {}
    153 
    154         std::string GetIdentifier() override { return m_details.Identifier; }
    155 
    156         AppInstaller::Repository::SourceDetails& GetDetails() override { return m_details; };
    157 
    158         bool SetCustomHeader(std::optional<std::string> header) override { m_header = header; return true; }
    159 
    160         bool ShouldUpdateBeforeOpenResult = false;
    161         bool ShouldUpdateBeforeOpen(const std::optional<AppInstaller::Repository::TimeSpan>&) override { return ShouldUpdateBeforeOpenResult; }
    162 
    163         std::shared_ptr<AppInstaller::Repository::ISource> Open(AppInstaller::IProgressCallback&) override
    164         {
    165             if (m_onOpenWithCustomHeader)
    166             {
    167                 return m_onOpenWithCustomHeader(m_details, m_header);
    168             }
    169             else
    170             {
    171                 return m_onOpen(m_details);
    172             }
    173         }
    174 
    175     private:
    176         AppInstaller::Repository::SourceDetails m_details;
    177         OpenFunctor m_onOpen;
    178         OpenFunctorWithCustomHeader m_onOpenWithCustomHeader;
    179         std::optional<std::string> m_header;
    180     };
    181 
    182     // An ISourceFactory implementation for use across the test code.
    183     struct TestSourceFactory : public AppInstaller::Repository::ISourceFactory
    184     {
    185         using OpenFunctor = std::function<std::shared_ptr<AppInstaller::Repository::ISource>(const AppInstaller::Repository::SourceDetails&)>;
    186         using OpenFunctorWithCustomHeader = std::function<std::shared_ptr<AppInstaller::Repository::ISource>(const AppInstaller::Repository::SourceDetails&, std::optional<std::string>)>;
    187         using AddFunctor = std::function<void(AppInstaller::Repository::SourceDetails&)>;
    188         using UpdateFunctor = std::function<void(const AppInstaller::Repository::SourceDetails&)>;
    189         using RemoveFunctor = std::function<void(const AppInstaller::Repository::SourceDetails&)>;
    190 
    191         TestSourceFactory(OpenFunctor open) : OnOpen(std::move(open)) {}
    192         TestSourceFactory(OpenFunctorWithCustomHeader open) : OnOpenWithCustomHeader(std::move(open)) {}
    193 
    194         // ISourceFactory
    195         std::string_view TypeName() const override;
    196         std::shared_ptr<AppInstaller::Repository::ISourceReference> Create(const AppInstaller::Repository::SourceDetails& details) override;
    197         bool Add(AppInstaller::Repository::SourceDetails& details, AppInstaller::IProgressCallback&) override;
    198         bool Update(const AppInstaller::Repository::SourceDetails& details, AppInstaller::IProgressCallback&) override;
    199         bool Remove(const AppInstaller::Repository::SourceDetails& details, AppInstaller::IProgressCallback&) override;
    200 
    201         // Make copies of self when requested.
    202         operator std::function<std::unique_ptr<AppInstaller::Repository::ISourceFactory>()>();
    203 
    204         bool ShouldUpdateBeforeOpenResult = false;
    205         OpenFunctor OnOpen;
    206         OpenFunctorWithCustomHeader OnOpenWithCustomHeader;
    207         AddFunctor OnAdd;
    208         UpdateFunctor OnUpdate;
    209         RemoveFunctor OnRemove;
    210     };
    211 
    212     bool AddSource(const AppInstaller::Repository::SourceDetails& details, AppInstaller::IProgressCallback& progress);
    213     bool UpdateSource(std::string_view name, AppInstaller::IProgressCallback& progress);
    214     bool RemoveSource(std::string_view name, AppInstaller::IProgressCallback& progress);
    215     AppInstaller::Repository::Source OpenSource(std::string_view name, AppInstaller::IProgressCallback& progress);
    216     void DropSource(std::string_view name);
    217     std::vector<AppInstaller::Repository::SourceDetails> GetSources();
    218 }