diff --git a/src/PowerShell/Microsoft.WinGet.Client/Commands/FindPackageCommand.cs b/src/PowerShell/Microsoft.WinGet.Client/Commands/FindPackageCommand.cs index 59c6589d36..ae8c76c057 100644 --- a/src/PowerShell/Microsoft.WinGet.Client/Commands/FindPackageCommand.cs +++ b/src/PowerShell/Microsoft.WinGet.Client/Commands/FindPackageCommand.cs @@ -5,17 +5,17 @@ // ----------------------------------------------------------------------------- namespace Microsoft.WinGet.Client.Commands -{ +{ using System.Management.Automation; using Microsoft.Management.Deployment; using Microsoft.WinGet.Client.Commands.Common; - using Microsoft.WinGet.Client.Common; - + using Microsoft.WinGet.Client.Common; + /// /// Searches configured sources for packages. /// [Cmdlet(VerbsCommon.Find, Constants.WinGetNouns.Package)] - [OutputType(typeof(MatchResult))] + [OutputType(typeof(PSObjects.FoundCatalogPackage))] public sealed class FindPackageCommand : BaseFinderExtendedCommand { /// @@ -27,7 +27,7 @@ protected override void ProcessRecord() var results = this.FindPackages(CompositeSearchBehavior.RemotePackagesFromRemoteCatalogs); for (var i = 0; i < results.Count; i++) { - this.WriteObject(results[i]); + this.WriteObject(new PSObjects.FoundCatalogPackage(results[i].CatalogPackage)); } } } diff --git a/src/PowerShell/Microsoft.WinGet.Client/Commands/GetPackageCommand.cs b/src/PowerShell/Microsoft.WinGet.Client/Commands/GetPackageCommand.cs index 6743e01988..635cebda86 100644 --- a/src/PowerShell/Microsoft.WinGet.Client/Commands/GetPackageCommand.cs +++ b/src/PowerShell/Microsoft.WinGet.Client/Commands/GetPackageCommand.cs @@ -15,7 +15,7 @@ namespace Microsoft.WinGet.Client.Commands /// Searches configured sources for packages. /// [Cmdlet(VerbsCommon.Get, Constants.WinGetNouns.Package)] - [OutputType(typeof(CatalogPackage))] + [OutputType(typeof(PSObjects.InstalledCatalogPackage))] public sealed class GetPackageCommand : BaseFinderExtendedCommand { /// @@ -27,7 +27,7 @@ protected override void ProcessRecord() var results = this.FindPackages(CompositeSearchBehavior.LocalCatalogs); for (var i = 0; i < results.Count; i++) { - this.WriteObject(results[i].CatalogPackage); + this.WriteObject(new PSObjects.InstalledCatalogPackage(results[i].CatalogPackage)); } } } diff --git a/src/PowerShell/Microsoft.WinGet.Client/Commands/GetSourceCommand.cs b/src/PowerShell/Microsoft.WinGet.Client/Commands/GetSourceCommand.cs index fbe52e02d8..56ebd38387 100644 --- a/src/PowerShell/Microsoft.WinGet.Client/Commands/GetSourceCommand.cs +++ b/src/PowerShell/Microsoft.WinGet.Client/Commands/GetSourceCommand.cs @@ -7,7 +7,6 @@ namespace Microsoft.WinGet.Client.Commands { using System.Management.Automation; - using Microsoft.Management.Deployment; using Microsoft.WinGet.Client.Commands.Common; using Microsoft.WinGet.Client.Common; @@ -15,7 +14,7 @@ namespace Microsoft.WinGet.Client.Commands /// Retrieves the list of configured sources. /// [Cmdlet(VerbsCommon.Get, Constants.WinGetNouns.Source)] - [OutputType(typeof(PackageCatalogReference))] + [OutputType(typeof(PSObjects.SourceResult))] public sealed class GetSourceCommand : BaseClientCommand { /// @@ -36,7 +35,7 @@ protected override void ProcessRecord() var results = GetPackageCatalogReferences(this.Name); for (var i = 0; i < results.Count; i++) { - this.WriteObject(results[i]); + this.WriteObject(new PSObjects.SourceResult(results[i])); } } } diff --git a/src/PowerShell/Microsoft.WinGet.Client/Commands/InstallPackageCommand.cs b/src/PowerShell/Microsoft.WinGet.Client/Commands/InstallPackageCommand.cs index 8d0342522d..73dc11b003 100644 --- a/src/PowerShell/Microsoft.WinGet.Client/Commands/InstallPackageCommand.cs +++ b/src/PowerShell/Microsoft.WinGet.Client/Commands/InstallPackageCommand.cs @@ -21,7 +21,7 @@ namespace Microsoft.WinGet.Client.Commands Constants.WinGetNouns.Package, DefaultParameterSetName = Constants.FoundSet, SupportsShouldProcess = true)] - [OutputType(typeof(InstallResult))] + [OutputType(typeof(PSObjects.InstallResult))] public sealed class InstallPackageCommand : BaseInstallCommand { private ProcessorArchitecture architecture; @@ -66,7 +66,7 @@ protected override void ProcessRecord() { InstallOptions options = this.GetInstallOptions(version); InstallResult result = this.InstallPackage(package, options); - this.WriteObject(result); + this.WriteObject(new PSObjects.InstallResult(result)); }); } diff --git a/src/PowerShell/Microsoft.WinGet.Client/Commands/UninstallPackageCommand.cs b/src/PowerShell/Microsoft.WinGet.Client/Commands/UninstallPackageCommand.cs index 0225facf19..63e80e1bc0 100644 --- a/src/PowerShell/Microsoft.WinGet.Client/Commands/UninstallPackageCommand.cs +++ b/src/PowerShell/Microsoft.WinGet.Client/Commands/UninstallPackageCommand.cs @@ -22,7 +22,7 @@ namespace Microsoft.WinGet.Client.Commands Constants.WinGetNouns.Package, DefaultParameterSetName = Constants.FoundSet, SupportsShouldProcess = true)] - [OutputType(typeof(UninstallResult))] + [OutputType(typeof(PSObjects.UninstallResult))] public sealed class UninstallPackageCommand : BasePackageCommand { /// @@ -46,7 +46,7 @@ protected override void ProcessRecord() { UninstallOptions options = this.GetUninstallOptions(version); UninstallResult result = this.UninstallPackage(package, options); - this.WriteObject(result); + this.WriteObject(new PSObjects.UninstallResult(result)); }); } diff --git a/src/PowerShell/Microsoft.WinGet.Client/Commands/UpdatePackageCommand.cs b/src/PowerShell/Microsoft.WinGet.Client/Commands/UpdatePackageCommand.cs index fd7c462587..892c216f3b 100644 --- a/src/PowerShell/Microsoft.WinGet.Client/Commands/UpdatePackageCommand.cs +++ b/src/PowerShell/Microsoft.WinGet.Client/Commands/UpdatePackageCommand.cs @@ -20,7 +20,7 @@ namespace Microsoft.WinGet.Client.Commands Constants.WinGetNouns.Package, DefaultParameterSetName = Constants.FoundSet, SupportsShouldProcess = true)] - [OutputType(typeof(InstallResult))] + [OutputType(typeof(PSObjects.InstallResult))] public sealed class UpdatePackageCommand : BaseInstallCommand { /// diff --git a/src/PowerShell/Microsoft.WinGet.Client/Module/Format.ps1xml b/src/PowerShell/Microsoft.WinGet.Client/Module/Format.ps1xml index 3c140ab2b5..bb349cd965 100644 --- a/src/PowerShell/Microsoft.WinGet.Client/Module/Format.ps1xml +++ b/src/PowerShell/Microsoft.WinGet.Client/Module/Format.ps1xml @@ -2,25 +2,63 @@ - Microsoft.Management.Deployment.MatchResult + Microsoft.WinGet.Client.PSObjects.FoundCatalogPackage - Microsoft.Management.Deployment.MatchResult + Microsoft.WinGet.Client.PSObjects.FoundCatalogPackage - 32 - 32 - + + + + + + + + $_.Name + + + $_.Id + + + $_.Version + + + $_.Source + + + + + + + + Microsoft.WinGet.Client.PSObjects.InstalledCatalogPackage + + Microsoft.WinGet.Client.PSObjects.InstalledCatalogPackage + + + + + + + + + + + + + + @@ -30,19 +68,19 @@ - $_.CatalogPackage.Name + $_.Name - $_.CatalogPackage.Id + $_.Id - $_.CatalogPackage.DefaultInstallVersion.Version + $_.Version - '{0}: {1}' -f $_.MatchCriteria.Field,$_.MatchCriteria.Value + if ($_.IsUpdateAvailable) { $_.AvailableVersions[0] } - $_.CatalogPackage.DefaultInstallVersion.PackageCatalog.Info.Name + $_.Source @@ -50,14 +88,105 @@ - Microsoft.Management.Deployment.PackageCatalogReference + Microsoft.WinGet.Client.PSObjects.InstallResult - Microsoft.Management.Deployment.PackageCatalogReference + Microsoft.WinGet.Client.PSObjects.InstallResult + + + + + + + + + + + + + + + + + + + + + + + + $_.InstallerErrorCode + + + $_.Status + + + $_.RebootRequired + + + $_.ExtendedErrorCode + + + $_.CorrelationData + + + + + + + + Microsoft.WinGet.Client.PSObjects.UninstallResult + + Microsoft.WinGet.Client.PSObjects.UninstallResult + + + + + + + + + + + + + + + + + + + + + + + + $_.UninstallerErrorCode + + + $_.Status + + + $_.RebootRequired + + + $_.ExtendedErrorCode + + + $_.CorrelationData + + + + + + + + Microsoft.WinGet.Client.PSObjects.SourceResult + + Microsoft.WinGet.Client.PSObjects.SourceResult - 32 @@ -71,13 +200,13 @@ - $_.Info.Name + $_.Name - $_.Info.Argument + $_.Argument - $_.Info.Type + $_.Type @@ -85,47 +214,52 @@ - Microsoft.Management.Deployment.CatalogPackage + Microsoft.WinGet.Client.PSObjects.PackageVersionInfo - Microsoft.Management.Deployment.CatalogPackage + Microsoft.WinGet.Client.PSObjects.PackageVersionInfo - 32 - + - + - + - + + + + - $_.Name + $_.DisplayName $_.Id - $_.InstalledVersion.Version + $_.Publisher - if ($_.IsUpdateAvailable) { $_.AvailableVersions[0].Version } + $_.Channel - $_.DefaultInstallVersion.PackageCatalog.Info.Name + $_.PackageFamilyNames + + $_.ProductCodes + diff --git a/src/PowerShell/Microsoft.WinGet.Client/PSObjects/CatalogPackage.cs b/src/PowerShell/Microsoft.WinGet.Client/PSObjects/CatalogPackage.cs new file mode 100644 index 0000000000..2a821196c1 --- /dev/null +++ b/src/PowerShell/Microsoft.WinGet.Client/PSObjects/CatalogPackage.cs @@ -0,0 +1,132 @@ +// ----------------------------------------------------------------------------- +// +// Copyright (c) Microsoft Corporation. Licensed under the MIT License. +// +// ----------------------------------------------------------------------------- + +namespace Microsoft.WinGet.Client.PSObjects +{ + using System.Linq; + using Microsoft.Management.Deployment; + using Microsoft.WinGet.Client.Exceptions; + + /// + /// CatalogPackage wrapper object for displaying to PowerShell. + /// + public abstract class CatalogPackage + { + /// + /// Initializes a new instance of the class. + /// + /// CatalogPackage COM object. + public CatalogPackage(Management.Deployment.CatalogPackage catalogPackage) + { + this.CatalogPackageCOM = catalogPackage; + } + + /// + /// Gets the name of the catalog package. + /// + public string Name + { + get + { + return this.CatalogPackageCOM.Name; + } + } + + /// + /// Gets the id of the catalog package. + /// + public string Id + { + get + { + return this.CatalogPackageCOM.Id; + } + } + + /// + /// Gets a value indicating whether an update is available. + /// + public bool IsUpdateAvailable + { + get + { + return this.CatalogPackageCOM.IsUpdateAvailable; + } + } + + /// + /// Gets the source name of the catalog package. + /// + public string Source + { + get + { + return this.CatalogPackageCOM.DefaultInstallVersion.PackageCatalog.Info.Name; + } + } + + /// + /// Gets list of strings representing the available versions. + /// + public string[] AvailableVersions + { + get + { + return this.AvailablePackageVersionIds.Select(i => i.Version).ToArray(); + } + } + + /// + /// Gets the version of the catalog package. + /// + public abstract string Version { get; } + + /// + /// Gets the catalog package COM object. + /// + protected Management.Deployment.CatalogPackage CatalogPackageCOM { get; } + + /// + /// Gets a list of available package version ids for the package. + /// + private PackageVersionId[] AvailablePackageVersionIds + { + get + { + return this.CatalogPackageCOM.AvailableVersions.ToArray(); + } + } + + /// + /// Checks the installed status of the catalog package. + /// + /// CheckInstalledStatus string. + public string CheckInstalledStatus() + { + return this.CatalogPackageCOM.CheckInstalledStatus().Status.ToString(); + } + + /// + /// Gets the PackageVersionInfo PSObject that corresponds with the version string. + /// + /// Version string. + /// PackageVersionInfo PSObject. + /// Throws an exception if no package is found. + public PackageVersionInfo GetPackageVersionInfo(string version) + { + // get specific version that matches + PackageVersionId packageVersionId = this.AvailablePackageVersionIds.FirstOrDefault(x => x.Version == version); + if (packageVersionId != null) + { + return new PackageVersionInfo(this.CatalogPackageCOM.GetPackageVersionInfo(packageVersionId)); + } + else + { + throw new NoPackageFoundException(); + } + } + } +} diff --git a/src/PowerShell/Microsoft.WinGet.Client/PSObjects/FoundCatalogPackage.cs b/src/PowerShell/Microsoft.WinGet.Client/PSObjects/FoundCatalogPackage.cs new file mode 100644 index 0000000000..59378883f8 --- /dev/null +++ b/src/PowerShell/Microsoft.WinGet.Client/PSObjects/FoundCatalogPackage.cs @@ -0,0 +1,29 @@ +// ----------------------------------------------------------------------------- +// +// Copyright (c) Microsoft Corporation. Licensed under the MIT License. +// +// ----------------------------------------------------------------------------- + +namespace Microsoft.WinGet.Client.PSObjects +{ + /// + /// FoundCatalogPackage wrapper object for displaying to PowerShell. + /// + public class FoundCatalogPackage : CatalogPackage + { + /// + /// Initializes a new instance of the class. + /// + /// The catalog package COM object. + public FoundCatalogPackage(Management.Deployment.CatalogPackage catalogPackage) + : base(catalogPackage) + { + } + + /// + public override string Version + { + get { return this.CatalogPackageCOM.DefaultInstallVersion.Version; } + } + } +} diff --git a/src/PowerShell/Microsoft.WinGet.Client/PSObjects/InstallResult.cs b/src/PowerShell/Microsoft.WinGet.Client/PSObjects/InstallResult.cs new file mode 100644 index 0000000000..e68f0f53eb --- /dev/null +++ b/src/PowerShell/Microsoft.WinGet.Client/PSObjects/InstallResult.cs @@ -0,0 +1,82 @@ +// ----------------------------------------------------------------------------- +// +// Copyright (c) Microsoft Corporation. Licensed under the MIT License. +// +// ----------------------------------------------------------------------------- + +namespace Microsoft.WinGet.Client.PSObjects +{ + using System; + + /// + /// InstallResult wrapper object for displaying to PowerShell. + /// + public class InstallResult + { + private Management.Deployment.InstallResult installResult; + + /// + /// Initializes a new instance of the class. + /// + /// The install result COM object. + public InstallResult(Management.Deployment.InstallResult installResult) + { + this.installResult = installResult; + } + + /// + /// Gets the correlation data of the install result. + /// + public string CorrelationData + { + get + { + return this.installResult.CorrelationData; + } + } + + /// + /// Gets the error code of an install. + /// + public uint InstallerErrorCode + { + get + { + return this.installResult.InstallerErrorCode; + } + } + + /// + /// Gets the extended error code exception of the failed install result. + /// + public Exception ExtendedErrorCode + { + get + { + return this.installResult.ExtendedErrorCode; + } + } + + /// + /// Gets a value indicating whether a reboot is required. + /// + public bool RebootRequired + { + get + { + return this.installResult.RebootRequired; + } + } + + /// + /// Gets the status of the install. + /// + public string Status + { + get + { + return this.installResult.Status.ToString(); + } + } + } +} diff --git a/src/PowerShell/Microsoft.WinGet.Client/PSObjects/InstalledCatalogPackage.cs b/src/PowerShell/Microsoft.WinGet.Client/PSObjects/InstalledCatalogPackage.cs new file mode 100644 index 0000000000..8815ee6488 --- /dev/null +++ b/src/PowerShell/Microsoft.WinGet.Client/PSObjects/InstalledCatalogPackage.cs @@ -0,0 +1,29 @@ +// ----------------------------------------------------------------------------- +// +// Copyright (c) Microsoft Corporation. Licensed under the MIT License. +// +// ----------------------------------------------------------------------------- + +namespace Microsoft.WinGet.Client.PSObjects +{ + /// + /// CatalogPackage wrapper object for displaying to PowerShell. + /// + public class InstalledCatalogPackage : CatalogPackage + { + /// + /// Initializes a new instance of the class. + /// + /// The catalog package COM object. + public InstalledCatalogPackage(Management.Deployment.CatalogPackage catalogPackage) + : base(catalogPackage) + { + } + + /// + public override string Version + { + get { return this.CatalogPackageCOM.InstalledVersion.Version; } + } + } +} diff --git a/src/PowerShell/Microsoft.WinGet.Client/PSObjects/PackageVersionInfo.cs b/src/PowerShell/Microsoft.WinGet.Client/PSObjects/PackageVersionInfo.cs new file mode 100644 index 0000000000..55faef8d3f --- /dev/null +++ b/src/PowerShell/Microsoft.WinGet.Client/PSObjects/PackageVersionInfo.cs @@ -0,0 +1,103 @@ +// ----------------------------------------------------------------------------- +// +// Copyright (c) Microsoft Corporation. Licensed under the MIT License. +// +// ----------------------------------------------------------------------------- + +namespace Microsoft.WinGet.Client.PSObjects +{ + using System.Linq; + + /// + /// CatalogPackage wrapper object for displaying to PowerShell. + /// + public class PackageVersionInfo + { + private Management.Deployment.PackageVersionInfo packageVersionInfo; + + /// + /// Initializes a new instance of the class. + /// + /// PackageVersionInfo COM object. + public PackageVersionInfo(Management.Deployment.PackageVersionInfo packageVersionInfo) + { + this.packageVersionInfo = packageVersionInfo; + } + + /// + /// Gets the name of the package version info. + /// + public string DisplayName + { + get + { + return this.packageVersionInfo.DisplayName; + } + } + + /// + /// Gets the id of the package version info. + /// + public string Id + { + get + { + return this.packageVersionInfo.Id; + } + } + + /// + /// Gets the publisher of the package version info. + /// + public string Publisher + { + get + { + return this.packageVersionInfo.Publisher; + } + } + + /// + /// Gets the channel of the package version info. + /// + public string Channel + { + get + { + return this.packageVersionInfo.Channel; + } + } + + /// + /// Gets the list of package family names of the package version info. + /// + public string[] PackageFamilyNames + { + get + { + return this.packageVersionInfo.PackageFamilyNames.ToArray(); + } + } + + /// + /// Gets the list of product codes of the package version info. + /// + public string[] ProductCodes + { + get + { + return this.packageVersionInfo.ProductCodes.ToArray(); + } + } + + /// + /// Compares the version string with the package version info and returns the CompareResult. + /// + /// Version string. + /// CompareResult string. + public string CompareToVersion(string version) + { + return this.packageVersionInfo.CompareToVersion(version).ToString(); + } + } +} diff --git a/src/PowerShell/Microsoft.WinGet.Client/PSObjects/SourceResult.cs b/src/PowerShell/Microsoft.WinGet.Client/PSObjects/SourceResult.cs new file mode 100644 index 0000000000..3c97347487 --- /dev/null +++ b/src/PowerShell/Microsoft.WinGet.Client/PSObjects/SourceResult.cs @@ -0,0 +1,41 @@ +// ----------------------------------------------------------------------------- +// +// Copyright (c) Microsoft Corporation. Licensed under the MIT License. +// +// ----------------------------------------------------------------------------- + +namespace Microsoft.WinGet.Client.PSObjects +{ + /// + /// SourceResult wrapper object for displaying to PowerShell. + /// + public class SourceResult + { + /// + /// Initializes a new instance of the class. + /// + /// The PackageCatalogReference COM object. + public SourceResult(Management.Deployment.PackageCatalogReference catalogReference) + { + var info = catalogReference.Info; + this.Name = info.Name; + this.Argument = info.Argument; + this.Type = info.Type; + } + + /// + /// Gets the name of the source. + /// + public string Name { get; private set; } + + /// + /// Gets the argument of the source. + /// + public string Argument { get; private set; } + + /// + /// Gets the type of the source. + /// + public string Type { get; private set; } + } +} diff --git a/src/PowerShell/Microsoft.WinGet.Client/PSObjects/UninstallResult.cs b/src/PowerShell/Microsoft.WinGet.Client/PSObjects/UninstallResult.cs new file mode 100644 index 0000000000..679a385b5b --- /dev/null +++ b/src/PowerShell/Microsoft.WinGet.Client/PSObjects/UninstallResult.cs @@ -0,0 +1,82 @@ +// ----------------------------------------------------------------------------- +// +// Copyright (c) Microsoft Corporation. Licensed under the MIT License. +// +// ----------------------------------------------------------------------------- + +namespace Microsoft.WinGet.Client.PSObjects +{ + using System; + + /// + /// UninstallResult wrapper object for displaying to PowerShell. + /// + public class UninstallResult + { + private Management.Deployment.UninstallResult uninstallResult; + + /// + /// Initializes a new instance of the class. + /// + /// The uninstall result COM object. + public UninstallResult(Management.Deployment.UninstallResult uninstallResult) + { + this.uninstallResult = uninstallResult; + } + + /// + /// Gets the correlation data of the uninstall result. + /// + public string CorrelationData + { + get + { + return this.uninstallResult.CorrelationData; + } + } + + /// + /// Gets the extended error code exception of the failed uninstall result. + /// + public Exception ExtendedErrorCode + { + get + { + return this.uninstallResult.ExtendedErrorCode; + } + } + + /// + /// Gets a value indicating whether a reboot is required. + /// + public bool RebootRequired + { + get + { + return this.uninstallResult.RebootRequired; + } + } + + /// + /// Gets the status of the uninstall. + /// + public string Status + { + get + { + return this.uninstallResult.Status.ToString(); + } + } + + /// + /// Gets the error code of an uninstall. + /// + public uint UninstallerErrorCode + { + get + { + return this.uninstallResult.UninstallerErrorCode; + } + } + } +} diff --git a/src/PowerShell/tests/Microsoft.WinGet.Client.Tests.ps1 b/src/PowerShell/tests/Microsoft.WinGet.Client.Tests.ps1 new file mode 100644 index 0000000000..00faba7012 --- /dev/null +++ b/src/PowerShell/tests/Microsoft.WinGet.Client.Tests.ps1 @@ -0,0 +1,185 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +<# +.Synopsis + Pester tests related to the Microsoft.WinGet.Client PowerShell module. + The tests require the localhost web server to be running and serving the test data. + 'Invoke-Pester' should be called in an admin powershell window with the 'Microsoft.WinGet.Client' PowerShell module already imported. +#> + +BeforeAll { + # Source Add requires admin privileges, this will only execute successfully in an elevated PowerShell. + wingetdev source add 'TestSource' 'https://localhost:5001/TestKit/' +} + +Describe 'Get-WinGetSource' { + + It 'Get Test Source' { + $source = Get-WinGetSource -Name 'TestSource' + $source.Name | Should -Be 'TestSource' + $source.Argument | Should -Be 'https://localhost:5001/TestKit/' + $source.Type | Should -Be 'Microsoft.PreIndexed.Package' + } +} + +Describe 'Find-WinGetPackage' { + It 'Given no parameters, lists all available packages' { + $allPackages = Find-WinGetPackage -Source TestSource + $allPackages.Count | Should -BeGreaterThan 0 + } + + It 'Find by Id' { + $package = Find-WinGetPackage -Source 'TestSource' -Id 'AppInstallerTest.TestExeInstaller' + + $package | Should -Not -BeNullOrEmpty -ErrorAction Stop + $package.Name | Should -Be 'TestExeInstaller' + $package.Id | Should -Be 'AppInstallerTest.TestExeInstaller' + $package.Version | Should -Be '2.0.0.0' + $package.Source | Should -Be 'TestSource' + } + + It 'Find by Name' { + $package = Find-WinGetPackage -Source 'TestSource' -Name 'TestPortableExe' + + $package | Should -Not -BeNullOrEmpty -ErrorAction Stop + $package[0].Name | Should -Be 'TestPortableExe' + $package[1].Name | Should -Be 'TestPortableExeWithCommand' + } + + It 'Find by Name sort by Version' { + $package = Find-WinGetPackage -Source 'TestSource' -Name 'TestPortableExe' | Sort-Object 'Version' + + $package | Should -Not -BeNullOrEmpty -ErrorAction Stop + $package[0].Name | Should -Be 'TestPortableExeWithCommand' + $package[1].Name | Should -Be 'TestPortableExe' + } + + It 'Find package and verify PackageVersionInfo' { + $package = Find-WinGetPackage -Source 'TestSource' -Id 'AppInstallerTest.TestPortableExe' -Exact + + $package | Should -Not -BeNullOrEmpty -ErrorAction Stop + $package.AvailableVersions[0] | Should -Be '3.0.0.0' + $package.AvailableVersions[1] | Should -Be '2.0.0.0' + $package.AvailableVersions.Count | Should -Be 4 + + $packageVersionInfo = $package.GetPackageVersionInfo("3.0.0.0") + $packageVersionInfo.DisplayName | Should -Be 'TestPortableExe' + $packageVersionInfo.Id | Should -Be 'AppInstallerTest.TestPortableExe' + $packageVersionInfo.CompareToVersion("2.0.0.0") | Should -Be 'Greater' + $packageVersionInfo.CompareToVersion("4.0.0.0") | Should -Be 'Lesser' + } +} + +Describe 'Install|Update|Uninstall-WinGetPackage' { + + It 'Install by Id' { + $result = Install-WinGetPackage -Id AppInstallerTest.TestExeInstaller -Version '1.0.0.0' + + $result.InstallerErrorCode | Should -Be 0 + $result.Status | Should -Be 'Ok' + $result.RebootRequired | Should -Be 'False' + } + + It 'Install by exact Name and Version' { + $result = Install-WinGetPackage -Name TestPortableExe -Version '2.0.0.0' -Exact + + $result.InstallerErrorCode | Should -Be 0 + $result.Status | Should -Be 'Ok' + $result.RebootRequired | Should -Be 'False' + } + + It 'Update by Id' { + $result = Update-WinGetPackage -Id AppInstallerTest.TestExeInstaller + + $result.InstallerErrorCode | Should -Be 0 + $result.Status | Should -Be 'Ok' + $result.RebootRequired | Should -Be 'False' + } + + It 'Update by Name and Version' { + $result = Update-WinGetPackage -Name TestPortableExe + + $result.InstallerErrorCode | Should -Be 0 + $result.Status | Should -Be 'Ok' + $result.RebootRequired | Should -Be 'False' + } + + It 'Uninstall by Id' { + $result = Uninstall-WinGetPackage -Id AppInstallerTest.TestExeInstaller + + $result.UninstallerErrorCode | Should -Be 0 + $result.Status | Should -Be 'Ok' + $result.RebootRequired | Should -Be 'False' + } + + It 'Uninstall by Name' { + $result = Uninstall-WinGetPackage -Name TestPortableExe + + $result.UninstallerErrorCode | Should -Be 0 + $result.Status | Should -Be 'Ok' + $result.RebootRequired | Should -Be 'False' + } + + AfterAll { + # Uninstall all test packages after each for proper cleanup. + $testExe = Get-WinGetPackage -Id AppInstallerTest.TestExeInstaller + if ($testExe.Count -gt 0) + { + Uninstall-WinGetPackage -Id AppInstallerTest.TestExeInstaller + } + + $testPortable = Get-WinGetPackage -Id AppInstallerTest.TestPortableExe + if ($testPortable.Count -gt 0) + { + Uninstall-WinGetPackage -Id AppInstallerTest.TestPortableExe + } + } +} + +Describe 'Get-WinGetPackage' { + + It 'Install by Id' { + $result = Install-WinGetPackage -Id AppInstallerTest.TestExeInstaller -Version '1.0.0.0' + + $result.InstallerErrorCode | Should -Be 0 + $result.Status | Should -Be 'Ok' + $result.RebootRequired | Should -Be 'False' + } + + It 'Get package by Id' { + $result = Get-WinGetPackage -Id AppInstallerTest.TestExeInstaller + + $result | Should -Not -BeNullOrEmpty -ErrorAction Stop + $result.Name | Should -Be 'TestExeInstaller' + $result.Id | Should -Be 'AppInstallerTest.TestExeInstaller' + $result.Version | Should -Be '1.0.0.0' + $result.Source | Should -Be 'TestSource' + $result.AvailableVersions[0] | Should -Be '2.0.0.0' + } + + It 'Get package by Name' { + $result = Get-WinGetPackage -Name TestExeInstaller + + $result | Should -Not -BeNullOrEmpty -ErrorAction Stop + $result.Name | Should -Be 'TestExeInstaller' + $result.Id | Should -Be 'AppInstallerTest.TestExeInstaller' + $result.Version | Should -Be '1.0.0.0' + $result.Source | Should -Be 'TestSource' + $result.AvailableVersions[0] | Should -Be '2.0.0.0' + } + + AfterAll { + # Uninstall all test packages after each for proper cleanup. + $testExe = Get-WinGetPackage -Id AppInstallerTest.TestExeInstaller + if ($testExe.Count -gt 0) + { + Uninstall-WinGetPackage -Id AppInstallerTest.TestExeInstaller + } + } +} + +AfterAll { + # Source Remove requires admin privileges, this will only execute successfully in an elevated PowerShell. + wingetdev source remove 'TestSource' +} \ No newline at end of file