winget-cli

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

SQLiteIndexSource.cpp (11162B)


      1 // Copyright (c) Microsoft Corporation.
      2 // Licensed under the MIT License.
      3 #include "pch.h"
      4 #include "TestCommon.h"
      5 #include <Microsoft/SQLiteIndexSource.h>
      6 #include <winget/ManifestYamlParser.h>
      7 
      8 using namespace std::string_literals;
      9 using namespace TestCommon;
     10 using namespace AppInstaller::Manifest;
     11 using namespace AppInstaller::Repository;
     12 using namespace AppInstaller::Repository::Microsoft;
     13 using namespace AppInstaller::SQLite;
     14 
     15 using SQLiteVersion = AppInstaller::SQLite::Version;
     16 
     17 static std::shared_ptr<SQLiteIndexSource> SimpleTestSetup(const std::string& filePath, SourceDetails& details, Manifest& manifest, std::string& relativePath, const std::filesystem::path& manifestFile = "Manifest-Good.yaml")
     18 {
     19     SQLiteVersion latest1 = Version::LatestForMajor(1);
     20     SQLiteVersion latest2 = Version::LatestForMajor(2);
     21 
     22     const SQLiteVersion versionToUse = GENERATE_COPY(SQLiteVersion{ latest1 }, SQLiteVersion{ latest2 });
     23 
     24     SQLiteIndex index = SQLiteIndex::CreateNew(filePath, versionToUse);
     25 
     26     TestDataFile testManifest(manifestFile);
     27     manifest = YamlParser::CreateFromPath(testManifest);
     28 
     29     std::filesystem::path testManifestPath = testManifest.GetPath();
     30     relativePath = testManifestPath.filename().u8string();
     31 
     32     TempDirectory sourceFilesDirectory{ "SQLiteIndexSource" };
     33     std::filesystem::path sourceFilesDirectoryPath = sourceFilesDirectory.GetPath();
     34     std::filesystem::create_directories(sourceFilesDirectoryPath);
     35     std::filesystem::copy_file(testManifestPath, sourceFilesDirectoryPath / relativePath);
     36     sourceFilesDirectory.Release();
     37 
     38     index.AddManifest(manifest, relativePath);
     39 
     40     details.Name = "TestName";
     41     details.Type = "TestType";
     42     details.Arg = sourceFilesDirectoryPath.u8string();
     43     details.Data = "";
     44     details.Identifier = "SimpleTestSetup";
     45 
     46     if (versionToUse.MajorVersion == 2)
     47     {
     48         index.SetProperty(SQLiteIndex::Property::IntermediateFileOutputPath, sourceFilesDirectoryPath.u8string());
     49     }
     50 
     51     index.PrepareForPackaging();
     52 
     53     return std::make_shared<SQLiteIndexSource>(details, std::move(index));
     54 }
     55 
     56 static bool SupportsChannel(const std::shared_ptr<SQLiteIndexSource>& source)
     57 {
     58     return source->GetIndex().GetVersion().MajorVersion == 1;
     59 }
     60 
     61 TEST_CASE("SQLiteIndexSource_Search_IdExactMatch", "[sqliteindexsource]")
     62 {
     63     TempFile tempFile{ "repolibtest_tempdb"s, ".db"s };
     64     INFO("Using temporary file named: " << tempFile.GetPath());
     65 
     66     SourceDetails details;
     67     Manifest manifest;
     68     std::string relativePath;
     69     std::shared_ptr<SQLiteIndexSource> source = SimpleTestSetup(tempFile, details, manifest, relativePath);
     70 
     71     SearchRequest request;
     72     request.Query = RequestMatch(MatchType::Exact, manifest.Id);
     73 
     74     auto results = source->Search(request);
     75     REQUIRE(results.Matches.size() == 1);
     76     REQUIRE(results.Matches[0].Package);
     77     REQUIRE(results.Matches[0].MatchCriteria.Field == PackageMatchField::Id);
     78     REQUIRE(results.Matches[0].MatchCriteria.Type == MatchType::Exact);
     79     REQUIRE(results.Matches[0].MatchCriteria.Value == manifest.Id);
     80 }
     81 
     82 TEST_CASE("SQLiteIndexSource_Search_NoMatch", "[sqliteindexsource]")
     83 {
     84     TempFile tempFile{ "repolibtest_tempdb"s, ".db"s };
     85     INFO("Using temporary file named: " << tempFile.GetPath());
     86 
     87     SourceDetails details;
     88     Manifest manifest;
     89     std::string relativePath;
     90     std::shared_ptr<SQLiteIndexSource> source = SimpleTestSetup(tempFile, details, manifest, relativePath);
     91 
     92     SearchRequest request;
     93     request.Query = RequestMatch(MatchType::Exact, "THIS DOES NOT MATCH ANYTHING!");
     94 
     95     auto results = source->Search(request);
     96     REQUIRE(results.Matches.size() == 0);
     97 }
     98 
     99 TEST_CASE("SQLiteIndexSource_Id", "[sqliteindexsource]")
    100 {
    101     TempFile tempFile{ "repolibtest_tempdb"s, ".db"s };
    102     INFO("Using temporary file named: " << tempFile.GetPath());
    103 
    104     SourceDetails details;
    105     Manifest manifest;
    106     std::string relativePath;
    107     std::shared_ptr<SQLiteIndexSource> source = SimpleTestSetup(tempFile, details, manifest, relativePath);
    108 
    109     SearchRequest request;
    110     request.Query = RequestMatch(MatchType::Exact, manifest.Id);
    111 
    112     auto results = source->Search(request);
    113     REQUIRE(results.Matches.size() == 1);
    114     REQUIRE(results.Matches[0].Package);
    115     auto latestVersion = results.Matches[0].Package->GetAvailable()[0]->GetLatestVersion();
    116 
    117     REQUIRE(latestVersion->GetProperty(PackageVersionProperty::Id).get() == manifest.Id);
    118 }
    119 
    120 TEST_CASE("SQLiteIndexSource_Name", "[sqliteindexsource]")
    121 {
    122     TempFile tempFile{ "repolibtest_tempdb"s, ".db"s };
    123     INFO("Using temporary file named: " << tempFile.GetPath());
    124 
    125     SourceDetails details;
    126     Manifest manifest;
    127     std::string relativePath;
    128     std::shared_ptr<SQLiteIndexSource> source = SimpleTestSetup(tempFile, details, manifest, relativePath);
    129 
    130     SearchRequest request;
    131     request.Query = RequestMatch(MatchType::Exact, manifest.Id);
    132 
    133     auto results = source->Search(request);
    134     REQUIRE(results.Matches.size() == 1);
    135     REQUIRE(results.Matches[0].Package);
    136     auto latestVersion = results.Matches[0].Package->GetAvailable()[0]->GetLatestVersion();
    137 
    138     REQUIRE(latestVersion->GetProperty(PackageVersionProperty::Name).get() == manifest.DefaultLocalization.Get<Localization::PackageName>());
    139 }
    140 
    141 TEST_CASE("SQLiteIndexSource_Versions", "[sqliteindexsource]")
    142 {
    143     TempFile tempFile{ "repolibtest_tempdb"s, ".db"s };
    144     INFO("Using temporary file named: " << tempFile.GetPath());
    145 
    146     SourceDetails details;
    147     Manifest manifest;
    148     std::string relativePath;
    149     std::shared_ptr<SQLiteIndexSource> source = SimpleTestSetup(tempFile, details, manifest, relativePath);
    150 
    151     SearchRequest request;
    152     request.Query = RequestMatch(MatchType::Exact, manifest.Id);
    153 
    154     auto results = source->Search(request);
    155     REQUIRE(results.Matches.size() == 1);
    156     REQUIRE(results.Matches[0].Package);
    157     REQUIRE(results.Matches[0].Package->GetAvailable().size() == 1);
    158 
    159     auto result = results.Matches[0].Package->GetAvailable()[0]->GetVersionKeys();
    160     REQUIRE(result.size() == 1);
    161     REQUIRE(result[0].Version == manifest.Version);
    162     if (SupportsChannel(source))
    163     {
    164         REQUIRE(result[0].Channel == manifest.Channel);
    165     }
    166 }
    167 
    168 TEST_CASE("SQLiteIndexSource_GetManifest", "[sqliteindexsource]")
    169 {
    170     TempFile tempFile{ "repolibtest_tempdb"s, ".db"s };
    171     INFO("Using temporary file named: " << tempFile.GetPath());
    172 
    173     SourceDetails details;
    174     Manifest manifest;
    175     std::string relativePath;
    176     std::shared_ptr<SQLiteIndexSource> source = SimpleTestSetup(tempFile, details, manifest, relativePath);
    177 
    178     SearchRequest request;
    179     request.Query = RequestMatch(MatchType::Exact, manifest.Id);
    180 
    181     auto results = source->Search(request);
    182     REQUIRE(results.Matches.size() == 1);
    183     REQUIRE(results.Matches[0].Package);
    184     REQUIRE(results.Matches[0].Package->GetAvailable().size() == 1);
    185     auto package = results.Matches[0].Package->GetAvailable()[0];
    186 
    187     auto specificResultVersion = package->GetVersion(PackageVersionKey("", manifest.Version, SupportsChannel(source) ? manifest.Channel : ""));
    188     REQUIRE(specificResultVersion);
    189     auto specificResult = specificResultVersion->GetManifest();
    190     REQUIRE(specificResult.Id == manifest.Id);
    191     REQUIRE(specificResult.DefaultLocalization.Get<Localization::PackageName>() == manifest.DefaultLocalization.Get<Localization::PackageName>());
    192     REQUIRE(specificResult.Version == manifest.Version);
    193     if (SupportsChannel(source))
    194     {
    195         REQUIRE(specificResult.Channel == manifest.Channel);
    196     }
    197 
    198     if (SupportsChannel(source))
    199     {
    200         auto latestResultVersion = package->GetVersion(PackageVersionKey("", "", manifest.Channel));
    201         REQUIRE(latestResultVersion);
    202         auto latestResult = latestResultVersion->GetManifest();
    203         REQUIRE(latestResult.Id == manifest.Id);
    204         REQUIRE(latestResult.DefaultLocalization.Get<Localization::PackageName>() == manifest.DefaultLocalization.Get<Localization::PackageName>());
    205         REQUIRE(latestResult.Version == manifest.Version);
    206         REQUIRE(latestResult.Channel == manifest.Channel);
    207     }
    208 
    209     auto noResultVersion = package->GetVersion(PackageVersionKey("", "blargle", "flargle"));
    210     REQUIRE(!noResultVersion);
    211 }
    212 
    213 TEST_CASE("SQLiteIndexSource_IsSame", "[sqliteindexsource]")
    214 {
    215     TempFile tempFile{ "repolibtest_tempdb"s, ".db"s };
    216     INFO("Using temporary file named: " << tempFile.GetPath());
    217 
    218     SourceDetails details;
    219     Manifest manifest;
    220     std::string relativePath;
    221     std::shared_ptr<SQLiteIndexSource> source = SimpleTestSetup(tempFile, details, manifest, relativePath);
    222 
    223     SearchRequest request;
    224     request.Query = RequestMatch(MatchType::Exact, manifest.Id);
    225 
    226     auto result1 = source->Search(request);
    227     REQUIRE(result1.Matches.size() == 1);
    228     REQUIRE(result1.Matches[0].Package->GetAvailable().size() == 1);
    229 
    230     auto result2 = source->Search(request);
    231     REQUIRE(result2.Matches.size() == 1);
    232     REQUIRE(result2.Matches[0].Package->GetAvailable().size() == 1);
    233 
    234     REQUIRE(result1.Matches[0].Package->GetAvailable()[0]->IsSame(result2.Matches[0].Package->GetAvailable()[0].get()));
    235 }
    236 
    237 TEST_CASE("SQLiteIndexSource_Package_ProductCodes", "[sqliteindexsource]")
    238 {
    239     TempFile tempFile{ "repolibtest_tempdb"s, ".db"s };
    240     INFO("Using temporary file named: " << tempFile.GetPath());
    241 
    242     SourceDetails details;
    243     Manifest manifest;
    244     std::string relativePath;
    245     std::shared_ptr<SQLiteIndexSource> source = SimpleTestSetup(tempFile, details, manifest, relativePath);
    246 
    247     SearchRequest request;
    248     request.Query = RequestMatch(MatchType::Exact, manifest.Id);
    249 
    250     auto results = source->Search(request);
    251     REQUIRE(results.Matches.size() == 1);
    252     REQUIRE(results.Matches[0].Package);
    253 
    254     auto package = results.Matches[0].Package->GetAvailable()[0];
    255 
    256     auto manifestPCs = manifest.GetProductCodes();
    257     auto propertyPCs = package->GetMultiProperty(PackageMultiProperty::ProductCode);
    258     REQUIRE(manifestPCs.size() == 1);
    259     REQUIRE(propertyPCs.size() == 1);
    260     REQUIRE(manifestPCs[0] == propertyPCs[0].get());
    261 }
    262 
    263 TEST_CASE("SQLiteIndexSource_VersionSelection", "[sqliteindexsource]")
    264 {
    265     TempFile tempFile{ "repolibtest_tempdb"s, ".db"s };
    266     INFO("Using temporary file named: " << tempFile.GetPath());
    267 
    268     SourceDetails details;
    269     Manifest manifest;
    270     std::string relativePath;
    271     std::shared_ptr<SQLiteIndexSource> source = SimpleTestSetup(tempFile, details, manifest, relativePath, "InstallFlowTest_Exe.yaml");
    272 
    273     SearchRequest request;
    274     request.Query = RequestMatch(MatchType::Exact, manifest.Id);
    275 
    276     auto results = source->Search(request);
    277     REQUIRE(results.Matches.size() == 1);
    278     REQUIRE(results.Matches[0].Package);
    279 
    280     auto package = results.Matches[0].Package->GetAvailable()[0];
    281 
    282     PackageVersionKey key{ {}, "1", {} };
    283     auto version = package->GetVersion(key);
    284     REQUIRE(version);
    285 }