winget-cli

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

ConfigurationProcessorFactoryTests.cs (9700B)


      1 // -----------------------------------------------------------------------------
      2 // <copyright file="ConfigurationProcessorFactoryTests.cs" company="Microsoft Corporation">
      3 //     Copyright (c) Microsoft Corporation. Licensed under the MIT License.
      4 // </copyright>
      5 // -----------------------------------------------------------------------------
      6 
      7 namespace Microsoft.Management.Configuration.UnitTests.Tests
      8 {
      9     using System.Collections.Generic;
     10     using Microsoft.Management.Configuration.Processor;
     11     using Microsoft.Management.Configuration.Processor.PowerShell.Set;
     12     using Microsoft.Management.Configuration.UnitTests.Fixtures;
     13     using Microsoft.Management.Configuration.UnitTests.Helpers;
     14     using WinRT;
     15     using Xunit;
     16     using Xunit.Abstractions;
     17     using static Microsoft.Management.Configuration.Processor.PowerShell.Constants.PowerShellConstants;
     18 
     19     /// <summary>
     20     /// Tests ConfigurationProcessorFactory.
     21     /// </summary>
     22     [Collection("UnitTestCollection")]
     23     [InProc]
     24     public class ConfigurationProcessorFactoryTests
     25     {
     26         private readonly UnitTestFixture fixture;
     27         private readonly ITestOutputHelper log;
     28 
     29         /// <summary>
     30         /// Initializes a new instance of the <see cref="ConfigurationProcessorFactoryTests"/> class.
     31         /// </summary>
     32         /// <param name="fixture">Unit test fixture.</param>
     33         /// <param name="log">Log helper.</param>
     34         public ConfigurationProcessorFactoryTests(UnitTestFixture fixture, ITestOutputHelper log)
     35         {
     36             this.fixture = fixture;
     37             this.log = log;
     38         }
     39 
     40         /// <summary>
     41         /// CreateSetProcessor test.
     42         /// </summary>
     43         [Fact]
     44         public void CreateSetProcessor_Test()
     45         {
     46             var configurationProcessorFactory = new PowerShellConfigurationSetProcessorFactory();
     47 
     48             var properties = configurationProcessorFactory.As<IPowerShellConfigurationProcessorFactoryProperties>();
     49             properties.ProcessorType = PowerShellConfigurationProcessorType.Hosted;
     50 
     51             var configurationSet = new ConfigurationSet();
     52 
     53             var configurationProcessorSet = configurationProcessorFactory.CreateSetProcessor(configurationSet);
     54 
     55             Assert.NotNull(configurationProcessorSet);
     56             Assert.IsType<PowerShellConfigurationSetProcessor>(configurationProcessorSet);
     57             var processorSet = configurationProcessorSet as PowerShellConfigurationSetProcessor;
     58             Assert.NotNull(processorSet);
     59         }
     60 
     61         /// <summary>
     62         /// AdditionalModulePaths test.
     63         /// </summary>
     64         [Fact]
     65         public void CreateSetProcessor_Properties_PsModulePath()
     66         {
     67             var configurationProcessorFactory = new PowerShellConfigurationSetProcessorFactory();
     68 
     69             var properties = configurationProcessorFactory.As<IPowerShellConfigurationProcessorFactoryProperties>();
     70             properties.ProcessorType = PowerShellConfigurationProcessorType.Hosted;
     71             properties.AdditionalModulePaths = new List<string>
     72                 {
     73                     "ThisIsOnePath",
     74                     "ThisIsAnotherPath",
     75                 };
     76 
     77             var configurationSet = new ConfigurationSet();
     78 
     79             var configurationProcessorSet = configurationProcessorFactory.CreateSetProcessor(configurationSet);
     80 
     81             Assert.IsType<PowerShellConfigurationSetProcessor>(configurationProcessorSet);
     82             var processorSet = configurationProcessorSet as PowerShellConfigurationSetProcessor;
     83             Assert.NotNull(processorSet);
     84 
     85             var modulePath = processorSet.ProcessorEnvironment.GetVariable<string>(Variables.PSModulePath);
     86             Assert.Contains("ThisIsOnePath;ThisIsAnotherPath", modulePath);
     87         }
     88 
     89         /// <summary>
     90         /// Make sure the winget path is always added to PSModulePath.
     91         /// </summary>
     92         /// <param name="location">Location.</param>
     93         [Theory]
     94         [InlineData(PowerShellConfigurationProcessorLocation.CurrentUser)]
     95         [InlineData(PowerShellConfigurationProcessorLocation.AllUsers)]
     96         [InlineData(PowerShellConfigurationProcessorLocation.WinGetModulePath)]
     97         [InlineData(PowerShellConfigurationProcessorLocation.Custom)]
     98         public void CreateSetProcessor_WinGetPath(PowerShellConfigurationProcessorLocation location)
     99         {
    100             var configurationProcessorFactory = new PowerShellConfigurationSetProcessorFactory();
    101 
    102             var properties = configurationProcessorFactory.As<IPowerShellConfigurationProcessorFactoryProperties>();
    103             properties.Location = location;
    104 
    105             if (properties.Location == PowerShellConfigurationProcessorLocation.Custom)
    106             {
    107                 properties.CustomLocation = @"c:\this\is\a\module\path";
    108             }
    109 
    110             var configurationSet = new ConfigurationSet();
    111 
    112             var setProcessor = configurationProcessorFactory.CreateSetProcessor(configurationSet) as PowerShellConfigurationSetProcessor;
    113             Assert.NotNull(setProcessor);
    114 
    115             var modulePath = setProcessor.ProcessorEnvironment.GetVariable<string>(Variables.PSModulePath);
    116             Assert.Contains($"{PowerShellConfigurationSetProcessorFactory.GetWinGetModulePath()};", modulePath);
    117         }
    118 
    119         /// <summary>
    120         /// Tests the custom location is added successfully.
    121         /// </summary>
    122         [Fact]
    123         public void CreateSetProcessor_CustomLocation()
    124         {
    125             var configurationProcessorFactory = new PowerShellConfigurationSetProcessorFactory();
    126 
    127             var properties = configurationProcessorFactory.As<IPowerShellConfigurationProcessorFactoryProperties>();
    128             properties.Location = PowerShellConfigurationProcessorLocation.Custom;
    129             properties.CustomLocation = @"c:\this\is\a\module\path";
    130 
    131             var configurationSet = new ConfigurationSet();
    132 
    133             var setProcessor = configurationProcessorFactory.CreateSetProcessor(configurationSet) as PowerShellConfigurationSetProcessor;
    134             Assert.NotNull(setProcessor);
    135 
    136             var modulePath = setProcessor.ProcessorEnvironment.GetVariable<string>(Variables.PSModulePath);
    137             Assert.Contains($"{properties.CustomLocation};", modulePath);
    138         }
    139 
    140         /// <summary>
    141         /// Tests the configuration set processor in limitation mode.
    142         /// </summary>
    143         [Fact]
    144         public void CreateSetProcessor_LimitMode()
    145         {
    146             var configurationProcessorFactory = new PowerShellConfigurationSetProcessorFactory();
    147             var configurationSet = new ConfigurationSet();
    148             configurationProcessorFactory.LimitationSet = configurationSet;
    149 
    150             Assert.Throws<System.InvalidOperationException>(() => configurationProcessorFactory.LimitationSet = configurationSet);
    151             Assert.Throws<System.InvalidOperationException>(() => configurationProcessorFactory.ProcessorType = PowerShellConfigurationProcessorType.Default);
    152             Assert.Throws<System.InvalidOperationException>(() => configurationProcessorFactory.AdditionalModulePaths = new List<string>());
    153             Assert.Throws<System.InvalidOperationException>(() => configurationProcessorFactory.ImplicitModulePaths = new List<string>());
    154             Assert.Throws<System.InvalidOperationException>(() => configurationProcessorFactory.Policy = PowerShellConfigurationProcessorPolicy.Unrestricted);
    155             Assert.Throws<System.InvalidOperationException>(() => configurationProcessorFactory.Location = PowerShellConfigurationProcessorLocation.Custom);
    156             Assert.Throws<System.InvalidOperationException>(() => configurationProcessorFactory.CustomLocation = @"c:\this\is\a\module\path");
    157 
    158             var setProcessor = configurationProcessorFactory.CreateSetProcessor(configurationSet) as PowerShellConfigurationSetProcessor;
    159             Assert.NotNull(setProcessor);
    160             Assert.True(setProcessor.IsLimitMode);
    161 
    162             // Create processor again in limit mode should fail
    163             Assert.Throws<System.InvalidOperationException>(() => configurationProcessorFactory.CreateSetProcessor(configurationSet));
    164         }
    165 
    166         /// <summary>
    167         /// Tests the configuration set processor factory with ImplicitModulePaths.
    168         /// </summary>
    169         [Fact]
    170         public void ImplicitModulePaths()
    171         {
    172             var configurationProcessorFactory = new PowerShellConfigurationSetProcessorFactory();
    173 
    174             // When ImplicitModulePaths module paths are not set
    175             configurationProcessorFactory.AdditionalModulePaths = new List<string> { @"c:\this\is\additional" };
    176             Assert.Equal(configurationProcessorFactory.AdditionalModulePaths, new List<string> { @"c:\this\is\additional" });
    177 
    178             // Implicit ModulePaths are set, it automatically populates AdditionalModulePaths
    179             configurationProcessorFactory.ImplicitModulePaths = new List<string> { @"c:\this\is\implicit" };
    180             Assert.Equal(configurationProcessorFactory.AdditionalModulePaths, new List<string> { @"c:\this\is\additional", @"c:\this\is\implicit" });
    181 
    182             // Set AdditionalModulePaths when ImplicitModulePaths module paths are set
    183             configurationProcessorFactory.AdditionalModulePaths = new List<string> { @"c:\this\is\additional\2" };
    184             Assert.Equal(configurationProcessorFactory.AdditionalModulePaths, new List<string> { @"c:\this\is\additional\2", @"c:\this\is\implicit" });
    185         }
    186     }
    187 }