winget-cli

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

PowerShellHelperTests.cs (7132B)


      1 // -----------------------------------------------------------------------------
      2 // <copyright file="PowerShellHelperTests.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;
     10     using Microsoft.Management.Configuration.Processor.PowerShell.Helpers;
     11     using Microsoft.Management.Configuration.UnitTests.Fixtures;
     12     using Microsoft.Management.Configuration.UnitTests.Helpers;
     13     using Xunit;
     14     using Xunit.Abstractions;
     15 
     16     /// <summary>
     17     /// PowerShell helper tests.
     18     /// </summary>
     19     [Collection("UnitTestCollection")]
     20     [InProc]
     21     public class PowerShellHelperTests
     22     {
     23         private readonly UnitTestFixture fixture;
     24         private readonly ITestOutputHelper log;
     25 
     26         /// <summary>
     27         /// Initializes a new instance of the <see cref="PowerShellHelperTests"/> class.
     28         /// </summary>
     29         /// <param name="fixture">Unit test fixture.</param>
     30         /// <param name="log">Log helper.</param>
     31         public PowerShellHelperTests(UnitTestFixture fixture, ITestOutputHelper log)
     32         {
     33             this.fixture = fixture;
     34             this.log = log;
     35         }
     36 
     37         /// <summary>
     38         /// Tests CreateModuleSpecification.
     39         /// </summary>
     40         [Fact]
     41         public void CreateModuleSpecification_Module_Test()
     42         {
     43             string moduleName = "MyModule";
     44 
     45             var moduleSpecification = PowerShellHelpers.CreateModuleSpecification(moduleName);
     46             Assert.Equal(moduleName, moduleSpecification.Name);
     47             Assert.Null(moduleSpecification.RequiredVersion);
     48             Assert.Null(moduleSpecification.Version);
     49             Assert.Null(moduleSpecification.MaximumVersion);
     50             Assert.Null(moduleSpecification.Guid);
     51         }
     52 
     53         /// <summary>
     54         /// Tests CreateModuleSpecification with arguments. use version.
     55         /// </summary>
     56         [Fact]
     57         public void CreateModuleSpecification_Required_Test()
     58         {
     59             string moduleName = "MyModule";
     60             string version = "0.1.0";
     61             string guid = Guid.NewGuid().ToString();
     62 
     63             var moduleSpecification = PowerShellHelpers.CreateModuleSpecification(
     64                 moduleName,
     65                 version,
     66                 null,
     67                 null,
     68                 guid);
     69             Assert.Equal(moduleName, moduleSpecification.Name);
     70             Assert.NotNull(moduleSpecification.RequiredVersion);
     71             Assert.Equal(version, moduleSpecification.RequiredVersion.ToString());
     72             Assert.Null(moduleSpecification.Version);
     73             Assert.Null(moduleSpecification.MaximumVersion);
     74             Assert.NotNull(moduleSpecification.Guid);
     75             Assert.Equal(guid, moduleSpecification.Guid.ToString());
     76         }
     77 
     78         /// <summary>
     79         /// Tests CreateModuleSpecification with arguments. Use min version.
     80         /// </summary>
     81         [Fact]
     82         public void CreateModuleSpecification_MinMax_Test()
     83         {
     84             string moduleName = "MyModule";
     85             string minVersion = "0.0.1";
     86             string maxVersion = "1.0.0";
     87             string guid = Guid.NewGuid().ToString();
     88 
     89             var moduleSpecification = PowerShellHelpers.CreateModuleSpecification(
     90                 moduleName,
     91                 null,
     92                 minVersion,
     93                 maxVersion,
     94                 guid);
     95             Assert.Equal(moduleName, moduleSpecification.Name);
     96             Assert.Null(moduleSpecification.RequiredVersion);
     97             Assert.NotNull(moduleSpecification.Version);
     98             Assert.Equal(minVersion, moduleSpecification.Version.ToString());
     99             Assert.NotNull(moduleSpecification.MaximumVersion);
    100             Assert.Equal(maxVersion, moduleSpecification.MaximumVersion.ToString());
    101             Assert.NotNull(moduleSpecification.Guid);
    102             Assert.Equal(guid, moduleSpecification.Guid.ToString());
    103         }
    104 
    105         /// <summary>
    106         /// Tests CreateModuleSpecification with prerelease versions.
    107         /// </summary>
    108         [Fact]
    109         public void CreateModuleSpecification_PrereleaseVersions_Required_Test()
    110         {
    111             string moduleName = "MyModule";
    112             string preVersion = "0.1.0-pre";
    113             string version = "0.1.0";
    114 
    115             var moduleSpecification = PowerShellHelpers.CreateModuleSpecification(
    116                 moduleName,
    117                 preVersion);
    118             Assert.Equal(moduleName, moduleSpecification.Name);
    119             Assert.NotNull(moduleSpecification.RequiredVersion);
    120             Assert.Equal(version, moduleSpecification.RequiredVersion.ToString());
    121             Assert.Null(moduleSpecification.Version);
    122             Assert.Null(moduleSpecification.MaximumVersion);
    123             Assert.Null(moduleSpecification.Guid);
    124         }
    125 
    126         /// <summary>
    127         /// Tests CreateModuleSpecification with prerelease versions.
    128         /// </summary>
    129         [Fact]
    130         public void CreateModuleSpecification_PrereleaseVersions_Test()
    131         {
    132             string moduleName = "MyModule";
    133             string preMinVersion = "0.0.1-pre";
    134             string minVersion = "0.0.1";
    135             string preMaxVersion = "1.0.0-pre";
    136             string maxVersion = "1.0.0";
    137 
    138             var moduleSpecification = PowerShellHelpers.CreateModuleSpecification(
    139                 moduleName,
    140                 null,
    141                 preMinVersion,
    142                 preMaxVersion);
    143             Assert.Equal(moduleName, moduleSpecification.Name);
    144             Assert.Null(moduleSpecification.RequiredVersion);
    145             Assert.NotNull(moduleSpecification.Version);
    146             Assert.Equal(minVersion, moduleSpecification.Version.ToString());
    147             Assert.NotNull(moduleSpecification.MaximumVersion);
    148             Assert.Equal(maxVersion, moduleSpecification.MaximumVersion.ToString());
    149             Assert.Null(moduleSpecification.Guid);
    150         }
    151 
    152         /// <summary>
    153         /// Tests CreateModuleSpecification with arguments. Don't use minVersion and version.
    154         /// </summary>
    155         [Fact]
    156         public void CreateModuleSpecification_Args_Throws_Test()
    157         {
    158             string moduleName = "MyModule";
    159             string version = "0.1.0";
    160             string minVersion = "0.0.1";
    161             string maxVersion = "1.0.0";
    162 
    163             Assert.Throws<ArgumentException>(() => PowerShellHelpers.CreateModuleSpecification(
    164                 moduleName,
    165                 version,
    166                 minVersion,
    167                 null,
    168                 null));
    169 
    170             Assert.Throws<ArgumentException>(() => PowerShellHelpers.CreateModuleSpecification(
    171                 moduleName,
    172                 version,
    173                 null,
    174                 maxVersion,
    175                 null));
    176         }
    177     }
    178 }