winget-cli

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

ProcessorEnvironmentTests.cs (10694B)


      1 // -----------------------------------------------------------------------------
      2 // <copyright file="ProcessorEnvironmentTests.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.UnitTests.Fixtures;
     11     using Microsoft.Management.Configuration.UnitTests.Helpers;
     12     using Moq;
     13     using Xunit;
     14     using Xunit.Abstractions;
     15     using static Microsoft.Management.Configuration.Processor.PowerShell.Constants.PowerShellConstants;
     16 
     17     /// <summary>
     18     /// HostedEnvironment tests, that is more ProcessorEnvironmentBase tests for non forwarding functions.
     19     /// </summary>
     20     [Collection("UnitTestCollection")]
     21     [InProc]
     22     public class ProcessorEnvironmentTests
     23     {
     24         private readonly UnitTestFixture fixture;
     25         private readonly ITestOutputHelper log;
     26 
     27         /// <summary>
     28         /// Initializes a new instance of the <see cref="ProcessorEnvironmentTests"/> class.
     29         /// </summary>
     30         /// <param name="fixture">Unit test fixture.</param>
     31         /// <param name="log">Log.</param>
     32         public ProcessorEnvironmentTests(UnitTestFixture fixture, ITestOutputHelper log)
     33         {
     34             this.fixture = fixture;
     35             this.log = log;
     36         }
     37 
     38         /// <summary>
     39         /// Test SetVariable and SetVariable methods.
     40         /// </summary>
     41         [Fact]
     42         public void HostedEnvironment_Variables()
     43         {
     44             var processorEnv = this.fixture.PrepareTestProcessorEnvironment();
     45 
     46             // As string.
     47             string var1Name = "var1";
     48             string var1 = "This is a string";
     49             processorEnv.SetVariable(var1Name, var1);
     50             string var1Result = processorEnv.GetVariable<string>(var1Name);
     51             Assert.Equal(var1, var1Result);
     52 
     53             // As int.
     54             string var2Name = "var2";
     55             int var2 = 42;
     56             processorEnv.SetVariable(var2Name, var2);
     57             int var2Result = processorEnv.GetVariable<int>(var2Name);
     58 
     59             // Wrong type.
     60             Assert.Throws<System.InvalidCastException>(() => processorEnv.GetVariable<int>(var1Name));
     61         }
     62 
     63         /// <summary>
     64         /// Tests SetPsModulePath.
     65         /// </summary>
     66         [Fact]
     67         public void HostedEnvironment_SetPSModulePath()
     68         {
     69             var processorEnv = this.fixture.PrepareTestProcessorEnvironment();
     70 
     71             string psModulePathInput = "SetPSModulePathModulePath";
     72             string psModulePath = processorEnv.GetVariable<string>(Variables.PSModulePath);
     73             Assert.NotEqual(psModulePathInput, psModulePath);
     74 
     75             processorEnv.SetPSModulePath(psModulePathInput);
     76             psModulePath = processorEnv.GetVariable<string>(Variables.PSModulePath);
     77             Assert.Equal(psModulePathInput, psModulePath);
     78         }
     79 
     80         /// <summary>
     81         /// Tests SetPsModulePaths.
     82         /// </summary>
     83         [Fact]
     84         public void HostedEnvironment_SetPSModulePaths()
     85         {
     86             var processorEnv = this.fixture.PrepareTestProcessorEnvironment();
     87 
     88             var psModulePathInput = new List<string>()
     89             {
     90                 "Path1",
     91                 "Path2",
     92                 "Path3",
     93                 "Path4",
     94             };
     95             string psModulePathExpected = "Path1;Path2;Path3;Path4";
     96 
     97             string psModulePath = processorEnv.GetVariable<string>(Variables.PSModulePath);
     98             Assert.NotEqual(psModulePathExpected, psModulePath);
     99 
    100             processorEnv.SetPSModulePaths(psModulePathInput);
    101             psModulePath = processorEnv.GetVariable<string>(Variables.PSModulePath);
    102             Assert.Equal(psModulePathExpected, psModulePath);
    103         }
    104 
    105         /// <summary>
    106         /// Tests AppendPSModulePath.
    107         /// </summary>
    108         [Fact]
    109         public void HostedEnvironment_AppendPSModulePath()
    110         {
    111             var processorEnv = this.fixture.PrepareTestProcessorEnvironment();
    112 
    113             string psModulePathInput = "AppendPSModulePathModulePath";
    114             string psModulePath = processorEnv.GetVariable<string>(Variables.PSModulePath);
    115             Assert.False(psModulePath.EndsWith($";{psModulePathInput}"));
    116 
    117             processorEnv.AppendPSModulePath(psModulePathInput);
    118             psModulePath = processorEnv.GetVariable<string>(Variables.PSModulePath);
    119             Assert.EndsWith($";{psModulePathInput}", psModulePath);
    120 
    121             // No duplicates
    122             processorEnv.AppendPSModulePath(psModulePathInput);
    123             psModulePath = processorEnv.GetVariable<string>(Variables.PSModulePath);
    124             Assert.False(psModulePath.EndsWith($";{psModulePathInput};{psModulePathInput}"));
    125             Assert.EndsWith($";{psModulePathInput}", psModulePath);
    126         }
    127 
    128         /// <summary>
    129         /// Tests AppendPSModulePaths.
    130         /// </summary>
    131         [Fact]
    132         public void HostedEnvironment_AppendPSModulePaths()
    133         {
    134             var processorEnv = this.fixture.PrepareTestProcessorEnvironment();
    135 
    136             var psModulePathInput = new List<string>()
    137             {
    138                 "AppendPSModulePathsPath1",
    139                 "AppendPSModulePathsPath2",
    140                 "AppendPSModulePathsPath3",
    141                 "AppendPSModulePathsPath4",
    142             };
    143             string psModulePathExpected = "AppendPSModulePathsPath1;AppendPSModulePathsPath2;AppendPSModulePathsPath3;AppendPSModulePathsPath4";
    144 
    145             string psModulePath = processorEnv.GetVariable<string>(Variables.PSModulePath);
    146             Assert.False(psModulePath.EndsWith($";{psModulePathExpected}"));
    147 
    148             processorEnv.AppendPSModulePaths(psModulePathInput);
    149             psModulePath = processorEnv.GetVariable<string>(Variables.PSModulePath);
    150             Assert.EndsWith($";{psModulePathExpected}", psModulePath);
    151 
    152             // No duplicates
    153             psModulePathInput = new List<string>()
    154             {
    155                 "AppendPSModulePathsPath1",
    156                 "AppendPSModulePathsPath5",
    157                 "AppendPSModulePathsPath2",
    158             };
    159             processorEnv.AppendPSModulePaths(psModulePathInput);
    160             psModulePath = processorEnv.GetVariable<string>(Variables.PSModulePath);
    161             Assert.EndsWith($";{psModulePathExpected};AppendPSModulePathsPath5", psModulePath);
    162         }
    163 
    164         /// <summary>
    165         /// Tests PrependPSModulePath.
    166         /// </summary>
    167         [Fact]
    168         public void HostedEnvironment_PrependPSModulePath()
    169         {
    170             var processorEnv = this.fixture.PrepareTestProcessorEnvironment();
    171 
    172             string psModulePathInput = "PrependPSModulePathModulePath";
    173             string psModulePath = processorEnv.GetVariable<string>(Variables.PSModulePath);
    174             Assert.False(psModulePath.StartsWith($";{psModulePathInput}"));
    175 
    176             processorEnv.PrependPSModulePath(psModulePathInput);
    177             psModulePath = processorEnv.GetVariable<string>(Variables.PSModulePath);
    178             Assert.StartsWith($"{psModulePathInput};", psModulePath);
    179 
    180             // No duplicates
    181             processorEnv.PrependPSModulePath(psModulePathInput);
    182             psModulePath = processorEnv.GetVariable<string>(Variables.PSModulePath);
    183             Assert.False(psModulePath.StartsWith($"{psModulePathInput};{psModulePathInput};"));
    184             Assert.StartsWith($"{psModulePathInput};", psModulePath);
    185         }
    186 
    187         /// <summary>
    188         /// Tests PrependPSModulePaths.
    189         /// </summary>
    190         [Fact]
    191         public void HostedEnvironment_PrependPSModulePaths()
    192         {
    193             var processorEnv = this.fixture.PrepareTestProcessorEnvironment();
    194 
    195             var psModulePathInput = new List<string>()
    196             {
    197                 "PrependPSModulePathsPath1",
    198                 "PrependPSModulePathsPath2",
    199                 "PrependPSModulePathsPath3",
    200                 "PrependPSModulePathsPath4",
    201             };
    202             string psModulePathExpected = "PrependPSModulePathsPath1;PrependPSModulePathsPath2;PrependPSModulePathsPath3;PrependPSModulePathsPath4";
    203 
    204             string psModulePath = processorEnv.GetVariable<string>(Variables.PSModulePath);
    205             Assert.False(psModulePath.StartsWith($";{psModulePathExpected}"));
    206 
    207             processorEnv.PrependPSModulePaths(psModulePathInput);
    208             psModulePath = processorEnv.GetVariable<string>(Variables.PSModulePath);
    209             Assert.StartsWith($"{psModulePathExpected};", psModulePath);
    210 
    211             // No duplicates
    212             psModulePathInput = new List<string>()
    213             {
    214                 "PrependPSModulePathsPath1",
    215                 "PrependPSModulePathsPath5",
    216                 "PrependPSModulePathsPath2",
    217             };
    218             processorEnv.PrependPSModulePaths(psModulePathInput);
    219             psModulePath = processorEnv.GetVariable<string>(Variables.PSModulePath);
    220             Assert.StartsWith($"PrependPSModulePathsPath5;{psModulePathExpected};", psModulePath);
    221         }
    222 
    223         /// <summary>
    224         /// Test CleanupPSModulePath.
    225         /// </summary>
    226         [Fact]
    227         public void HostedEnvironment_CleanupPSModulePath()
    228         {
    229             var processorEnv = this.fixture.PrepareTestProcessorEnvironment();
    230             var psModulePathInput = new List<string>()
    231             {
    232                 "CleanupPSModulePathPath1",
    233                 "CleanupPSModulePathPath2",
    234                 "CleanupPSModulePathPath3",
    235                 "CleanupPSModulePathPath1",
    236                 "CleanupPSModulePathPath1",
    237             };
    238 
    239             string psModulePathExpected = "CleanupPSModulePathPath1;CleanupPSModulePathPath2;CleanupPSModulePathPath3;CleanupPSModulePathPath1;CleanupPSModulePathPath1";
    240             processorEnv.SetPSModulePaths(psModulePathInput);
    241             string psModulePath = processorEnv.GetVariable<string>(Variables.PSModulePath);
    242             Assert.Equal(psModulePathExpected, psModulePath);
    243 
    244             processorEnv.CleanupPSModulePath("CleanupPSModulePathPath1");
    245 
    246             psModulePathExpected = "CleanupPSModulePathPath2;CleanupPSModulePathPath3";
    247             psModulePath = processorEnv.GetVariable<string>(Variables.PSModulePath);
    248             Assert.Equal(psModulePathExpected, psModulePath);
    249         }
    250     }
    251 }