winget-cli

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

BaseInterop.cs (3436B)


      1 // -----------------------------------------------------------------------------
      2 // <copyright file="BaseInterop.cs" company="Microsoft Corporation">
      3 //     Copyright (c) Microsoft Corporation. Licensed under the MIT License.
      4 // </copyright>
      5 // -----------------------------------------------------------------------------
      6 
      7 namespace AppInstallerCLIE2ETests.Interop
      8 {
      9     using System.Collections.Generic;
     10     using System.Linq;
     11     using Microsoft.Management.Deployment;
     12     using Microsoft.Management.Deployment.Projection;
     13     using NUnit.Framework;
     14 
     15     /// <summary>
     16     /// Base interop class.
     17     /// </summary>
     18     public abstract class BaseInterop
     19     {
     20         /// <summary>
     21         /// Initializes a new instance of the <see cref="BaseInterop"/> class.
     22         /// </summary>
     23         /// <param name="initializer">Initializer.</param>
     24         public BaseInterop(IInstanceInitializer initializer)
     25         {
     26             this.TestFactory = new (initializer);
     27         }
     28 
     29         /// <summary>
     30         /// Gets the test factory.
     31         /// </summary>
     32         public WinGetProjectionFactory TestFactory { get; }
     33 
     34         /// <summary>
     35         /// Find one filtered package from a provided package catalog reference.
     36         /// </summary>
     37         /// <param name="packageCatalogReference">Package catalog reference.</param>
     38         /// <param name="field">Package match field.</param>
     39         /// <param name="option">Package field match option.</param>
     40         /// <param name="value">Package match value.</param>
     41         /// <returns>List of matches.</returns>
     42         public MatchResult FindOnePackage(
     43             PackageCatalogReference packageCatalogReference,
     44             PackageMatchField field,
     45             PackageFieldMatchOption option,
     46             string value)
     47         {
     48             var findPackages = this.FindAllPackages(packageCatalogReference, field, option, value);
     49             Assert.AreEqual(1, findPackages.Count, $"Expected exactly one package but found {findPackages.Count}");
     50             return findPackages.First();
     51         }
     52 
     53         /// <summary>
     54         /// Find all filtered package from a provided package catalog reference.
     55         /// </summary>
     56         /// <param name="packageCatalogReference">Package catalog reference.</param>
     57         /// <param name="field">Package match field.</param>
     58         /// <param name="option">Package field match option.</param>
     59         /// <param name="value">Package match value.</param>
     60         /// <returns>List of matches.</returns>
     61         protected IReadOnlyList<MatchResult> FindAllPackages(
     62             PackageCatalogReference packageCatalogReference,
     63             PackageMatchField field,
     64             PackageFieldMatchOption option,
     65             string value)
     66         {
     67             Assert.NotNull(packageCatalogReference, "Package catalog reference cannot be null");
     68 
     69             // Prepare filter
     70             var filter = this.TestFactory.CreatePackageMatchFilter();
     71             filter.Field = field;
     72             filter.Option = option;
     73             filter.Value = value;
     74 
     75             // Add filter
     76             var findPackageOptions = this.TestFactory.CreateFindPackagesOptions();
     77             findPackageOptions.Filters.Add(filter);
     78 
     79             // Connect and find package
     80             var source = packageCatalogReference.Connect().PackageCatalog;
     81 
     82             return source.FindPackages(findPackageOptions).Matches;
     83         }
     84     }
     85 }